diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/statecoder/NeighbourhoodBasedStateCoderFactory.xtend')
1 files changed, 82 insertions, 207 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/statecoder/NeighbourhoodBasedStateCoderFactory.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/statecoder/NeighbourhoodBasedStateCoderFactory.xtend index 19c70574..f19ac30f 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/statecoder/NeighbourhoodBasedStateCoderFactory.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage/src/hu/bme/mit/inf/dslreasoner/viatrasolver/partialinterpretationlanguage/statecoder/NeighbourhoodBasedStateCoderFactory.xtend | |||
@@ -1,223 +1,98 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.statecoder |
2 | 2 | ||
3 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.AbstractNodeDescriptor | 3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement |
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.NeighbourhoodOptions | ||
5 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2Hash | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2ImmutableTypeLattice | 6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2ImmutableTypeLattice |
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2NeighbourhoodRepresentation | ||
5 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | 8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation |
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationPackage | ||
7 | import java.util.ArrayList | 9 | import java.util.ArrayList |
8 | import java.util.LinkedList | ||
9 | import java.util.List | ||
10 | import java.util.Map | 10 | import java.util.Map |
11 | import org.eclipse.emf.common.notify.Notifier | ||
12 | import org.eclipse.emf.ecore.EClass | ||
13 | import org.eclipse.emf.ecore.EObject | ||
14 | import org.eclipse.emf.ecore.EStructuralFeature | ||
15 | import org.eclipse.viatra.dse.statecode.IStateCoder | ||
16 | import org.eclipse.viatra.dse.statecode.IStateCoderFactory | ||
17 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | 11 | import org.eclipse.viatra.query.runtime.api.IPatternMatch |
18 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine | 12 | |
19 | import org.eclipse.viatra.query.runtime.base.api.FeatureListener | 13 | class NeighbourhoodBasedStateCoderFactory extends AbstractNeighbourhoodBasedStateCoderFactory { |
20 | import org.eclipse.viatra.query.runtime.base.api.IndexingLevel | 14 | new() { |
21 | import org.eclipse.viatra.query.runtime.base.api.InstanceListener | 15 | } |
22 | import org.eclipse.viatra.query.runtime.emf.EMFBaseIndexWrapper | 16 | |
23 | import org.eclipse.viatra.query.runtime.emf.EMFScope | 17 | new(NeighbourhoodOptions options) { |
24 | import org.eclipse.xtend.lib.annotations.Accessors | 18 | super(options) |
25 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement | ||
26 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem | ||
27 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialTypeInterpratation | ||
28 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialRelationInterpretation | ||
29 | import java.util.Set | ||
30 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration | ||
31 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration | ||
32 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.neighbourhood.PartialInterpretation2NeighbourhoodRepresentation | ||
33 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation | ||
34 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialPrimitiveInterpretation | ||
35 | |||
36 | class NeighbourhoodBasedStateCoderFactory implements IStateCoderFactory { | ||
37 | val List<NeighbourhoodBasedPartialInterpretationStateCoder> statecoders = new LinkedList | ||
38 | |||
39 | val int range | ||
40 | val int parallels | ||
41 | val int maxNumber | ||
42 | val Set<TypeDeclaration> relevantTypes | ||
43 | val Set<RelationDeclaration> relevantRelations | ||
44 | |||
45 | public new() { | ||
46 | this.range = PartialInterpretation2NeighbourhoodRepresentation::GraphWidthRange | ||
47 | this.parallels = PartialInterpretation2NeighbourhoodRepresentation::FullParallels | ||
48 | this.maxNumber = PartialInterpretation2NeighbourhoodRepresentation::MaxNumbers | ||
49 | this.relevantTypes = null | ||
50 | this.relevantRelations = null | ||
51 | } | 19 | } |
52 | 20 | ||
53 | public new(int range, int parallels, int maxNumber, Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations) { | 21 | override protected doCreateStateCoder(NeighbourhoodOptions options) { |
54 | this.range = range | 22 | new NeighbourhoodBasedPartialInterpretationStateCoder(new PartialInterpretation2ImmutableTypeLattice, options) |
55 | this.parallels = parallels | 23 | } |
56 | this.maxNumber = maxNumber | 24 | } |
57 | this.relevantTypes = relevantTypes | 25 | |
58 | this.relevantRelations = relevantRelations | 26 | class NeighbourhoodBasedHashStateCoderFactory extends AbstractNeighbourhoodBasedStateCoderFactory { |
27 | new() { | ||
28 | } | ||
29 | |||
30 | new(NeighbourhoodOptions options) { | ||
31 | super(options) | ||
32 | } | ||
33 | |||
34 | override protected doCreateStateCoder(NeighbourhoodOptions options) { | ||
35 | new NeighbourhoodBasedPartialInterpretationStateCoder(new PartialInterpretation2Hash, options) | ||
59 | } | 36 | } |
60 | |||
61 | synchronized override createStateCoder() { | ||
62 | val res = new NeighbourhoodBasedPartialInterpretationStateCoder(statecoders.size, | ||
63 | range,parallels,maxNumber,relevantTypes,relevantRelations) | ||
64 | statecoders += res | ||
65 | return res | ||
66 | } | ||
67 | def getSumStatecoderRuntime() { | ||
68 | statecoders.map[statecoderRuntime].reduce[p1, p2|p1+p2] | ||
69 | } | ||
70 | } | 37 | } |
71 | 38 | ||
72 | class NeighbourhoodBasedPartialInterpretationStateCoder implements IStateCoder{ | 39 | class NeighbourhoodBasedPartialInterpretationStateCoder<ModelRep, NodeRep> extends AbstractNeighbourhoodBasedPartialInterpretationStateCoder { |
73 | val int id; | 40 | val PartialInterpretation2NeighbourhoodRepresentation<ModelRep, NodeRep> calculator |
74 | val int range | 41 | var Map<DefinedElement, ? extends NodeRep> nodeRepresentations = null |
75 | val int parallels | 42 | var ModelRep modelRepresentation = null |
76 | val int maxNumber | 43 | |
77 | val Set<TypeDeclaration> relevantTypes | 44 | new(PartialInterpretation2NeighbourhoodRepresentation<ModelRep, NodeRep> calculator, NeighbourhoodOptions options) { |
78 | val Set<RelationDeclaration> relevantRelations | 45 | super(options) |
79 | 46 | this.calculator = calculator | |
80 | val calculator = | 47 | } |
81 | new PartialInterpretation2ImmutableTypeLattice | 48 | |
82 | var PartialInterpretation target | 49 | override protected isRefreshNeeded() { |
83 | 50 | nodeRepresentations === null || modelRepresentation === null | |
84 | private var Map<DefinedElement, ? extends AbstractNodeDescriptor> nodeRepresentations = null | 51 | } |
85 | private var Map<? extends AbstractNodeDescriptor, Integer> modelRepresentation = null | 52 | |
86 | 53 | override doRefreshStateCodes(PartialInterpretation target, NeighbourhoodOptions options) { | |
87 | /*public new(int id) { | 54 | val code = calculator.createRepresentation(target, options) |
88 | this.id = id | 55 | modelRepresentation = code.modelRepresentation |
89 | this.range = PartialInterpretation2NeighbourhoodRepresentation::FixPointRage | 56 | nodeRepresentations = code.nodeRepresentations |
90 | this.parallels = PartialInterpretation2NeighbourhoodRepresentation::FullParallels | ||
91 | this.maxNumber = maxNumber = PartialInterpretation2NeighbourhoodRepresentation::MaxNumbers | ||
92 | this.relevantTypes = relevantTypes | ||
93 | this.relevantRelations = relevantRelations | ||
94 | }*/ | ||
95 | |||
96 | public new(int id, int range, int parallels, int maxNumber, Set<TypeDeclaration> relevantTypes, Set<RelationDeclaration> relevantRelations) { | ||
97 | this.id = id | ||
98 | this.range = range | ||
99 | this.parallels = parallels | ||
100 | this.maxNumber = maxNumber | ||
101 | this.relevantTypes = relevantTypes | ||
102 | this.relevantRelations = relevantRelations | ||
103 | } | 57 | } |
104 | 58 | ||
105 | @Accessors(PUBLIC_GETTER) var long statecoderRuntime = 0 | 59 | override doCreateActivationCode(IPatternMatch match) { |
106 | 60 | val size = match.specification.parameters.size | |
107 | // val range = -1 | 61 | val res = new ArrayList(size) |
108 | // val par = Integer.MAX_VALUE | 62 | var int equivalenceHash = 0 |
109 | //val deeprepresentation = false | 63 | val prime = 31 |
110 | 64 | ||
111 | ///////// | 65 | for (var int index = 0; index < size; index++) { |
112 | // Caching version | 66 | val matchArgument = match.get(index) |
113 | ///////// | 67 | res.add(getCode(matchArgument)) |
114 | synchronized private def refreshStateCodes() { | 68 | for (var i = 0; i < index; i++) { |
115 | if(this.nodeRepresentations === null || this.modelRepresentation === null) { | 69 | val number = if (matchArgument === match.get(i)) { |
116 | val startTime = System.nanoTime | 70 | 1 |
117 | //relevantObjects.forEach[println(it)] | 71 | } else { |
118 | val code = calculator.createRepresentation(target,range,parallels,maxNumber,relevantTypes,relevantRelations) | 72 | 0 |
119 | this.modelRepresentation = code.modelRepresentation | 73 | } |
120 | this.nodeRepresentations = code.nodeRepresentations | 74 | equivalenceHash = prime * equivalenceHash + number |
121 | statecoderRuntime += (System.nanoTime - startTime) | ||
122 | } | ||
123 | } | ||
124 | synchronized override createActivationCode(IPatternMatch match) { | ||
125 | refreshStateCodes | ||
126 | |||
127 | val startTime = System.nanoTime | ||
128 | val size = match.specification.parameters.size | ||
129 | val res = new ArrayList(size) | ||
130 | var int index = 0 | ||
131 | var int equivalenceHash = 0 | ||
132 | val prime = 31 | ||
133 | |||
134 | while(index < size) { | ||
135 | res.add(getCode(match.get(index))) | ||
136 | for(var i = 0; i<index; i++) { | ||
137 | val number = if(match.get(index) === match.get(i)){1}else{0} | ||
138 | equivalenceHash = prime * equivalenceHash + number | ||
139 | } | ||
140 | index++ | ||
141 | } | ||
142 | |||
143 | statecoderRuntime += (System.nanoTime - startTime) | ||
144 | return match.specification.fullyQualifiedName->(res->equivalenceHash).hashCode | ||
145 | } | ||
146 | |||
147 | |||
148 | def private getCode(Object o) { | ||
149 | if(o instanceof DefinedElement) { | ||
150 | this.nodeRepresentations.get(o) | ||
151 | } else if(o instanceof PartialInterpretation || o instanceof LogicProblem) { | ||
152 | return null | ||
153 | } else if(o instanceof PartialRelationInterpretation) { | ||
154 | return o.interpretationOf.name | ||
155 | } else if(o instanceof PartialTypeInterpratation) { | ||
156 | if(o instanceof PartialPrimitiveInterpretation) { | ||
157 | o.class.simpleName.hashCode | ||
158 | } else if (o instanceof PartialComplexTypeInterpretation){ | ||
159 | return o.interpretationOf.name.hashCode | ||
160 | } else { | ||
161 | throw new UnsupportedOperationException('''Unsupported type: «o.class.simpleName»''') | ||
162 | } | 75 | } |
163 | } else { | ||
164 | throw new UnsupportedOperationException('''Unsupported type: «o.class.simpleName»''') | ||
165 | } | 76 | } |
166 | } | 77 | |
167 | 78 | match.specification.fullyQualifiedName -> (res -> equivalenceHash).hashCode | |
168 | synchronized override createStateCode() { | 79 | } |
169 | refreshStateCodes | 80 | |
170 | return this.modelRepresentation.hashCode | 81 | def private getCode(Object o) { |
171 | } | 82 | switch (o) { |
172 | ///////// | 83 | DefinedElement: |
173 | // Caching version | 84 | nodeRepresentations.get(o) |
174 | ///////// | 85 | default: |
175 | 86 | getFallbackCode(o) | |
176 | ///////// | 87 | } |
177 | // Recalculating version | 88 | } |
178 | ///////// | 89 | |
179 | // synchronized override createActivationCode(IPatternMatch match) { | 90 | override doCreateStateCode() { |
180 | // val nodes = calculator.createRepresentation(getRelevantObjects().toList,range,par).nodeRepresentations | 91 | modelRepresentation.hashCode |
181 | // val res = match.toArray.map[objectInMatch | | 92 | } |
182 | // nodes.get(objectInMatch) | 93 | |
183 | // ] | 94 | override doInvalidate() { |
184 | // return res | 95 | nodeRepresentations = null |
185 | // } | 96 | modelRepresentation = null |
186 | // | ||
187 | // override createStateCode() { | ||
188 | // return this.calculator.createRepresentation(getRelevantObjects().toList,range,par).modelRepresentation | ||
189 | // } | ||
190 | ///////// | ||
191 | // Recalculating version | ||
192 | ///////// | ||
193 | |||
194 | override init(Notifier notifier) { | ||
195 | this.target = notifier as PartialInterpretation | ||
196 | val queryEngine = ViatraQueryEngine.on(new EMFScope(notifier)) | ||
197 | val baseIndex = queryEngine.getBaseIndex() as EMFBaseIndexWrapper | ||
198 | val navigationHelper = baseIndex.getNavigationHelper(); | ||
199 | |||
200 | val classes = PartialinterpretationPackage.eINSTANCE.EClassifiers.filter(EClass).toSet | ||
201 | val features = classes.map[it.EAllStructuralFeatures].flatten.toSet | ||
202 | navigationHelper.registerObservedTypes( | ||
203 | classes, | ||
204 | null, | ||
205 | features, | ||
206 | IndexingLevel.FULL); | ||
207 | |||
208 | |||
209 | navigationHelper.addFeatureListener(features, new FeatureListener() { | ||
210 | override public void featureInserted(EObject host, EStructuralFeature feature, Object value) { invalidate } | ||
211 | override public void featureDeleted(EObject host, EStructuralFeature feature, Object value) { invalidate } | ||
212 | }); | ||
213 | navigationHelper.addInstanceListener(classes, new InstanceListener() { | ||
214 | override public void instanceInserted(EClass clazz, EObject instance) { invalidate } | ||
215 | override public void instanceDeleted(EClass clazz, EObject instance) { invalidate } | ||
216 | }); | ||
217 | } | ||
218 | |||
219 | synchronized def public invalidate() { | ||
220 | this.nodeRepresentations = null | ||
221 | this.modelRepresentation = null | ||
222 | } | 97 | } |
223 | } | 98 | } |