aboutsummaryrefslogtreecommitdiffstats
path: root/Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter
diff options
context:
space:
mode:
authorLibravatar ArenBabikian <aren.babikian@mail.mcgill.ca>2020-05-09 03:48:41 -0400
committerLibravatar ArenBabikian <aren.babikian@mail.mcgill.ca>2020-05-09 03:48:41 -0400
commit51f00c1a2bbc52deb48be61b6d594e65d778e686 (patch)
tree0c33ffca58b3e8526d5a5b76836e8ecd6e3a30b7 /Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter
parentremoved unnecesary logging (diff)
downloadVIATRA-Generator-51f00c1a2bbc52deb48be61b6d594e65d778e686.tar.gz
VIATRA-Generator-51f00c1a2bbc52deb48be61b6d594e65d778e686.tar.zst
VIATRA-Generator-51f00c1a2bbc52deb48be61b6d594e65d778e686.zip
Implement UML Instance 2 GML visualization mapping
Diffstat (limited to 'Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter')
-rw-r--r--Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter/UML2TGF.xtend212
1 files changed, 212 insertions, 0 deletions
diff --git a/Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter/UML2TGF.xtend b/Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter/UML2TGF.xtend
new file mode 100644
index 00000000..f3762d54
--- /dev/null
+++ b/Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter/UML2TGF.xtend
@@ -0,0 +1,212 @@
1package converter
2
3import java.io.PrintWriter
4import java.text.SimpleDateFormat
5import java.util.ArrayList
6import java.util.Collection
7import java.util.Date
8import java.util.HashMap
9import java.util.HashSet
10import org.eclipse.emf.common.util.URI
11import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
12import org.eclipse.uml2.uml.EnumerationLiteral
13import org.eclipse.uml2.uml.InstanceSpecification
14import org.eclipse.uml2.uml.InstanceValue
15import org.eclipse.uml2.uml.Model
16import org.eclipse.uml2.uml.Slot
17import org.eclipse.uml2.uml.UMLPackage
18import org.eclipse.uml2.uml.resource.UMLResource
19import org.eclipse.emf.ecore.xmi.XMLResource
20
21class UML2TGF {
22 def static void main(String[] args) {
23
24 // access UML
25 val resourceSet = new ResourceSetImpl();
26 resourceSet.packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE)
27 resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put(UMLResource.FILE_EXTENSION,
28 UMLResource.Factory.INSTANCE)
29
30// resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xodr",
31// new OpenDrive15ResourceFactoryImpl);
32 val res = resourceSet.getResource(URI.createFileURI("instance//Instance_model_A.uml"), true);
33 val docRoot = res.contents.get(0) as Model
34
35
36
37 val nodes = docRoot.allOwnedElements.filter[it instanceof InstanceSpecification]
38
39 val allTypes = new HashSet<Object>
40
41 for(e : nodes){
42 allTypes.add(e.class)
43 }
44
45 // create file
46 val Date date = new Date(System.currentTimeMillis)
47 val SimpleDateFormat format = new SimpleDateFormat("dd-HHmm");
48 val formattedDate = format.format(date)
49 val map = new HashMap<InstanceSpecification, Integer>
50 val set = new HashSet<String>
51 val unidentified = new ArrayList<Object>
52 val enums = new ArrayList<Object>
53
54/*
55 val writer = new PrintWriter("output//graph-" + formattedDate + ".tgf")
56
57 println("begin Writing")
58
59 // transform Nodes
60 var ind = 0
61 for (node : nodes) {
62 val n = node as InstanceSpecification
63 map.put(n, ind)
64
65 writer.append(ind + " " + n.name + "\n")
66 ind++
67 }
68 writer.append("#\n")
69
70// println(node.name)
71//
72// println(node.slots)
73// val sl = node.slots
74//
75// for (s : sl) {
76// val v = s.values.get(0)
77// if (v instanceof InstanceValue) {
78// println(map.get(v.instance))
79// } else {
80// println("--" + v.class)
81// }
82// }
83 // transform Edges
84 var Slot s = null
85 try {
86 for (node : nodes) {
87 val n = node as InstanceSpecification
88 val nind = map.get(n)
89 for (slot : n.slots) {
90 s = slot
91 var fname = "null"
92 if(slot.definingFeature !== null) fname = slot.definingFeature.name
93 for (v : slot.values) {
94 if (v instanceof InstanceValue) {
95 val vind = map.get(v.instance)
96
97 writer.append(nind + " " + vind + " " + fname + "\n")
98 }
99// else {
100// println("--" + v.class)
101// }
102 }
103 }
104 }
105 } catch (Exception e) {
106 println(s.definingFeature)
107 }
108
109 writer.close
110 println("End")*/
111
112 val writer = new PrintWriter("output//graph-" + formattedDate + ".gml")
113
114 println("begin Writing")
115 writer.append("graph\n[\n")
116
117 // transform Nodes
118 var ind = 0
119 var numNodes = nodes.length
120 var numEdges = 0
121 for (node : nodes) {
122 val n = node as InstanceSpecification
123 map.put(n, ind)
124
125 writer.append(''' node
126 [
127 id «ind»
128 label "«IF n.name !== null»«n.name»«ENDIF»"
129«IF n.name !== null && n.name.contains("Household")» graphics
130 [
131 fill "#00FF00"
132 ]
133 «ENDIF»
134 ]
135 ''')
136 ind++
137 }
138
139// println(node.name)
140//
141// println(node.slots)
142// val sl = node.slots
143//
144// for (s : sl) {
145// val v = s.values.get(0)
146// if (v instanceof InstanceValue) {
147// println(map.get(v.instance))
148// } else {
149// println("--" + v.class)
150// }
151// }
152 // transform Edges
153 var Slot s = null
154 try {
155 for (node : nodes) {
156 if (node instanceof EnumerationLiteral != true) {
157 val n = node as InstanceSpecification
158 val nind = map.get(n)
159 for (slot : n.slots) {
160 s = slot
161 var fname = ""
162 if(slot.definingFeature !== null) fname = slot.definingFeature.name
163 for (v : slot.values) {
164 if (v instanceof InstanceValue) {
165 val value = v as InstanceValue
166 if (map.containsKey(value.instance)) {
167 val vind = map.get(value.instance)
168 writer.append(''' edge
169 [
170 source «nind»
171 target «vind»
172 label "«fname»"
173 ]
174 ''')
175// writer.append(nind + " " + vind + " " + fname + "\n")
176 numEdges++
177 } else {
178 unidentified.add(value + " -> " + value.instance)
179 }
180 } else {
181 set.add(v.class.toString)
182 }
183 }
184 }
185 }
186 else{
187 enums.add(node)
188 }
189
190 }
191 } catch (Exception e) {
192 println(s.definingFeature)
193 }
194// printset("All types", allTypes)
195 printset("Unhandled Edges", set)
196 printset("Unidentified trgs", unidentified)
197// printset("Enums:", enums)
198 writer.append("]")
199 writer.close
200 println("End")
201 println("#nodes:" + numNodes + ", #edges:" + numEdges)
202
203 }
204
205 static def printset(String name, Collection s) {
206 println(name + ":")
207 for (k : s) {
208 println("- " + k)
209 }
210 println()
211 }
212}