diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage')
1 files changed, 337 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Neighbourhood2Gml.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Neighbourhood2Gml.xtend new file mode 100644 index 00000000..6179c838 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Neighbourhood2Gml.xtend | |||
@@ -0,0 +1,337 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
5 | import java.util.ArrayList | ||
6 | import java.util.HashMap | ||
7 | import java.util.HashSet | ||
8 | import java.util.List | ||
9 | import java.util.Map | ||
10 | import java.util.Set | ||
11 | import java.util.logging.Handler | ||
12 | |||
13 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | ||
14 | |||
15 | class Neighbourhood2Gml { | ||
16 | |||
17 | static val neighbourhoodComputer = new PartialInterpretation2ImmutableTypeLattice | ||
18 | val protected nodeNameSize = 14 | ||
19 | val protected nodeHeight = 40 | ||
20 | val protected borderDistance = 6 | ||
21 | val protected ratio = 11.0 / 20.0 | ||
22 | var protected depth = 0 | ||
23 | |||
24 | def public transform(NeighbourhoodWithTraces n, PartialInterpretation pm) { | ||
25 | |||
26 | val Map<Object, Integer> relevantObjectToID = new HashMap | ||
27 | val List<CharSequence> fullNodeText = newArrayList | ||
28 | val List<CharSequence> fullEdgeText = newArrayList | ||
29 | val List<CharSequence> allEdgesText = newArrayList | ||
30 | val Map<AbstractNodeDescriptor, List<FurtherNodeDescriptor>> children = new HashMap | ||
31 | |||
32 | // TODO these should not be hashmaps, as a given node can have multiple sameNamed edges to different | ||
33 | val Map<IncomingRelation, Object> edgeNameIn2targetNode = new HashMap | ||
34 | val Map<OutgoingRelation, Object> edgeNameOut2targetNode = new HashMap | ||
35 | val Map<IncomingRelation, List<Integer>> edgeName2inMultips = new HashMap | ||
36 | val Map<OutgoingRelation, List<Integer>> edgeName2outMultips = new HashMap | ||
37 | |||
38 | val modRep = n.modelRepresentation as HashMap | ||
39 | // Store node text | ||
40 | for (nodeKey : modRep.keySet) { | ||
41 | fullNodeText.add(transformNode(nodeKey, modRep, relevantObjectToID)) | ||
42 | } | ||
43 | |||
44 | // calculate deeper neighbourhood | ||
45 | calculateDepth(modRep.keySet.get(0)) | ||
46 | val deeperNeighbourhood = neighbourhoodComputer.createRepresentation(pm, depth + 1, Integer.MAX_VALUE, | ||
47 | Integer.MAX_VALUE) | ||
48 | val deepModRep = deeperNeighbourhood.modelRepresentation as HashMap | ||
49 | |||
50 | // Associate each deepNode to their parent | ||
51 | for (deepNodeKey : deepModRep.keySet) { | ||
52 | val deepNodeDesc = deepNodeKey as FurtherNodeDescriptor | ||
53 | val parentDesc = deepNodeDesc.previousRepresentation as AbstractNodeDescriptor | ||
54 | if (relevantObjectToID.keySet.contains(parentDesc)) { | ||
55 | if (children.keySet.contains(parentDesc)) { | ||
56 | parentDesc.lookup(children).add(deepNodeDesc) | ||
57 | } else { | ||
58 | children.put(parentDesc, newArrayList(deepNodeDesc)) | ||
59 | } | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // edge transforming | ||
64 | for (currentNode : children.keySet) { | ||
65 | println(currentNode) | ||
66 | transformEdge(edgeNameIn2targetNode, edgeNameOut2targetNode, edgeName2inMultips, edgeName2outMultips, | ||
67 | allEdgesText, currentNode, relevantObjectToID, children) | ||
68 | for (edgeText : allEdgesText) { | ||
69 | fullEdgeText.add(edgeText) | ||
70 | } | ||
71 | allEdgesText.clear | ||
72 | } | ||
73 | |||
74 | // mapPrinter(children) | ||
75 | ''' | ||
76 | graph | ||
77 | [ | ||
78 | «FOR nodeText : fullNodeText» | ||
79 | «nodeText» | ||
80 | «ENDFOR» | ||
81 | «FOR edgeText : fullEdgeText» | ||
82 | «edgeText» | ||
83 | «ENDFOR» | ||
84 | ] | ||
85 | '''.toString | ||
86 | } | ||
87 | |||
88 | protected def transformEdge(Map<IncomingRelation, Object> edgeNameIn2targetNode, | ||
89 | Map<OutgoingRelation, Object> edgeNameOut2targetNode, Map<IncomingRelation, List<Integer>> edgeName2inMultips, | ||
90 | Map<OutgoingRelation, List<Integer>> edgeName2outMultips, List<CharSequence> allEdgesText, | ||
91 | AbstractNodeDescriptor currentNode, Map<Object, Integer> relevantObjectToID, | ||
92 | Map<AbstractNodeDescriptor, List<FurtherNodeDescriptor>> children) { | ||
93 | |||
94 | edgeNameIn2targetNode.clear | ||
95 | edgeNameOut2targetNode.clear | ||
96 | edgeName2inMultips.clear | ||
97 | edgeName2outMultips.clear | ||
98 | val List<Object> modifiedEdgeNames = newArrayList | ||
99 | |||
100 | val List<FurtherNodeDescriptor> subNodes = currentNode.lookup(children) | ||
101 | for (subNode : subNodes) { | ||
102 | // println(subNode) | ||
103 | // mapPrinter(subNode.outgoingEdges) | ||
104 | // println("---------") | ||
105 | // mapPrinter(subNode.incomingEdges) | ||
106 | // println | ||
107 | // println | ||
108 | // handling outgoing edges | ||
109 | for (outEdge : subNode.outgoingEdges.keySet) { | ||
110 | val edgeName = (outEdge as OutgoingRelation) | ||
111 | val edgePointingTo = (outEdge as OutgoingRelation).to | ||
112 | val edgeOutMultip = outEdge.lookup(subNode.outgoingEdges) as Integer | ||
113 | |||
114 | // add multiplicity and target | ||
115 | if (edgeName2outMultips.containsKey(edgeName)) { | ||
116 | edgeName.lookup(edgeName2outMultips).add(edgeOutMultip) | ||
117 | } else { | ||
118 | edgeName2outMultips.put(edgeName, newArrayList(edgeOutMultip)) | ||
119 | } | ||
120 | edgeNameOut2targetNode.put(edgeName, edgePointingTo) | ||
121 | |||
122 | } | ||
123 | |||
124 | } | ||
125 | |||
126 | // handling incoming edges | ||
127 | for (outEdgeTarget : edgeNameOut2targetNode.values) { | ||
128 | // currentNode = sourceParent | ||
129 | for (subNode : (outEdgeTarget as AbstractNodeDescriptor).lookup(children)) { | ||
130 | for (inEdge : subNode.incomingEdges.keySet) { | ||
131 | val edgeName = (inEdge as IncomingRelation) | ||
132 | val edgePointingFrom = (inEdge as IncomingRelation).from | ||
133 | val edgeInMultip = inEdge.lookup(subNode.incomingEdges) as Integer | ||
134 | |||
135 | if (edgePointingFrom.equals(currentNode)) { | ||
136 | if (edgeName2inMultips.containsKey(edgeName)) { | ||
137 | edgeName.lookup(edgeName2inMultips).add(edgeInMultip) | ||
138 | } else { | ||
139 | edgeName2inMultips.put(edgeName, newArrayList(edgeInMultip)) | ||
140 | modifiedEdgeNames.add(edgeName) | ||
141 | } | ||
142 | edgeNameIn2targetNode.put(edgeName, edgePointingFrom) | ||
143 | } | ||
144 | // edgeName2targetNode.put(edgeName, edgePointingFrom) | ||
145 | } | ||
146 | } | ||
147 | |||
148 | // mapPrinter(edgeName2inMultips) | ||
149 | // fill in the 0 multiplicities (INCOMING) | ||
150 | for (edgeSoFar : modifiedEdgeNames) { | ||
151 | val edgeAsRelation = edgeSoFar as IncomingRelation | ||
152 | var inEdgesNum = edgeAsRelation.lookup(edgeName2inMultips).size | ||
153 | val targetNode = outEdgeTarget as AbstractNodeDescriptor | ||
154 | val targetChildrenNum = targetNode.lookup(children).size | ||
155 | println("in " + edgeSoFar + "=>" + inEdgesNum + " != " + outEdgeTarget + "=>" + targetChildrenNum) | ||
156 | while (inEdgesNum != targetChildrenNum) { | ||
157 | // println("in" + inEdgesNum + "!=" + targetChildrenNum) | ||
158 | edgeAsRelation.lookup(edgeName2inMultips).add(0) | ||
159 | inEdgesNum++ | ||
160 | } | ||
161 | } | ||
162 | modifiedEdgeNames.clear | ||
163 | |||
164 | } | ||
165 | |||
166 | mapPrinter(edgeName2outMultips) | ||
167 | |||
168 | // fill in the 0 multiplicities (OUTGOING) | ||
169 | for (edge : edgeName2outMultips.keySet) { | ||
170 | // handling outgoing edges | ||
171 | var outEdgesNum = edge.lookup(edgeName2outMultips).size | ||
172 | val sourceChildrenNum = currentNode.lookup(children).size | ||
173 | |||
174 | println("out " + edge + "=>" + outEdgesNum + " != " + currentNode + "=>" + sourceChildrenNum) | ||
175 | while (outEdgesNum != sourceChildrenNum) { | ||
176 | edge.lookup(edgeName2outMultips).add(0) | ||
177 | outEdgesNum++ | ||
178 | } | ||
179 | } | ||
180 | println("xxxxxxxx") | ||
181 | mapPrinter(edgeNameIn2targetNode) | ||
182 | mapPrinter(edgeNameOut2targetNode) | ||
183 | |||
184 | |||
185 | |||
186 | updateEdgeList(currentNode, relevantObjectToID, edgeNameIn2targetNode, edgeNameOut2targetNode, edgeName2inMultips, edgeName2outMultips, | ||
187 | allEdgesText) | ||
188 | |||
189 | } | ||
190 | |||
191 | def updateEdgeList(AbstractNodeDescriptor currentNode, Map<Object, Integer> relevantObjectToID, | ||
192 | Map<IncomingRelation, Object> edgeNameIn2targetNode, | ||
193 | Map<OutgoingRelation, Object> edgeNameOut2targetNode, | ||
194 | Map<IncomingRelation, List<Integer>> edgeName2inMultips, | ||
195 | Map<OutgoingRelation, List<Integer>> edgeName2outMultips, List<CharSequence> allEdgesTexts) { | ||
196 | |||
197 | for (outEdge : edgeNameOut2targetNode.keySet) { | ||
198 | // TODO | ||
199 | val sourceID = currentNode.lookup(relevantObjectToID) | ||
200 | val targetNode = outEdge.lookup(edgeNameOut2targetNode) | ||
201 | val targetID = targetNode.lookup(relevantObjectToID) | ||
202 | val edgeName = outEdge.type | ||
203 | |||
204 | //finding corresponding Incoming edge | ||
205 | var correspInEdgeSet = edgeNameIn2targetNode.keySet.filter[type.equals(edgeName) && lookup(edgeNameIn2targetNode).equals(currentNode)] | ||
206 | println("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy") | ||
207 | println(currentNode) | ||
208 | println(outEdge) | ||
209 | println(targetNode) | ||
210 | println(edgeNameIn2targetNode) | ||
211 | println(correspInEdgeSet.size) | ||
212 | val correspInEdge = correspInEdgeSet.get(0) | ||
213 | |||
214 | |||
215 | allEdgesTexts.add( | ||
216 | ''' | ||
217 | edge | ||
218 | [ | ||
219 | source «sourceID» | ||
220 | target «targetID» | ||
221 | label "«edgeName»" | ||
222 | graphics | ||
223 | [ | ||
224 | fill "#000000" | ||
225 | targetArrow "standard" | ||
226 | Line | ||
227 | [ | ||
228 | ] | ||
229 | ] | ||
230 | LabelGraphics | ||
231 | [ | ||
232 | text "«edgeName»" | ||
233 | fontSize 12 | ||
234 | fontName "Dialog" | ||
235 | configuration "AutoFlippingLabel" | ||
236 | model "six_pos" | ||
237 | position "head" | ||
238 | ] | ||
239 | «IF edgeName2outMultips.containsKey(outEdge)» | ||
240 | LabelGraphics | ||
241 | [ | ||
242 | text "«outEdge.lookup(edgeName2outMultips).toString»" | ||
243 | fontSize 12 | ||
244 | fontName "Dialog" | ||
245 | configuration "AutoFlippingLabel" | ||
246 | model "six_pos" | ||
247 | position "stail" | ||
248 | ] | ||
249 | «ENDIF» | ||
250 | «IF edgeName2inMultips.containsKey(correspInEdge)» | ||
251 | LabelGraphics | ||
252 | [ | ||
253 | text "«correspInEdge.lookup(edgeName2inMultips).toString»" | ||
254 | fontSize 12 | ||
255 | fontName "Dialog" | ||
256 | configuration "AutoFlippingLabel" | ||
257 | model "six_pos" | ||
258 | position "ttail" | ||
259 | ] | ||
260 | «ENDIF» | ||
261 | ] | ||
262 | ''' | ||
263 | ) | ||
264 | } | ||
265 | } | ||
266 | |||
267 | def calculateDepth(Object node) { | ||
268 | var keyDescriptor = node | ||
269 | |||
270 | while (!keyDescriptor.class.equals(LocalNodeDescriptor)) { | ||
271 | keyDescriptor = (keyDescriptor as FurtherNodeDescriptor).previousRepresentation | ||
272 | depth++ | ||
273 | } | ||
274 | |||
275 | } | ||
276 | |||
277 | def protected transformNode(Object nodeKey, Map<Object, Integer> modRep, Map<Object, Integer> objectToID) { | ||
278 | |||
279 | var topKeyDescriptor = nodeKey | ||
280 | |||
281 | while (!topKeyDescriptor.class.equals(LocalNodeDescriptor)) { | ||
282 | topKeyDescriptor = (topKeyDescriptor as FurtherNodeDescriptor).previousRepresentation | ||
283 | } | ||
284 | |||
285 | var keyType = (topKeyDescriptor as LocalNodeDescriptor).types | ||
286 | |||
287 | if (!keyType.empty) { | ||
288 | val id = objectToID.size | ||
289 | objectToID.put(nodeKey, id) | ||
290 | ''' | ||
291 | node | ||
292 | [ | ||
293 | id «id» | ||
294 | graphics | ||
295 | [ | ||
296 | w «((keyType.toString.length+4)*nodeNameSize + 2 * borderDistance)* ratio» | ||
297 | h «nodeHeight» | ||
298 | type "roundrectangle" | ||
299 | fill "#FFCC00" | ||
300 | outline "#000000" | ||
301 | ] | ||
302 | LabelGraphics | ||
303 | [ | ||
304 | ] | ||
305 | LabelGraphics | ||
306 | [ | ||
307 | text "«keyType.toString» («nodeKey.lookup(modRep)»)" | ||
308 | fontSize «nodeNameSize» | ||
309 | fontName "Dialog" | ||
310 | ] | ||
311 | ] | ||
312 | ''' | ||
313 | } | ||
314 | } | ||
315 | |||
316 | def protected transformEdges(FurtherNodeDescriptor deepNodeKey, Map<Object, Integer> deepModRep, | ||
317 | Map<Object, Integer> objectToID) { | ||
318 | print(depth) | ||
319 | println(deepNodeKey.incomingEdges) | ||
320 | println(deepNodeKey.outgoingEdges) | ||
321 | // get | ||
322 | } | ||
323 | |||
324 | def mapPrinter(Map x) { | ||
325 | for (m : x.keySet) { | ||
326 | println("New Entry --") | ||
327 | println(" key = " + m) | ||
328 | println(" value = " + m.lookup(x)) | ||
329 | } | ||
330 | } | ||
331 | |||
332 | def printer2(Set<FurtherNodeDescriptor> descriptors) { | ||
333 | for (x : descriptors) { | ||
334 | println(x) | ||
335 | } | ||
336 | } | ||
337 | } | ||