diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/NumericSolver.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/NumericSolver.xtend | 114 |
1 files changed, 114 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/NumericSolver.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/NumericSolver.xtend new file mode 100644 index 00000000..b72bdb44 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner/src/hu/bme/mit/inf/dslreasoner/viatrasolver/reasoner/dse/NumericSolver.xtend | |||
@@ -0,0 +1,114 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.reasoner.dse | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.viatra2logic.NumericProblemSolver | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.ModelGenerationMethod | ||
5 | import java.util.HashMap | ||
6 | import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine | ||
7 | import org.eclipse.viatra.query.runtime.api.IPatternMatch | ||
8 | import org.eclipse.viatra.query.runtime.api.ViatraQueryMatcher | ||
9 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint | ||
10 | import hu.bme.mit.inf.dslreasoner.viatra2logic.NumericTranslator | ||
11 | import org.eclipse.viatra.dse.base.ThreadContext | ||
12 | import org.eclipse.emf.ecore.EObject | ||
13 | import java.util.Map | ||
14 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
15 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PrimitiveElement | ||
16 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.BooleanElement | ||
17 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.IntegerElement | ||
18 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.StringElement | ||
19 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.RealElement | ||
20 | import java.util.List | ||
21 | import java.math.BigDecimal | ||
22 | |||
23 | class NumericSolver { | ||
24 | val ThreadContext threadContext; | ||
25 | val constraint2UnitPropagationPrecondition = new HashMap<PConstraint,ViatraQueryMatcher<? extends IPatternMatch>> | ||
26 | NumericTranslator t = new NumericTranslator | ||
27 | |||
28 | new(ThreadContext threadContext, ModelGenerationMethod method) { | ||
29 | this.threadContext = threadContext | ||
30 | val engine = threadContext.queryEngine | ||
31 | for(entry : method.unitPropagationPreconditions.entrySet) { | ||
32 | val constraint = entry.key | ||
33 | val querySpec = entry.value | ||
34 | val matcher = querySpec.getMatcher(engine); | ||
35 | constraint2UnitPropagationPrecondition.put(constraint,matcher) | ||
36 | } | ||
37 | } | ||
38 | |||
39 | def boolean isSatisfiable() { | ||
40 | if(constraint2UnitPropagationPrecondition.empty) return true | ||
41 | else { | ||
42 | val propagatedConstraints = new HashMap | ||
43 | for(entry : constraint2UnitPropagationPrecondition.entrySet) { | ||
44 | val constraint = entry.key | ||
45 | //println(constraint) | ||
46 | val allMatches = entry.value.allMatches.map[it.toArray] | ||
47 | //println(allMatches.toList) | ||
48 | propagatedConstraints.put(constraint,allMatches) | ||
49 | } | ||
50 | |||
51 | if(propagatedConstraints.values.forall[empty]) { | ||
52 | return true | ||
53 | } else { | ||
54 | val res = t.delegateIsSatisfiable(propagatedConstraints) | ||
55 | //println(res) | ||
56 | return res | ||
57 | } | ||
58 | } | ||
59 | } | ||
60 | |||
61 | def fillSolutionCopy(Map<EObject, EObject> trace) { | ||
62 | val model = threadContext.getModel as PartialInterpretation | ||
63 | val dataObjects = model.newElements.filter(PrimitiveElement).filter[!model.openWorldElements.contains(it)].toList | ||
64 | if(constraint2UnitPropagationPrecondition.empty) { | ||
65 | fillWithDefaultValues(dataObjects,trace) | ||
66 | } else { | ||
67 | val propagatedConstraints = new HashMap | ||
68 | for(entry : constraint2UnitPropagationPrecondition.entrySet) { | ||
69 | val constraint = entry.key | ||
70 | val allMatches = entry.value.allMatches.map[it.toArray] | ||
71 | propagatedConstraints.put(constraint,allMatches) | ||
72 | } | ||
73 | |||
74 | if(propagatedConstraints.values.forall[empty]) { | ||
75 | fillWithDefaultValues(dataObjects,trace) | ||
76 | } else { | ||
77 | val solution = t.delegateGetSolution(dataObjects,propagatedConstraints) | ||
78 | fillWithSolutions(dataObjects,solution,trace) | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | def protected fillWithDefaultValues(List<PrimitiveElement> elements, Map<EObject, EObject> trace) { | ||
84 | for(element : elements) { | ||
85 | if(element.valueSet==false) { | ||
86 | val value = getDefaultValue(element) | ||
87 | val target = trace.get(element) as PrimitiveElement | ||
88 | target.fillWithValue(value) | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | |||
93 | def protected dispatch getDefaultValue(BooleanElement e) {false} | ||
94 | def protected dispatch getDefaultValue(IntegerElement e) {0} | ||
95 | def protected dispatch getDefaultValue(RealElement e) {0.0} | ||
96 | def protected dispatch getDefaultValue(StringElement e) {""} | ||
97 | |||
98 | def protected fillWithSolutions(List<PrimitiveElement> elements, Map<PrimitiveElement, Integer> solution, Map<EObject, EObject> trace) { | ||
99 | for(element : elements) { | ||
100 | if(element.valueSet==false) { | ||
101 | if(solution.containsKey(element)) { | ||
102 | val value = solution.get(element) | ||
103 | val target = trace.get(element) as PrimitiveElement | ||
104 | target.fillWithValue(value) | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | |||
110 | def protected dispatch fillWithValue(BooleanElement e, Object value) {e.valueSet=true e.value=value as Boolean} | ||
111 | def protected dispatch fillWithValue(IntegerElement e, Object value) {e.valueSet=true e.value=value as Integer} | ||
112 | def protected dispatch fillWithValue(RealElement e, Object value) {e.valueSet=true e.value=BigDecimal.valueOf(value as Double) } | ||
113 | def protected dispatch fillWithValue(StringElement e, Object value) {e.valueSet=true e.value=value as String} | ||
114 | } \ No newline at end of file | ||