aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2NeighbourhoodRepresentation.xtend
diff options
context:
space:
mode:
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2NeighbourhoodRepresentation.xtend')
-rw-r--r--Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2NeighbourhoodRepresentation.xtend709
1 files changed, 411 insertions, 298 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 bf593add..8b5d7f2c 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,109 +4,189 @@ import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
4import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration 4import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration
5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration 5import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration
6import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink 6import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BinaryElementRelationLink
7import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation
7import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation 8import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
9import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialPrimitiveInterpretation
8import java.util.HashMap 10import java.util.HashMap
9import java.util.HashSet 11import java.util.HashSet
10import java.util.LinkedList 12import java.util.LinkedList
11import java.util.List 13import java.util.List
12import java.util.Map 14import java.util.Map
13import java.util.Set 15import java.util.Set
16import org.eclipse.viatra.query.runtime.api.IPatternMatch
17import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher
14 18
15import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* 19import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*
16import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialPrimitiveInterpretation 20import java.util.ArrayList
17import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation
18 21
19abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresentation,NodeRepresentation> { 22abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresentation, NodeRepresentation> {
23 /**
24 * Includes shape of a smaller range in the current shape
25 */
20 private val boolean deepRepresentation 26 private val boolean deepRepresentation
27 /**
28 * Treats similar shapes as one (recommended)
29 */
21 private val boolean mergeSimilarNeighbourhood 30 private val boolean mergeSimilarNeighbourhood
22 31
23 protected new(boolean deeprepresentation, boolean mergeSimilarNeighbourhood) { 32 protected new(boolean deeprepresentation, boolean mergeSimilarNeighbourhood) {
24 this.deepRepresentation = deeprepresentation 33 this.deepRepresentation = deeprepresentation
25 this.mergeSimilarNeighbourhood = mergeSimilarNeighbourhood 34 this.mergeSimilarNeighbourhood = mergeSimilarNeighbourhood
26 } 35 }
27 36
37 /**
38 * Constant to be used as the range parameter of the createRepresentation function for enforcing fix point semantics instead of a predefined range
39 */
28 public static val FixPointRage = -1 40 public static val FixPointRage = -1
41 /**
42 * Constant to be used as the range parameter of the createRepresentation function for using the width of the graph as the range
43 */
29 public static val GraphWidthRange = -2 44 public static val GraphWidthRange = -2
45 /**
46 * Constant to be used as the parallels parameter of the createRepresentation function for prohibiting cardinality abstractions
47 */
30 public static val FullParallels = Integer.MAX_VALUE 48 public static val FullParallels = Integer.MAX_VALUE
49 /**
50 * Constant to be used as the parallels parameter of the createRepresentation function for prohibiting multiplicity abstractions
51 */
31 public static val MaxNumbers = Integer.MAX_VALUE 52 public static val MaxNumbers = Integer.MAX_VALUE
32 53
33 /** 54 /**
34 * Creates a neighbourhood representation with traces 55 * Creates a neighbourhood representation with traces
35 * @param model The model to be represented. 56 * @param model The model to be represented.
36 * @param range The range of the neighbourhood. 57 * @param range The range of the neighbourhood.
37 * @param parallels The maximal number of parallel references to be differentiated. 58 * @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. 59 * @param maxNumber The maximal number of elements in a equivalence class that can be differentiated.
39 */ 60 */
40 def public createRepresentation(PartialInterpretation model, int range, int parallels, int maxNumber) { 61 def public createRepresentation(PartialInterpretation model, int range, int parallels, int maxNumber) {
41 return createRepresentation(model,range,parallels,maxNumber,null,null) 62 return createRepresentation(model, range, parallels, maxNumber, null, null, null)
42 } 63 }
43 64
44 def public createRepresentation( 65 /**
45 PartialInterpretation model, 66 * Creates a neighbourhood representation with traces
46 int range, int parallels, int maxNumber, 67 * @param model The model to be represented.
47 Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations) 68 * @param range The range of the neighbourhood.
48 { 69 * @param parallels The maximal number of parallel references to be differentiated.
70 * @param maxNumber The maximal number of elements in a equivalence class that can be differentiated.
71 * @param relevantTypes The set of types to include in the diversity calculations (when value is null, all types are considered)
72 * @param relevantRelations The set of relations to include in the diversity calculations (when value is null, all relations are considered)
73 * @param relevantPatterns The set of patterns to include in the diversity calculations
74 * (NOT the well-formedness constraints) - when value is null, NO patterns are considered
75 */
76 def public createRepresentation(PartialInterpretation model, int range, int parallels, int maxNumber,
77 Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations,
78 Set<ViatraQueryMatcher<? extends IPatternMatch>> relevantPatterns) {
79
49 val Map<DefinedElement, Set<String>> types = new HashMap 80 val Map<DefinedElement, Set<String>> types = new HashMap
50 fillTypes(model,types,relevantTypes) 81 fillTypes(model, types, relevantTypes)
51 val Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations = new HashMap; 82 val Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations = new HashMap;
52 val Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations = new HashMap; 83 val Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations = new HashMap;
53 fillReferences(model,IncomingRelations,OutgoingRelations,relevantRelations) 84 fillReferences(model, IncomingRelations, OutgoingRelations, relevantRelations);
85 val Map<DefinedElement, List<PatternRelation<DefinedElement>>> PatternRelations = createPatternRelations(model,
86 relevantPatterns);
54 87
55 val res = doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,range,parallels,maxNumber);
56 88
89
90 val res = doRecursiveNeighbourCalculation(model, types, IncomingRelations, OutgoingRelations, PatternRelations,range, parallels,
91 maxNumber);
92
57 return res; 93 return res;
58 } 94 }
59 95
96 /**
97 * Collects all pattern relations for shaping
98 */
99 def Map<DefinedElement, List<PatternRelation<DefinedElement>>> createPatternRelations(
100 PartialInterpretation model, Set<ViatraQueryMatcher<? extends IPatternMatch>> relevantPatterns) {
101 val Map<DefinedElement, List<PatternRelation<DefinedElement>>> result=new HashMap;
102
103 for (element : model.elements) {
104 result.put(element,new LinkedList<PatternRelation<DefinedElement>>)
105 }
106 if (relevantPatterns===null) return result;
107 for (pattern : relevantPatterns) {
108 val name=pattern.patternName
109 val arity=pattern.parameterNames.size
110
111 val ignoredParams=new HashSet<String>();
112
113 for (match : pattern.allMatches) {
114 val sanitizedMatch=new ArrayList<DefinedElement>();
115 for (var i=0; i<arity;i++) {
116 val matchedElement=match.get(i)
117 if (matchedElement instanceof DefinedElement) {
118 sanitizedMatch.add(matchedElement)
119 } else {
120 ignoredParams.add(pattern.parameterNames.get(i))
121 }
122 }
123 var id=0;
124 for (DefinedElement e:sanitizedMatch) {
125 if (e!==null) {
126 result.get(e).add(new PatternRelation<DefinedElement>(name,id,sanitizedMatch))
127 }
128 id++
129 }
130 }
131
132 println('''Ignored params '''+ignoredParams+''' of pattern '''+ name)
133 }
134
135 return result;
136
137 }
138
60 def private isRelevant(TypeDeclaration t, Set<TypeDeclaration> relevantTypes) { 139 def private isRelevant(TypeDeclaration t, Set<TypeDeclaration> relevantTypes) {
61 if(relevantTypes === null) { 140 if (relevantTypes === null) {
62 return true 141 return true
63 } else { 142 } else {
64 return relevantTypes.contains(t) 143 return relevantTypes.contains(t)
65 } 144 }
66 } 145 }
146
67 def private isRelevant(RelationDeclaration r, Set<RelationDeclaration> relevantRelations) { 147 def private isRelevant(RelationDeclaration r, Set<RelationDeclaration> relevantRelations) {
68 if(relevantRelations === null) { 148 if (relevantRelations === null) {
69 return true 149 return true
70 } else { 150 } else {
71 return relevantRelations.contains(r) 151 return relevantRelations.contains(r)
72 } 152 }
73 } 153 }
154
74 /** 155 /**
75 * Gets the largest 156 * @returns the diameter (maximal size of shortest paths between nodes) of the partial model
76 */ 157 */
77 def private getWidth(Map<DefinedElement, Set<String>> types, 158 def private getWidth(Map<DefinedElement, Set<String>> types,
78 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, 159 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
79 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations) 160 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations) {
80 { 161 val elements = types.keySet
81 val elements = types.keySet 162 val Map<DefinedElement, Set<DefinedElement>> reachable = new HashMap
82 val Map<DefinedElement,Set<DefinedElement>> reachable = new HashMap 163 for (element : elements) {
83 for(element : elements) {
84 val set = new HashSet 164 val set = new HashSet
85 set.add(element) 165 set.add(element)
86 reachable.put(element,set) 166 reachable.put(element, set)
87 } 167 }
88 168
89 var int width = 0 169 var int width = 0
90 var boolean newAdded 170 var boolean newAdded
91 do { 171 do {
92 newAdded = false 172 newAdded = false
93 for(element : elements) { 173 for (element : elements) {
94 val elementNeigbours = element.lookup(reachable) 174 val elementNeigbours = element.lookup(reachable)
95 val size = elementNeigbours.size 175 val size = elementNeigbours.size
96 for(incoming : element.lookup(IncomingRelations)) { 176 for (incoming : element.lookup(IncomingRelations)) {
97 elementNeigbours.addAll(incoming.from.lookup(reachable)) 177 elementNeigbours.addAll(incoming.from.lookup(reachable))
98 } 178 }
99 for(outgoing : element.lookup(OutgoingRelations)) { 179 for (outgoing : element.lookup(OutgoingRelations)) {
100 elementNeigbours.addAll(outgoing.to.lookup(reachable)) 180 elementNeigbours.addAll(outgoing.to.lookup(reachable))
101 } 181 }
102 newAdded = newAdded || (elementNeigbours.size > size) 182 newAdded = newAdded || (elementNeigbours.size > size)
103 } 183 }
104 184
105 width +=1 185 width += 1
106 } while(newAdded) 186 } while (newAdded)
107 return width 187 return width
108 } 188 }
109 189
110 /** 190 /**
111 * Creates a neighbourhood representation with traces 191 * Creates a neighbourhood representation with traces
112 * @param model The model to be represented. 192 * @param model The model to be represented.
@@ -115,281 +195,314 @@ abstract class PartialInterpretation2NeighbourhoodRepresentation<ModelRepresenta
115 * @param range The range of the neighbourhood. 195 * @param range The range of the neighbourhood.
116 * @param parallels The maximal number of parallel references to be differentiated. 196 * @param parallels The maximal number of parallel references to be differentiated.
117 */ 197 */
118 def private NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> doRecursiveNeighbourCalculation( 198 def private NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> doRecursiveNeighbourCalculation(
119 PartialInterpretation model, 199 PartialInterpretation model, Map<DefinedElement, Set<String>> types,
120 Map<DefinedElement, Set<String>> types,
121 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, 200 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
122 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, 201 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations,
123 int range, int parallels, int maxNumber) 202 Map<DefinedElement, List<PatternRelation<DefinedElement>>> PatternRelations, int range, int parallels,
124 { 203 int maxNumber) {
125 if(range == 0){ 204 if (range == 0) {
126 val r = calculateLocalNodeDescriptors(model,types,maxNumber) 205 val r = calculateLocalNodeDescriptors(model, types, maxNumber)
127 val res = this.createLocalRepresentation(r.value,r.key) 206 val res = this.createLocalRepresentation(r.value, r.key)
128 if(res.modelRepresentation === null) { 207 if (res.modelRepresentation === null) {
129 throw new IllegalArgumentException('''Model representation is null''') 208 throw new IllegalArgumentException('''Model representation is null''')
130 } else if(res.nodeRepresentations === null || res.nodeRepresentations.empty) { 209 } else if (res.nodeRepresentations === null || res.nodeRepresentations.empty) {
131 throw new IllegalArgumentException('''No node representation''') 210 throw new IllegalArgumentException('''No node representation''')
132 } else if(res.previousRepresentation !== null) { 211 } else if (res.previousRepresentation !== null) {
133 throw new IllegalArgumentException('''The previous representation of the first neighbourhood have to be null''') 212 throw new IllegalArgumentException('''The previous representation of the first neighbourhood have to be null''')
134 } else return res 213 } else
135 } else if(range > 0) { 214 return res
136 val previous = doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,range-1,parallels,maxNumber) 215 } else if (range > 0) {
137 val r = calculateFurtherNodeDescriptors(model,previous,IncomingRelations,OutgoingRelations,parallels,maxNumber) 216 val previous = doRecursiveNeighbourCalculation(model, types, IncomingRelations, OutgoingRelations, PatternRelations,
138 //println('''Level «range» finished.''') 217 range - 1, parallels, maxNumber)
139 val res = createFurtherRepresentation(r.key,r.value,previous,deepRepresentation) 218 val r = calculateFurtherNodeDescriptors(model, previous, IncomingRelations, OutgoingRelations,PatternRelations,
140 if(res.modelRepresentation === null) { 219 parallels, maxNumber)
141 throw new IllegalArgumentException('''Model representation is null''') 220 // println('''Level «range» finished.''')
142 } else if(res.nodeRepresentations === null || res.nodeRepresentations.empty) { 221 val res = createFurtherRepresentation(r.key, r.value, previous, deepRepresentation)
143 throw new IllegalArgumentException('''No node representation''') 222 if (res.modelRepresentation === null) {
144 } else if(res.previousRepresentation === null && deepRepresentation) { 223 throw new IllegalArgumentException('''Model representation is null''')
145 throw new IllegalArgumentException('''Need previous representations''') 224 } else if (res.nodeRepresentations === null || res.nodeRepresentations.empty) {
146 } else return res 225 throw new IllegalArgumentException('''No node representation''')
147 } else if (range == FixPointRage) { 226 } else if (res.previousRepresentation === null && deepRepresentation) {
148 return refineUntilFixpoint(model,types,IncomingRelations,OutgoingRelations,parallels,maxNumber) 227 throw new IllegalArgumentException('''Need previous representations''')
149 } else if (range == GraphWidthRange) { 228 } else
150 val width = this.getWidth(types,IncomingRelations,OutgoingRelations) 229 return res
151 //println(width) 230 } else if (range == FixPointRage) {
152 return doRecursiveNeighbourCalculation(model,types,IncomingRelations,OutgoingRelations,width,parallels,maxNumber) 231 return refineUntilFixpoint(model, types, IncomingRelations, OutgoingRelations, PatternRelations, parallels, maxNumber)
232 } else if (range == GraphWidthRange) {
233 val width = this.getWidth(types, IncomingRelations, OutgoingRelations)
234 // println(width)
235 return doRecursiveNeighbourCalculation(model, types, IncomingRelations, OutgoingRelations,PatternRelations, width,
236 parallels, maxNumber)
237 }
153 } 238 }
154 } 239
155 240 def private refineUntilFixpoint(PartialInterpretation model, Map<DefinedElement, Set<String>> types,
156 def private refineUntilFixpoint( 241 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
157 PartialInterpretation model, 242 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations,
158 Map<DefinedElement, Set<String>> types, 243 Map<DefinedElement, List<PatternRelation<DefinedElement>>> PatternRelations,
159 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, 244 int parallels,
160 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, 245 int maxNumbers) {
161 int parallels, int maxNumbers) 246 var lastRange = 0
162 { 247 val last = calculateLocalNodeDescriptors(model, types, maxNumbers)
163 var lastRange = 0 248 var lastRepresentation = this.createLocalRepresentation(last.value, last.key)
164 val last = calculateLocalNodeDescriptors(model,types,maxNumbers) 249 // println('''Level 0 finished.''')
165 var lastRepresentation = this.createLocalRepresentation(last.value,last.key) 250 var boolean hasRefined
166 //println('''Level 0 finished.''') 251 do {
167 var boolean hasRefined 252 val nextRange = lastRange + 1
168 do { 253 val next = calculateFurtherNodeDescriptors(model, lastRepresentation, IncomingRelations,
169 val nextRange = lastRange+1 254 OutgoingRelations, PatternRelations, parallels, maxNumbers)
170 val next = calculateFurtherNodeDescriptors(model,lastRepresentation,IncomingRelations,OutgoingRelations,parallels,maxNumbers) 255 val nextRepresentation = createFurtherRepresentation(next.key, next.value, lastRepresentation,
171 val nextRepresentation = createFurtherRepresentation(next.key,next.value,lastRepresentation,deepRepresentation) 256 deepRepresentation)
172 257
173 val previousNumberOfTypes =lastRepresentation.nodeRepresentations.values.toSet.size 258 val previousNumberOfTypes = lastRepresentation.nodeRepresentations.values.toSet.size
174 val nextNumberOfTypes = nextRepresentation.nodeRepresentations.values.toSet.size 259 val nextNumberOfTypes = nextRepresentation.nodeRepresentations.values.toSet.size
175 hasRefined = nextNumberOfTypes > previousNumberOfTypes 260 hasRefined = nextNumberOfTypes > previousNumberOfTypes
176 261
177 lastRange = nextRange 262 lastRange = nextRange
178 lastRepresentation = nextRepresentation 263 lastRepresentation = nextRepresentation
179 264
180// if(hasRefined) { 265// if(hasRefined) {
181// println('''Level «nextRange» is calculated, number of types is refined: «previousNumberOfTypes» -> «nextNumberOfTypes»''') 266// println('''Level «nextRange» is calculated, number of types is refined: «previousNumberOfTypes» -> «nextNumberOfTypes»''')
182// } else { 267// } else {
183// println('''Level «nextRange» is calculated, but the number of types is not refined: «previousNumberOfTypes» = «nextNumberOfTypes»''') 268// println('''Level «nextRange» is calculated, but the number of types is not refined: «previousNumberOfTypes» = «nextNumberOfTypes»''')
184// } 269// }
185 } while (hasRefined) 270 } while (hasRefined)
186 return lastRepresentation 271 return lastRepresentation
187 } 272 }
188 273
189 def private getElements(PartialInterpretation model) { 274 def private getElements(PartialInterpretation model) {
190 return 275 return model.problem.elements + model.newElements + model.openWorldElements
191 model.problem.elements + 276 }
192 model.newElements + 277
193 model.openWorldElements 278 def private fillTypes(PartialInterpretation model, Map<DefinedElement, Set<String>> node2Type,
194 } 279 Set<TypeDeclaration> relevantTypes) {
195 280 for (element : model.elements) {
196 def private fillTypes(PartialInterpretation model, Map<DefinedElement, Set<String>> node2Type, Set<TypeDeclaration> relevantTypes) { 281 node2Type.put(element, new HashSet)
197 for(element : model.elements) { 282 }
198 node2Type.put(element, new HashSet) 283
199 } 284 // for(typeDefinition : model.problem.types.filter(TypeDefinition)) {
200 285 // // Dont need
201// for(typeDefinition : model.problem.types.filter(TypeDefinition)) { 286 // }
202// // Dont need 287 for (typeInterpretation : model.partialtypeinterpratation) {
203// } 288 if (typeInterpretation instanceof PartialPrimitiveInterpretation) {
204 for(typeInterpretation : model.partialtypeinterpratation) { 289 } else if (typeInterpretation instanceof PartialComplexTypeInterpretation) {
205 if(typeInterpretation instanceof PartialPrimitiveInterpretation) { 290 val type = typeInterpretation.interpretationOf
206 291 if (type.isRelevant(relevantTypes)) {
207 } else if(typeInterpretation instanceof PartialComplexTypeInterpretation) { 292 for (element : typeInterpretation.elements) {
208 val type = typeInterpretation.interpretationOf 293 element.lookup(node2Type).add(type.name)
209 if(type.isRelevant(relevantTypes)) { 294 }
210 for(element : typeInterpretation.elements) { 295 }
211 element.lookup(node2Type).add(type.name)
212 } 296 }
213 } 297 }
214 } 298 }
215 } 299
216 } 300 /**
217 301 * Indexes the references
218 /** 302 */
219 * Indexes the references 303 def private fillReferences(PartialInterpretation model,
220 */ 304 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
221 def private fillReferences(PartialInterpretation model, 305 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations,
222 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations, 306 Set<RelationDeclaration> relevantRelations) {
223 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations, 307 for (object : model.elements) {
224 Set<RelationDeclaration> relevantRelations) 308 IncomingRelations.put(object, new LinkedList)
225 { 309 OutgoingRelations.put(object, new LinkedList)
226 for(object : model.elements) { 310 }
227 IncomingRelations.put(object,new LinkedList) 311 for (relationInterpretation : model.partialrelationinterpretation) {
228 OutgoingRelations.put(object,new LinkedList) 312 val type = relationInterpretation.interpretationOf
229 } 313 if (type.isRelevant(relevantRelations)) {
230 for(relationInterpretation : model.partialrelationinterpretation) { 314 for (link : relationInterpretation.relationlinks) {
231 val type = relationInterpretation.interpretationOf 315 if (link instanceof BinaryElementRelationLink) {
232 if(type.isRelevant(relevantRelations)) { 316 OutgoingRelations.get(link.param1) += new OutgoingRelation(link.param2, type.name)
233 for(link : relationInterpretation.relationlinks) { 317 IncomingRelations.get(link.param2) += new IncomingRelation(link.param1, type.name)
234 if(link instanceof BinaryElementRelationLink) { 318 } else
235 OutgoingRelations.get(link.param1) += new OutgoingRelation(link.param2,type.name) 319 throw new UnsupportedOperationException
236 IncomingRelations.get(link.param2) += new IncomingRelation(link.param1,type.name) 320 }
237 } else throw new UnsupportedOperationException 321 }
322 }
238 } 323 }
239 } 324
240 } 325 /**
241 } 326 * Creates the representation of the initial shape (aka zero range neighbourhood)
242 327 */
243 /** 328 def abstract protected NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> createLocalRepresentation(
244 * Creates a local representation of the objects (aka zero range neighbourhood) 329 Map<DefinedElement, LocalNodeDescriptor> node2Representation,
245 */ 330 Map<LocalNodeDescriptor, Integer> representation2Amount
246 def abstract protected NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> createLocalRepresentation( 331 )
247 Map<DefinedElement, LocalNodeDescriptor> node2Representation, 332
248 Map<LocalNodeDescriptor, Integer> representation2Amount 333 /**
249 ) 334 * Creates the representation of a shape (aka neighbourhood) with positive range
250 335 */
251 /** 336 def abstract protected NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> createFurtherRepresentation(
252 * Creates a 337 Map<FurtherNodeDescriptor<NodeRepresentation>, Integer> nodeDescriptors,
253 */ 338 Map<DefinedElement, FurtherNodeDescriptor<NodeRepresentation>> node2Representation,
254 def abstract protected NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> createFurtherRepresentation( 339 NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> previous,
255 Map<FurtherNodeDescriptor<NodeRepresentation>, Integer> nodeDescriptors, 340 boolean deepRepresentation
256 Map<DefinedElement, FurtherNodeDescriptor<NodeRepresentation>> node2Representation, 341 )
257 NeighbourhoodWithTraces<ModelRepresentation,NodeRepresentation> previous, 342
258 boolean deepRepresentation 343 def private addOne(int original, int max) {
259 ) 344 if(original == Integer.MAX_VALUE) return Integer.MAX_VALUE
260 345 if(original + 1 > max) return Integer.MAX_VALUE else return original + 1
261 def private addOne(int original, int max) {
262 if(original == Integer.MAX_VALUE) return Integer.MAX_VALUE
263 if(original +1 > max) return Integer.MAX_VALUE
264 else return original+1
265 }
266
267 private def calculateIncomingEdges(Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
268 DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel)
269 {
270 val Map<IncomingRelation<NodeRepresentation>, Integer> res = new HashMap
271 for (incomingConcreteEdge : IncomingRelations.get(object)) {
272 val IncomingRelation<NodeRepresentation> e = new IncomingRelation(
273 previousNodeRepresentations.get(incomingConcreteEdge.from), incomingConcreteEdge.type)
274 if (res.containsKey(e)) {
275 res.put(e, addOne(res.get(e),parallel))
276 } else {
277 res.put(e, 1)
278 }
279 }
280 return res
281 }
282
283 private def calcuateOutgoingEdges(Map<DefinedElement,List<OutgoingRelation<DefinedElement>>> OutgoingRelations,
284 DefinedElement object, Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel)
285 {
286 val Map<OutgoingRelation<NodeRepresentation>,Integer> res= new HashMap
287 for(outgoingConcreteEdge : OutgoingRelations.get(object)) {
288 val OutgoingRelation<NodeRepresentation> e =
289 new OutgoingRelation(
290 previousNodeRepresentations.get(outgoingConcreteEdge.to),
291 outgoingConcreteEdge.type)
292 if(res.containsKey(e)) {
293 res.put(e,addOne(res.get(e),parallel))
294 } else {
295 res.put(e,1)
296 }
297 }
298 return res;
299 }
300
301 /*def private <KEY,VALUE> void addOrCreate_Set(Map<KEY,Set<VALUE>> map, KEY key, VALUE value) {
302 var Set<VALUE> s;
303 if(map.containsKey(key)) {
304 s = map.get(key);
305 } else {
306 s = new HashSet
307 map.put(key,s)
308 }
309 s.add(value)
310 }*/
311
312
313 private def calculateFurtherNodeDescriptors(
314 PartialInterpretation model,
315 NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> previous,
316 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
317 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations,
318 int parallels, int maxNumber)
319 {
320 val previousNodeRepresentations = previous.nodeRepresentations
321 val node2Representation = new HashMap<DefinedElement,FurtherNodeDescriptor<NodeRepresentation>>
322 val Map<FurtherNodeDescriptor<NodeRepresentation>,Integer> descriptor2Number =
323 if(this.mergeSimilarNeighbourhood){ new HashMap } else { null }
324 val Map<FurtherNodeDescriptor<NodeRepresentation>,FurtherNodeDescriptor<NodeRepresentation>> uniqueDescription =
325 if(this.mergeSimilarNeighbourhood){ new HashMap } else { null }
326
327 for(object: model.elements) {
328 val incomingEdges = this.calculateIncomingEdges(IncomingRelations, object, previousNodeRepresentations,parallels)
329 val outgoingEdges = this.calcuateOutgoingEdges(OutgoingRelations,object, previousNodeRepresentations,parallels)
330
331 val previousType = previousNodeRepresentations.get(object)
332
333 if(previousType === null) {
334 println("Error in state coder")
335 }
336
337 val nodeDescriptor = new FurtherNodeDescriptor(
338 previousType,
339 incomingEdges,
340 outgoingEdges)
341
342 if(this.mergeSimilarNeighbourhood) {
343 if(descriptor2Number.containsKey(nodeDescriptor)) {
344 descriptor2Number.put(
345 nodeDescriptor,
346 addOne(descriptor2Number.get(nodeDescriptor),maxNumber)
347 )
348 node2Representation.put(object,uniqueDescription.get(nodeDescriptor))
349 } else {
350 descriptor2Number.put(nodeDescriptor,if(1>maxNumber){Integer.MAX_VALUE}else{1})
351 uniqueDescription.put(nodeDescriptor,nodeDescriptor)
352 node2Representation.put(object,nodeDescriptor)
353 } 346 }
354 } else { 347
355 node2Representation.put(object,nodeDescriptor) 348 private def calculateIncomingEdges(
356 } 349 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
357 } 350 DefinedElement object,
358 351 Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel) {
359 return descriptor2Number -> node2Representation 352 val Map<IncomingRelation<NodeRepresentation>, Integer> res = new HashMap
360 } 353 for (incomingConcreteEdge : IncomingRelations.get(object)) {
361 354 val IncomingRelation<NodeRepresentation> e = new IncomingRelation(
362 private def calculateLocalNodeDescriptors( 355 previousNodeRepresentations.get(incomingConcreteEdge.from), incomingConcreteEdge.type)
363 PartialInterpretation model, 356 if (res.containsKey(e)) {
364 Map<DefinedElement, Set<String>> types, 357 res.put(e, addOne(res.get(e), parallel))
365 int maxNumber) 358 } else {
366 { 359 res.put(e, 1)
367 val Map<DefinedElement, LocalNodeDescriptor> node2Representation = new HashMap 360 }
368 val Map<LocalNodeDescriptor, Integer> representation2Amount = 361 }
369 if(mergeSimilarNeighbourhood){ new HashMap } else { null } 362 return res
370 val Map<LocalNodeDescriptor, LocalNodeDescriptor> uniqueRepresentation =
371 if(this.mergeSimilarNeighbourhood){ new HashMap } else { null }
372
373 for(element : model.elements) {
374 var newDescriptor = new LocalNodeDescriptor(element.name,element.lookup(types))
375 if(this.mergeSimilarNeighbourhood){
376 if(uniqueRepresentation.containsKey(newDescriptor)) {
377 newDescriptor = newDescriptor.lookup(uniqueRepresentation)
378 node2Representation.put(element,newDescriptor)
379 representation2Amount.put(
380 newDescriptor,
381 addOne(newDescriptor.lookup(representation2Amount),maxNumber)
382 )
383 } else {
384 uniqueRepresentation.put(newDescriptor,newDescriptor)
385 node2Representation.put(element,newDescriptor)
386 representation2Amount.put(newDescriptor, if(1>maxNumber){Integer.MAX_VALUE}else{1})
387 } 363 }
388 } else { 364
389 node2Representation.put(element,newDescriptor) 365 private def calcuateOutgoingEdges(
390 } 366 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations,
391 } 367 DefinedElement object,
392 368 Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations, int parallel) {
393 return representation2Amount -> node2Representation 369 val Map<OutgoingRelation<NodeRepresentation>, Integer> res = new HashMap
394 } 370 for (outgoingConcreteEdge : OutgoingRelations.get(object)) {
395} \ No newline at end of file 371 val OutgoingRelation<NodeRepresentation> e = new OutgoingRelation(
372 previousNodeRepresentations.get(outgoingConcreteEdge.to), outgoingConcreteEdge.type)
373 if (res.containsKey(e)) {
374 res.put(e, addOne(res.get(e), parallel))
375 } else {
376 res.put(e, 1)
377 }
378 }
379 return res;
380 }
381
382 private def calculatePatterns(Map<DefinedElement, List<PatternRelation<DefinedElement>>> PatternRelations,
383 DefinedElement object,
384 Map<DefinedElement, ? extends NodeRepresentation> previousNodeRepresentations) {
385 val res = new HashSet<PatternRelation<NodeRepresentation>>()
386
387 for (patternRelation : PatternRelations.get(object)) {
388 val matchshape=patternRelation.parameters.map[previousNodeRepresentations.getOrDefault(it,null)]
389 res.add(new PatternRelation<NodeRepresentation>(patternRelation.patternName,patternRelation.param,matchshape))
390 }
391
392 return res
393 }
394
395 /*def private <KEY,VALUE> void addOrCreate_Set(Map<KEY,Set<VALUE>> map, KEY key, VALUE value) {
396 * var Set<VALUE> s;
397 * if(map.containsKey(key)) {
398 * s = map.get(key);
399 * } else {
400 * s = new HashSet
401 * map.put(key,s)
402 * }
403 * s.add(value)
404 }*/
405 private def calculateFurtherNodeDescriptors(PartialInterpretation model,
406 NeighbourhoodWithTraces<ModelRepresentation, NodeRepresentation> previous,
407 Map<DefinedElement, List<IncomingRelation<DefinedElement>>> IncomingRelations,
408 Map<DefinedElement, List<OutgoingRelation<DefinedElement>>> OutgoingRelations,
409 Map<DefinedElement, List<PatternRelation<DefinedElement>>> PatternRelations,
410 int parallels,
411 int maxNumber) {
412 val previousNodeRepresentations = previous.nodeRepresentations
413 val node2Representation = new HashMap<DefinedElement, FurtherNodeDescriptor<NodeRepresentation>>
414 val Map<FurtherNodeDescriptor<NodeRepresentation>, Integer> descriptor2Number = if (this.
415 mergeSimilarNeighbourhood) {
416 new HashMap
417 } else {
418 null
419 }
420 val Map<FurtherNodeDescriptor<NodeRepresentation>, FurtherNodeDescriptor<NodeRepresentation>> uniqueDescription = if (this.
421 mergeSimilarNeighbourhood) {
422 new HashMap
423 } else {
424 null
425 }
426
427 for (object : model.elements) {
428 val incomingEdges = this.calculateIncomingEdges(IncomingRelations, object,
429 previousNodeRepresentations, parallels)
430 val outgoingEdges = this.calcuateOutgoingEdges(OutgoingRelations, object,
431 previousNodeRepresentations, parallels)
432 val patterns = this.calculatePatterns(PatternRelations, object, previousNodeRepresentations)
433
434 val previousType = previousNodeRepresentations.get(object)
435
436 if (previousType === null) {
437 println("Error in state coder")
438 }
439
440 val nodeDescriptor = new FurtherNodeDescriptor(previousType, incomingEdges, outgoingEdges, patterns)
441
442 if (this.mergeSimilarNeighbourhood) {
443 if (descriptor2Number.containsKey(nodeDescriptor)) {
444 descriptor2Number.put(
445 nodeDescriptor,
446 addOne(descriptor2Number.get(nodeDescriptor), maxNumber)
447 )
448 node2Representation.put(object, uniqueDescription.get(nodeDescriptor))
449 } else {
450 descriptor2Number.put(nodeDescriptor, if (1 > maxNumber) {
451 Integer.MAX_VALUE
452 } else {
453 1
454 })
455 uniqueDescription.put(nodeDescriptor, nodeDescriptor)
456 node2Representation.put(object, nodeDescriptor)
457 }
458 } else {
459 node2Representation.put(object, nodeDescriptor)
460 }
461 }
462
463 return descriptor2Number -> node2Representation
464 }
465
466 private def calculateLocalNodeDescriptors(PartialInterpretation model,
467 Map<DefinedElement, Set<String>> types, int maxNumber) {
468 val Map<DefinedElement, LocalNodeDescriptor> node2Representation = new HashMap
469 val Map<LocalNodeDescriptor, Integer> representation2Amount = if (mergeSimilarNeighbourhood) {
470 new HashMap
471 } else {
472 null
473 }
474 val Map<LocalNodeDescriptor, LocalNodeDescriptor> uniqueRepresentation = if (this.
475 mergeSimilarNeighbourhood) {
476 new HashMap
477 } else {
478 null
479 }
480
481 for (element : model.elements) {
482 var newDescriptor = new LocalNodeDescriptor(element.name, element.lookup(types))
483 if (this.mergeSimilarNeighbourhood) {
484 if (uniqueRepresentation.containsKey(newDescriptor)) {
485 newDescriptor = newDescriptor.lookup(uniqueRepresentation)
486 node2Representation.put(element, newDescriptor)
487 representation2Amount.put(
488 newDescriptor,
489 addOne(newDescriptor.lookup(representation2Amount), maxNumber)
490 )
491 } else {
492 uniqueRepresentation.put(newDescriptor, newDescriptor)
493 node2Representation.put(element, newDescriptor)
494 representation2Amount.put(newDescriptor, if (1 > maxNumber) {
495 Integer.MAX_VALUE
496 } else {
497 1
498 })
499 }
500 } else {
501 node2Representation.put(element, newDescriptor)
502 }
503 }
504
505 return representation2Amount -> node2Representation
506 }
507 }
508 \ No newline at end of file