aboutsummaryrefslogtreecommitdiffstats
path: root/Tests/MODELS2020-CaseStudies/case.study.pledge.originalFiles/src/converter/UML2TGF.xtend
blob: f3762d540984ce759ff4ebef5413791a91f0cd2c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package converter

import java.io.PrintWriter
import java.text.SimpleDateFormat
import java.util.ArrayList
import java.util.Collection
import java.util.Date
import java.util.HashMap
import java.util.HashSet
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.uml2.uml.EnumerationLiteral
import org.eclipse.uml2.uml.InstanceSpecification
import org.eclipse.uml2.uml.InstanceValue
import org.eclipse.uml2.uml.Model
import org.eclipse.uml2.uml.Slot
import org.eclipse.uml2.uml.UMLPackage
import org.eclipse.uml2.uml.resource.UMLResource
import org.eclipse.emf.ecore.xmi.XMLResource

class UML2TGF {
	def static void main(String[] args) {

		// access UML
		val resourceSet = new ResourceSetImpl();
		resourceSet.packageRegistry.put(UMLPackage.eNS_URI, UMLPackage.eINSTANCE)
		resourceSet.resourceFactoryRegistry.extensionToFactoryMap.put(UMLResource.FILE_EXTENSION,
			UMLResource.Factory.INSTANCE)

//				resourceSet.getResourceFactoryRegistry().getExtensionToFactoryMap().put("xodr",
//			new OpenDrive15ResourceFactoryImpl);
		val res = resourceSet.getResource(URI.createFileURI("instance//Instance_model_A.uml"), true);
		val docRoot = res.contents.get(0) as Model
		
		

		val nodes = docRoot.allOwnedElements.filter[it instanceof InstanceSpecification]
		
		val allTypes = new HashSet<Object>
		
		for(e : nodes){
			allTypes.add(e.class)
		}

		// create file
		val Date date = new Date(System.currentTimeMillis)
		val SimpleDateFormat format = new SimpleDateFormat("dd-HHmm");
		val formattedDate = format.format(date)
		val map = new HashMap<InstanceSpecification, Integer>
		val set = new HashSet<String>
		val unidentified = new ArrayList<Object>
		val enums = new ArrayList<Object>

/*
		val writer = new PrintWriter("output//graph-" + formattedDate + ".tgf")

		println("begin Writing")

		// transform Nodes
		var ind = 0
		for (node : nodes) {
			val n = node as InstanceSpecification
			map.put(n, ind)

			writer.append(ind + " " + n.name + "\n")
			ind++
		}
		writer.append("#\n")

//		println(node.name)
//
//		println(node.slots)
//		val sl = node.slots
//
//		for (s : sl) {
//			val v = s.values.get(0)
//			if (v instanceof InstanceValue) {
//				println(map.get(v.instance))
//			} else {
//				println("--" + v.class)
//			}
//		}
		// transform Edges
		var Slot s = null
		try {
			for (node : nodes) {
				val n = node as InstanceSpecification
				val nind = map.get(n)
				for (slot : n.slots) {
					s = slot
					var fname = "null"
					if(slot.definingFeature !== null) fname = slot.definingFeature.name
					for (v : slot.values) {
						if (v instanceof InstanceValue) {
							val vind = map.get(v.instance)

							writer.append(nind + " " + vind + " " + fname + "\n")
						}
//					else {
//						println("--" + v.class)
//					}
					}
				}
			}
		} catch (Exception e) {
			println(s.definingFeature)
		}

		writer.close
		println("End")*/
		
		val writer = new PrintWriter("output//graph-" + formattedDate + ".gml")

		println("begin Writing")
		writer.append("graph\n[\n")

		// transform Nodes
		var ind = 0
		var numNodes = nodes.length
		var numEdges = 0
		for (node : nodes) {
			val n = node as InstanceSpecification
			map.put(n, ind)

			writer.append('''  node
  [
   id «ind»
   label "«IF n.name !== null»«n.name»«ENDIF»"
«IF n.name !== null && n.name.contains("Household")»     graphics
       [
         fill	"#00FF00"
       ]
         «ENDIF»
  ]
			  ''')
			ind++
		}

//		println(node.name)
//
//		println(node.slots)
//		val sl = node.slots
//
//		for (s : sl) {
//			val v = s.values.get(0)
//			if (v instanceof InstanceValue) {
//				println(map.get(v.instance))
//			} else {
//				println("--" + v.class)
//			}
//		}
		// transform Edges
		var Slot s = null
		try {
			for (node : nodes) {
				if (node instanceof EnumerationLiteral != true) {
					val n = node as InstanceSpecification
					val nind = map.get(n)
					for (slot : n.slots) {
						s = slot
						var fname = ""
						if(slot.definingFeature !== null) fname = slot.definingFeature.name
						for (v : slot.values) {
							if (v instanceof InstanceValue) {
								val value = v as InstanceValue
								if (map.containsKey(value.instance)) {
									val vind = map.get(value.instance)
									writer.append('''  edge
  [
   source «nind»
   target «vind»
   label "«fname»"
  ]
							  ''')
//							writer.append(nind + " " + vind + " " + fname + "\n")
									numEdges++
								} else {
									unidentified.add(value + " -> " + value.instance)
								}
							} else {
								set.add(v.class.toString)
							}
						}
					}
				}
				else{
					enums.add(node)
				}
				
			}
		} catch (Exception e) {
			println(s.definingFeature)
		}
//		printset("All types", allTypes)
		printset("Unhandled Edges", set)
		printset("Unidentified trgs", unidentified)
//		printset("Enums:", enums)
		writer.append("]")
		writer.close
		println("End")
		println("#nodes:" + numNodes + ", #edges:" + numEdges)

	}

	static def printset(String name, Collection s) {
		println(name + ":")
		for (k : s) {
			println("- " + k)
		}
		println()
	}
}