diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf')
8 files changed, 379 insertions, 145 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend index 431ae386..3bcd9116 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/ModelGenerationMethodProvider.xtend | |||
@@ -49,6 +49,12 @@ class ModelGenerationStatistics { | |||
49 | synchronized def addScopePropagationTime(long amount) { | 49 | synchronized def addScopePropagationTime(long amount) { |
50 | scopePropagationTime += amount | 50 | scopePropagationTime += amount |
51 | } | 51 | } |
52 | |||
53 | public var long mustRelationPropagationTime = 0 | ||
54 | |||
55 | synchronized def addMustRelationPropagationTime(long amount) { | ||
56 | mustRelationPropagationTime += amount | ||
57 | } | ||
52 | 58 | ||
53 | public var long preliminaryTypeAnalisisTime = 0 | 59 | public var long preliminaryTypeAnalisisTime = 0 |
54 | 60 | ||
@@ -133,9 +139,11 @@ class ModelGenerationMethodProvider { | |||
133 | 139 | ||
134 | val scopePropagator = createScopePropagator(scopePropagatorStrategy, emptySolution, hints, queries, statistics) | 140 | val scopePropagator = createScopePropagator(scopePropagatorStrategy, emptySolution, hints, queries, statistics) |
135 | scopePropagator.propagateAllScopeConstraints | 141 | scopePropagator.propagateAllScopeConstraints |
142 | val unitRulePropagator = refinementRuleProvider.createUnitPrulePropagator(logicProblem, emptySolution, | ||
143 | queries, scopePropagator, statistics) | ||
136 | val objectRefinementRules = refinementRuleProvider.createObjectRefinementRules(logicProblem, emptySolution, | 144 | val objectRefinementRules = refinementRuleProvider.createObjectRefinementRules(logicProblem, emptySolution, |
137 | queries, scopePropagator, nameNewElements, statistics) | 145 | queries, unitRulePropagator, nameNewElements, statistics) |
138 | val relationRefinementRules = refinementRuleProvider.createRelationRefinementRules(queries, scopePropagator, | 146 | val relationRefinementRules = refinementRuleProvider.createRelationRefinementRules(queries, unitRulePropagator, |
139 | statistics) | 147 | statistics) |
140 | 148 | ||
141 | val unfinishedMultiplicities = goalConstraintProvider.getUnfinishedMultiplicityQueries(logicProblem, queries, | 149 | val unfinishedMultiplicities = goalConstraintProvider.getUnfinishedMultiplicityQueries(logicProblem, queries, |
@@ -158,7 +166,7 @@ class ModelGenerationMethodProvider { | |||
158 | val currentUnitPropagationPreconditions = queries.getCurrentUnitPropagationPreconditionPatterns | 166 | val currentUnitPropagationPreconditions = queries.getCurrentUnitPropagationPreconditionPatterns |
159 | 167 | ||
160 | val queriesToPrepare = ImmutableSet.builder.addAll(queries.refineObjectQueries.values).addAll( | 168 | val queriesToPrepare = ImmutableSet.builder.addAll(queries.refineObjectQueries.values).addAll( |
161 | queries.refineTypeQueries.values).addAll(queries.refinerelationQueries.values).addAll(queries. | 169 | queries.refineTypeQueries.values).addAll(queries.refineRelationQueries.values).addAll(queries. |
162 | multiplicityConstraintQueries.values.flatMap[allQueries]).addAll(queries.unfinishedWFQueries.values).addAll( | 170 | multiplicityConstraintQueries.values.flatMap[allQueries]).addAll(queries.unfinishedWFQueries.values).addAll( |
163 | queries.invalidWFQueries.values).addAll(queries.mustUnitPropagationPreconditionPatterns.values).addAll( | 171 | queries.invalidWFQueries.values).addAll(queries.mustUnitPropagationPreconditionPatterns.values).addAll( |
164 | queries.currentUnitPropagationPreconditionPatterns.values).add(queries.hasElementInContainmentQuery).build | 172 | queries.currentUnitPropagationPreconditionPatterns.values).add(queries.hasElementInContainmentQuery).build |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/LinearTypeConstraintHint.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/LinearTypeConstraintHint.xtend index 8c21ca1d..31f98e36 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/LinearTypeConstraintHint.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/LinearTypeConstraintHint.xtend | |||
@@ -3,8 +3,10 @@ package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type | 3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type |
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.PatternGenerator | 4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns.PatternGenerator |
5 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | 5 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation |
6 | import java.util.Map | ||
6 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | 7 | import org.eclipse.viatra.query.runtime.api.IPatternMatch |
7 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | 8 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher |
9 | import org.eclipse.viatra.query.runtime.matchers.psystem.queries.PQuery | ||
8 | 10 | ||
9 | interface LinearTypeExpressionBuilderFactory { | 11 | interface LinearTypeExpressionBuilderFactory { |
10 | def ViatraQueryMatcher<? extends IPatternMatch> createMatcher(String queryName) | 12 | def ViatraQueryMatcher<? extends IPatternMatch> createMatcher(String queryName) |
@@ -24,7 +26,7 @@ interface RelationConstraintUpdater { | |||
24 | } | 26 | } |
25 | 27 | ||
26 | interface LinearTypeConstraintHint { | 28 | interface LinearTypeConstraintHint { |
27 | def CharSequence getAdditionalPatterns(PatternGenerator patternGenerator) | 29 | def CharSequence getAdditionalPatterns(PatternGenerator patternGenerator, Map<String, PQuery> fqnToPQuery) |
28 | 30 | ||
29 | def RelationConstraintUpdater createConstraintUpdater(LinearTypeExpressionBuilderFactory builderFactory) | 31 | def RelationConstraintUpdater createConstraintUpdater(LinearTypeExpressionBuilderFactory builderFactory) |
30 | } | 32 | } |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend index 93b83577..cacba3c6 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend | |||
@@ -19,6 +19,8 @@ class ScopePropagator { | |||
19 | val Map<PartialTypeInterpratation, Scope> type2Scope | 19 | val Map<PartialTypeInterpratation, Scope> type2Scope |
20 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> superScopes | 20 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> superScopes |
21 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> subScopes | 21 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> subScopes |
22 | |||
23 | @Accessors(PUBLIC_GETTER) var scopePropagationNeeded = false | ||
22 | 24 | ||
23 | new(PartialInterpretation p, ModelGenerationStatistics statistics) { | 25 | new(PartialInterpretation p, ModelGenerationStatistics statistics) { |
24 | partialInterpretation = p | 26 | partialInterpretation = p |
@@ -64,7 +66,8 @@ class ScopePropagator { | |||
64 | copyScopeBoundsToHeuristic() | 66 | copyScopeBoundsToHeuristic() |
65 | } | 67 | } |
66 | 68 | ||
67 | def propagateAllScopeConstraints() { | 69 | def void propagateAllScopeConstraints() { |
70 | scopePropagationNeeded = false | ||
68 | if (!valid) { | 71 | if (!valid) { |
69 | return | 72 | return |
70 | } | 73 | } |
@@ -93,6 +96,7 @@ class ScopePropagator { | |||
93 | if (isPrimitive) { | 96 | if (isPrimitive) { |
94 | return | 97 | return |
95 | } | 98 | } |
99 | scopePropagationNeeded = true | ||
96 | // println('''Adding to «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') | 100 | // println('''Adding to «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') |
97 | val targetScope = type2Scope.get(t) | 101 | val targetScope = type2Scope.get(t) |
98 | if (targetScope !== null) { | 102 | if (targetScope !== null) { |
@@ -117,6 +121,12 @@ class ScopePropagator { | |||
117 | // this.partialInterpretation.scopes.forEach[println(''' «(it.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»: «it.minNewElements»-«it.maxNewElements»''')] | 121 | // this.partialInterpretation.scopes.forEach[println(''' «(it.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»: «it.minNewElements»-«it.maxNewElements»''')] |
118 | // println('''All constraints are propagated upon increasing «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') | 122 | // println('''All constraints are propagated upon increasing «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') |
119 | } | 123 | } |
124 | |||
125 | def addedToRelation(Relation r) { | ||
126 | if (isPropagationNeededAfterAdditionToRelation(r)) { | ||
127 | scopePropagationNeeded = true | ||
128 | } | ||
129 | } | ||
120 | 130 | ||
121 | protected def setScopesInvalid() { | 131 | protected def setScopesInvalid() { |
122 | partialInterpretation.minNewElements = Integer.MAX_VALUE | 132 | partialInterpretation.minNewElements = Integer.MAX_VALUE |
@@ -127,7 +137,7 @@ class ScopePropagator { | |||
127 | } | 137 | } |
128 | } | 138 | } |
129 | 139 | ||
130 | def isPropagationNeededAfterAdditionToRelation(Relation r) { | 140 | protected def isPropagationNeededAfterAdditionToRelation(Relation r) { |
131 | false | 141 | false |
132 | } | 142 | } |
133 | 143 | ||
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternGenerator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternGenerator.xtend index a3efcf76..edf92343 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternGenerator.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternGenerator.xtend | |||
@@ -369,13 +369,13 @@ class PatternGenerator { | |||
369 | ////////// | 369 | ////////// |
370 | // 4.3 Relation refinement | 370 | // 4.3 Relation refinement |
371 | ////////// | 371 | ////////// |
372 | «relationRefinementGenerator.generateRefineReference(problem)» | 372 | «relationRefinementGenerator.generateRefineReference(problem, unitPropagationPatternGenerators)» |
373 | 373 | ||
374 | ////////// | 374 | ////////// |
375 | // 5 Hints | 375 | // 5 Hints |
376 | ////////// | 376 | ////////// |
377 | «FOR hint : hints» | 377 | «FOR hint : hints» |
378 | «hint.getAdditionalPatterns(this)» | 378 | «hint.getAdditionalPatterns(this, fqn2PQuery)» |
379 | «ENDFOR» | 379 | «ENDFOR» |
380 | «FOR generator : unitPropagationPatternGenerators» | 380 | «FOR generator : unitPropagationPatternGenerators» |
381 | «generator.getAdditionalPatterns(this, fqn2PQuery)» | 381 | «generator.getAdditionalPatterns(this, fqn2PQuery)» |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternProvider.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternProvider.xtend index 21fd1989..2e786286 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternProvider.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PatternProvider.xtend | |||
@@ -38,7 +38,8 @@ import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | |||
38 | public IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hasElementInContainmentQuery | 38 | public IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>> hasElementInContainmentQuery |
39 | public Map<ObjectCreationPrecondition, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refineObjectQueries | 39 | public Map<ObjectCreationPrecondition, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refineObjectQueries |
40 | public Map<? extends Type, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refineTypeQueries | 40 | public Map<? extends Type, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refineTypeQueries |
41 | public Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refinerelationQueries | 41 | public Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refineRelationQueries |
42 | public Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustRelationPropagationQueries | ||
42 | public Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustUnitPropagationPreconditionPatterns | 43 | public Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustUnitPropagationPreconditionPatterns |
43 | public Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> currentUnitPropagationPreconditionPatterns | 44 | public Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> currentUnitPropagationPreconditionPatterns |
44 | public Map<RelationDefinition, ModalPatternQueries> modalRelationQueries | 45 | public Map<RelationDefinition, ModalPatternQueries> modalRelationQueries |
@@ -70,7 +71,6 @@ class UnifinishedMultiplicityQueries { | |||
70 | } | 71 | } |
71 | 72 | ||
72 | class PatternProvider { | 73 | class PatternProvider { |
73 | |||
74 | val TypeAnalysis typeAnalysis = new TypeAnalysis | 74 | val TypeAnalysis typeAnalysis = new TypeAnalysis |
75 | 75 | ||
76 | def generateQueries(LogicProblem problem, PartialInterpretation emptySolution, ModelGenerationStatistics statistics, | 76 | def generateQueries(LogicProblem problem, PartialInterpretation emptySolution, ModelGenerationStatistics statistics, |
@@ -98,7 +98,8 @@ class PatternProvider { | |||
98 | val generatedQueries = parseUtil.parse(patternGeneratorResult.patternText) | 98 | val generatedQueries = parseUtil.parse(patternGeneratorResult.patternText) |
99 | val runtimeQueries = calclulateRuntimeQueries(patternGenerator, problem, emptySolution, typeAnalysisResult, | 99 | val runtimeQueries = calclulateRuntimeQueries(patternGenerator, problem, emptySolution, typeAnalysisResult, |
100 | patternGeneratorResult.constraint2MustPreconditionName, | 100 | patternGeneratorResult.constraint2MustPreconditionName, |
101 | patternGeneratorResult.constraint2CurrentPreconditionName, relationConstraints, generatedQueries) | 101 | patternGeneratorResult.constraint2CurrentPreconditionName, relationConstraints, |
102 | unitPropagationPatternGenerators, generatedQueries) | ||
102 | return runtimeQueries | 103 | return runtimeQueries |
103 | } | 104 | } |
104 | 105 | ||
@@ -110,6 +111,7 @@ class PatternProvider { | |||
110 | HashMap<PConstraint, String> mustUnitPropagationTrace, | 111 | HashMap<PConstraint, String> mustUnitPropagationTrace, |
111 | HashMap<PConstraint, String> currentUnitPropagationTrace, | 112 | HashMap<PConstraint, String> currentUnitPropagationTrace, |
112 | RelationConstraints relationConstraints, | 113 | RelationConstraints relationConstraints, |
114 | Collection<UnitPropagationPatternGenerator> unitPropagationPatternGenerators, | ||
113 | Map<String, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> queries | 115 | Map<String, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> queries |
114 | ) { | 116 | ) { |
115 | val Map<Relation, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> invalidWFQueries = patternGenerator. | 117 | val Map<Relation, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> invalidWFQueries = patternGenerator. |
@@ -136,6 +138,8 @@ class PatternProvider { | |||
136 | ] | 138 | ] |
137 | val Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refineRelationQueries = patternGenerator. | 139 | val Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> refineRelationQueries = patternGenerator. |
138 | relationRefinementGenerator.getRefineRelationQueries(problem).mapValues[it.lookup(queries)] | 140 | relationRefinementGenerator.getRefineRelationQueries(problem).mapValues[it.lookup(queries)] |
141 | val Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustRelationPropagationQueries = patternGenerator. | ||
142 | relationRefinementGenerator.getMustPropagationQueries(problem, unitPropagationPatternGenerators).mapValues[it.lookup(queries)] | ||
139 | val Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustUnitPropagationPreconditionPatterns = mustUnitPropagationTrace. | 143 | val Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustUnitPropagationPreconditionPatterns = mustUnitPropagationTrace. |
140 | mapValues[it.lookup(queries)] | 144 | mapValues[it.lookup(queries)] |
141 | val Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> currentUnitPropagationPreconditionPatterns = currentUnitPropagationTrace. | 145 | val Map<PConstraint, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> currentUnitPropagationPreconditionPatterns = currentUnitPropagationTrace. |
@@ -158,6 +162,7 @@ class PatternProvider { | |||
158 | refineObjectsQueries, | 162 | refineObjectsQueries, |
159 | refineTypeQueries, | 163 | refineTypeQueries, |
160 | refineRelationQueries, | 164 | refineRelationQueries, |
165 | mustRelationPropagationQueries, | ||
161 | mustUnitPropagationPreconditionPatterns, | 166 | mustUnitPropagationPreconditionPatterns, |
162 | currentUnitPropagationPreconditionPatterns, | 167 | currentUnitPropagationPreconditionPatterns, |
163 | modalRelationQueries, | 168 | modalRelationQueries, |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend index 29d3eb61..23ba3cad 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationDeclarationIndexer.xtend | |||
@@ -113,7 +113,7 @@ class RelationDeclarationIndexer { | |||
113 | // 2. Circle in the containment hierarchy | 113 | // 2. Circle in the containment hierarchy |
114 | neg «base.containmentIndexer.referTransitiveMustContains("source","target")» | 114 | neg «base.containmentIndexer.referTransitiveMustContains("source","target")» |
115 | «ENDIF» | 115 | «ENDIF» |
116 | «IF mustNotRelations.empty» | 116 | «IF !mustNotRelations.empty» |
117 | // ![] unit propagation relations | 117 | // ![] unit propagation relations |
118 | «FOR mustNotRelation : mustNotRelations» | 118 | «FOR mustNotRelation : mustNotRelations» |
119 | neg find «mustNotRelation»(problem, interpretation, source, target); | 119 | neg find «mustNotRelation»(problem, interpretation, source, target); |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationRefinementGenerator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationRefinementGenerator.xtend index 783cd36b..6f5f2402 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationRefinementGenerator.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/RelationRefinementGenerator.xtend | |||
@@ -1,10 +1,17 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns |
2 | 2 | ||
3 | import com.google.common.collect.ImmutableList | ||
4 | import com.google.common.collect.ImmutableMap | ||
5 | import com.google.common.collect.ImmutableSet | ||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation | 6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation |
4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration | 7 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RelationDeclaration |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem | 8 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem |
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.Modality | 9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.Modality |
10 | import java.util.Collection | ||
7 | import java.util.LinkedList | 11 | import java.util.LinkedList |
12 | import java.util.Map | ||
13 | import java.util.Set | ||
14 | import org.eclipse.xtend2.lib.StringConcatenationClient | ||
8 | 15 | ||
9 | class RelationRefinementGenerator { | 16 | class RelationRefinementGenerator { |
10 | PatternGenerator base; | 17 | PatternGenerator base; |
@@ -13,53 +20,61 @@ class RelationRefinementGenerator { | |||
13 | this.base = base | 20 | this.base = base |
14 | } | 21 | } |
15 | 22 | ||
16 | def CharSequence generateRefineReference(LogicProblem p) ''' | 23 | def CharSequence generateRefineReference(LogicProblem p, |
17 | «FOR relationRefinement : this.getRelationRefinements(p)» | 24 | Collection<UnitPropagationPatternGenerator> unitPropagationPatternGenerators) { |
18 | pattern «relationRefinementQueryName(relationRefinement.key,relationRefinement.value)»( | 25 | val mustRelations = getMustRelations(unitPropagationPatternGenerators) |
19 | problem:LogicProblem, interpretation:PartialInterpretation, | 26 | |
20 | relationIterpretation:PartialRelationInterpretation«IF relationRefinement.value !== null», oppositeInterpretation:PartialRelationInterpretation«ENDIF», | 27 | ''' |
21 | from: DefinedElement, to: DefinedElement) | 28 | «FOR relationRefinement : this.getRelationRefinements(p)» |
22 | { | 29 | pattern «relationRefinementQueryName(relationRefinement.key,relationRefinement.value)»( |
23 | find interpretation(problem,interpretation); | 30 | problem:LogicProblem, interpretation:PartialInterpretation, |
24 | PartialInterpretation.partialrelationinterpretation(interpretation,relationIterpretation); | 31 | relationIterpretation:PartialRelationInterpretation«IF relationRefinement.value !== null», oppositeInterpretation:PartialRelationInterpretation«ENDIF», |
25 | PartialRelationInterpretation.interpretationOf.name(relationIterpretation,"«relationRefinement.key.name»"); | 32 | from: DefinedElement, to: DefinedElement) |
26 | «IF relationRefinement.value !== null» | 33 | { |
27 | PartialInterpretation.partialrelationinterpretation(interpretation,oppositeInterpretation); | 34 | find interpretation(problem,interpretation); |
28 | PartialRelationInterpretation.interpretationOf.name(oppositeInterpretation,"«relationRefinement.value.name»"); | 35 | PartialInterpretation.partialrelationinterpretation(interpretation,relationIterpretation); |
36 | PartialRelationInterpretation.interpretationOf.name(relationIterpretation,"«relationRefinement.key.name»"); | ||
37 | «IF relationRefinement.value !== null» | ||
38 | PartialInterpretation.partialrelationinterpretation(interpretation,oppositeInterpretation); | ||
39 | PartialRelationInterpretation.interpretationOf.name(oppositeInterpretation,"«relationRefinement.value.name»"); | ||
40 | «ENDIF» | ||
41 | find mustExist(problem, interpretation, from); | ||
42 | find mustExist(problem, interpretation, to); | ||
43 | «base.typeIndexer.referInstanceOfByReference(relationRefinement.key.parameters.get(0), Modality::MUST,"from")» | ||
44 | «base.typeIndexer.referInstanceOfByReference(relationRefinement.key.parameters.get(1), Modality::MUST,"to")» | ||
45 | «base.relationDeclarationIndexer.referRelation(relationRefinement.key,"from","to",Modality.MAY)» | ||
46 | neg «base.relationDeclarationIndexer.referRelation(relationRefinement.key,"from","to",Modality.MUST)» | ||
47 | } | ||
48 | |||
49 | «IF isMustPropagationQueryNeeded(relationRefinement.key, relationRefinement.value, mustRelations)» | ||
50 | pattern «mustPropagationQueryName(relationRefinement.key)»( | ||
51 | problem:LogicProblem, interpretation:PartialInterpretation, | ||
52 | relationIterpretation:PartialRelationInterpretation«IF relationRefinement.value !== null», oppositeInterpretation:PartialRelationInterpretation«ENDIF», | ||
53 | from: DefinedElement, to: DefinedElement) | ||
54 | «FOR body : getMustPropagationBodies(relationRefinement.key, relationRefinement.value, mustRelations) SEPARATOR " or "» | ||
55 | { | ||
56 | «referRefinementQuery(relationRefinement.key, relationRefinement.value, "relationIterpretation", "oppositeInterpretation", "from", "to")» | ||
57 | «body» | ||
58 | } | ||
59 | «ENDFOR» | ||
29 | «ENDIF» | 60 | «ENDIF» |
30 | find mustExist(problem, interpretation, from); | 61 | «ENDFOR» |
31 | find mustExist(problem, interpretation, to); | 62 | ''' |
32 | «base.typeIndexer.referInstanceOfByReference(relationRefinement.key.parameters.get(0), Modality::MUST,"from")» | 63 | } |
33 | «base.typeIndexer.referInstanceOfByReference(relationRefinement.key.parameters.get(1), Modality::MUST,"to")» | ||
34 | «base.relationDeclarationIndexer.referRelation(relationRefinement.key,"from","to",Modality.MAY)» | ||
35 | neg «base.relationDeclarationIndexer.referRelation(relationRefinement.key,"from","to",Modality.MUST)» | ||
36 | } | ||
37 | «ENDFOR» | ||
38 | ''' | ||
39 | 64 | ||
40 | def String relationRefinementQueryName(RelationDeclaration relation, Relation inverseRelation) { | 65 | def String relationRefinementQueryName(RelationDeclaration relation, Relation inverseRelation) { |
41 | '''«IF inverseRelation !== null»refineRelation_«base.canonizeName(relation.name)»_and_«base.canonizeName(inverseRelation.name)»«ELSE»refineRelation_«base.canonizeName(relation.name)»«ENDIF»''' | 66 | '''«IF inverseRelation !== null»refineRelation_«base.canonizeName(relation.name)»_and_«base.canonizeName(inverseRelation.name)»«ELSE»refineRelation_«base.canonizeName(relation.name)»«ENDIF»''' |
42 | } | 67 | } |
43 | 68 | ||
69 | def String mustPropagationQueryName(RelationDeclaration relation) { | ||
70 | '''mustPropagation_«base.canonizeName(relation.name)»''' | ||
71 | } | ||
72 | |||
44 | def referRefinementQuery(RelationDeclaration relation, Relation inverseRelation, String relInterpretationName, | 73 | def referRefinementQuery(RelationDeclaration relation, Relation inverseRelation, String relInterpretationName, |
45 | String inverseInterpretationName, String sourceName, | 74 | String inverseInterpretationName, String sourceName, |
46 | String targetName) '''find «this.relationRefinementQueryName(relation,inverseRelation)»(problem, interpretation, «relInterpretationName», «IF inverseRelation !== null»«inverseInterpretationName», «ENDIF»«sourceName», «targetName»);''' | 75 | String targetName) '''find «this.relationRefinementQueryName(relation,inverseRelation)»(problem, interpretation, «relInterpretationName», «IF inverseRelation !== null»«inverseInterpretationName», «ENDIF»«sourceName», «targetName»);''' |
47 | 76 | ||
48 | def getRefineRelationQueries(LogicProblem p) { | 77 | def getRefineRelationQueries(LogicProblem p) { |
49 | // val containmentRelations = p.containmentHierarchies.map[containmentRelations].flatten.toSet | ||
50 | // p.relations.filter(RelationDeclaration).filter[!containmentRelations.contains(it)].toInvertedMap['''refineRelation_«base.canonizeName(it.name)»'''] | ||
51 | /* | ||
52 | * val res = new LinkedHashMap | ||
53 | * for(relation: getRelationRefinements(p)) { | ||
54 | * if(inverseRelations.containsKey(relation)) { | ||
55 | * val name = '''refineRelation_«base.canonizeName(relation.name)»_and_«base.canonizeName(inverseRelations.get(relation).name)»''' | ||
56 | * res.put(relation -> inverseRelations.get(relation),name) | ||
57 | * } else { | ||
58 | * val name = '''refineRelation_«base.canonizeName(relation.name)»''' | ||
59 | * res.put(relation -> null,name) | ||
60 | * } | ||
61 | * } | ||
62 | return res*/ | ||
63 | getRelationRefinements(p).toInvertedMap[relationRefinementQueryName(it.key, it.value)] | 78 | getRelationRefinements(p).toInvertedMap[relationRefinementQueryName(it.key, it.value)] |
64 | } | 79 | } |
65 | 80 | ||
@@ -83,4 +98,54 @@ class RelationRefinementGenerator { | |||
83 | } | 98 | } |
84 | return list | 99 | return list |
85 | } | 100 | } |
101 | |||
102 | def getMustPropagationQueries(LogicProblem p, | ||
103 | Collection<UnitPropagationPatternGenerator> unitPropagationPatternGenerators) { | ||
104 | val refinements = getRelationRefinements(p) | ||
105 | val mustRelations = getMustRelations(unitPropagationPatternGenerators) | ||
106 | refinements.filter[isMustPropagationQueryNeeded(key, value, mustRelations)].toInvertedMap [ | ||
107 | mustPropagationQueryName(key) | ||
108 | ] | ||
109 | } | ||
110 | |||
111 | private def getMustRelations(Collection<UnitPropagationPatternGenerator> unitPropagationPatternGenerators) { | ||
112 | ImmutableMap.copyOf(unitPropagationPatternGenerators.flatMap[mustPatterns.entrySet].groupBy[key].mapValues [ | ||
113 | ImmutableSet.copyOf(map[value]) | ||
114 | ]) | ||
115 | } | ||
116 | |||
117 | private def isMustPropagationQueryNeeded(Relation relation, Relation inverseRelation, | ||
118 | Map<Relation, ? extends Set<String>> mustRelations) { | ||
119 | val mustSet = mustRelations.get(relation) | ||
120 | if (mustSet !== null && !mustSet.empty) { | ||
121 | return true | ||
122 | } | ||
123 | if (inverseRelation !== null) { | ||
124 | val inverseMustSet = mustRelations.get(inverseRelation) | ||
125 | if (inverseMustSet !== null && !inverseMustSet.empty) { | ||
126 | return true | ||
127 | } | ||
128 | } | ||
129 | false | ||
130 | } | ||
131 | |||
132 | private def getMustPropagationBodies(Relation relation, Relation inverseRelation, | ||
133 | Map<Relation, ? extends Set<String>> mustRelations) { | ||
134 | val builder = ImmutableList.<StringConcatenationClient>builder() | ||
135 | val mustSet = mustRelations.get(relation) | ||
136 | if (mustSet !== null) { | ||
137 | for (refinementQuery : mustSet) { | ||
138 | builder.add('''find «refinementQuery»(problem, interpretation, from, to);''') | ||
139 | } | ||
140 | } | ||
141 | if (inverseRelation !== null && inverseRelation != relation) { | ||
142 | val inverseMustSet = mustRelations.get(inverseRelation) | ||
143 | if (inverseMustSet !== null) { | ||
144 | for (refinementQuery : inverseMustSet) { | ||
145 | builder.add('''find «refinementQuery»(problem, interpretation, to, from);''') | ||
146 | } | ||
147 | } | ||
148 | } | ||
149 | builder.build | ||
150 | } | ||
86 | } | 151 | } |
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend index 699b095d..dca10baf 100644 --- a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/rules/RefinementRuleProvider.xtend | |||
@@ -1,5 +1,6 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.rules | 1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.rules |
2 | 2 | ||
3 | import com.google.common.collect.ImmutableList | ||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.InverseRelationAssertion | 4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.InverseRelationAssertion |
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.LowerMultiplicityAssertion | 5 | import hu.bme.mit.inf.dslreasoner.ecore2logic.ecore2logicannotations.LowerMultiplicityAssertion |
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolTypeReference | 6 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.BoolTypeReference |
@@ -29,12 +30,14 @@ import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.par | |||
29 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationFactory | 30 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialinterpretationFactory |
30 | import java.lang.reflect.Field | 31 | import java.lang.reflect.Field |
31 | import java.util.HashMap | 32 | import java.util.HashMap |
33 | import java.util.Iterator | ||
32 | import java.util.LinkedHashMap | 34 | import java.util.LinkedHashMap |
33 | import java.util.LinkedList | 35 | import java.util.LinkedList |
34 | import java.util.List | 36 | import java.util.List |
35 | import java.util.Map | 37 | import java.util.Map |
36 | import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine | 38 | import org.eclipse.viatra.query.runtime.api.AdvancedViatraQueryEngine |
37 | import org.eclipse.viatra.query.runtime.api.GenericPatternMatch | 39 | import org.eclipse.viatra.query.runtime.api.GenericPatternMatch |
40 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
38 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification | 41 | import org.eclipse.viatra.query.runtime.api.IQuerySpecification |
39 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine | 42 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine |
40 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | 43 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher |
@@ -43,6 +46,7 @@ import org.eclipse.viatra.query.runtime.rete.matcher.ReteBackendFactory | |||
43 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule | 46 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRule |
44 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory | 47 | import org.eclipse.viatra.transformation.runtime.emf.rules.batch.BatchTransformationRuleFactory |
45 | import org.eclipse.xtend.lib.annotations.Data | 48 | import org.eclipse.xtend.lib.annotations.Data |
49 | import org.eclipse.xtend.lib.annotations.FinalFieldsConstructor | ||
46 | import org.eclipse.xtext.xbase.lib.Functions.Function0 | 50 | import org.eclipse.xtext.xbase.lib.Functions.Function0 |
47 | 51 | ||
48 | class RefinementRuleProvider { | 52 | class RefinementRuleProvider { |
@@ -50,57 +54,55 @@ class RefinementRuleProvider { | |||
50 | val extension PartialinterpretationFactory factory2 = PartialinterpretationFactory.eINSTANCE | 54 | val extension PartialinterpretationFactory factory2 = PartialinterpretationFactory.eINSTANCE |
51 | val extension LogiclanguageFactory factory3 = LogiclanguageFactory.eINSTANCE | 55 | val extension LogiclanguageFactory factory3 = LogiclanguageFactory.eINSTANCE |
52 | 56 | ||
53 | var AdvancedViatraQueryEngine queryEngine | ||
54 | var Field delayMessageDelivery | ||
55 | |||
56 | def canonizeName(String name) { | 57 | def canonizeName(String name) { |
57 | return name.replace(' ', '_') | 58 | return name.replace(' ', '_') |
58 | } | 59 | } |
59 | 60 | ||
61 | def createUnitPrulePropagator(LogicProblem p, PartialInterpretation i, GeneratedPatterns patterns, | ||
62 | ScopePropagator scopePropagator, ModelGenerationStatistics statistics) { | ||
63 | new UnitRulePropagator(p, i, this, scopePropagator, patterns.mustRelationPropagationQueries, statistics) | ||
64 | } | ||
65 | |||
60 | def LinkedHashMap<ObjectCreationPrecondition, BatchTransformationRule<GenericPatternMatch, ViatraQueryMatcher<GenericPatternMatch>>> createObjectRefinementRules( | 66 | def LinkedHashMap<ObjectCreationPrecondition, BatchTransformationRule<GenericPatternMatch, ViatraQueryMatcher<GenericPatternMatch>>> createObjectRefinementRules( |
61 | LogicProblem p, | 67 | LogicProblem p, |
62 | PartialInterpretation i, | 68 | PartialInterpretation i, |
63 | GeneratedPatterns patterns, | 69 | GeneratedPatterns patterns, |
64 | ScopePropagator scopePropagator, | 70 | UnitRulePropagator unitRulePropagator, |
65 | boolean nameNewElement, | 71 | boolean nameNewElement, |
66 | ModelGenerationStatistics statistics | 72 | ModelGenerationStatistics statistics |
67 | ) { | 73 | ) { |
68 | val res = new LinkedHashMap | 74 | val res = new LinkedHashMap |
69 | val recursiveObjectCreation = recursiveObjectCreation(p, i) | 75 | val recursiveObjectCreation = recursiveObjectCreation(p, i) |
70 | queryEngine = ViatraQueryEngine.on(new EMFScope(i)) as AdvancedViatraQueryEngine | ||
71 | delayMessageDelivery = queryEngine.class.getDeclaredField("delayMessageDelivery") | ||
72 | delayMessageDelivery.accessible = true | ||
73 | for (LHSEntry : patterns.refineObjectQueries.entrySet) { | 76 | for (LHSEntry : patterns.refineObjectQueries.entrySet) { |
74 | val containmentRelation = LHSEntry.key.containmentRelation | 77 | val containmentRelation = LHSEntry.key.containmentRelation |
75 | val inverseRelation = LHSEntry.key.inverseContainment | 78 | val inverseRelation = LHSEntry.key.inverseContainment |
76 | val type = LHSEntry.key.newType | 79 | val type = LHSEntry.key.newType |
77 | val lhs = LHSEntry.value as IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> | 80 | val lhs = LHSEntry.value as IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> |
78 | val rule = createObjectCreationRule(p, containmentRelation, inverseRelation, type, | 81 | val rule = createObjectCreationRule(p, containmentRelation, inverseRelation, type, |
79 | recursiveObjectCreation.get(type), lhs, nameNewElement, scopePropagator, statistics) | 82 | recursiveObjectCreation.get(type), lhs, nameNewElement, unitRulePropagator, statistics) |
80 | res.put(LHSEntry.key, rule) | 83 | res.put(LHSEntry.key, rule) |
81 | } | 84 | } |
82 | return res | 85 | return res |
83 | } | 86 | } |
84 | 87 | ||
85 | def private createObjectCreationRule(LogicProblem p, Relation containmentRelation, Relation inverseRelation, | 88 | def private createObjectCreationRule(LogicProblem p, Relation containmentRelation, Relation inverseRelation, |
86 | Type type, List<ObjectCreationInterpretationData> recursiceObjectCreations, | 89 | Type type, List<ObjectCreationInterpretationData> recursiveObjectCreations, |
87 | IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> lhs, boolean nameNewElement, | 90 | IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> lhs, boolean nameNewElement, |
88 | ScopePropagator scopePropagator, ModelGenerationStatistics statistics) { | 91 | UnitRulePropagator unitRulePropagator, ModelGenerationStatistics statistics) { |
89 | val name = '''addObject_«type.name.canonizeName»«IF containmentRelation!==null»_by_«containmentRelation.name.canonizeName»«ENDIF»''' | 92 | val name = '''addObject_«type.name.canonizeName»«IF containmentRelation!==null»_by_«containmentRelation.name.canonizeName»«ENDIF»''' |
90 | val ruleBuilder = factory.createRule(lhs).name(name) | 93 | val ruleBuilder = factory.createRule(lhs).name(name) |
91 | if (containmentRelation !== null) { | 94 | if (containmentRelation !== null) { |
92 | if (inverseRelation !== null) { | 95 | if (inverseRelation !== null) { |
93 | ruleBuilder.action [ match | | 96 | ruleBuilder.action [ match | |
94 | statistics.incrementTransformationCount | 97 | statistics.incrementTransformationCount |
95 | // println(name) | 98 | // println(name) |
99 | val startTime = System.nanoTime | ||
96 | // val problem = match.get(0) as LogicProblem | 100 | // val problem = match.get(0) as LogicProblem |
97 | val interpretation = match.get(1) as PartialInterpretation | 101 | val interpretation = match.get(1) as PartialInterpretation |
98 | val relationInterpretation = match.get(2) as PartialRelationInterpretation | 102 | val relationInterpretation = match.get(2) as PartialRelationInterpretation |
99 | val inverseRelationInterpretation = match.get(3) as PartialRelationInterpretation | 103 | val inverseRelationInterpretation = match.get(3) as PartialRelationInterpretation |
100 | val typeInterpretation = match.get(4) as PartialComplexTypeInterpretation | 104 | val typeInterpretation = match.get(4) as PartialComplexTypeInterpretation |
101 | val container = match.get(5) as DefinedElement | 105 | val container = match.get(5) as DefinedElement |
102 | |||
103 | val startTime = System.nanoTime | ||
104 | createObjectActionWithContainmentAndInverse( | 106 | createObjectActionWithContainmentAndInverse( |
105 | nameNewElement, | 107 | nameNewElement, |
106 | interpretation, | 108 | interpretation, |
@@ -109,29 +111,24 @@ class RefinementRuleProvider { | |||
109 | relationInterpretation, | 111 | relationInterpretation, |
110 | inverseRelationInterpretation, | 112 | inverseRelationInterpretation, |
111 | [createDefinedElement], | 113 | [createDefinedElement], |
112 | recursiceObjectCreations, | 114 | recursiveObjectCreations, |
113 | scopePropagator | 115 | unitRulePropagator |
114 | ) | 116 | ) |
115 | statistics.addExecutionTime(System.nanoTime - startTime) | 117 | statistics.addExecutionTime(System.nanoTime - startTime) |
116 | 118 | ||
117 | flushQueryEngine(scopePropagator) | 119 | unitRulePropagator.propagate |
118 | |||
119 | // Scope propagation | ||
120 | val propagatorStartTime = System.nanoTime | ||
121 | scopePropagator.propagateAllScopeConstraints() | ||
122 | statistics.addScopePropagationTime(System.nanoTime - propagatorStartTime) | ||
123 | ] | 120 | ] |
124 | } else { | 121 | } else { |
125 | ruleBuilder.action [ match | | 122 | ruleBuilder.action [ match | |
126 | statistics.incrementTransformationCount | 123 | statistics.incrementTransformationCount |
127 | // println(name) | 124 | // println(name) |
125 | val startTime = System.nanoTime | ||
128 | // val problem = match.get(0) as LogicProblem | 126 | // val problem = match.get(0) as LogicProblem |
129 | val interpretation = match.get(1) as PartialInterpretation | 127 | val interpretation = match.get(1) as PartialInterpretation |
130 | val relationInterpretation = match.get(2) as PartialRelationInterpretation | 128 | val relationInterpretation = match.get(2) as PartialRelationInterpretation |
131 | val typeInterpretation = match.get(3) as PartialComplexTypeInterpretation | 129 | val typeInterpretation = match.get(3) as PartialComplexTypeInterpretation |
132 | val container = match.get(4) as DefinedElement | 130 | val container = match.get(4) as DefinedElement |
133 | 131 | ||
134 | val startTime = System.nanoTime | ||
135 | createObjectActionWithContainment( | 132 | createObjectActionWithContainment( |
136 | nameNewElement, | 133 | nameNewElement, |
137 | interpretation, | 134 | interpretation, |
@@ -139,44 +136,34 @@ class RefinementRuleProvider { | |||
139 | container, | 136 | container, |
140 | relationInterpretation, | 137 | relationInterpretation, |
141 | [createDefinedElement], | 138 | [createDefinedElement], |
142 | recursiceObjectCreations, | 139 | recursiveObjectCreations, |
143 | scopePropagator | 140 | unitRulePropagator |
144 | ) | 141 | ) |
145 | statistics.addExecutionTime(System.nanoTime - startTime) | 142 | statistics.addExecutionTime(System.nanoTime - startTime) |
146 | 143 | ||
147 | flushQueryEngine(scopePropagator) | 144 | unitRulePropagator.propagate |
148 | |||
149 | // Scope propagation | ||
150 | val propagatorStartTime = System.nanoTime | ||
151 | scopePropagator.propagateAllScopeConstraints() | ||
152 | statistics.addScopePropagationTime(System.nanoTime - propagatorStartTime) | ||
153 | ] | 145 | ] |
154 | } | 146 | } |
155 | } else { | 147 | } else { |
156 | ruleBuilder.action [ match | | 148 | ruleBuilder.action [ match | |
157 | statistics.incrementTransformationCount | 149 | statistics.incrementTransformationCount |
158 | // println(name) | 150 | // println(name) |
151 | val startTime = System.nanoTime | ||
159 | // val problem = match.get(0) as LogicProblem | 152 | // val problem = match.get(0) as LogicProblem |
160 | val interpretation = match.get(1) as PartialInterpretation | 153 | val interpretation = match.get(1) as PartialInterpretation |
161 | val typeInterpretation = match.get(2) as PartialComplexTypeInterpretation | 154 | val typeInterpretation = match.get(2) as PartialComplexTypeInterpretation |
162 | 155 | ||
163 | val startTime = System.nanoTime | ||
164 | createObjectAction( | 156 | createObjectAction( |
165 | nameNewElement, | 157 | nameNewElement, |
166 | interpretation, | 158 | interpretation, |
167 | typeInterpretation, | 159 | typeInterpretation, |
168 | [createDefinedElement], | 160 | [createDefinedElement], |
169 | recursiceObjectCreations, | 161 | recursiveObjectCreations, |
170 | scopePropagator | 162 | unitRulePropagator |
171 | ) | 163 | ) |
172 | statistics.addExecutionTime(System.nanoTime - startTime) | 164 | statistics.addExecutionTime(System.nanoTime - startTime) |
173 | 165 | ||
174 | flushQueryEngine(scopePropagator) | 166 | unitRulePropagator.propagate |
175 | |||
176 | // Scope propagation | ||
177 | val propagatorStartTime = System.nanoTime | ||
178 | scopePropagator.propagateAllScopeConstraints() | ||
179 | statistics.addScopePropagationTime(System.nanoTime - propagatorStartTime) | ||
180 | ] | 167 | ] |
181 | } | 168 | } |
182 | return ruleBuilder.build | 169 | return ruleBuilder.build |
@@ -342,14 +329,14 @@ class RefinementRuleProvider { | |||
342 | [createStringElement] | 329 | [createStringElement] |
343 | } | 330 | } |
344 | 331 | ||
345 | def createRelationRefinementRules(GeneratedPatterns patterns, ScopePropagator scopePropagator, | 332 | def createRelationRefinementRules(GeneratedPatterns patterns, UnitRulePropagator unitRulePropagator, |
346 | ModelGenerationStatistics statistics) { | 333 | ModelGenerationStatistics statistics) { |
347 | val res = new LinkedHashMap | 334 | val res = new LinkedHashMap |
348 | for (LHSEntry : patterns.refinerelationQueries.entrySet) { | 335 | for (LHSEntry : patterns.refineRelationQueries.entrySet) { |
349 | val declaration = LHSEntry.key.key | 336 | val declaration = LHSEntry.key.key |
350 | val inverseReference = LHSEntry.key.value | 337 | val inverseReference = LHSEntry.key.value |
351 | val lhs = LHSEntry.value as IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> | 338 | val lhs = LHSEntry.value as IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> |
352 | val rule = createRelationRefinementRule(declaration, inverseReference, lhs, scopePropagator, statistics) | 339 | val rule = createRelationRefinementRule(declaration, inverseReference, lhs, unitRulePropagator, statistics) |
353 | res.put(LHSEntry.key, rule) | 340 | res.put(LHSEntry.key, rule) |
354 | } | 341 | } |
355 | return res | 342 | return res |
@@ -357,57 +344,29 @@ class RefinementRuleProvider { | |||
357 | 344 | ||
358 | def private BatchTransformationRule<GenericPatternMatch, ViatraQueryMatcher<GenericPatternMatch>> createRelationRefinementRule( | 345 | def private BatchTransformationRule<GenericPatternMatch, ViatraQueryMatcher<GenericPatternMatch>> createRelationRefinementRule( |
359 | RelationDeclaration declaration, Relation inverseRelation, | 346 | RelationDeclaration declaration, Relation inverseRelation, |
360 | IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> lhs, ScopePropagator scopePropagator, | 347 | IQuerySpecification<ViatraQueryMatcher<GenericPatternMatch>> lhs, UnitRulePropagator unitRulePropagator, |
361 | ModelGenerationStatistics statistics) { | 348 | ModelGenerationStatistics statistics) { |
362 | val name = '''addRelation_«declaration.name.canonizeName»«IF inverseRelation !== null»_and_«inverseRelation.name.canonizeName»«ENDIF»''' | 349 | val name = '''addRelation_«declaration.name.canonizeName»«IF inverseRelation !== null»_and_«inverseRelation.name.canonizeName»«ENDIF»''' |
363 | val ruleBuilder = factory.createRule(lhs).name(name) | 350 | val ruleBuilder = factory.createRule(lhs).name(name) |
364 | if (inverseRelation === null) { | 351 | if (inverseRelation === null) { |
365 | ruleBuilder.action [ match | | 352 | ruleBuilder.action [ match | |
366 | statistics.incrementTransformationCount | 353 | statistics.incrementTransformationCount |
367 | |||
368 | // println(name) | 354 | // println(name) |
369 | // val problem = match.get(0) as LogicProblem | ||
370 | // val interpretation = match.get(1) as PartialInterpretation | ||
371 | val relationInterpretation = match.get(2) as PartialRelationInterpretation | ||
372 | val src = match.get(3) as DefinedElement | ||
373 | val trg = match.get(4) as DefinedElement | ||
374 | |||
375 | val startTime = System.nanoTime | 355 | val startTime = System.nanoTime |
376 | createRelationLinkAction(src, trg, relationInterpretation) | 356 | createRelationLinkAction(match, unitRulePropagator) |
377 | statistics.addExecutionTime(System.nanoTime - startTime) | 357 | statistics.addExecutionTime(System.nanoTime - startTime) |
378 | 358 | ||
379 | // Scope propagation | 359 | unitRulePropagator.propagate |
380 | if (scopePropagator.isPropagationNeededAfterAdditionToRelation(declaration)) { | ||
381 | flushQueryEngine(scopePropagator) | ||
382 | |||
383 | val propagatorStartTime = System.nanoTime | ||
384 | scopePropagator.propagateAllScopeConstraints() | ||
385 | statistics.addScopePropagationTime(System.nanoTime - propagatorStartTime) | ||
386 | } | ||
387 | ] | 360 | ] |
388 | } else { | 361 | } else { |
389 | ruleBuilder.action [ match | | 362 | ruleBuilder.action [ match | |
390 | statistics.incrementTransformationCount | 363 | statistics.incrementTransformationCount |
391 | // println(name) | 364 | // println(name) |
392 | // val problem = match.get(0) as LogicProblem | ||
393 | // val interpretation = match.get(1) as PartialInterpretation | ||
394 | val relationInterpretation = match.get(2) as PartialRelationInterpretation | ||
395 | val inverseInterpretation = match.get(3) as PartialRelationInterpretation | ||
396 | val src = match.get(4) as DefinedElement | ||
397 | val trg = match.get(5) as DefinedElement | ||
398 | |||
399 | val startTime = System.nanoTime | 365 | val startTime = System.nanoTime |
400 | createRelationLinkWithInverse(src, trg, relationInterpretation, inverseInterpretation) | 366 | createRelationLinkWithInverse(match, unitRulePropagator) |
401 | statistics.addExecutionTime(System.nanoTime - startTime) | 367 | statistics.addExecutionTime(System.nanoTime - startTime) |
402 | 368 | ||
403 | // Scope propagation | 369 | unitRulePropagator.propagate |
404 | if (scopePropagator.isPropagationNeededAfterAdditionToRelation(declaration)) { | ||
405 | flushQueryEngine(scopePropagator) | ||
406 | |||
407 | val propagatorStartTime = System.nanoTime | ||
408 | scopePropagator.propagateAllScopeConstraints() | ||
409 | statistics.addScopePropagationTime(System.nanoTime - propagatorStartTime) | ||
410 | } | ||
411 | ] | 370 | ] |
412 | } | 371 | } |
413 | 372 | ||
@@ -418,7 +377,7 @@ class RefinementRuleProvider { | |||
418 | // Actions | 377 | // Actions |
419 | // /////////////////////// | 378 | // /////////////////////// |
420 | protected def void createObjectAction(boolean nameNewElement, ObjectCreationInterpretationData data, | 379 | protected def void createObjectAction(boolean nameNewElement, ObjectCreationInterpretationData data, |
421 | DefinedElement container, ScopePropagator scopePropagator) { | 380 | DefinedElement container, UnitRulePropagator unitRulePropagator) { |
422 | if (data.containerInterpretation !== null) { | 381 | if (data.containerInterpretation !== null) { |
423 | if (data.containerInverseInterpretation !== null) { | 382 | if (data.containerInverseInterpretation !== null) { |
424 | createObjectActionWithContainmentAndInverse( | 383 | createObjectActionWithContainmentAndInverse( |
@@ -430,7 +389,7 @@ class RefinementRuleProvider { | |||
430 | data.containerInverseInterpretation, | 389 | data.containerInverseInterpretation, |
431 | data.constructor, | 390 | data.constructor, |
432 | data.recursiveConstructors, | 391 | data.recursiveConstructors, |
433 | scopePropagator | 392 | unitRulePropagator |
434 | ) | 393 | ) |
435 | } else { | 394 | } else { |
436 | createObjectActionWithContainment( | 395 | createObjectActionWithContainment( |
@@ -441,7 +400,7 @@ class RefinementRuleProvider { | |||
441 | data.containerInterpretation, | 400 | data.containerInterpretation, |
442 | data.constructor, | 401 | data.constructor, |
443 | data.recursiveConstructors, | 402 | data.recursiveConstructors, |
444 | scopePropagator | 403 | unitRulePropagator |
445 | ) | 404 | ) |
446 | } | 405 | } |
447 | } else { | 406 | } else { |
@@ -451,7 +410,7 @@ class RefinementRuleProvider { | |||
451 | data.typeInterpretation, | 410 | data.typeInterpretation, |
452 | data.constructor, | 411 | data.constructor, |
453 | data.recursiveConstructors, | 412 | data.recursiveConstructors, |
454 | scopePropagator | 413 | unitRulePropagator |
455 | ) | 414 | ) |
456 | } | 415 | } |
457 | 416 | ||
@@ -466,7 +425,7 @@ class RefinementRuleProvider { | |||
466 | PartialRelationInterpretation inverseRelationInterpretation, | 425 | PartialRelationInterpretation inverseRelationInterpretation, |
467 | Function0<DefinedElement> constructor, | 426 | Function0<DefinedElement> constructor, |
468 | List<ObjectCreationInterpretationData> recursiceObjectCreations, | 427 | List<ObjectCreationInterpretationData> recursiceObjectCreations, |
469 | ScopePropagator scopePropagator | 428 | UnitRulePropagator unitRulePropagator |
470 | ) { | 429 | ) { |
471 | val newElement = constructor.apply | 430 | val newElement = constructor.apply |
472 | if (nameNewElement) { | 431 | if (nameNewElement) { |
@@ -486,14 +445,16 @@ class RefinementRuleProvider { | |||
486 | inverseRelationInterpretation.relationlinks += newLink2 | 445 | inverseRelationInterpretation.relationlinks += newLink2 |
487 | 446 | ||
488 | // Scope propagation | 447 | // Scope propagation |
489 | scopePropagator.decrementTypeScope(typeInterpretation) | 448 | unitRulePropagator.decrementTypeScope(typeInterpretation) |
449 | unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf) | ||
450 | unitRulePropagator.addedToRelation(inverseRelationInterpretation.interpretationOf) | ||
490 | 451 | ||
491 | // Existence | 452 | // Existence |
492 | interpretation.newElements += newElement | 453 | interpretation.newElements += newElement |
493 | 454 | ||
494 | // Do recursive object creation | 455 | // Do recursive object creation |
495 | for (newConstructor : recursiceObjectCreations) { | 456 | for (newConstructor : recursiceObjectCreations) { |
496 | createObjectAction(nameNewElement, newConstructor, newElement, scopePropagator) | 457 | createObjectAction(nameNewElement, newConstructor, newElement, unitRulePropagator) |
497 | } | 458 | } |
498 | 459 | ||
499 | return newElement | 460 | return newElement |
@@ -507,7 +468,7 @@ class RefinementRuleProvider { | |||
507 | PartialRelationInterpretation relationInterpretation, | 468 | PartialRelationInterpretation relationInterpretation, |
508 | Function0<DefinedElement> constructor, | 469 | Function0<DefinedElement> constructor, |
509 | List<ObjectCreationInterpretationData> recursiceObjectCreations, | 470 | List<ObjectCreationInterpretationData> recursiceObjectCreations, |
510 | ScopePropagator scopePropagator | 471 | UnitRulePropagator unitRulePropagator |
511 | ) { | 472 | ) { |
512 | val newElement = constructor.apply | 473 | val newElement = constructor.apply |
513 | if (nameNewElement) { | 474 | if (nameNewElement) { |
@@ -522,16 +483,17 @@ class RefinementRuleProvider { | |||
522 | // ContainmentRelation | 483 | // ContainmentRelation |
523 | val newLink = factory2.createBinaryElementRelationLink => [it.param1 = container it.param2 = newElement] | 484 | val newLink = factory2.createBinaryElementRelationLink => [it.param1 = container it.param2 = newElement] |
524 | relationInterpretation.relationlinks += newLink | 485 | relationInterpretation.relationlinks += newLink |
486 | unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf) | ||
525 | 487 | ||
526 | // Scope propagation | 488 | // Scope propagation |
527 | scopePropagator.decrementTypeScope(typeInterpretation) | 489 | unitRulePropagator.decrementTypeScope(typeInterpretation) |
528 | 490 | ||
529 | // Existence | 491 | // Existence |
530 | interpretation.newElements += newElement | 492 | interpretation.newElements += newElement |
531 | 493 | ||
532 | // Do recursive object creation | 494 | // Do recursive object creation |
533 | for (newConstructor : recursiceObjectCreations) { | 495 | for (newConstructor : recursiceObjectCreations) { |
534 | createObjectAction(nameNewElement, newConstructor, newElement, scopePropagator) | 496 | createObjectAction(nameNewElement, newConstructor, newElement, unitRulePropagator) |
535 | } | 497 | } |
536 | 498 | ||
537 | return newElement | 499 | return newElement |
@@ -539,7 +501,7 @@ class RefinementRuleProvider { | |||
539 | 501 | ||
540 | protected def createObjectAction(boolean nameNewElement, PartialInterpretation interpretation, | 502 | protected def createObjectAction(boolean nameNewElement, PartialInterpretation interpretation, |
541 | PartialTypeInterpratation typeInterpretation, Function0<DefinedElement> constructor, | 503 | PartialTypeInterpratation typeInterpretation, Function0<DefinedElement> constructor, |
542 | List<ObjectCreationInterpretationData> recursiceObjectCreations, ScopePropagator scopePropagator) { | 504 | List<ObjectCreationInterpretationData> recursiceObjectCreations, UnitRulePropagator unitRulePropagator) { |
543 | val newElement = constructor.apply | 505 | val newElement = constructor.apply |
544 | if (nameNewElement) { | 506 | if (nameNewElement) { |
545 | newElement.name = '''new «interpretation.newElements.size»''' | 507 | newElement.name = '''new «interpretation.newElements.size»''' |
@@ -552,38 +514,220 @@ class RefinementRuleProvider { | |||
552 | } | 514 | } |
553 | 515 | ||
554 | // Scope propagation | 516 | // Scope propagation |
555 | scopePropagator.decrementTypeScope(typeInterpretation) | 517 | unitRulePropagator.decrementTypeScope(typeInterpretation) |
556 | 518 | ||
557 | // Existence | 519 | // Existence |
558 | interpretation.newElements += newElement | 520 | interpretation.newElements += newElement |
559 | 521 | ||
560 | // Do recursive object creation | 522 | // Do recursive object creation |
561 | for (newConstructor : recursiceObjectCreations) { | 523 | for (newConstructor : recursiceObjectCreations) { |
562 | createObjectAction(nameNewElement, newConstructor, newElement, scopePropagator) | 524 | createObjectAction(nameNewElement, newConstructor, newElement, unitRulePropagator) |
563 | } | 525 | } |
564 | 526 | ||
565 | return newElement | 527 | return newElement |
566 | } | 528 | } |
567 | 529 | ||
568 | protected def boolean createRelationLinkAction(DefinedElement src, DefinedElement trg, | 530 | protected def createRelationLinkAction(IPatternMatch match, UnitRulePropagator unitRulePropagator) { |
569 | PartialRelationInterpretation relationInterpretation) { | 531 | // val problem = match.get(0) as LogicProblem |
532 | // val interpretation = match.get(1) as PartialInterpretation | ||
533 | val relationInterpretation = match.get(2) as PartialRelationInterpretation | ||
534 | val src = match.get(3) as DefinedElement | ||
535 | val trg = match.get(4) as DefinedElement | ||
536 | createRelationLinkAction(src, trg, relationInterpretation, unitRulePropagator) | ||
537 | } | ||
538 | |||
539 | protected def void createRelationLinkAction(DefinedElement src, DefinedElement trg, | ||
540 | PartialRelationInterpretation relationInterpretation, UnitRulePropagator unitRulePropagator) { | ||
570 | val link = createBinaryElementRelationLink => [it.param1 = src it.param2 = trg] | 541 | val link = createBinaryElementRelationLink => [it.param1 = src it.param2 = trg] |
571 | relationInterpretation.relationlinks += link | 542 | relationInterpretation.relationlinks += link |
543 | unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf) | ||
572 | } | 544 | } |
573 | 545 | ||
574 | protected def boolean createRelationLinkWithInverse(DefinedElement src, DefinedElement trg, | 546 | protected def void createRelationLinkWithInverse(IPatternMatch match, UnitRulePropagator unitRulePropagator) { |
575 | PartialRelationInterpretation relationInterpretation, PartialRelationInterpretation inverseInterpretation) { | 547 | // val problem = match.get(0) as LogicProblem |
548 | // val interpretation = match.get(1) as PartialInterpretation | ||
549 | val relationInterpretation = match.get(2) as PartialRelationInterpretation | ||
550 | val inverseInterpretation = match.get(3) as PartialRelationInterpretation | ||
551 | val src = match.get(4) as DefinedElement | ||
552 | val trg = match.get(5) as DefinedElement | ||
553 | createRelationLinkWithInverse(src, trg, relationInterpretation, inverseInterpretation, unitRulePropagator) | ||
554 | } | ||
555 | |||
556 | protected def void createRelationLinkWithInverse(DefinedElement src, DefinedElement trg, | ||
557 | PartialRelationInterpretation relationInterpretation, PartialRelationInterpretation inverseInterpretation, | ||
558 | UnitRulePropagator unitRulePropagator) { | ||
576 | val link = createBinaryElementRelationLink => [it.param1 = src it.param2 = trg] | 559 | val link = createBinaryElementRelationLink => [it.param1 = src it.param2 = trg] |
577 | relationInterpretation.relationlinks += link | 560 | relationInterpretation.relationlinks += link |
578 | val inverseLink = createBinaryElementRelationLink => [it.param1 = trg it.param2 = src] | 561 | val inverseLink = createBinaryElementRelationLink => [it.param1 = trg it.param2 = src] |
579 | inverseInterpretation.relationlinks += inverseLink | 562 | inverseInterpretation.relationlinks += inverseLink |
563 | unitRulePropagator.addedToRelation(relationInterpretation.interpretationOf) | ||
564 | unitRulePropagator.addedToRelation(inverseInterpretation.interpretationOf) | ||
580 | } | 565 | } |
581 | 566 | ||
582 | protected def flushQueryEngine(ScopePropagator scopePropagator) { | 567 | static class UnitRulePropagator { |
583 | if (scopePropagator.queryEngineFlushRequiredBeforePropagation && queryEngine.updatePropagationDelayed) { | 568 | val LogicProblem p |
584 | delayMessageDelivery.setBoolean(queryEngine, false) | 569 | val PartialInterpretation i |
585 | queryEngine.getQueryBackend(ReteBackendFactory.INSTANCE).flushUpdates | 570 | val RefinementRuleProvider refinementRuleProvider |
586 | delayMessageDelivery.setBoolean(queryEngine, true) | 571 | var AdvancedViatraQueryEngine queryEngine |
572 | var Field delayMessageDelivery | ||
573 | val ScopePropagator scopePropagator | ||
574 | val List<AbstractMustRelationPropagator<? extends IPatternMatch>> propagators | ||
575 | val ModelGenerationStatistics statistics | ||
576 | |||
577 | new(LogicProblem p, PartialInterpretation i, RefinementRuleProvider refinementRuleProvider, | ||
578 | ScopePropagator scopePropagator, | ||
579 | Map<Pair<RelationDeclaration, Relation>, IQuerySpecification<? extends ViatraQueryMatcher<? extends IPatternMatch>>> mustRelationPropagationQueries, | ||
580 | ModelGenerationStatistics statistics) { | ||
581 | this.p = p | ||
582 | this.i = i | ||
583 | this.refinementRuleProvider = refinementRuleProvider | ||
584 | queryEngine = ViatraQueryEngine.on(new EMFScope(i)) as AdvancedViatraQueryEngine | ||
585 | delayMessageDelivery = queryEngine.class.getDeclaredField("delayMessageDelivery") | ||
586 | delayMessageDelivery.accessible = true | ||
587 | this.scopePropagator = scopePropagator | ||
588 | propagators = ImmutableList.copyOf(mustRelationPropagationQueries.entrySet.map [ entry | | ||
589 | val matcher = queryEngine.getMatcher(entry.value) | ||
590 | getPropagator(entry.key.key, entry.key.value, matcher) | ||
591 | ]) | ||
592 | this.statistics = statistics | ||
593 | } | ||
594 | |||
595 | def decrementTypeScope(PartialTypeInterpratation partialTypeInterpratation) { | ||
596 | scopePropagator.decrementTypeScope(partialTypeInterpratation) | ||
597 | } | ||
598 | |||
599 | def addedToRelation(Relation r) { | ||
600 | scopePropagator.addedToRelation(r) | ||
601 | } | ||
602 | |||
603 | def propagate() { | ||
604 | var boolean changed | ||
605 | do { | ||
606 | val scopeChanged = propagateScope() | ||
607 | val mustChanged = propagateMustRelations() | ||
608 | changed = scopeChanged || mustChanged | ||
609 | } while (changed) | ||
610 | } | ||
611 | |||
612 | protected def flushQueryEngine() { | ||
613 | if (queryEngine.updatePropagationDelayed) { | ||
614 | delayMessageDelivery.setBoolean(queryEngine, false) | ||
615 | queryEngine.getQueryBackend(ReteBackendFactory.INSTANCE).flushUpdates | ||
616 | delayMessageDelivery.setBoolean(queryEngine, true) | ||
617 | } | ||
618 | } | ||
619 | |||
620 | protected def propagateScope() { | ||
621 | if (scopePropagator.scopePropagationNeeded) { | ||
622 | if (scopePropagator.queryEngineFlushRequiredBeforePropagation) { | ||
623 | flushQueryEngine() | ||
624 | } | ||
625 | val propagatorStartTime = System.nanoTime | ||
626 | scopePropagator.propagateAllScopeConstraints() | ||
627 | statistics.addScopePropagationTime(System.nanoTime - propagatorStartTime) | ||
628 | true | ||
629 | } else { | ||
630 | false | ||
631 | } | ||
632 | } | ||
633 | |||
634 | protected def propagateMustRelations() { | ||
635 | if (propagators.empty) { | ||
636 | return false | ||
637 | } | ||
638 | flushQueryEngine() | ||
639 | val propagatorStartTime = System.nanoTime | ||
640 | var changed = false | ||
641 | for (propagator : propagators) { | ||
642 | changed = propagator.propagate(p, i, refinementRuleProvider, this) || changed | ||
643 | } | ||
644 | statistics.addMustRelationPropagationTime(System.nanoTime - propagatorStartTime) | ||
645 | changed | ||
646 | } | ||
647 | |||
648 | private static def <T extends IPatternMatch> getPropagator(Relation relation, Relation inverseRelation, | ||
649 | ViatraQueryMatcher<T> matcher) { | ||
650 | if (inverseRelation === null) { | ||
651 | new MustRelationPropagator(matcher) | ||
652 | } else if (relation == inverseRelation) { | ||
653 | new MustRelationPropagatorWithSelfInverse(matcher) | ||
654 | } else { | ||
655 | new MustRelationPropagatorWithInverse(matcher) | ||
656 | } | ||
657 | } | ||
658 | |||
659 | @FinalFieldsConstructor | ||
660 | private static abstract class AbstractMustRelationPropagator<T extends IPatternMatch> { | ||
661 | val ViatraQueryMatcher<T> matcher | ||
662 | |||
663 | def propagate(LogicProblem p, PartialInterpretation i, RefinementRuleProvider refinementRuleProvider, | ||
664 | UnitRulePropagator unitRulePropagator) { | ||
665 | val iterator = getIterator(p, i) | ||
666 | if (!iterator.hasNext) { | ||
667 | return false | ||
668 | } | ||
669 | iterate(iterator, refinementRuleProvider, unitRulePropagator) | ||
670 | true | ||
671 | } | ||
672 | |||
673 | def iterate(Iterator<T> iterator, RefinementRuleProvider refinementRuleProvider, | ||
674 | UnitRulePropagator unitRulePropagator) { | ||
675 | while (iterator.hasNext) { | ||
676 | doPropagate(iterator.next, refinementRuleProvider, unitRulePropagator) | ||
677 | } | ||
678 | } | ||
679 | |||
680 | protected def getIterator(LogicProblem p, PartialInterpretation i) { | ||
681 | val partialMatch = matcher.newEmptyMatch | ||
682 | partialMatch.set(0, p) | ||
683 | partialMatch.set(1, i) | ||
684 | matcher.streamAllMatches(partialMatch).iterator | ||
685 | } | ||
686 | |||
687 | protected def void doPropagate(T match, RefinementRuleProvider refinementRuleProvider, | ||
688 | UnitRulePropagator unitRulePropagator) | ||
689 | } | ||
690 | |||
691 | private static class MustRelationPropagator<T extends IPatternMatch> extends AbstractMustRelationPropagator<T> { | ||
692 | new(ViatraQueryMatcher<T> matcher) { | ||
693 | super(matcher) | ||
694 | } | ||
695 | |||
696 | override protected doPropagate(T match, RefinementRuleProvider refinementRuleProvider, | ||
697 | UnitRulePropagator unitRulePropagator) { | ||
698 | refinementRuleProvider.createRelationLinkAction(match, unitRulePropagator) | ||
699 | } | ||
700 | } | ||
701 | |||
702 | private static class MustRelationPropagatorWithInverse<T extends IPatternMatch> extends AbstractMustRelationPropagator<T> { | ||
703 | new(ViatraQueryMatcher<T> matcher) { | ||
704 | super(matcher) | ||
705 | } | ||
706 | |||
707 | override protected doPropagate(T match, RefinementRuleProvider refinementRuleProvider, | ||
708 | UnitRulePropagator unitRulePropagator) { | ||
709 | refinementRuleProvider.createRelationLinkWithInverse(match, unitRulePropagator) | ||
710 | } | ||
711 | } | ||
712 | |||
713 | private static class MustRelationPropagatorWithSelfInverse<T extends IPatternMatch> extends MustRelationPropagatorWithInverse<T> { | ||
714 | new(ViatraQueryMatcher<T> matcher) { | ||
715 | super(matcher) | ||
716 | } | ||
717 | |||
718 | override iterate(Iterator<T> iterator, RefinementRuleProvider refinementRuleProvider, | ||
719 | UnitRulePropagator unitRulePropagator) { | ||
720 | val pairs = newHashSet | ||
721 | while (iterator.hasNext) { | ||
722 | val match = iterator.next | ||
723 | val src = match.get(4) as DefinedElement | ||
724 | val trg = match.get(5) as DefinedElement | ||
725 | if (!pairs.contains(trg -> src)) { | ||
726 | pairs.add(src -> trg) | ||
727 | doPropagate(match, refinementRuleProvider, unitRulePropagator) | ||
728 | } | ||
729 | } | ||
730 | } | ||
587 | } | 731 | } |
588 | } | 732 | } |
589 | } | 733 | } |