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 | 161 |
1 files changed, 161 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..cacba3c6 --- /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,161 @@ | |||
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.PartialPrimitiveInterpretation | ||
8 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialTypeInterpratation | ||
9 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.Scope | ||
10 | import java.util.HashMap | ||
11 | import java.util.HashSet | ||
12 | import java.util.Map | ||
13 | import java.util.Set | ||
14 | import org.eclipse.xtend.lib.annotations.Accessors | ||
15 | |||
16 | class ScopePropagator { | ||
17 | @Accessors(PROTECTED_GETTER) val PartialInterpretation partialInterpretation | ||
18 | @Accessors(PROTECTED_GETTER) val ModelGenerationStatistics statistics | ||
19 | val Map<PartialTypeInterpratation, Scope> type2Scope | ||
20 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> superScopes | ||
21 | @Accessors(PROTECTED_GETTER) val Map<Scope, Set<Scope>> subScopes | ||
22 | |||
23 | @Accessors(PUBLIC_GETTER) var scopePropagationNeeded = false | ||
24 | |||
25 | new(PartialInterpretation p, ModelGenerationStatistics statistics) { | ||
26 | partialInterpretation = p | ||
27 | this.statistics = statistics | ||
28 | type2Scope = new HashMap | ||
29 | for (scope : p.scopes) { | ||
30 | type2Scope.put(scope.targetTypeInterpretation, scope) | ||
31 | } | ||
32 | |||
33 | superScopes = new HashMap | ||
34 | subScopes = new HashMap | ||
35 | for (scope : p.scopes) { | ||
36 | superScopes.put(scope, new HashSet) | ||
37 | subScopes.put(scope, new HashSet) | ||
38 | } | ||
39 | |||
40 | for (scope : p.scopes) { | ||
41 | val target = scope.targetTypeInterpretation | ||
42 | if (target instanceof PartialComplexTypeInterpretation) { | ||
43 | val supertypeInterpretations = target.supertypeInterpretation | ||
44 | for (supertypeInterpretation : supertypeInterpretations) { | ||
45 | val supertypeScope = type2Scope.get(supertypeInterpretation) | ||
46 | superScopes.get(scope).add(supertypeScope) | ||
47 | subScopes.get(supertypeScope).add(scope) | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | var boolean changed | ||
52 | do { | ||
53 | changed = false | ||
54 | for (scope : p.scopes) { | ||
55 | val subScopeSet = subScopes.get(scope) | ||
56 | val superScopeSet = superScopes.get(scope) | ||
57 | for (subScope : subScopeSet) { | ||
58 | changed = changed || superScopes.get(subScope).addAll(superScopeSet) | ||
59 | } | ||
60 | for (superScope : superScopeSet) { | ||
61 | changed = changed || subScopes.get(superScope).addAll(subScopeSet) | ||
62 | } | ||
63 | } | ||
64 | } while (changed) | ||
65 | |||
66 | copyScopeBoundsToHeuristic() | ||
67 | } | ||
68 | |||
69 | def void propagateAllScopeConstraints() { | ||
70 | scopePropagationNeeded = false | ||
71 | if (!valid) { | ||
72 | return | ||
73 | } | ||
74 | statistics.incrementScopePropagationCount() | ||
75 | doPropagateAllScopeConstraints() | ||
76 | } | ||
77 | |||
78 | def isValid() { | ||
79 | partialInterpretation.maxNewElements == -1 || | ||
80 | partialInterpretation.minNewElements <= partialInterpretation.maxNewElements | ||
81 | } | ||
82 | |||
83 | protected def copyScopeBoundsToHeuristic() { | ||
84 | partialInterpretation.minNewElementsHeuristic = partialInterpretation.minNewElements | ||
85 | for (scope : partialInterpretation.scopes) { | ||
86 | scope.minNewElementsHeuristic = scope.minNewElements | ||
87 | } | ||
88 | } | ||
89 | |||
90 | protected def void doPropagateAllScopeConstraints() { | ||
91 | // Nothing to propagate. | ||
92 | } | ||
93 | |||
94 | def decrementTypeScope(PartialTypeInterpratation t) { | ||
95 | val isPrimitive = t instanceof PartialPrimitiveInterpretation || t === null | ||
96 | if (isPrimitive) { | ||
97 | return | ||
98 | } | ||
99 | scopePropagationNeeded = true | ||
100 | // println('''Adding to «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') | ||
101 | val targetScope = type2Scope.get(t) | ||
102 | if (targetScope !== null) { | ||
103 | targetScope.removeOne | ||
104 | val sups = superScopes.get(targetScope) | ||
105 | sups.forEach[removeOne] | ||
106 | } | ||
107 | if (this.partialInterpretation.minNewElements > 0) { | ||
108 | this.partialInterpretation.minNewElements = this.partialInterpretation.minNewElements - 1 | ||
109 | } | ||
110 | if (this.partialInterpretation.minNewElementsHeuristic > 0) { | ||
111 | this.partialInterpretation.minNewElementsHeuristic = this.partialInterpretation.minNewElementsHeuristic - 1 | ||
112 | } | ||
113 | if (this.partialInterpretation.maxNewElements > 0) { | ||
114 | this.partialInterpretation.maxNewElements = this.partialInterpretation.maxNewElements - 1 | ||
115 | } else if (this.partialInterpretation.maxNewElements === 0) { | ||
116 | setScopesInvalid() | ||
117 | } | ||
118 | |||
119 | // println('''Target Scope: «targetScope.minNewElements» - «targetScope.maxNewElements»''') | ||
120 | // println(''' «this.partialInterpretation.minNewElements» - «this.partialInterpretation.maxNewElements»''') | ||
121 | // this.partialInterpretation.scopes.forEach[println(''' «(it.targetTypeInterpretation as PartialComplexTypeInterpretation).interpretationOf.name»: «it.minNewElements»-«it.maxNewElements»''')] | ||
122 | // println('''All constraints are propagated upon increasing «(t as PartialComplexTypeInterpretation).interpretationOf.name»''') | ||
123 | } | ||
124 | |||
125 | def addedToRelation(Relation r) { | ||
126 | if (isPropagationNeededAfterAdditionToRelation(r)) { | ||
127 | scopePropagationNeeded = true | ||
128 | } | ||
129 | } | ||
130 | |||
131 | protected def setScopesInvalid() { | ||
132 | partialInterpretation.minNewElements = Integer.MAX_VALUE | ||
133 | partialInterpretation.maxNewElements = 0 | ||
134 | for (scope : partialInterpretation.scopes) { | ||
135 | scope.minNewElements = Integer.MAX_VALUE | ||
136 | scope.maxNewElements = 0 | ||
137 | } | ||
138 | } | ||
139 | |||
140 | protected def isPropagationNeededAfterAdditionToRelation(Relation r) { | ||
141 | false | ||
142 | } | ||
143 | |||
144 | def isQueryEngineFlushRequiredBeforePropagation() { | ||
145 | false | ||
146 | } | ||
147 | |||
148 | private def removeOne(Scope scope) { | ||
149 | if (scope.minNewElements > 0) { | ||
150 | scope.minNewElements = scope.minNewElements - 1 | ||
151 | } | ||
152 | if (scope.minNewElementsHeuristic > 0) { | ||
153 | scope.minNewElementsHeuristic = scope.minNewElementsHeuristic - 1 | ||
154 | } | ||
155 | if (scope.maxNewElements > 0) { | ||
156 | scope.maxNewElements = scope.maxNewElements - 1 | ||
157 | } else if (scope.maxNewElements === 0) { | ||
158 | setScopesInvalid() | ||
159 | } | ||
160 | } | ||
161 | } | ||