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
|
package hu.bme.mit.inf.dslreasoner.smt.reasoner
import hu.bme.mit.inf.dslreasoner.smtLanguage.SMTSortedVariable
import hu.bme.mit.inf.dslreasoner.smtLanguage.SMTSymbolicValue
import hu.bme.mit.inf.dslreasoner.smtLanguage.SMTTerm
import hu.bme.mit.inf.dslreasoner.smtLanguage.SmtLanguageFactory
import java.util.ArrayList
import java.util.List
import java.util.Map
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.util.EcoreUtil
import org.eclipse.xtend.lib.annotations.Data
import java.util.HashMap
import java.util.LinkedList
abstract class TransformedSubterm {
val static extension SmtLanguageFactory factory = SmtLanguageFactory.eINSTANCE
def LogicSubterm expectLogic()
def NumericSubterm expectNumber()
def List<SubtermOption> getPossibleValues()
public static def List<List<SubtermOption>> allCombinations(List<SubtermOption>... possibleValues) {
return allCombinations(possibleValues.toList)
}
public static def List<List<SubtermOption>> allCombinations(Iterable<List<SubtermOption>> possibleValues) {
if(possibleValues.size == 1) {
return possibleValues.head.map[x|#[x.copy]]
} else {
val head = possibleValues.head
val tails = possibleValues.tail.allCombinations
val res = new ArrayList(tails.size * head.size)
for(headElement : head) {
for(tail : tails) {
val combination = new ArrayList(tail.size+1)
combination.add(headElement)
combination.addAll(tail.map[copy])
res.add(combination)
}
}
return res
}
}
public static def resolveToLogic(List<SubtermOption> subterms, SMTTerm value) {
val allDefinitions = subterms.map[it.variables].flatten
val allPreconditions = subterms.map[it.preconditions].flatten
val preconditionedExpression = if(allPreconditions.empty) {
value
} else {
createSMTAnd => [it.operands += allPreconditions it.operands += value]
}
val quantifiedExpression = if(allDefinitions.empty) {
preconditionedExpression
} else {
createSMTForall => [
it.quantifiedVariables += allDefinitions
it.expression = preconditionedExpression]
}
return quantifiedExpression
}
}
@Data class SubtermOption {
List<SMTSortedVariable> variables
List<SMTTerm> preconditions
SMTTerm expression
TypeDescriptor type
public new(
List<SMTSortedVariable> variables,
List<SMTTerm> preconditions,
SMTTerm expression,
TypeDescriptor type)
{
this.variables = variables
this.preconditions = preconditions
this.expression = expression
this.type = type
}
public new(
List<SubtermOption> previousSubterms,
List<SMTSortedVariable> newVariables,
List<SMTTerm> newPreconditions,
SMTTerm newExpression,
TypeDescriptor newType)
{
val old2New = new HashMap
variables = new LinkedList
for(variable : previousSubterms.map[getVariables].flatten) {
val newVariable = EcoreUtil.copy(variable)
old2New.put(variable,newVariable)
variables+=newVariable
}
preconditions = new LinkedList
preconditions += previousSubterms.map[getPreconditions].flatten.map[x|EcoreUtil.copy(x)]
preconditions += newPreconditions.map[x|EcoreUtil.copy(x)]
preconditions.forEach[relinkAllVariableReference(old2New)]
expression = EcoreUtil.copy(newExpression)
relinkAllVariableReference(expression,old2New)
type = newType
}
public def copy() {
val old2New = variables.toInvertedMap[EcoreUtil.copy(it)]
val newPreconditions = preconditions.map[EcoreUtil.copy(it)]
newPreconditions.forEach[it.relinkAllVariableReference(old2New)]
val newExpression = EcoreUtil.copy(expression)
return new SubtermOption(variables.map[old2New.get(it)],newPreconditions,newExpression,type)
}
def private relinkAllVariableReference(EObject root,Map<SMTSortedVariable, SMTSortedVariable> map) {
if(root instanceof SMTSymbolicValue) root.relinkVariableReference(map)
root.eAllContents().filter(SMTSymbolicValue).forEach[relinkVariableReference(map)]
}
def private relinkVariableReference(SMTSymbolicValue target,Map<SMTSortedVariable, SMTSortedVariable> map) {
if(map.containsKey(target.symbolicReference)) {
target.symbolicReference = map.get(target.symbolicReference)
}
}
}
@Data
class LogicSubterm extends TransformedSubterm{
SMTTerm value
override expectLogic() {return this}
override expectNumber() { throw new AssertionError("A term is not a number") }
public new(SMTTerm expression)
{
this.value = expression
}
override getPossibleValues() {
#[new SubtermOption(#[],#[],value,TypeDescriptor::logicTypeDescriptor_Instance)]
}
}
@Data
class NumericSubterm extends TransformedSubterm {
List<SubtermOption> values
public new(
List<SMTSortedVariable> variables,
List<SMTTerm> preconditions,
SMTTerm expression) {
this.values = #[new SubtermOption(variables,preconditions,expression,TypeDescriptor.numericTypeDescriptor_Instance)]
}
public new(List<SubtermOption> values) {
this.values = values
}
override expectLogic() { throw new AssertionError("A term is not a logic value") }
override expectNumber() { return this }
override getPossibleValues() {
values
}
}
@Data
class ComplexSubterm extends TransformedSubterm {
List<SubtermOption> options
override expectLogic() { throw new AssertionError("A term is not a logic value") }
override expectNumber() { throw new AssertionError("A term is not a number") }
public new(List<SMTSortedVariable> variables,
List<SMTTerm> preconditions,
SMTTerm expression,
TypeDescriptor type)
{
this.options = #[new SubtermOption(variables,preconditions,expression,type)]
}
public new(List<SubtermOption> subterms) {
this.options = subterms
}
override getPossibleValues() {
this.options
}
}
|