diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend | 109 |
1 files changed, 109 insertions, 0 deletions
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 new file mode 100644 index 00000000..0bdb202e --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/cardinality/ScopePropagator.xtend | |||
@@ -0,0 +1,109 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.cardinality | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Relation | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationStatistics | ||
5 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialComplexTypeInterpretation | ||
6 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
7 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialTypeInterpratation | ||
8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.Scope | ||
9 | import java.util.HashMap | ||
10 | import java.util.HashSet | ||
11 | import java.util.Map | ||
12 | import java.util.Set | ||
13 | import org.eclipse.xtend.lib.annotations.Accessors | ||
14 | |||
15 | class ScopePropagator { | ||
16 | @Accessors(PROTECTED_GETTER) val PartialInterpretation partialInterpretation | ||
17 | val ModelGenerationStatistics statistics | ||
18 | val Map<PartialTypeInterpratation, Scope> type2Scope | ||
19 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> superScopes | ||
20 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> subScopes | ||
21 | |||
22 | new(PartialInterpretation p, ModelGenerationStatistics statistics) { | ||
23 | partialInterpretation = p | ||
24 | this.statistics = statistics | ||
25 | type2Scope = new HashMap | ||
26 | for (scope : p.scopes) { | ||
27 | type2Scope.put(scope.targetTypeInterpretation, scope) | ||
28 | } | ||
29 | |||
30 | superScopes = new HashMap | ||
31 | subScopes = new HashMap | ||
32 | for (scope : p.scopes) { | ||
33 | superScopes.put(scope, new HashSet) | ||
34 | subScopes.put(scope, new HashSet) | ||
35 | } | ||
36 | |||
37 | for (scope : p.scopes) { | ||
38 | val target = scope.targetTypeInterpretation | ||
39 | if (target instanceof PartialComplexTypeInterpretation) { | ||
40 | val supertypeInterpretations = target.supertypeInterpretation | ||
41 | for (supertypeInterpretation : supertypeInterpretations) { | ||
42 | val supertypeScope = type2Scope.get(supertypeInterpretation) | ||
43 | superScopes.get(scope).add(supertypeScope) | ||
44 | subScopes.get(supertypeScope).add(scope) | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | var boolean changed | ||
49 | do { | ||
50 | changed = false | ||
51 | for (scope : p.scopes) { | ||
52 | val subScopeSet = subScopes.get(scope) | ||
53 | val superScopeSet = superScopes.get(scope) | ||
54 | for (subScope : subScopeSet) { | ||
55 | changed = changed || superScopes.get(subScope).addAll(superScopeSet) | ||
56 | } | ||
57 | for (superScope : superScopeSet) { | ||
58 | changed = changed || subScopes.get(superScope).addAll(subScopeSet) | ||
59 | } | ||
60 | } | ||
61 | } while (changed) | ||
62 | } | ||
63 | |||
64 | def propagateAllScopeConstraints() { | ||
65 | statistics.incrementScopePropagationCount() | ||
66 | doPropagateAllScopeConstraints() | ||
67 | } | ||
68 | |||
69 | protected def void doPropagateAllScopeConstraints() { | ||
70 | // Nothing to propagate. | ||
71 | } | ||
72 | |||
73 | def propagateAdditionToType(PartialTypeInterpratation t) { | ||
74 | // println('''Adding to «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') | ||
75 | val targetScope = type2Scope.get(t) | ||
76 | targetScope.removeOne | ||
77 | val sups = superScopes.get(targetScope) | ||
78 | sups.forEach[removeOne] | ||
79 | if (this.partialInterpretation.minNewElements > 0) { | ||
80 | this.partialInterpretation.minNewElements = this.partialInterpretation.minNewElements - 1 | ||
81 | } | ||
82 | if (this.partialInterpretation.maxNewElements > 0) { | ||
83 | this.partialInterpretation.maxNewElements = this.partialInterpretation.maxNewElements - 1 | ||
84 | } else if (this.partialInterpretation.maxNewElements === 0) { | ||
85 | throw new IllegalArgumentException('''Inconsistent object creation: lower node limit is 0!''') | ||
86 | } | ||
87 | propagateAllScopeConstraints | ||
88 | |||
89 | // println('''Target Scope: «targetScope.minNewElements» - «targetScope.maxNewElements»''') | ||
90 | // println(''' «this.partialInterpretation.minNewElements» - «this.partialInterpretation.maxNewElements»''') | ||
91 | // this.partialInterpretation.scopes.forEach[println(''' «(it.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»: «it.minNewElements»-«it.maxNewElements»''')] | ||
92 | // println('''All constraints are propagated upon increasing «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') | ||
93 | } | ||
94 | |||
95 | def void propagateAdditionToRelation(Relation r) { | ||
96 | // Nothing to propagate. | ||
97 | } | ||
98 | |||
99 | private def removeOne(Scope scope) { | ||
100 | if (scope.maxNewElements === 0) { | ||
101 | throw new IllegalArgumentException('''Inconsistent object creation: «scope.targetTypeInterpretation»''') | ||
102 | } else if (scope.maxNewElements > 0) { | ||
103 | scope.maxNewElements = scope.maxNewElements - 1 | ||
104 | } | ||
105 | if (scope.minNewElements > 0) { | ||
106 | scope.minNewElements = scope.minNewElements - 1 | ||
107 | } | ||
108 | } | ||
109 | } | ||