diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood')
7 files changed, 691 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/Descriptor.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Descriptor.xtend new file mode 100644 index 00000000..41482b28 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/Descriptor.xtend | |||
@@ -0,0 +1,214 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood | ||
2 | |||
3 | import java.util.HashMap | ||
4 | import java.util.Map | ||
5 | import java.util.Set | ||
6 | import org.eclipse.xtend.lib.annotations.Data | ||
7 | |||
8 | @Data abstract class AbstractNodeDescriptor { | ||
9 | long dataHash | ||
10 | |||
11 | // @Pure | ||
12 | // @Override | ||
13 | // override public boolean equals(Object obj) { | ||
14 | // if (this === obj) | ||
15 | // return true; | ||
16 | // if (obj === null) | ||
17 | // return false; | ||
18 | // if (getClass() != obj.getClass()) | ||
19 | // return false; | ||
20 | // val AbstractNodeDescriptor other = obj as AbstractNodeDescriptor; | ||
21 | // if (other.dataHash != this.dataHash) | ||
22 | // return false; | ||
23 | // return true; | ||
24 | // } | ||
25 | } | ||
26 | |||
27 | @Data class LocalNodeDescriptor extends AbstractNodeDescriptor{ | ||
28 | Set<String> types | ||
29 | String id; | ||
30 | new(String id, Set<String> types) { | ||
31 | super(calcualteDataHash(id,types)) | ||
32 | this.types = types | ||
33 | this.id = id | ||
34 | } | ||
35 | |||
36 | def private static calcualteDataHash(String id, Set<String> types) { | ||
37 | val int prime = 31; | ||
38 | var result = 0 | ||
39 | if(id !== null) | ||
40 | result = id.hashCode(); | ||
41 | if(types !== null) { | ||
42 | result = prime * result + types.hashCode | ||
43 | } | ||
44 | return result | ||
45 | } | ||
46 | |||
47 | override hashCode() { | ||
48 | return this.dataHash.hashCode | ||
49 | } | ||
50 | |||
51 | override toString() { | ||
52 | return class.name + this.dataHash | ||
53 | } | ||
54 | |||
55 | // @Pure | ||
56 | // @Override | ||
57 | // override public boolean equals(Object obj) { | ||
58 | // if (this === obj) | ||
59 | // return true; | ||
60 | // if (obj === null) | ||
61 | // return false; | ||
62 | // if (getClass() != obj.getClass()) | ||
63 | // return false; | ||
64 | // val AbstractNodeDescriptor other = obj as AbstractNodeDescriptor; | ||
65 | // if (other.dataHash != this.dataHash) | ||
66 | // return false; | ||
67 | // return true; | ||
68 | // } | ||
69 | |||
70 | // @Pure | ||
71 | // override public boolean equals(Object obj) { | ||
72 | // if (this === obj) | ||
73 | // return true; | ||
74 | // if (obj === null) | ||
75 | // return false; | ||
76 | // if (getClass() != obj.getClass()) | ||
77 | // return false; | ||
78 | // if (!super.equals(obj)) | ||
79 | // return false; | ||
80 | // val LocalNodeDescriptor other = obj as LocalNodeDescriptor; | ||
81 | // if (this.clazz === null) { | ||
82 | // if (other.clazz != null) | ||
83 | // return false; | ||
84 | // } else if (!this.clazz.equals(other.clazz)) | ||
85 | // return false; | ||
86 | // return true; | ||
87 | // } | ||
88 | } | ||
89 | |||
90 | @Data class IncomingRelation<FROM> { | ||
91 | FROM from | ||
92 | String type | ||
93 | } | ||
94 | |||
95 | @Data class OutgoingRelation<TO> { | ||
96 | TO to | ||
97 | String type | ||
98 | } | ||
99 | |||
100 | @Data class FurtherNodeDescriptor<NodeRep> extends AbstractNodeDescriptor{ | ||
101 | |||
102 | NodeRep previousRepresentation | ||
103 | Map<IncomingRelation<NodeRep>,Integer> incomingEdges | ||
104 | Map<OutgoingRelation<NodeRep>,Integer> outgoingEdges | ||
105 | |||
106 | new( | ||
107 | NodeRep previousRepresentation, | ||
108 | Map<IncomingRelation<NodeRep>,Integer> incomingEdges, | ||
109 | Map<OutgoingRelation<NodeRep>,Integer> outgoingEdges) | ||
110 | { | ||
111 | super(calculateDataHash(previousRepresentation,incomingEdges,outgoingEdges)) | ||
112 | this.previousRepresentation = previousRepresentation | ||
113 | this.incomingEdges = new HashMap(incomingEdges) | ||
114 | this.outgoingEdges = new HashMap(outgoingEdges) | ||
115 | } | ||
116 | |||
117 | static def private <NodeRep> int calculateDataHash( | ||
118 | NodeRep previousRepresentation, | ||
119 | Map<IncomingRelation<NodeRep>,Integer> incomingEdges, | ||
120 | Map<OutgoingRelation<NodeRep>,Integer> outgoingEdges) | ||
121 | { | ||
122 | val int prime = 31; | ||
123 | var int result = previousRepresentation.hashCode; | ||
124 | if(incomingEdges !== null) | ||
125 | result = prime * result + incomingEdges.hashCode(); | ||
126 | if(outgoingEdges !== null) | ||
127 | result = prime * result + outgoingEdges.hashCode(); | ||
128 | return result; | ||
129 | } | ||
130 | |||
131 | override hashCode() { | ||
132 | return this.dataHash.hashCode | ||
133 | } | ||
134 | |||
135 | override toString() { | ||
136 | return class.name + dataHash | ||
137 | // return '''[«previousRepresentation»,(«FOR | ||
138 | // in: incomingEdges.entrySet»(«in.key.type.name»=«in.key.from»,«in.value»)«ENDFOR»),(«FOR | ||
139 | // out: outgoingEdges.entrySet»(«out.key.type.name»=«out.key.to»,«out.value»)«ENDFOR»),«FOR | ||
140 | // att: attributeValues»(«att.type.name»=«att.value»)«ENDFOR»]''' | ||
141 | } | ||
142 | |||
143 | // @Pure | ||
144 | // @Override | ||
145 | // override public boolean equals(Object obj) { | ||
146 | // if (this === obj) | ||
147 | // return true; | ||
148 | // if (obj === null) | ||
149 | // return false; | ||
150 | // if (getClass() != obj.getClass()) | ||
151 | // return false; | ||
152 | // val AbstractNodeDescriptor other = obj as AbstractNodeDescriptor; | ||
153 | // if (other.dataHash != this.dataHash) | ||
154 | // return false; | ||
155 | // return true; | ||
156 | // } | ||
157 | |||
158 | // @Pure | ||
159 | // override public boolean equals(Object obj) { | ||
160 | // if (this === obj) | ||
161 | // return true; | ||
162 | // if (obj === null) | ||
163 | // return false; | ||
164 | // if (getClass() != obj.getClass()) | ||
165 | // return false; | ||
166 | // if (!super.equals(obj)) | ||
167 | // return false; | ||
168 | // val FurtherNodeDescriptor<?> other = obj as FurtherNodeDescriptor<?>; | ||
169 | // if (this.previousRepresentation === null) { | ||
170 | // if (other.previousRepresentation != null) | ||
171 | // return false; | ||
172 | // | ||
173 | // } | ||
174 | //// } else if (!this.previousRepresentation.equals(other.previousRepresentation)) | ||
175 | //// return false; | ||
176 | // if (this.incomingEdges === null) { | ||
177 | // if (other.incomingEdges != null) | ||
178 | // return false; | ||
179 | // } else if (!this.incomingEdges.equals(other.incomingEdges)) | ||
180 | // return false; | ||
181 | // if (this.outgoingEdges === null) { | ||
182 | // if (other.outgoingEdges != null) | ||
183 | // return false; | ||
184 | // } else if (!this.outgoingEdges.equals(other.outgoingEdges)) | ||
185 | // return false; | ||
186 | // if (this.attributeValues === null) { | ||
187 | // if (other.attributeValues != null) | ||
188 | // return false; | ||
189 | // } else if (!this.attributeValues.equals(other.attributeValues)) | ||
190 | // return false; | ||
191 | // return true; | ||
192 | // } | ||
193 | } | ||
194 | |||
195 | /* | ||
196 | @Data | ||
197 | class ModelDescriptor { | ||
198 | int dataHash | ||
199 | int unknownElements | ||
200 | Map<? extends AbstractNodeDescriptor,Integer> knownElements | ||
201 | |||
202 | public new(Map<? extends AbstractNodeDescriptor,Integer> knownElements, int unknownElements) { | ||
203 | this.dataHash = calculateDataHash(knownElements,unknownElements) | ||
204 | this.unknownElements = unknownElements | ||
205 | this.knownElements = knownElements | ||
206 | } | ||
207 | |||
208 | def private static calculateDataHash(Map<? extends AbstractNodeDescriptor,Integer> knownElements, int unknownElements) | ||
209 | { | ||
210 | val int prime = 31; | ||
211 | return knownElements.hashCode * prime + unknownElements.hashCode | ||
212 | } | ||
213 | } | ||
214 | */ \ No newline at end of file | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/FurtherNodeDescriptorWithEquivalenceCounter.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/FurtherNodeDescriptorWithEquivalenceCounter.xtend new file mode 100644 index 00000000..22e890a2 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/FurtherNodeDescriptorWithEquivalenceCounter.xtend | |||
@@ -0,0 +1,16 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood | ||
2 | |||
3 | import java.util.Map | ||
4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
5 | |||
6 | class FurtherNodeDescriptorWithEquivalenceCounter extends FurtherNodeDescriptor<AbstractNodeDescriptor> { | ||
7 | |||
8 | new(AbstractNodeDescriptor previousRepresentation, | ||
9 | Map<IncomingRelation<AbstractNodeDescriptor>, Integer> incomingEdges, | ||
10 | Map<OutgoingRelation<AbstractNodeDescriptor>, Integer> outgoingEdges, | ||
11 | Map<DefinedElement, FurtherNodeDescriptor<AbstractNodeDescriptor>> node2Representation) | ||
12 | { | ||
13 | super(previousRepresentation, incomingEdges, outgoingEdges) | ||
14 | } | ||
15 | |||
16 | } \ No newline at end of file | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/NeighbourhoodWithTraces.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/NeighbourhoodWithTraces.xtend new file mode 100644 index 00000000..76cc4ae1 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/NeighbourhoodWithTraces.xtend | |||
@@ -0,0 +1,11 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
4 | import java.util.Map | ||
5 | import org.eclipse.xtend.lib.annotations.Data | ||
6 | |||
7 | @Data class NeighbourhoodWithTraces<ModelRep,NodeRep> { | ||
8 | ModelRep modelRepresentation | ||
9 | Map<DefinedElement,? extends NodeRep> nodeRepresentations | ||
10 | NeighbourhoodWithTraces<ModelRep,NodeRep> previousRepresentation | ||
11 | } | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2Hash.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2Hash.xtend new file mode 100644 index 00000000..d474877d --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2Hash.xtend | |||
@@ -0,0 +1,25 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood | ||
2 | |||
3 | import java.util.Map | ||
4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
5 | |||
6 | class PartialInterpretation2Hash extends PartialInterpretation2NeighbourhoodRepresentation<Integer, Integer>{ | ||
7 | |||
8 | protected new() { | ||
9 | super(false, true) | ||
10 | } | ||
11 | |||
12 | override protected createLocalRepresentation(Map<DefinedElement, LocalNodeDescriptor> node2Representation, Map<LocalNodeDescriptor, Integer> representation2Amount) { | ||
13 | return new NeighbourhoodWithTraces( | ||
14 | representation2Amount.hashCode,node2Representation.mapValues[it.hashCode], | ||
15 | null) | ||
16 | } | ||
17 | |||
18 | override protected createFurtherRepresentation(Map<FurtherNodeDescriptor<Integer>, Integer> nodeDescriptors, Map<DefinedElement, FurtherNodeDescriptor<Integer>> node2Representation, NeighbourhoodWithTraces<Integer, Integer> previous, boolean deepRepresentation) { | ||
19 | return new NeighbourhoodWithTraces( | ||
20 | nodeDescriptors.hashCode, | ||
21 | node2Representation.mapValues[it.hashCode], | ||
22 | if(deepRepresentation) {previous} else {null}) | ||
23 | } | ||
24 | |||
25 | } | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2ImmutableTypeLattice.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2ImmutableTypeLattice.xtend new file mode 100644 index 00000000..31181370 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2ImmutableTypeLattice.xtend | |||
@@ -0,0 +1,24 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
4 | import java.util.Map | ||
5 | |||
6 | class PartialInterpretation2ImmutableTypeLattice extends | ||
7 | PartialInterpretation2NeighbourhoodRepresentation<Map<? extends AbstractNodeDescriptor,Integer>, AbstractNodeDescriptor>{ | ||
8 | |||
9 | public new() { | ||
10 | super(false, true) | ||
11 | } | ||
12 | |||
13 | override protected createLocalRepresentation(Map<DefinedElement, LocalNodeDescriptor> node2Representation, Map<LocalNodeDescriptor, Integer> representation2Amount) { | ||
14 | return new NeighbourhoodWithTraces(node2Representation.immutableCopy,node2Representation.immutableCopy,null) | ||
15 | } | ||
16 | |||
17 | override protected createFurtherRepresentation(Map<FurtherNodeDescriptor<AbstractNodeDescriptor>, Integer> nodeDescriptors, Map<DefinedElement, FurtherNodeDescriptor<AbstractNodeDescriptor>> node2Representation, NeighbourhoodWithTraces<Map<? extends AbstractNodeDescriptor, Integer>, AbstractNodeDescriptor> previous, boolean deepRepresentation) { | ||
18 | if(deepRepresentation) { | ||
19 | return new NeighbourhoodWithTraces<Map<? extends AbstractNodeDescriptor, Integer>, AbstractNodeDescriptor>(nodeDescriptors,node2Representation,previous) | ||
20 | } else { | ||
21 | return new NeighbourhoodWithTraces<Map<? extends AbstractNodeDescriptor, Integer>, AbstractNodeDescriptor>(nodeDescriptors,node2Representation,null) | ||
22 | } | ||
23 | } | ||
24 | } | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2ImmutableTypeLatticeWithEquivalenceCounter.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2ImmutableTypeLatticeWithEquivalenceCounter.xtend new file mode 100644 index 00000000..251a82f0 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/neighbourhood/PartialInterpretation2ImmutableTypeLatticeWithEquivalenceCounter.xtend | |||
@@ -0,0 +1,56 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
4 | import java.util.Map | ||
5 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | ||
6 | import java.util.HashMap | ||
7 | |||
8 | class PartialInterpretation2ImmutableTypeLatticeWithEquivalenceCounter extends | ||
9 | PartialInterpretation2NeighbourhoodRepresentation< | ||
10 | Map<? extends AbstractNodeDescriptor,Integer>, // <- Model Descriptor | ||
11 | AbstractNodeDescriptor> // <- Node Descriptor | ||
12 | { | ||
13 | |||
14 | protected new() { | ||
15 | super(true,false) | ||
16 | } | ||
17 | |||
18 | override protected createLocalRepresentation(Map<DefinedElement, LocalNodeDescriptor> node2Representation, Map<LocalNodeDescriptor, Integer> representation2Amount) { | ||
19 | return new NeighbourhoodWithTraces(node2Representation.immutableCopy,node2Representation.immutableCopy,null) | ||
20 | } | ||
21 | |||
22 | override protected createFurtherRepresentation( | ||
23 | Map<FurtherNodeDescriptor<AbstractNodeDescriptor>, Integer> nodeDescriptors, | ||
24 | Map<DefinedElement, FurtherNodeDescriptor<AbstractNodeDescriptor>> node2Representation, | ||
25 | NeighbourhoodWithTraces<Map<? extends AbstractNodeDescriptor, Integer>, AbstractNodeDescriptor> previous, | ||
26 | boolean deepRepresentation | ||
27 | ) { | ||
28 | if(deepRepresentation) { | ||
29 | return new NeighbourhoodWithTraces<Map<? extends AbstractNodeDescriptor, Integer>, AbstractNodeDescriptor>(nodeDescriptors,node2Representation,previous) | ||
30 | } else { | ||
31 | return new NeighbourhoodWithTraces<Map<? extends AbstractNodeDescriptor, Integer>, AbstractNodeDescriptor>(nodeDescriptors,node2Representation,null) | ||
32 | } | ||
33 | } | ||
34 | |||
35 | public def finalRepresentation(NeighbourhoodWithTraces<Map<? extends AbstractNodeDescriptor, Integer>,AbstractNodeDescriptor> representation) { | ||
36 | val node2Representation = representation.nodeRepresentations | ||
37 | val node2LocalRepresentation = new HashMap | ||
38 | //val localRepresentation2Node = new HashMap | ||
39 | for(entry: node2Representation.entrySet) { | ||
40 | val node = entry.key | ||
41 | val localNodeDescriptor = entry.value.toLocalDescriptor | ||
42 | node2LocalRepresentation.put(node,localNodeDescriptor) | ||
43 | } | ||
44 | |||
45 | } | ||
46 | |||
47 | public def LocalNodeDescriptor toLocalDescriptor(AbstractNodeDescriptor descriptor) { | ||
48 | if(descriptor instanceof LocalNodeDescriptor) { | ||
49 | return descriptor | ||
50 | } else if(descriptor instanceof FurtherNodeDescriptor<?>) { | ||
51 | (descriptor.previousRepresentation as AbstractNodeDescriptor).toLocalDescriptor | ||
52 | } else throw new IllegalArgumentException('''Unsupported descriptor type: «descriptor.class.simpleName»''') | ||
53 | } | ||
54 | |||
55 | |||
56 | } | ||
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 | ||