diff options
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, 245 insertions, 211 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 index 6dc40705..54b0f54a 100644 --- 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 | |||
@@ -4,32 +4,34 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | |||
4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration | 4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration | 5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration |
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink | 6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink |
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | 8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation |
9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialPrimitiveInterpretation | ||
10 | import java.util.ArrayList | ||
8 | import java.util.HashMap | 11 | import java.util.HashMap |
9 | import java.util.HashSet | 12 | import java.util.HashSet |
10 | import java.util.LinkedList | ||
11 | import java.util.List | 13 | import java.util.List |
12 | import java.util.Map | 14 | import java.util.Map |
13 | import java.util.Set | 15 | import java.util.Set |
14 | 16 | ||
15 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | 17 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* |
16 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialPrimitiveInterpretation | ||
17 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation | ||
18 | 18 | ||
19 | abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresentation,NodeRepresentation> { | 19 | abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresentation, NodeRepresentation> { |
20 | private val boolean deepRepresentation | 20 | val boolean deepRepresentation |
21 | private val boolean mergeSimilarNeighbourhood | 21 | val boolean mergeSimilarNeighbourhood |
22 | 22 | ||
23 | protected new(boolean deeprepresentation, boolean mergeSimilarNeighbourhood) { | 23 | protected new(boolean deeprepresentation, boolean mergeSimilarNeighbourhood) { |
24 | this.deepRepresentation = deeprepresentation | 24 | this.deepRepresentation = deeprepresentation |
25 | this.mergeSimilarNeighbourhood = mergeSimilarNeighbourhood | 25 | this.mergeSimilarNeighbourhood = mergeSimilarNeighbourhood |
26 | } | 26 | } |
27 | 27 | ||
28 | public static val FixPointRage = -1 | 28 | public static val FixPointRage = NeighbourhoodOptions.FixPointRage |
29 | public static val GraphWidthRange = -2 | 29 | public static val GraphWidthRange = NeighbourhoodOptions.GraphWidthRange |
30 | public static val FullParallels = Integer.MAX_VALUE | 30 | public static val FullParallels = NeighbourhoodOptions.FullParallels |
31 | public static val MaxNumbers = Integer.MAX_VALUE | 31 | public static val MaxNumbers = NeighbourhoodOptions.MaxNumbers |
32 | 32 | ||
33 | static val FOCUSED_ELEMENT_NAME = "<<THIS>>" | ||
34 | |||
33 | /** | 35 | /** |
34 | * Creates a neighbourhood representation with traces | 36 | * Creates a neighbourhood representation with traces |
35 | * @param model The model to be represented. | 37 | * @param model The model to be represented. |
@@ -37,10 +39,15 @@ abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresenta | |||
37 | * @param parallels The maximal number of parallel references to be differentiated. | 39 | * @param parallels The maximal number of parallel references to be differentiated. |
38 | * @param maxNumber The maximal number of elements in a equivalence class that chan be differentiated. | 40 | * @param maxNumber The maximal number of elements in a equivalence class that chan be differentiated. |
39 | */ | 41 | */ |
40 | def public createRepresentation(PartialInterpretation model, int range, int parallels, int maxNumber) { | 42 | def createRepresentation(PartialInterpretation model, int range, int parallels, int maxNumber) { |
41 | return createRepresentation(model,range,parallels,maxNumber,null,null) | 43 | return createRepresentation(model, range, parallels, maxNumber, null, null) |
44 | } | ||
45 | |||
46 | def createRepresentation(PartialInterpretation model, NeighbourhoodOptions options) { | ||
47 | createRepresentation(model, options.range, options.parallels, options.maxNumber, options.relevantTypes, | ||
48 | options.relevantRelations) | ||
42 | } | 49 | } |
43 | 50 | ||
44 | /** | 51 | /** |
45 | * Creates a neighbourhood representation with traces | 52 | * Creates a neighbourhood representation with traces |
46 | * @param model The model to be represented. | 53 | * @param model The model to be represented. |
@@ -48,72 +55,88 @@ abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresenta | |||
48 | * @param parallels The maximal number of parallel references to be differentiated. | 55 | * @param parallels The maximal number of parallel references to be differentiated. |
49 | * @param maxNumber The maximal number of elements in a equivalence class that chan be differentiated. | 56 | * @param maxNumber The maximal number of elements in a equivalence class that chan be differentiated. |
50 | */ | 57 | */ |
51 | def public createRepresentation( | 58 | def createRepresentation(PartialInterpretation model, int range, int parallels, int maxNumber, |
52 | PartialInterpretation model, | 59 | Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations) { |
53 | int range, int parallels, int maxNumber, | 60 | createRepresentationWithFocus(model, range, parallels, maxNumber, relevantTypes, relevantRelations, null) |
54 | Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations) | 61 | } |
55 | { | 62 | |
63 | def createRepresentationWithFocus(PartialInterpretation model, NeighbourhoodOptions options, | ||
64 | DefinedElement focusedElement) { | ||
65 | createRepresentationWithFocus(model, options.range, options.parallels, options.maxNumber, options.relevantTypes, | ||
66 | options.relevantRelations, focusedElement) | ||
67 | } | ||
68 | |||
69 | def createRepresentationWithFocus(PartialInterpretation model, int range, int parallels, int maxNumber, | ||
70 | Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations, DefinedElement focusedElement) { | ||
56 | val Map<DefinedElement, Set<String>> types = new HashMap | 71 | val Map<DefinedElement, Set<String>> types = new HashMap |
57 | fillTypes(model,types,relevantTypes) | 72 | fillTypes(model, types, relevantTypes) |
58 | val Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations = new HashMap; | 73 | val Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations = new HashMap; |
59 | val Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations = new HashMap; | 74 | val Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations = new HashMap; |
60 | fillReferences(model,IncomingRelations,OutgoingRelations,relevantRelations) | 75 | fillReferences(model, IncomingRelations, OutgoingRelations, relevantRelations) |
61 | 76 | ||
62 | val res = doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,range,parallels,maxNumber); | 77 | val res = doRecursiveNeighbourCalculation(model, types, IncomingRelations, OutgoingRelations, range, parallels, |
63 | 78 | maxNumber, focusedElement); | |
79 | |||
64 | return res; | 80 | return res; |
65 | } | 81 | } |
66 | 82 | ||
67 | def private isRelevant(TypeDeclaration t, Set<TypeDeclaration> relevantTypes) { | 83 | def private isRelevant(TypeDeclaration t, Set<TypeDeclaration> relevantTypes) { |
68 | if(relevantTypes === null) { | 84 | if (relevantTypes === null) { |
69 | return true | 85 | return true |
70 | } else { | 86 | } else { |
71 | return relevantTypes.contains(t) | 87 | return relevantTypes.contains(t) |
72 | } | 88 | } |
73 | } | 89 | } |
90 | |||
74 | def private isRelevant(RelationDeclaration r, Set<RelationDeclaration> relevantRelations) { | 91 | def private isRelevant(RelationDeclaration r, Set<RelationDeclaration> relevantRelations) { |
75 | if(relevantRelations === null) { | 92 | if (relevantRelations === null) { |
76 | return true | 93 | return true |
77 | } else { | 94 | } else { |
78 | return relevantRelations.contains(r) | 95 | return relevantRelations.contains(r) |
79 | } | 96 | } |
80 | } | 97 | } |
98 | |||
81 | /** | 99 | /** |
82 | * Gets the largest | 100 | * Gets the minimal neighbourhood size such that every reachable node appears in the shape of every other at least once. |
83 | */ | 101 | */ |
84 | def private getWidth(Map<DefinedElement, Set<String>> types, | 102 | def private getWidth(Map<DefinedElement, Set<String>> types, |
85 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | 103 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, |
86 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations) | 104 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations) { |
87 | { | 105 | val elements = types.keySet |
88 | val elements = types.keySet | 106 | var Map<DefinedElement, Set<DefinedElement>> reachable = new HashMap |
89 | val Map<DefinedElement,Set<DefinedElement>> reachable = new HashMap | 107 | var Map<DefinedElement, Set<DefinedElement>> newReachable = new HashMap |
90 | for(element : elements) { | 108 | for (element : elements) { |
91 | val set = new HashSet | 109 | val set = new HashSet |
92 | set.add(element) | 110 | set.add(element) |
93 | reachable.put(element,set) | 111 | reachable.put(element, new HashSet) |
112 | newReachable.put(element, set) | ||
94 | } | 113 | } |
95 | 114 | ||
96 | var int width = 0 | 115 | var int width = 0 |
97 | var boolean newAdded | 116 | var boolean newAdded |
98 | do { | 117 | do { |
118 | var tmp = reachable | ||
119 | reachable = newReachable | ||
120 | newReachable = tmp | ||
99 | newAdded = false | 121 | newAdded = false |
100 | for(element : elements) { | 122 | for (element : elements) { |
101 | val elementNeigbours = element.lookup(reachable) | 123 | val elementNeigbours = element.lookup(reachable) |
102 | val size = elementNeigbours.size | 124 | val newElementNeigbours = element.lookup(newReachable) |
103 | for(incoming : element.lookup(IncomingRelations)) { | 125 | newElementNeigbours.addAll(elementNeigbours) |
104 | elementNeigbours.addAll(incoming.from.lookup(reachable)) | 126 | for (incoming : element.lookup(IncomingRelations)) { |
127 | newElementNeigbours.addAll(incoming.from.lookup(reachable)) | ||
105 | } | 128 | } |
106 | for(outgoing : element.lookup(OutgoingRelations)) { | 129 | for (outgoing : element.lookup(OutgoingRelations)) { |
107 | elementNeigbours.addAll(outgoing.to.lookup(reachable)) | 130 | newElementNeigbours.addAll(outgoing.to.lookup(reachable)) |
108 | } | 131 | } |
109 | newAdded = newAdded || (elementNeigbours.size > size) | 132 | newAdded = newAdded || (newElementNeigbours.size > elementNeigbours.size) |
110 | } | 133 | } |
111 | 134 | ||
112 | width +=1 | 135 | width += 1 |
113 | } while(newAdded) | 136 | } while (newAdded) |
114 | return width | 137 | return width |
115 | } | 138 | } |
116 | 139 | ||
117 | /** | 140 | /** |
118 | * Creates a neighbourhood representation with traces | 141 | * Creates a neighbourhood representation with traces |
119 | * @param model The model to be represented. | 142 | * @param model The model to be represented. |
@@ -122,68 +145,71 @@ abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresenta | |||
122 | * @param range The range of the neighbourhood. | 145 | * @param range The range of the neighbourhood. |
123 | * @param parallels The maximal number of parallel references to be differentiated. | 146 | * @param parallels The maximal number of parallel references to be differentiated. |
124 | */ | 147 | */ |
125 | def private NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> doRecursiveNeighbourCalculation( | 148 | def private NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> doRecursiveNeighbourCalculation( |
126 | PartialInterpretation model, | 149 | PartialInterpretation model, Map<DefinedElement, Set<String>> types, |
127 | Map<DefinedElement, Set<String>> types, | ||
128 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | 150 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, |
129 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | 151 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, int range, int parallels, |
130 | int range, int parallels, int maxNumber) | 152 | int maxNumber, DefinedElement focusedElement) { |
131 | { | 153 | if (range == 0) { |
132 | if(range == 0){ | 154 | val r = calculateLocalNodeDescriptors(model, types, maxNumber, focusedElement) |
133 | val r = calculateLocalNodeDescriptors(model,types,maxNumber) | 155 | val res = this.createLocalRepresentation(r.value, r.key) |
134 | val res = this.createLocalRepresentation(r.value,r.key) | 156 | if (res.modelRepresentation === null) { |
135 | if(res.modelRepresentation === null) { | ||
136 | throw new IllegalArgumentException('''Model representation is null''') | 157 | throw new IllegalArgumentException('''Model representation is null''') |
137 | } else if(res.nodeRepresentations === null || res.nodeRepresentations.empty) { | 158 | } else if (res.nodeRepresentations === null || res.nodeRepresentations.empty) { |
138 | throw new IllegalArgumentException('''No node representation''') | 159 | throw new IllegalArgumentException('''No node representation''') |
139 | } else if(res.previousRepresentation !== null) { | 160 | } else if (res.previousRepresentation !== null) { |
140 | throw new IllegalArgumentException('''The previous representation of the first neighbourhood have to be null''') | 161 | throw new IllegalArgumentException('''The previous representation of the first neighbourhood have to be null''') |
141 | } else return res | 162 | } else |
142 | } else if(range > 0) { | 163 | return res |
143 | val previous = doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,range-1,parallels,maxNumber) | 164 | } else if (range > 0) { |
144 | val r = calculateFurtherNodeDescriptors(model,previous,IncomingRelations,OutgoingRelations,parallels,maxNumber) | 165 | val previous = doRecursiveNeighbourCalculation(model, types, IncomingRelations, OutgoingRelations, |
145 | //println('''Level «range» finished.''') | 166 | range - 1, parallels, maxNumber, focusedElement) |
146 | val res = createFurtherRepresentation(r.key,r.value,previous,deepRepresentation) | 167 | val r = calculateFurtherNodeDescriptors(model, previous, IncomingRelations, OutgoingRelations, parallels, |
147 | if(res.modelRepresentation === null) { | 168 | maxNumber) |
169 | // println('''Level «range» finished.''') | ||
170 | val res = createFurtherRepresentation(r.key, r.value, previous, deepRepresentation) | ||
171 | if (res.modelRepresentation === null) { | ||
148 | throw new IllegalArgumentException('''Model representation is null''') | 172 | throw new IllegalArgumentException('''Model representation is null''') |
149 | } else if(res.nodeRepresentations === null || res.nodeRepresentations.empty) { | 173 | } else if (res.nodeRepresentations === null || res.nodeRepresentations.empty) { |
150 | throw new IllegalArgumentException('''No node representation''') | 174 | throw new IllegalArgumentException('''No node representation''') |
151 | } else if(res.previousRepresentation === null && deepRepresentation) { | 175 | } else if (res.previousRepresentation === null && deepRepresentation) { |
152 | throw new IllegalArgumentException('''Need previous representations''') | 176 | throw new IllegalArgumentException('''Need previous representations''') |
153 | } else return res | 177 | } else |
178 | return res | ||
154 | } else if (range == FixPointRage) { | 179 | } else if (range == FixPointRage) { |
155 | return refineUntilFixpoint(model,types,IncomingRelations,OutgoingRelations,parallels,maxNumber) | 180 | return refineUntilFixpoint(model, types, IncomingRelations, OutgoingRelations, parallels, maxNumber, |
181 | focusedElement) | ||
156 | } else if (range == GraphWidthRange) { | 182 | } else if (range == GraphWidthRange) { |
157 | val width = this.getWidth(types,IncomingRelations,OutgoingRelations) | 183 | val width = this.getWidth(types, IncomingRelations, OutgoingRelations) |
158 | //println(width) | 184 | // println(width) |
159 | return doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,width,parallels,maxNumber) | 185 | return doRecursiveNeighbourCalculation(model, types, IncomingRelations, OutgoingRelations, width, parallels, |
186 | maxNumber, focusedElement) | ||
160 | } | 187 | } |
161 | } | 188 | } |
162 | 189 | ||
163 | def private refineUntilFixpoint( | 190 | def private refineUntilFixpoint(PartialInterpretation model, Map<DefinedElement, Set<String>> types, |
164 | PartialInterpretation model, | ||
165 | Map<DefinedElement, Set<String>> types, | ||
166 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | 191 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, |
167 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | 192 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, int parallels, int maxNumbers, |
168 | int parallels, int maxNumbers) | 193 | DefinedElement focusedElement) { |
169 | { | ||
170 | var lastRange = 0 | 194 | var lastRange = 0 |
171 | val last = calculateLocalNodeDescriptors(model,types,maxNumbers) | 195 | val last = calculateLocalNodeDescriptors(model, types, maxNumbers, focusedElement) |
172 | var lastRepresentation = this.createLocalRepresentation(last.value,last.key) | 196 | var lastRepresentation = this.createLocalRepresentation(last.value, last.key) |
173 | //println('''Level 0 finished.''') | 197 | // println('''Level 0 finished.''') |
174 | var boolean hasRefined | 198 | var boolean hasRefined |
175 | do { | 199 | do { |
176 | val nextRange = lastRange+1 | 200 | val nextRange = lastRange + 1 |
177 | val next = calculateFurtherNodeDescriptors(model,lastRepresentation,IncomingRelations,OutgoingRelations,parallels,maxNumbers) | 201 | val next = calculateFurtherNodeDescriptors(model, lastRepresentation, IncomingRelations, OutgoingRelations, |
178 | val nextRepresentation = createFurtherRepresentation(next.key,next.value,lastRepresentation,deepRepresentation) | 202 | parallels, maxNumbers) |
179 | 203 | val nextRepresentation = createFurtherRepresentation(next.key, next.value, lastRepresentation, | |
180 | val previousNumberOfTypes =lastRepresentation.nodeRepresentations.values.toSet.size | 204 | deepRepresentation) |
205 | |||
206 | val previousNumberOfTypes = lastRepresentation.nodeRepresentations.values.toSet.size | ||
181 | val nextNumberOfTypes = nextRepresentation.nodeRepresentations.values.toSet.size | 207 | val nextNumberOfTypes = nextRepresentation.nodeRepresentations.values.toSet.size |
182 | hasRefined = nextNumberOfTypes > previousNumberOfTypes | 208 | hasRefined = nextNumberOfTypes > previousNumberOfTypes |
183 | 209 | ||
184 | lastRange = nextRange | 210 | lastRange = nextRange |
185 | lastRepresentation = nextRepresentation | 211 | lastRepresentation = nextRepresentation |
186 | 212 | ||
187 | // if(hasRefined) { | 213 | // if(hasRefined) { |
188 | // println('''Level «nextRange» is calculated, number of types is refined: «previousNumberOfTypes» -> «nextNumberOfTypes»''') | 214 | // println('''Level «nextRange» is calculated, number of types is refined: «previousNumberOfTypes» -> «nextNumberOfTypes»''') |
189 | // } else { | 215 | // } else { |
@@ -192,211 +218,219 @@ abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresenta | |||
192 | } while (hasRefined) | 218 | } while (hasRefined) |
193 | return lastRepresentation | 219 | return lastRepresentation |
194 | } | 220 | } |
195 | 221 | ||
196 | def private getElements(PartialInterpretation model) { | 222 | def private getElements(PartialInterpretation model) { |
197 | return | 223 | return model.problem.elements + model.newElements + model.openWorldElements |
198 | model.problem.elements + | ||
199 | model.newElements + | ||
200 | model.openWorldElements | ||
201 | } | 224 | } |
202 | 225 | ||
203 | def private fillTypes(PartialInterpretation model, Map<DefinedElement, Set<String>> node2Type, Set<TypeDeclaration> relevantTypes) { | 226 | def private fillTypes(PartialInterpretation model, Map<DefinedElement, Set<String>> node2Type, |
204 | for(element : model.elements) { | 227 | Set<TypeDeclaration> relevantTypes) { |
228 | for (element : model.elements) { | ||
205 | node2Type.put(element, new HashSet) | 229 | node2Type.put(element, new HashSet) |
206 | } | 230 | } |
207 | 231 | ||
208 | // for(typeDefinition : model.problem.types.filter(TypeDefinition)) { | 232 | // for(typeDefinition : model.problem.types.filter(TypeDefinition)) { |
209 | // // Dont need | 233 | // // Dont need |
210 | // } | 234 | // } |
211 | for(typeInterpretation : model.partialtypeinterpratation) { | 235 | for (typeInterpretation : model.partialtypeinterpratation) { |
212 | if(typeInterpretation instanceof PartialPrimitiveInterpretation) { | 236 | if (typeInterpretation instanceof PartialPrimitiveInterpretation) { |
213 | 237 | } else if (typeInterpretation instanceof PartialComplexTypeInterpretation) { | |
214 | } else if(typeInterpretation instanceof PartialComplexTypeInterpretation) { | ||
215 | val type = typeInterpretation.interpretationOf | 238 | val type = typeInterpretation.interpretationOf |
216 | if(type.isRelevant(relevantTypes)) { | 239 | if (type.isRelevant(relevantTypes)) { |
217 | for(element : typeInterpretation.elements) { | 240 | for (element : typeInterpretation.elements) { |
218 | element.lookup(node2Type).add(type.name) | 241 | element.lookup(node2Type).add(type.name) |
219 | } | 242 | } |
220 | } | 243 | } |
221 | } | 244 | } |
222 | } | 245 | } |
223 | } | 246 | } |
224 | 247 | ||
225 | /** | 248 | /** |
226 | * Indexes the references | 249 | * Indexes the references |
227 | */ | 250 | */ |
228 | def private fillReferences(PartialInterpretation model, | 251 | def private fillReferences(PartialInterpretation model, |
229 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | 252 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, |
230 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | 253 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, |
231 | Set<RelationDeclaration> relevantRelations) | 254 | Set<RelationDeclaration> relevantRelations) { |
232 | { | 255 | for (object : model.elements) { |
233 | for(object : model.elements) { | 256 | IncomingRelations.put(object, new ArrayList) |
234 | IncomingRelations.put(object,new LinkedList) | 257 | OutgoingRelations.put(object, new ArrayList) |
235 | OutgoingRelations.put(object,new LinkedList) | ||
236 | } | 258 | } |
237 | for(relationInterpretation : model.partialrelationinterpretation) { | 259 | for (relationInterpretation : model.partialrelationinterpretation) { |
238 | val type = relationInterpretation.interpretationOf | 260 | val type = relationInterpretation.interpretationOf |
239 | if(type.isRelevant(relevantRelations)) { | 261 | if (type.isRelevant(relevantRelations)) { |
240 | for(link : relationInterpretation.relationlinks) { | 262 | for (link : relationInterpretation.relationlinks) { |
241 | if(link instanceof BinaryElementRelationLink) { | 263 | if (link instanceof BinaryElementRelationLink) { |
242 | OutgoingRelations.get(link.param1) += new OutgoingRelation(link.param2,type.name) | 264 | OutgoingRelations.get(link.param1) += new OutgoingRelation(link.param2, type.name) |
243 | IncomingRelations.get(link.param2) += new IncomingRelation(link.param1,type.name) | 265 | IncomingRelations.get(link.param2) += new IncomingRelation(link.param1, type.name) |
244 | } else throw new UnsupportedOperationException | 266 | } else |
267 | throw new UnsupportedOperationException | ||
245 | } | 268 | } |
246 | } | 269 | } |
247 | } | 270 | } |
248 | } | 271 | } |
249 | 272 | ||
250 | /** | 273 | /** |
251 | * Creates a local representation of the objects (aka zero range neighbourhood) | 274 | * Creates a local representation of the objects (aka zero range neighbourhood) |
252 | */ | 275 | */ |
253 | def abstract protected NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> createLocalRepresentation( | 276 | def abstract protected NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> createLocalRepresentation( |
254 | Map<DefinedElement, LocalNodeDescriptor> node2Representation, | 277 | Map<DefinedElement, LocalNodeDescriptor> node2Representation, |
255 | Map<LocalNodeDescriptor, Integer> representation2Amount | 278 | Map<LocalNodeDescriptor, Integer> representation2Amount |
256 | ) | 279 | ) |
257 | 280 | ||
258 | /** | 281 | /** |
259 | * Creates a | 282 | * Creates a |
260 | */ | 283 | */ |
261 | def abstract protected NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> createFurtherRepresentation( | 284 | def abstract protected NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> createFurtherRepresentation( |
262 | Map<FurtherNodeDescriptor<NodeRepresentation>, Integer> nodeDescriptors, | 285 | Map<FurtherNodeDescriptor<NodeRepresentation>, Integer> nodeDescriptors, |
263 | Map<DefinedElement, FurtherNodeDescriptor<NodeRepresentation>> node2Representation, | 286 | Map<DefinedElement, FurtherNodeDescriptor<NodeRepresentation>> node2Representation, |
264 | NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> previous, | 287 | NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> previous, |
265 | boolean deepRepresentation | 288 | boolean deepRepresentation |
266 | ) | 289 | ) |
267 | 290 | ||
268 | def private addOne(int original, int max) { | 291 | def private addOne(int original, int max) { |
269 | if(original == Integer.MAX_VALUE) return Integer.MAX_VALUE | 292 | if(original == Integer.MAX_VALUE) return Integer.MAX_VALUE |
270 | if(original +1 > max) return Integer.MAX_VALUE | 293 | if(original + 1 > max) return Integer.MAX_VALUE else return original + 1 |
271 | else return original+1 | ||
272 | } | 294 | } |
273 | 295 | ||
274 | private def calculateIncomingEdges(Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | 296 | private def calculateIncomingEdges(Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, |
275 | DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel) | 297 | DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, |
276 | { | 298 | int parallel) { |
277 | val Map<IncomingRelation<NodeRepresentation>, Integer> res = new HashMap | 299 | val Map<IncomingRelation<NodeRepresentation>, Integer> res = new HashMap |
278 | for (incomingConcreteEdge : IncomingRelations.get(object)) { | 300 | for (incomingConcreteEdge : IncomingRelations.get(object)) { |
279 | val IncomingRelation<NodeRepresentation> e = new IncomingRelation( | 301 | val IncomingRelation<NodeRepresentation> e = new IncomingRelation( |
280 | previousNodeRepresentations.get(incomingConcreteEdge.from), incomingConcreteEdge.type) | 302 | previousNodeRepresentations.get(incomingConcreteEdge.from), incomingConcreteEdge.type) |
281 | if (res.containsKey(e)) { | 303 | if (res.containsKey(e)) { |
282 | res.put(e, addOne(res.get(e),parallel)) | 304 | res.put(e, addOne(res.get(e), parallel)) |
283 | } else { | 305 | } else { |
284 | res.put(e, 1) | 306 | res.put(e, 1) |
285 | } | 307 | } |
286 | } | 308 | } |
287 | return res | 309 | return res |
288 | } | 310 | } |
289 | 311 | ||
290 | private def calcuateOutgoingEdges(Map<DefinedElement,List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | 312 | private def calcuateOutgoingEdges(Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, |
291 | DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel) | 313 | DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, |
292 | { | 314 | int parallel) { |
293 | val Map<OutgoingRelation<NodeRepresentation>,Integer> res= new HashMap | 315 | val Map<OutgoingRelation<NodeRepresentation>, Integer> res = new HashMap |
294 | for(outgoingConcreteEdge : OutgoingRelations.get(object)) { | 316 | for (outgoingConcreteEdge : OutgoingRelations.get(object)) { |
295 | val OutgoingRelation<NodeRepresentation> e = | 317 | val OutgoingRelation<NodeRepresentation> e = new OutgoingRelation( |
296 | new OutgoingRelation( | 318 | previousNodeRepresentations.get(outgoingConcreteEdge.to), outgoingConcreteEdge.type) |
297 | previousNodeRepresentations.get(outgoingConcreteEdge.to), | 319 | if (res.containsKey(e)) { |
298 | outgoingConcreteEdge.type) | 320 | res.put(e, addOne(res.get(e), parallel)) |
299 | if(res.containsKey(e)) { | ||
300 | res.put(e,addOne(res.get(e),parallel)) | ||
301 | } else { | 321 | } else { |
302 | res.put(e,1) | 322 | res.put(e, 1) |
303 | } | 323 | } |
304 | } | 324 | } |
305 | return res; | 325 | return res; |
306 | } | 326 | } |
307 | 327 | ||
308 | /*def private <KEY,VALUE> void addOrCreate_Set(Map<KEY,Set<VALUE>> map, KEY key, VALUE value) { | 328 | /*def private <KEY,VALUE> void addOrCreate_Set(Map<KEY,Set<VALUE>> map, KEY key, VALUE value) { |
309 | var Set<VALUE> s; | 329 | * var Set<VALUE> s; |
310 | if(map.containsKey(key)) { | 330 | * if(map.containsKey(key)) { |
311 | s = map.get(key); | 331 | * s = map.get(key); |
312 | } else { | 332 | * } else { |
313 | s = new HashSet | 333 | * s = new HashSet |
314 | map.put(key,s) | 334 | * map.put(key,s) |
315 | } | 335 | * } |
316 | s.add(value) | 336 | * s.add(value) |
317 | }*/ | 337 | }*/ |
318 | 338 | private def calculateFurtherNodeDescriptors(PartialInterpretation model, | |
319 | |||
320 | private def calculateFurtherNodeDescriptors( | ||
321 | PartialInterpretation model, | ||
322 | NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> previous, | 339 | NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> previous, |
323 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, | 340 | Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, |
324 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, | 341 | Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, int parallels, int maxNumber) { |
325 | int parallels, int maxNumber) | ||
326 | { | ||
327 | val previousNodeRepresentations = previous.nodeRepresentations | 342 | val previousNodeRepresentations = previous.nodeRepresentations |
328 | val node2Representation = new HashMap<DefinedElement,FurtherNodeDescriptor<NodeRepresentation>> | 343 | val node2Representation = new HashMap<DefinedElement, FurtherNodeDescriptor<NodeRepresentation>> |
329 | val Map<FurtherNodeDescriptor<NodeRepresentation>,Integer> descriptor2Number = | 344 | val Map<FurtherNodeDescriptor<NodeRepresentation>, Integer> descriptor2Number = if (this. |
330 | if(this.mergeSimilarNeighbourhood){ new HashMap } else { null } | 345 | mergeSimilarNeighbourhood) { |
331 | val Map<FurtherNodeDescriptor<NodeRepresentation>,FurtherNodeDescriptor<NodeRepresentation>> uniqueDescription = | 346 | new HashMap |
332 | if(this.mergeSimilarNeighbourhood){ new HashMap } else { null } | 347 | } else { |
333 | 348 | null | |
334 | for(object: model.elements) { | 349 | } |
335 | val incomingEdges = this.calculateIncomingEdges(IncomingRelations, object, previousNodeRepresentations,parallels) | 350 | val Map<FurtherNodeDescriptor<NodeRepresentation>, FurtherNodeDescriptor<NodeRepresentation>> uniqueDescription = if (this. |
336 | val outgoingEdges = this.calcuateOutgoingEdges(OutgoingRelations,object, previousNodeRepresentations,parallels) | 351 | mergeSimilarNeighbourhood) { |
337 | 352 | new HashMap | |
353 | } else { | ||
354 | null | ||
355 | } | ||
356 | |||
357 | for (object : model.elements) { | ||
358 | val incomingEdges = this.calculateIncomingEdges(IncomingRelations, object, previousNodeRepresentations, | ||
359 | parallels) | ||
360 | val outgoingEdges = this.calcuateOutgoingEdges(OutgoingRelations, object, previousNodeRepresentations, | ||
361 | parallels) | ||
362 | |||
338 | val previousType = previousNodeRepresentations.get(object) | 363 | val previousType = previousNodeRepresentations.get(object) |
339 | 364 | ||
340 | if(previousType === null) { | 365 | if (previousType === null) { |
341 | println("Error in state coder") | 366 | println("Error in state coder") |
342 | } | 367 | } |
343 | 368 | ||
344 | val nodeDescriptor = new FurtherNodeDescriptor( | 369 | val nodeDescriptor = new FurtherNodeDescriptor(previousType, incomingEdges, outgoingEdges) |
345 | previousType, | 370 | |
346 | incomingEdges, | 371 | if (this.mergeSimilarNeighbourhood) { |
347 | outgoingEdges) | 372 | if (descriptor2Number.containsKey(nodeDescriptor)) { |
348 | |||
349 | if(this.mergeSimilarNeighbourhood) { | ||
350 | if(descriptor2Number.containsKey(nodeDescriptor)) { | ||
351 | descriptor2Number.put( | 373 | descriptor2Number.put( |
352 | nodeDescriptor, | 374 | nodeDescriptor, |
353 | addOne(descriptor2Number.get(nodeDescriptor),maxNumber) | 375 | addOne(descriptor2Number.get(nodeDescriptor), maxNumber) |
354 | ) | 376 | ) |
355 | node2Representation.put(object,uniqueDescription.get(nodeDescriptor)) | 377 | node2Representation.put(object, uniqueDescription.get(nodeDescriptor)) |
356 | } else { | 378 | } else { |
357 | descriptor2Number.put(nodeDescriptor,if(1>maxNumber){Integer.MAX_VALUE}else{1}) | 379 | descriptor2Number.put(nodeDescriptor, if (1 > maxNumber) { |
358 | uniqueDescription.put(nodeDescriptor,nodeDescriptor) | 380 | Integer.MAX_VALUE |
359 | node2Representation.put(object,nodeDescriptor) | 381 | } else { |
382 | 1 | ||
383 | }) | ||
384 | uniqueDescription.put(nodeDescriptor, nodeDescriptor) | ||
385 | node2Representation.put(object, nodeDescriptor) | ||
360 | } | 386 | } |
361 | } else { | 387 | } else { |
362 | node2Representation.put(object,nodeDescriptor) | 388 | node2Representation.put(object, nodeDescriptor) |
363 | } | 389 | } |
364 | } | 390 | } |
365 | 391 | ||
366 | return descriptor2Number -> node2Representation | 392 | return descriptor2Number -> node2Representation |
367 | } | 393 | } |
368 | 394 | ||
369 | private def calculateLocalNodeDescriptors( | 395 | private def calculateLocalNodeDescriptors(PartialInterpretation model, Map<DefinedElement, Set<String>> types, |
370 | PartialInterpretation model, | 396 | int maxNumber, DefinedElement focusedElement) { |
371 | Map<DefinedElement, Set<String>> types, | ||
372 | int maxNumber) | ||
373 | { | ||
374 | val Map<DefinedElement, LocalNodeDescriptor> node2Representation = new HashMap | 397 | val Map<DefinedElement, LocalNodeDescriptor> node2Representation = new HashMap |
375 | val Map<LocalNodeDescriptor, Integer> representation2Amount = | 398 | val Map<LocalNodeDescriptor, Integer> representation2Amount = if (mergeSimilarNeighbourhood) { |
376 | if(mergeSimilarNeighbourhood){ new HashMap } else { null } | 399 | new HashMap |
377 | val Map<LocalNodeDescriptor, LocalNodeDescriptor> uniqueRepresentation = | 400 | } else { |
378 | if(this.mergeSimilarNeighbourhood){ new HashMap } else { null } | 401 | null |
379 | 402 | } | |
380 | for(element : model.elements) { | 403 | val Map<LocalNodeDescriptor, LocalNodeDescriptor> uniqueRepresentation = if (this.mergeSimilarNeighbourhood) { |
381 | var newDescriptor = new LocalNodeDescriptor(element.name,element.lookup(types)) | 404 | new HashMap |
382 | if(this.mergeSimilarNeighbourhood){ | 405 | } else { |
383 | if(uniqueRepresentation.containsKey(newDescriptor)) { | 406 | null |
407 | } | ||
408 | |||
409 | for (element : model.elements) { | ||
410 | val name = if(element == focusedElement) FOCUSED_ELEMENT_NAME else element.name | ||
411 | var newDescriptor = new LocalNodeDescriptor(name, element.lookup(types)) | ||
412 | if (this.mergeSimilarNeighbourhood) { | ||
413 | if (uniqueRepresentation.containsKey(newDescriptor)) { | ||
384 | newDescriptor = newDescriptor.lookup(uniqueRepresentation) | 414 | newDescriptor = newDescriptor.lookup(uniqueRepresentation) |
385 | node2Representation.put(element,newDescriptor) | 415 | node2Representation.put(element, newDescriptor) |
386 | representation2Amount.put( | 416 | representation2Amount.put( |
387 | newDescriptor, | 417 | newDescriptor, |
388 | addOne(newDescriptor.lookup(representation2Amount),maxNumber) | 418 | addOne(newDescriptor.lookup(representation2Amount), maxNumber) |
389 | ) | 419 | ) |
390 | } else { | 420 | } else { |
391 | uniqueRepresentation.put(newDescriptor,newDescriptor) | 421 | uniqueRepresentation.put(newDescriptor, newDescriptor) |
392 | node2Representation.put(element,newDescriptor) | 422 | node2Representation.put(element, newDescriptor) |
393 | representation2Amount.put(newDescriptor, if(1>maxNumber){Integer.MAX_VALUE}else{1}) | 423 | representation2Amount.put(newDescriptor, if (1 > maxNumber) { |
424 | Integer.MAX_VALUE | ||
425 | } else { | ||
426 | 1 | ||
427 | }) | ||
394 | } | 428 | } |
395 | } else { | 429 | } else { |
396 | node2Representation.put(element,newDescriptor) | 430 | node2Representation.put(element, newDescriptor) |
397 | } | 431 | } |
398 | } | 432 | } |
399 | 433 | ||
400 | return representation2Amount -> node2Representation | 434 | return representation2Amount -> node2Representation |
401 | } | 435 | } |
402 | } \ No newline at end of file | 436 | } |