diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend index 4f0c8f20..ce357272 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/PolyhedronScopePropagator.xtend | |||
@@ -3,6 +3,7 @@ package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality | |||
3 | import com.google.common.collect.ImmutableList | 3 | import com.google.common.collect.ImmutableList |
4 | import com.google.common.collect.ImmutableMap | 4 | import com.google.common.collect.ImmutableMap |
5 | import com.google.common.collect.Maps | 5 | import com.google.common.collect.Maps |
6 | import com.google.common.collect.Sets | ||
6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type | 7 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type |
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.UnifinishedMultiplicityQueries | 8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.UnifinishedMultiplicityQueries |
8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation | 9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation |
@@ -18,6 +19,7 @@ import java.util.Map | |||
18 | import java.util.Set | 19 | import java.util.Set |
19 | import javax.naming.OperationNotSupportedException | 20 | import javax.naming.OperationNotSupportedException |
20 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | 21 | import org.eclipse.viatra.query.runtime.api.IPatternMatch |
22 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification | ||
21 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine | 23 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine |
22 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | 24 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher |
23 | import org.eclipse.viatra.query.runtime.emf.EMFScope | 25 | import org.eclipse.viatra.query.runtime.emf.EMFScope |
@@ -32,6 +34,7 @@ class PolyhedronScopePropagator extends ScopePropagator { | |||
32 | 34 | ||
33 | new(PartialInterpretation p, Set<? extends Type> possibleNewDynamicTypes, | 35 | new(PartialInterpretation p, Set<? extends Type> possibleNewDynamicTypes, |
34 | Map<RelationMultiplicityConstraint, UnifinishedMultiplicityQueries> unfinishedMultiplicityQueries, | 36 | Map<RelationMultiplicityConstraint, UnifinishedMultiplicityQueries> unfinishedMultiplicityQueries, |
37 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hasElementInContainmentQuery, | ||
35 | PolyhedronSolver solver, boolean propagateRelations) { | 38 | PolyhedronSolver solver, boolean propagateRelations) { |
36 | super(p) | 39 | super(p) |
37 | val builder = new PolyhedronBuilder(p) | 40 | val builder = new PolyhedronBuilder(p) |
@@ -49,7 +52,8 @@ class PolyhedronScopePropagator extends ScopePropagator { | |||
49 | if (maximumNumberOfNewNodes <= 0) { | 52 | if (maximumNumberOfNewNodes <= 0) { |
50 | throw new IllegalStateException("Maximum number of new nodes is negative") | 53 | throw new IllegalStateException("Maximum number of new nodes is negative") |
51 | } | 54 | } |
52 | builder.buildMultiplicityConstraints(unfinishedMultiplicityQueries, maximumNumberOfNewNodes) | 55 | builder.buildMultiplicityConstraints(unfinishedMultiplicityQueries, hasElementInContainmentQuery, |
56 | maximumNumberOfNewNodes) | ||
53 | updaters = builder.updaters | 57 | updaters = builder.updaters |
54 | } | 58 | } |
55 | } | 59 | } |
@@ -57,6 +61,7 @@ class PolyhedronScopePropagator extends ScopePropagator { | |||
57 | override void propagateAllScopeConstraints() { | 61 | override void propagateAllScopeConstraints() { |
58 | resetBounds() | 62 | resetBounds() |
59 | populatePolyhedronFromScope() | 63 | populatePolyhedronFromScope() |
64 | println(polyhedron) | ||
60 | val result = operator.saturate() | 65 | val result = operator.saturate() |
61 | if (result == PolyhedronSaturationResult.EMPTY) { | 66 | if (result == PolyhedronSaturationResult.EMPTY) { |
62 | throw new IllegalStateException("Scope bounds cannot be satisfied") | 67 | throw new IllegalStateException("Scope bounds cannot be satisfied") |
@@ -66,7 +71,7 @@ class PolyhedronScopePropagator extends ScopePropagator { | |||
66 | super.propagateAllScopeConstraints() | 71 | super.propagateAllScopeConstraints() |
67 | } | 72 | } |
68 | } | 73 | } |
69 | // println(polyhedron) | 74 | println(polyhedron) |
70 | } | 75 | } |
71 | 76 | ||
72 | def resetBounds() { | 77 | def resetBounds() { |
@@ -188,13 +193,16 @@ class PolyhedronScopePropagator extends ScopePropagator { | |||
188 | 193 | ||
189 | def buildMultiplicityConstraints( | 194 | def buildMultiplicityConstraints( |
190 | Map<RelationMultiplicityConstraint, UnifinishedMultiplicityQueries> constraints, | 195 | Map<RelationMultiplicityConstraint, UnifinishedMultiplicityQueries> constraints, |
196 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hasElementInContainmentQuery, | ||
191 | int maximumNuberOfNewNodes) { | 197 | int maximumNuberOfNewNodes) { |
192 | infinity = maximumNuberOfNewNodes * INFINITY_SCALE | 198 | infinity = maximumNuberOfNewNodes * INFINITY_SCALE |
193 | queryEngine = ViatraQueryEngine.on(new EMFScope(p)) | 199 | queryEngine = ViatraQueryEngine.on(new EMFScope(p)) |
194 | updatersBuilder = ImmutableList.builder | 200 | updatersBuilder = ImmutableList.builder |
195 | for (pair : constraints.entrySet.filter[key.containment].groupBy[key.targetType].entrySet) { | 201 | val containmentConstraints = constraints.entrySet.filter[key.containment].groupBy[key.targetType] |
202 | for (pair : containmentConstraints.entrySet) { | ||
196 | buildContainmentConstraints(pair.key, pair.value) | 203 | buildContainmentConstraints(pair.key, pair.value) |
197 | } | 204 | } |
205 | buildConstainmentRootConstraints(containmentConstraints.keySet, hasElementInContainmentQuery) | ||
198 | for (pair : constraints.entrySet) { | 206 | for (pair : constraints.entrySet) { |
199 | val constraint = pair.key | 207 | val constraint = pair.key |
200 | if (!constraint.containment) { | 208 | if (!constraint.containment) { |
@@ -249,6 +257,19 @@ class PolyhedronScopePropagator extends ScopePropagator { | |||
249 | updatersBuilder.add(updater) | 257 | updatersBuilder.add(updater) |
250 | } | 258 | } |
251 | 259 | ||
260 | private def buildConstainmentRootConstraints(Set<Type> containedTypes, | ||
261 | IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hasElementInContainmentQuery) { | ||
262 | val matcher = hasElementInContainmentQuery.getMatcher(queryEngine) | ||
263 | val rootDimensions = Sets.newHashSet(instanceCounts.values) | ||
264 | for (type : containedTypes) { | ||
265 | val containedDimensions = subtypeDimensions.get(type).keySet | ||
266 | rootDimensions.removeAll(containedDimensions) | ||
267 | } | ||
268 | for (dimension : rootDimensions) { | ||
269 | updatersBuilder.add(new ContainmentRootConstraintUpdater(dimension, matcher)) | ||
270 | } | ||
271 | } | ||
272 | |||
252 | private def buildNonContainmentConstraints(RelationMultiplicityConstraint constraint, | 273 | private def buildNonContainmentConstraints(RelationMultiplicityConstraint constraint, |
253 | UnifinishedMultiplicityQueries queries) { | 274 | UnifinishedMultiplicityQueries queries) { |
254 | } | 275 | } |
@@ -366,12 +387,23 @@ class PolyhedronScopePropagator extends ScopePropagator { | |||
366 | orphansUpperBound.tightenLowerBound(sum) | 387 | orphansUpperBound.tightenLowerBound(sum) |
367 | } | 388 | } |
368 | } | 389 | } |
369 | 390 | ||
370 | @FinalFieldsConstructor | 391 | @FinalFieldsConstructor |
371 | static class ContainmentRootConstraintUpdater implements RelationConstraintUpdater { | 392 | static class ContainmentRootConstraintUpdater implements RelationConstraintUpdater { |
372 | 393 | val LinearBoundedExpression typeCardinality | |
394 | val ViatraQueryMatcher<? extends IPatternMatch> hasElementInContainmentMatcher | ||
395 | |||
373 | override update(PartialInterpretation p) { | 396 | override update(PartialInterpretation p) { |
374 | throw new UnsupportedOperationException("TODO: auto-generated method stub") | 397 | if (hasElementInContainmentMatcher.hasMatch(p)) { |
398 | typeCardinality.tightenUpperBound(0) | ||
399 | } else { | ||
400 | typeCardinality.tightenUpperBound(1) | ||
401 | } | ||
402 | } | ||
403 | |||
404 | private static def <T extends IPatternMatch> hasMatch(ViatraQueryMatcher<T> matcher, PartialInterpretation p) { | ||
405 | val match = matcher.newMatch(p.problem, p) | ||
406 | matcher.countMatches(match) != 0 | ||
375 | } | 407 | } |
376 | } | 408 | } |
377 | } | 409 | } |