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