diff options
author | OszkarSemerath <oszka@152.66.252.189> | 2017-06-10 19:05:05 +0200 |
---|---|---|
committer | OszkarSemerath <oszka@152.66.252.189> | 2017-06-10 19:05:05 +0200 |
commit | 60f01f46ba232ed6416054f0a6115cb2a9b70b4e (patch) | |
tree | 5edf8aeb07abc51f3fec63bbd15c926e1de09552 /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2NeighbourhoodRepresentation.xtend | |
parent | Initial commit, migrating from SVN (diff) | |
download | VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.gz VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.tar.zst VIATRA-Generator-60f01f46ba232ed6416054f0a6115cb2a9b70b4e.zip |
Migrating Additional projects
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2NeighbourhoodRepresentation.xtend')
1 files changed, 345 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/PartialInterpretation2NeighbourhoodRepresentation.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2NeighbourhoodRepresentation.xtend new file mode 100644 index 00000000..df6fb6ae --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2NeighbourhoodRepresentation.xtend | |||
@@ -0,0 +1,345 @@ | |||
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.logic.model.logiclanguage.RelationDeclaration | ||
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration | ||
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
8 | import java.util.HashMap | ||
9 | import java.util.HashSet | ||
10 | import java.util.LinkedList | ||
11 | import java.util.List | ||
12 | import java.util.Map | ||
13 | import java.util.Set | ||
14 | |||
15 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | ||
16 | |||
17 | abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresentation,NodeRepresentation> { | ||
18 | private val boolean deepRepresentation | ||
19 | private val boolean mergeSimilarNeighbourhood | ||
20 | |||
21 | protected new(boolean deeprepresentation, boolean mergeSimilarNeighbourhood) { | ||
22 | this.deepRepresentation = deeprepresentation | ||
23 | this.mergeSimilarNeighbourhood = mergeSimilarNeighbourhood | ||
24 | } | ||
25 | |||
26 | public static val FixPointRage = -1 | ||
27 | public static val FullParallels = Integer.MAX_VALUE | ||
28 | public static val MaxNumbers = Integer.MAX_VALUE | ||
29 | |||
30 | /** | ||
31 | * Creates a neighbourhood representation with traces | ||
32 | * @param model The model to be represented. | ||
33 | * @param range The range of the neighbourhood. | ||
34 | * @param parallels The maximal number of parallel references to be differentiated. | ||
35 | */ | ||
36 | def public createRepresentation(PartialInterpretation model, int range, int parallels, int maxNumber) { | ||
37 | return createRepresentation(model,range,parallels,maxNumber,null,null) | ||
38 | } | ||
39 | |||
40 | def public createRepresentation( | ||
41 | PartialInterpretation model, | ||
42 | int range, int parallels, int maxNumber, | ||
43 | Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations) | ||
44 | { | ||
45 | val Map<DefinedElement, Set<String>> types = new HashMap | ||
46 | fillTypes(model,types,relevantTypes) | ||
47 | val Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations = new HashMap; | ||
48 | val Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations = new HashMap; | ||
49 | fillReferences(model,IncomingRelations,OutgoingRelations,relevantRelations) | ||
50 | |||
51 | val res = doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,range,parallels,maxNumber); | ||
52 | |||
53 | return res; | ||
54 | } | ||
55 | |||
56 | def private isRelevant(TypeDeclaration t, Set<TypeDeclaration> relevantTypes) { | ||
57 | if(relevantTypes === null) { | ||
58 | return true | ||
59 | } else { | ||
60 | return relevantTypes.contains(t) | ||
61 | } | ||
62 | } | ||
63 | def private isRelevant(RelationDeclaration r, Set<RelationDeclaration> relevantRelations) { | ||
64 | if(relevantRelations === null) { | ||
65 | return true | ||
66 | } else { | ||
67 | return relevantRelations.contains(r) | ||
68 | } | ||
69 | } | ||
70 | |||
71 | /** | ||
72 | * Creates a neighbourhood representation with traces | ||
73 | * @param model The model to be represented. | ||
74 | * @param IncomingRelations Requires the previously indexed incoming references. | ||
75 | * @param OutgoingRelations Requires the previously indexed outgoing references. | ||
76 | * @param range The range of the neighbourhood. | ||
77 | * @param parallels The maximal number of parallel references to be differentiated. | ||
78 | */ | ||
79 | def private NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> doRecursiveNeighbourCalculation( | ||
80 | PartialInterpretation model, | ||
81 | Map<DefinedElement, Set<String>> types, | ||
82 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | ||
83 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | ||
84 | int range, int parallels, int maxNumber) | ||
85 | { | ||
86 | if(range == 0){ | ||
87 | val r = calculateLocalNodeDescriptors(model,types,maxNumber) | ||
88 | val res = this.createLocalRepresentation(r.value,r.key) | ||
89 | if(res.modelRepresentation === null) { | ||
90 | throw new IllegalArgumentException('''Model representation is null''') | ||
91 | } else if(res.nodeRepresentations === null || res.nodeRepresentations.empty) { | ||
92 | throw new IllegalArgumentException('''No node representation''') | ||
93 | } else if(res.previousRepresentation !== null) { | ||
94 | throw new IllegalArgumentException('''The previous representation of the first neighbourhood have to be null''') | ||
95 | } else return res | ||
96 | } else if(range > 0) { | ||
97 | val previous = doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,range-1,parallels,maxNumber) | ||
98 | val r = calculateFurtherNodeDescriptors(model,previous,IncomingRelations,OutgoingRelations,parallels,maxNumber) | ||
99 | //println('''Level «range» finished.''') | ||
100 | val res = createFurtherRepresentation(r.key,r.value,previous,deepRepresentation) | ||
101 | if(res.modelRepresentation === null) { | ||
102 | throw new IllegalArgumentException('''Model representation is null''') | ||
103 | } else if(res.nodeRepresentations === null || res.nodeRepresentations.empty) { | ||
104 | throw new IllegalArgumentException('''No node representation''') | ||
105 | } else if(res.previousRepresentation === null && deepRepresentation) { | ||
106 | throw new IllegalArgumentException('''Need previous representations''') | ||
107 | } else return res | ||
108 | } else if (range == FixPointRage) { | ||
109 | return refineUntilFixpoint(model,types,IncomingRelations,OutgoingRelations,parallels,maxNumber) | ||
110 | } | ||
111 | } | ||
112 | |||
113 | def private refineUntilFixpoint( | ||
114 | PartialInterpretation model, | ||
115 | Map<DefinedElement, Set<String>> types, | ||
116 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | ||
117 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | ||
118 | int parallels, int maxNumbers) | ||
119 | { | ||
120 | var lastRange = 0 | ||
121 | val last = calculateLocalNodeDescriptors(model,types,maxNumbers) | ||
122 | var lastRepresentation = this.createLocalRepresentation(last.value,last.key) | ||
123 | //println('''Level 0 finished.''') | ||
124 | var boolean hasRefined | ||
125 | do { | ||
126 | val nextRange = lastRange+1 | ||
127 | val next = calculateFurtherNodeDescriptors(model,lastRepresentation,IncomingRelations,OutgoingRelations,parallels,maxNumbers) | ||
128 | val nextRepresentation = createFurtherRepresentation(next.key,next.value,lastRepresentation,deepRepresentation) | ||
129 | |||
130 | val previousNumberOfTypes =lastRepresentation.nodeRepresentations.values.toSet.size | ||
131 | val nextNumberOfTypes = nextRepresentation.nodeRepresentations.values.toSet.size | ||
132 | hasRefined = nextNumberOfTypes > previousNumberOfTypes | ||
133 | |||
134 | lastRange = nextRange | ||
135 | lastRepresentation = nextRepresentation | ||
136 | |||
137 | // if(hasRefined) { | ||
138 | // println('''Level «nextRange» is calculated, number of types is refined: «previousNumberOfTypes» -> «nextNumberOfTypes»''') | ||
139 | // } else { | ||
140 | // println('''Level «nextRange» is calculated, but the number of types is not refined: «previousNumberOfTypes» = «nextNumberOfTypes»''') | ||
141 | // } | ||
142 | } while (hasRefined) | ||
143 | return lastRepresentation | ||
144 | } | ||
145 | |||
146 | def private getElements(PartialInterpretation model) { | ||
147 | return model.problem.elements + model.newElements | ||
148 | } | ||
149 | |||
150 | def private fillTypes(PartialInterpretation model, Map<DefinedElement, Set<String>> node2Type, Set<TypeDeclaration> relevantTypes) { | ||
151 | for(element : model.elements) { | ||
152 | node2Type.put(element, new HashSet) | ||
153 | } | ||
154 | |||
155 | // for(typeDefinition : model.problem.types.filter(TypeDefinition)) { | ||
156 | // // Dont need | ||
157 | // } | ||
158 | for(typeInterpretation : model.partialtypeinterpratation) { | ||
159 | val type = typeInterpretation.interpretationOf | ||
160 | if(type.isRelevant(relevantTypes)) { | ||
161 | for(element : typeInterpretation.elements) { | ||
162 | element.lookup(node2Type).add(type.name) | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | } | ||
167 | |||
168 | /** | ||
169 | * Indexes the references | ||
170 | */ | ||
171 | def private fillReferences(PartialInterpretation model, | ||
172 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | ||
173 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | ||
174 | Set<RelationDeclaration> relevantRelations) | ||
175 | { | ||
176 | for(object : model.elements) { | ||
177 | IncomingRelations.put(object,new LinkedList) | ||
178 | OutgoingRelations.put(object,new LinkedList) | ||
179 | } | ||
180 | for(relationInterpretation : model.partialrelationinterpretation) { | ||
181 | val type = relationInterpretation.interpretationOf | ||
182 | if(type.isRelevant(relevantRelations)) { | ||
183 | for(link : relationInterpretation.relationlinks) { | ||
184 | if(link instanceof BinaryElementRelationLink) { | ||
185 | OutgoingRelations.get(link.param1) += new OutgoingRelation(link.param2,type.name) | ||
186 | IncomingRelations.get(link.param2) += new IncomingRelation(link.param1,type.name) | ||
187 | } else throw new UnsupportedOperationException | ||
188 | } | ||
189 | } | ||
190 | } | ||
191 | } | ||
192 | |||
193 | /** | ||
194 | * Creates a local representation of the objects (aka zero range neighbourhood) | ||
195 | */ | ||
196 | def abstract protected NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> createLocalRepresentation( | ||
197 | Map<DefinedElement, LocalNodeDescriptor> node2Representation, | ||
198 | Map<LocalNodeDescriptor, Integer> representation2Amount | ||
199 | ) | ||
200 | |||
201 | /** | ||
202 | * Creates a | ||
203 | */ | ||
204 | def abstract protected NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> createFurtherRepresentation( | ||
205 | Map<FurtherNodeDescriptor<NodeRepresentation>, Integer> nodeDescriptors, | ||
206 | Map<DefinedElement, FurtherNodeDescriptor<NodeRepresentation>> node2Representation, | ||
207 | NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> previous, | ||
208 | boolean deepRepresentation | ||
209 | ) | ||
210 | |||
211 | def private addOne(int original, int max) { | ||
212 | if(original == Integer.MAX_VALUE) return Integer.MAX_VALUE | ||
213 | if(original +1 > max) return Integer.MAX_VALUE | ||
214 | else return original+1 | ||
215 | } | ||
216 | |||
217 | private def calculateIncomingEdges(Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | ||
218 | DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel) | ||
219 | { | ||
220 | val Map<IncomingRelation<NodeRepresentation>, Integer> res = new HashMap | ||
221 | for (incomingConcreteEdge : IncomingRelations.get(object)) { | ||
222 | val IncomingRelation<NodeRepresentation> e = new IncomingRelation( | ||
223 | previousNodeRepresentations.get(incomingConcreteEdge.from), incomingConcreteEdge.type) | ||
224 | if (res.containsKey(e)) { | ||
225 | res.put(e, addOne(res.get(e),parallel)) | ||
226 | } else { | ||
227 | res.put(e, 1) | ||
228 | } | ||
229 | } | ||
230 | return res | ||
231 | } | ||
232 | |||
233 | private def calcuateOutgoingEdges(Map<DefinedElement,List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | ||
234 | DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel) | ||
235 | { | ||
236 | val Map<OutgoingRelation<NodeRepresentation>,Integer> res= new HashMap | ||
237 | for(outgoingConcreteEdge : OutgoingRelations.get(object)) { | ||
238 | val OutgoingRelation<NodeRepresentation> e = | ||
239 | new OutgoingRelation( | ||
240 | previousNodeRepresentations.get(outgoingConcreteEdge.to), | ||
241 | outgoingConcreteEdge.type) | ||
242 | if(res.containsKey(e)) { | ||
243 | res.put(e,addOne(res.get(e),parallel)) | ||
244 | } else { | ||
245 | res.put(e,1) | ||
246 | } | ||
247 | } | ||
248 | return res; | ||
249 | } | ||
250 | |||
251 | /*def private <KEY,VALUE> void addOrCreate_Set(Map<KEY,Set<VALUE>> map, KEY key, VALUE value) { | ||
252 | var Set<VALUE> s; | ||
253 | if(map.containsKey(key)) { | ||
254 | s = map.get(key); | ||
255 | } else { | ||
256 | s = new HashSet | ||
257 | map.put(key,s) | ||
258 | } | ||
259 | s.add(value) | ||
260 | }*/ | ||
261 | |||
262 | |||
263 | private def calculateFurtherNodeDescriptors( | ||
264 | PartialInterpretation model, | ||
265 | NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> previous, | ||
266 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | ||
267 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | ||
268 | int parallels, int maxNumber) | ||
269 | { | ||
270 | val previousNodeRepresentations = previous.nodeRepresentations | ||
271 | val node2Representation = new HashMap<DefinedElement,FurtherNodeDescriptor<NodeRepresentation>> | ||
272 | val Map<FurtherNodeDescriptor<NodeRepresentation>,Integer> descriptor2Number = | ||
273 | if(this.mergeSimilarNeighbourhood){ new HashMap } else { null } | ||
274 | val Map<FurtherNodeDescriptor<NodeRepresentation>,FurtherNodeDescriptor<NodeRepresentation>> uniqueDescription = | ||
275 | if(this.mergeSimilarNeighbourhood){ new HashMap } else { null } | ||
276 | |||
277 | for(object: model.elements) { | ||
278 | val incomingEdges = this.calculateIncomingEdges(IncomingRelations, object, previousNodeRepresentations,parallels) | ||
279 | val outgoingEdges = this.calcuateOutgoingEdges(OutgoingRelations,object, previousNodeRepresentations,parallels) | ||
280 | |||
281 | val previousType = previousNodeRepresentations.get(object) | ||
282 | |||
283 | if(previousType === null) { | ||
284 | println("Error in state coder") | ||
285 | } | ||
286 | |||
287 | val nodeDescriptor = new FurtherNodeDescriptor( | ||
288 | previousType, | ||
289 | incomingEdges, | ||
290 | outgoingEdges) | ||
291 | |||
292 | if(this.mergeSimilarNeighbourhood) { | ||
293 | if(descriptor2Number.containsKey(nodeDescriptor)) { | ||
294 | descriptor2Number.put( | ||
295 | nodeDescriptor, | ||
296 | addOne(descriptor2Number.get(nodeDescriptor),maxNumber) | ||
297 | ) | ||
298 | node2Representation.put(object,uniqueDescription.get(nodeDescriptor)) | ||
299 | } else { | ||
300 | descriptor2Number.put(nodeDescriptor,1) | ||
301 | uniqueDescription.put(nodeDescriptor,nodeDescriptor) | ||
302 | node2Representation.put(object,nodeDescriptor) | ||
303 | } | ||
304 | } else { | ||
305 | node2Representation.put(object,nodeDescriptor) | ||
306 | } | ||
307 | } | ||
308 | |||
309 | return descriptor2Number -> node2Representation | ||
310 | } | ||
311 | |||
312 | private def calculateLocalNodeDescriptors( | ||
313 | PartialInterpretation model, | ||
314 | Map<DefinedElement, Set<String>> types, | ||
315 | int maxNumber) | ||
316 | { | ||
317 | val Map<DefinedElement, LocalNodeDescriptor> node2Representation = new HashMap | ||
318 | val Map<LocalNodeDescriptor, Integer> representation2Amount = | ||
319 | if(mergeSimilarNeighbourhood){ new HashMap } else { null } | ||
320 | val Map<LocalNodeDescriptor, LocalNodeDescriptor> uniqueRepresentation = | ||
321 | if(this.mergeSimilarNeighbourhood){ new HashMap } else { null } | ||
322 | |||
323 | for(element : model.elements) { | ||
324 | var newDescriptor = new LocalNodeDescriptor(element.name,element.lookup(types)) | ||
325 | if(this.mergeSimilarNeighbourhood){ | ||
326 | if(uniqueRepresentation.containsKey(newDescriptor)) { | ||
327 | newDescriptor = newDescriptor.lookup(uniqueRepresentation) | ||
328 | node2Representation.put(element,newDescriptor) | ||
329 | representation2Amount.put( | ||
330 | newDescriptor, | ||
331 | addOne(newDescriptor.lookup(representation2Amount),maxNumber) | ||
332 | ) | ||
333 | } else { | ||
334 | uniqueRepresentation.put(newDescriptor,newDescriptor) | ||
335 | node2Representation.put(element,newDescriptor) | ||
336 | representation2Amount.put(newDescriptor, 1) | ||
337 | } | ||
338 | } else { | ||
339 | node2Representation.put(element,newDescriptor) | ||
340 | } | ||
341 | } | ||
342 | |||
343 | return representation2Amount -> node2Representation | ||
344 | } | ||
345 | } \ No newline at end of file | ||