1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
|
package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse
import hu.bme.mit.inf.dslreasoner.viatra2logic.NumericTranslator
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BooleanElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.IntegerElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RealElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.StringElement
import java.math.BigDecimal
import java.util.HashMap
import java.util.LinkedHashMap
import java.util.LinkedHashSet
import java.util.List
import java.util.Map
import org.eclipse.emf.ecore.EObject
import org.eclipse.viatra.dse.base.ThreadContext
import org.eclipse.viatra.query.runtime.api.IPatternMatch
import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher
import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint
class NumericSolver {
val ModelGenerationMethod method
var ThreadContext threadContext
val constraint2MustUnitPropagationPrecondition = new HashMap<PConstraint,ViatraQueryMatcher<? extends IPatternMatch>>
val constraint2CurrentUnitPropagationPrecondition = new HashMap<PConstraint,ViatraQueryMatcher<? extends IPatternMatch>>
NumericTranslator t = new NumericTranslator
val boolean intermediateConsistencyCheck
val boolean caching;
Map<LinkedHashMap<PConstraint, Iterable<List<Integer>>>,Boolean> satisfiabilityCache = new HashMap
var long runtime = 0
var long cachingTime = 0
var int numberOfSolverCalls = 0
var int numberOfCachedSolverCalls = 0
new(ModelGenerationMethod method, boolean intermediateConsistencyCheck, boolean caching) {
this.method = method
this.intermediateConsistencyCheck = intermediateConsistencyCheck
this.caching = caching
}
def init(ThreadContext context) {
// This makes the NumericSolver single-threaded,
// but that's not a problem, because we only use the solver on a single thread anyways.
this.threadContext = context
val engine = threadContext.queryEngine
for(entry : method.mustUnitPropagationPreconditions.entrySet) {
val constraint = entry.key
val querySpec = entry.value
val matcher = querySpec.getMatcher(engine);
constraint2MustUnitPropagationPrecondition.put(constraint,matcher)
}
for(entry : method.currentUnitPropagationPreconditions.entrySet) {
val constraint = entry.key
val querySpec = entry.value
val matcher = querySpec.getMatcher(engine);
constraint2CurrentUnitPropagationPrecondition.put(constraint,matcher)
}
}
def getRuntime(){runtime}
def getCachingTime(){cachingTime}
def getNumberOfSolverCalls(){numberOfSolverCalls}
def getNumberOfCachedSolverCalls(){numberOfCachedSolverCalls}
def getSolverFormingProblem(){this.t.formingProblemTime}
def getSolverSolvingProblem(){this.t.solvingProblemTime}
def getSolverSolution(){this.t.formingSolutionTime}
def boolean maySatisfiable() {
if(intermediateConsistencyCheck) {
return isSatisfiable(this.constraint2MustUnitPropagationPrecondition)
} else {
return true
}
}
def boolean currentSatisfiable() {
isSatisfiable(this.constraint2CurrentUnitPropagationPrecondition)
}
private def boolean isSatisfiable(Map<PConstraint,ViatraQueryMatcher<? extends IPatternMatch>> matches) {
val start = System.nanoTime
var boolean finalResult
if(matches.empty){
finalResult=true
} else {
val propagatedConstraints = new HashMap
for(entry : matches.entrySet) {
val constraint = entry.key
//println(constraint)
val allMatches = entry.value.allMatches.map[it.toArray]
//println(allMatches.toList)
propagatedConstraints.put(constraint,allMatches)
}
if(propagatedConstraints.values.forall[empty]) {
finalResult=true
} else {
if(caching) {
val code = getCode(propagatedConstraints)
val cachedResult = satisfiabilityCache.get(code)
if(cachedResult === null) {
// println('''new problem, call solver''')
// for(entry : code.entrySet) {
// println('''«entry.key» -> «entry.value»''')
// }
//println(code.hashCode)
this.numberOfSolverCalls++
val res = t.delegateIsSatisfiable(propagatedConstraints)
satisfiabilityCache.put(code,res)
finalResult=res
} else {
//println('''similar problem, answer from cache''')
finalResult=cachedResult
this.numberOfCachedSolverCalls++
}
} else {
finalResult= t.delegateIsSatisfiable(propagatedConstraints)
this.numberOfSolverCalls++
}
}
}
this.runtime+=System.nanoTime-start
return finalResult
}
def getCode(HashMap<PConstraint, Iterable<Object[]>> propagatedConstraints) {
val start = System.nanoTime
val involvedObjects = new LinkedHashSet(propagatedConstraints.values.flatten.map[toList].flatten.toList).toList
val res = new LinkedHashMap(propagatedConstraints.mapValues[matches | matches.map[objects | objects.map[object | involvedObjects.indexOf(object)].toList]])
this.cachingTime += System.nanoTime-start
return res
}
def fillSolutionCopy(Map<EObject, EObject> trace) {
val model = threadContext.getModel as PartialInterpretation
val dataObjects = model.newElements.filter(PrimitiveElement).filter[!model.openWorldElements.contains(it)].toList
if(constraint2CurrentUnitPropagationPrecondition.empty) {
fillWithDefaultValues(dataObjects,trace)
} else {
val propagatedConstraints = new HashMap
for(entry : constraint2CurrentUnitPropagationPrecondition.entrySet) {
val constraint = entry.key
val allMatches = entry.value.allMatches.map[it.toArray]
propagatedConstraints.put(constraint,allMatches)
}
if(propagatedConstraints.values.forall[empty]) {
fillWithDefaultValues(dataObjects,trace)
} else {
val solution = t.delegateGetSolution(dataObjects,propagatedConstraints)
fillWithSolutions(dataObjects,solution,trace)
}
}
}
def protected fillWithDefaultValues(List<PrimitiveElement> elements, Map<EObject, EObject> trace) {
for(element : elements) {
if(element.valueSet==false) {
val value = getDefaultValue(element)
val target = trace.get(element) as PrimitiveElement
target.fillWithValue(value)
}
}
}
def protected dispatch getDefaultValue(BooleanElement e) {false}
def protected dispatch getDefaultValue(IntegerElement e) {0}
def protected dispatch getDefaultValue(RealElement e) {0.0}
def protected dispatch getDefaultValue(StringElement e) {""}
def protected fillWithSolutions(List<PrimitiveElement> elements, Map<PrimitiveElement, Number> solution, Map<EObject, EObject> trace) {
for(element : elements) {
if(element.valueSet==false) {
if(solution.containsKey(element)) {
val value = solution.get(element)
val target = trace.get(element) as PrimitiveElement
target.fillWithValue(value)
} else {
val target = trace.get(element) as PrimitiveElement
target.fillWithValue(target.defaultValue)
}
}
}
}
def protected dispatch fillWithValue(BooleanElement e, Object value) {e.valueSet=true e.value=value as Boolean}
def protected dispatch fillWithValue(IntegerElement e, Object value) {e.valueSet=true e.value=value as Integer}
def protected dispatch fillWithValue(RealElement e, Object value) {e.valueSet=true e.value=BigDecimal.valueOf(value as Double) }
def protected dispatch fillWithValue(StringElement e, Object value) {e.valueSet=true e.value=value as String}
}
|