diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2020-05-10 22:13:21 +0200 |
---|---|---|
committer | Kristóf Marussy <marussy@mit.bme.hu> | 2020-05-10 22:13:21 +0200 |
commit | 5a6eea9bd15597eab77bbcedb4b5116020f0a67d (patch) | |
tree | 6c86f7e5bd3d86fde29d3fee4fb4f912f35057bc /Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter | |
parent | Add satellite case study (diff) | |
parent | Merge branch 'master' of https://github.com/viatra/VIATRA-Generator (diff) | |
download | VIATRA-Generator-5a6eea9bd15597eab77bbcedb4b5116020f0a67d.tar.gz VIATRA-Generator-5a6eea9bd15597eab77bbcedb4b5116020f0a67d.tar.zst VIATRA-Generator-5a6eea9bd15597eab77bbcedb4b5116020f0a67d.zip |
Merge branch 'master' of github.com:viatra/VIATRA-Generator
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.xtend | 216 |
1 files changed, 216 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..283ddf79 --- /dev/null +++ b/Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter/UML2TGF.xtend | |||
@@ -0,0 +1,216 @@ | |||
1 | package converter | ||
2 | |||
3 | import java.io.PrintWriter | ||
4 | import java.text.SimpleDateFormat | ||
5 | import java.util.ArrayList | ||
6 | import java.util.Collection | ||
7 | import java.util.Date | ||
8 | import java.util.HashMap | ||
9 | import java.util.HashSet | ||
10 | import org.eclipse.emf.common.util.URI | ||
11 | import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl | ||
12 | import org.eclipse.uml2.uml.EnumerationLiteral | ||
13 | import org.eclipse.uml2.uml.InstanceSpecification | ||
14 | import org.eclipse.uml2.uml.InstanceValue | ||
15 | import org.eclipse.uml2.uml.Model | ||
16 | import org.eclipse.uml2.uml.Slot | ||
17 | import org.eclipse.uml2.uml.UMLPackage | ||
18 | import org.eclipse.uml2.uml.resource.UMLResource | ||
19 | import org.eclipse.emf.ecore.xmi.XMLResource | ||
20 | |||
21 | class 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 | // val nodes = docRoot.allOwnedElements.filter [it instanceof InstanceSpecification && it instanceof EnumerationLiteral != true] | ||
36 | val nodes = docRoot.allOwnedElements.filter[it instanceof InstanceSpecification] | ||
37 | val allTypes = new HashSet<Object> | ||
38 | |||
39 | for (e : nodes) { | ||
40 | allTypes.add(e.class) | ||
41 | } | ||
42 | |||
43 | // create file | ||
44 | val Date date = new Date(System.currentTimeMillis) | ||
45 | val SimpleDateFormat format = new SimpleDateFormat("dd-HHmm"); | ||
46 | val formattedDate = format.format(date) | ||
47 | val map = new HashMap<InstanceSpecification, Integer> | ||
48 | val set = new HashSet<String> | ||
49 | val unidentified = new ArrayList<Object> | ||
50 | val enums = new ArrayList<Object> | ||
51 | |||
52 | /* | ||
53 | * val writer = new PrintWriter("output//graph-" + formattedDate + ".tgf") | ||
54 | |||
55 | * println("begin Writing") | ||
56 | |||
57 | * // transform Nodes | ||
58 | * var ind = 0 | ||
59 | * for (node : nodes) { | ||
60 | * val n = node as InstanceSpecification | ||
61 | * map.put(n, ind) | ||
62 | |||
63 | * writer.append(ind + " " + n.name + "\n") | ||
64 | * ind++ | ||
65 | * } | ||
66 | * writer.append("#\n") | ||
67 | |||
68 | * // println(node.name) | ||
69 | * // | ||
70 | * // println(node.slots) | ||
71 | * // val sl = node.slots | ||
72 | * // | ||
73 | * // for (s : sl) { | ||
74 | * // val v = s.values.get(0) | ||
75 | * // if (v instanceof InstanceValue) { | ||
76 | * // println(map.get(v.instance)) | ||
77 | * // } else { | ||
78 | * // println("--" + v.class) | ||
79 | * // } | ||
80 | * // } | ||
81 | * // transform Edges | ||
82 | * var Slot s = null | ||
83 | * try { | ||
84 | * for (node : nodes) { | ||
85 | * val n = node as InstanceSpecification | ||
86 | * val nind = map.get(n) | ||
87 | * for (slot : n.slots) { | ||
88 | * s = slot | ||
89 | * var fname = "null" | ||
90 | * if(slot.definingFeature !== null) fname = slot.definingFeature.name | ||
91 | * for (v : slot.values) { | ||
92 | * if (v instanceof InstanceValue) { | ||
93 | * val vind = map.get(v.instance) | ||
94 | |||
95 | * writer.append(nind + " " + vind + " " + fname + "\n") | ||
96 | * } | ||
97 | * // else { | ||
98 | * // println("--" + v.class) | ||
99 | * // } | ||
100 | * } | ||
101 | * } | ||
102 | * } | ||
103 | * } catch (Exception e) { | ||
104 | * println(s.definingFeature) | ||
105 | * } | ||
106 | |||
107 | * writer.close | ||
108 | println("End")*/ | ||
109 | val writer = new PrintWriter("output//graph-" + formattedDate + ".gml") | ||
110 | |||
111 | println("begin Writing") | ||
112 | writer.append("graph\n[\n") | ||
113 | |||
114 | // transform Nodes | ||
115 | var ind = 0 | ||
116 | var numNodes = nodes.length | ||
117 | var numEdges = 0 | ||
118 | for (node : nodes) { | ||
119 | val n = node as InstanceSpecification | ||
120 | if(map.keySet.contains(n)) System.err.println("contained") | ||
121 | map.put(n, ind) | ||
122 | |||
123 | writer.append(''' node | ||
124 | [ | ||
125 | id «ind» | ||
126 | label "«IF n.name !== null»«n.name»«ENDIF»" | ||
127 | «IF n.name !== null && (n.name.contains("Household")|| n.name.contains("Household"))» graphics | ||
128 | [ | ||
129 | fill "#00FF00" | ||
130 | ] | ||
131 | «ENDIF» | ||
132 | «IF node instanceof EnumerationLiteral» graphics | ||
133 | [ | ||
134 | fill "#FF0000" | ||
135 | ] | ||
136 | «ENDIF» | ||
137 | ] | ||
138 | ''') | ||
139 | ind++ | ||
140 | } | ||
141 | |||
142 | // println(node.name) | ||
143 | // | ||
144 | // println(node.slots) | ||
145 | // val sl = node.slots | ||
146 | // | ||
147 | // for (s : sl) { | ||
148 | // val v = s.values.get(0) | ||
149 | // if (v instanceof InstanceValue) { | ||
150 | // println(map.get(v.instance)) | ||
151 | // } else { | ||
152 | // println("--" + v.class) | ||
153 | // } | ||
154 | // } | ||
155 | // transform Edges | ||
156 | var Slot s = null | ||
157 | try { | ||
158 | for (node : nodes) { | ||
159 | // if (node instanceof EnumerationLiteral != true) { | ||
160 | val n = node as InstanceSpecification | ||
161 | val nind = map.get(n) | ||
162 | for (slot : n.slots) { | ||
163 | s = slot | ||
164 | var fname = "" | ||
165 | if(slot.definingFeature !== null) fname = slot.definingFeature.name | ||
166 | for (v : slot.values) { | ||
167 | if (v instanceof InstanceValue) { | ||
168 | val value = v as InstanceValue | ||
169 | if (map.containsKey(value.instance)) { | ||
170 | if (value.instance instanceof EnumerationLiteral != true) { | ||
171 | val vind = map.get(value.instance) | ||
172 | writer.append(''' edge | ||
173 | [ | ||
174 | source «nind» | ||
175 | target «vind» | ||
176 | label "«fname»" | ||
177 | ] | ||
178 | ''') | ||
179 | // writer.append(nind + " " + vind + " " + fname + "\n") | ||
180 | numEdges++ | ||
181 | } else { | ||
182 | enums.add(value + " -> " + value.instance) | ||
183 | } | ||
184 | } else { | ||
185 | unidentified.add(value + " -> " + value.instance) | ||
186 | } | ||
187 | } else { | ||
188 | set.add(v.class.toString) | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | // } | ||
193 | |||
194 | } | ||
195 | } catch (Exception e) { | ||
196 | println(s.definingFeature) | ||
197 | } | ||
198 | // printset("All types", allTypes) | ||
199 | printset("Unhandled Edges", set) | ||
200 | printset("Unidentified trgs", unidentified) | ||
201 | printset("Enums:", enums) | ||
202 | writer.append("]") | ||
203 | writer.close | ||
204 | println("End") | ||
205 | println("#nodes:" + numNodes + ", #edges:" + numEdges) | ||
206 | |||
207 | } | ||
208 | |||
209 | static def printset(String name, Collection s) { | ||
210 | println(name + ":") | ||
211 | for (k : s) { | ||
212 | println("- " + k) | ||
213 | } | ||
214 | println() | ||
215 | } | ||
216 | } | ||