diff options
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend')
-rw-r--r-- | Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend | 217 |
1 files changed, 217 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend new file mode 100644 index 00000000..414af4c8 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/Constraint2Logic.xtend | |||
@@ -0,0 +1,217 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatra2logic | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic | ||
4 | import hu.bme.mit.inf.dslreasoner.ecore2logic.Ecore2Logic_Trace | ||
5 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | ||
6 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.TracedOutput | ||
7 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term | ||
8 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Variable | ||
9 | import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem | ||
10 | import java.util.LinkedList | ||
11 | import java.util.Map | ||
12 | import org.eclipse.emf.common.util.Enumerator | ||
13 | import org.eclipse.emf.ecore.EAttribute | ||
14 | import org.eclipse.emf.ecore.EClass | ||
15 | import org.eclipse.emf.ecore.EReference | ||
16 | import org.eclipse.emf.ecore.EStructuralFeature | ||
17 | import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey | ||
18 | import org.eclipse.viatra.query.runtime.emf.types.EDataTypeInSlotsKey | ||
19 | import org.eclipse.viatra.query.runtime.emf.types.EStructuralFeatureInstancesKey | ||
20 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint | ||
21 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable | ||
22 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality | ||
23 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter | ||
24 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Inequality | ||
25 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.NegativePatternCall | ||
26 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure | ||
27 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.ConstantValue | ||
28 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall | ||
29 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint | ||
30 | |||
31 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | ||
32 | |||
33 | class Constraint2Logic { | ||
34 | val extension LogicProblemBuilder builder = new LogicProblemBuilder | ||
35 | val Ecore2Logic ecore2Logic | ||
36 | |||
37 | new(Ecore2Logic ecore2Logic) { | ||
38 | this.ecore2Logic = ecore2Logic | ||
39 | } | ||
40 | |||
41 | def dispatch Term transformConstraint(Equality constraint, | ||
42 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
43 | Viatra2LogicTrace viatra2LogicTrace, | ||
44 | Map<PVariable, Variable> variable2Variable, | ||
45 | Viatra2LogicConfiguration config) | ||
46 | { | ||
47 | constraint.who.lookup(variable2Variable) | ||
48 | == | ||
49 | constraint.withWhom.lookup(variable2Variable) | ||
50 | } | ||
51 | |||
52 | def dispatch Term transformConstraint(ExportedParameter constraint, | ||
53 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
54 | Viatra2LogicTrace viatra2LogicTrace, | ||
55 | Map<PVariable, Variable> variable2Variable, | ||
56 | Viatra2LogicConfiguration config) | ||
57 | { | ||
58 | return null | ||
59 | } | ||
60 | |||
61 | def dispatch Term transformConstraint(Inequality constraint, | ||
62 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
63 | Viatra2LogicTrace viatra2LogicTrace, | ||
64 | Map<PVariable, Variable> variable2Variable, | ||
65 | Viatra2LogicConfiguration config) | ||
66 | { | ||
67 | constraint.who.lookup(variable2Variable) | ||
68 | != | ||
69 | constraint.withWhom.lookup(variable2Variable) | ||
70 | } | ||
71 | |||
72 | def dispatch Term transformConstraint(NegativePatternCall constraint, | ||
73 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
74 | Viatra2LogicTrace viatra2LogicTrace, | ||
75 | Map<PVariable, Variable> variable2Variable, | ||
76 | Viatra2LogicConfiguration config) | ||
77 | { | ||
78 | val parameterSubstitution = new LinkedList | ||
79 | for(index : 0..<constraint.actualParametersTuple.size) { | ||
80 | val variable = constraint.actualParametersTuple.get(index) as PVariable | ||
81 | parameterSubstitution += variable.lookup(variable2Variable).toTerm | ||
82 | } | ||
83 | |||
84 | val res = constraint.referredQuery.lookup(viatra2LogicTrace.query2Relation).call(parameterSubstitution) | ||
85 | return res.Not | ||
86 | } | ||
87 | |||
88 | def dispatch Term transformConstraint(BinaryTransitiveClosure constraint, | ||
89 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
90 | Viatra2LogicTrace viatra2LogicTrace, | ||
91 | Map<PVariable, Variable> variable2Variable, | ||
92 | Viatra2LogicConfiguration config) | ||
93 | { | ||
94 | throw new UnsupportedOperationException | ||
95 | } | ||
96 | |||
97 | def dispatch Term transformConstraint(ConstantValue constant, | ||
98 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
99 | Viatra2LogicTrace viatra2LogicTrace, | ||
100 | Map<PVariable, Variable> variable2Variable, | ||
101 | Viatra2LogicConfiguration config) | ||
102 | { | ||
103 | val tuple = constant.variablesTuple | ||
104 | if(tuple.size == 1) { | ||
105 | val variable = tuple.get(0) as PVariable | ||
106 | //println(variable.name + " == " + constant.supplierKey + "["+constant.supplierKey.class.name+"]") | ||
107 | val translatedConstant = transformConstantValue(constant.supplierKey,ecore2LogicTrace,viatra2LogicTrace,config); | ||
108 | return variable.lookup(variable2Variable) == translatedConstant | ||
109 | } else throw new AssertionError | ||
110 | } | ||
111 | |||
112 | private def dispatch transformConstantValue( | ||
113 | Enumerator enumerator, | ||
114 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
115 | Viatra2LogicTrace viatra2LogicTrace, | ||
116 | Viatra2LogicConfiguration config) | ||
117 | { | ||
118 | ecore2Logic.Literal(ecore2LogicTrace.trace,enumerator) | ||
119 | } | ||
120 | private def dispatch transformConstantValue( | ||
121 | Integer value, | ||
122 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
123 | Viatra2LogicTrace viatra2LogicTrace, | ||
124 | Viatra2LogicConfiguration config) | ||
125 | { | ||
126 | return value.asTerm | ||
127 | } | ||
128 | private def dispatch transformConstantValue(Object other, | ||
129 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
130 | Viatra2LogicTrace viatra2LogicTrace, | ||
131 | Viatra2LogicConfiguration config) | ||
132 | { | ||
133 | throw new UnsupportedOperationException('''Unknown constant «other»:«other.class.name»''') | ||
134 | } | ||
135 | |||
136 | def dispatch Term transformConstraint(PositivePatternCall constraint, | ||
137 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
138 | Viatra2LogicTrace viatra2LogicTrace, | ||
139 | Map<PVariable, Variable> variable2Variable, | ||
140 | Viatra2LogicConfiguration config) | ||
141 | { | ||
142 | |||
143 | val parameterSubstitution = new LinkedList | ||
144 | for(index : 0..<constraint.variablesTuple.size) { | ||
145 | val variable = constraint.variablesTuple.get(index) as PVariable | ||
146 | parameterSubstitution += variable.lookup(variable2Variable).toTerm | ||
147 | } | ||
148 | val res = constraint.referredQuery.lookup(viatra2LogicTrace.query2Relation).call(parameterSubstitution) | ||
149 | return res | ||
150 | } | ||
151 | |||
152 | def dispatch Term transformConstraint(TypeConstraint constraint, | ||
153 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
154 | Viatra2LogicTrace viatra2LogicTrace, | ||
155 | Map<PVariable, Variable> variable2Variable, | ||
156 | Viatra2LogicConfiguration config) | ||
157 | { | ||
158 | val tuple = constraint.variablesTuple | ||
159 | if(tuple.size == 1) { | ||
160 | val typeConstraint = constraint.equivalentJudgement.inputKey | ||
161 | if(typeConstraint instanceof EClassTransitiveInstancesKey) { | ||
162 | val type = typeConstraint.emfKey | ||
163 | val variable = tuple.get(0) as PVariable | ||
164 | return transformTypeConstraint(type,variable,ecore2LogicTrace,variable2Variable,viatra2LogicTrace) | ||
165 | } else if(typeConstraint instanceof EDataTypeInSlotsKey) { | ||
166 | // If the type is a primitive type or EEnum, then instanceof is an unnecessary constraint | ||
167 | return null | ||
168 | } | ||
169 | } else if(tuple.size == 2) { | ||
170 | val type = (constraint.equivalentJudgement.inputKey as EStructuralFeatureInstancesKey).emfKey | ||
171 | val src = tuple.get(0) as PVariable | ||
172 | val trg = tuple.get(1) as PVariable | ||
173 | return transformPathConstraint(type,src,trg,ecore2LogicTrace,variable2Variable,viatra2LogicTrace) | ||
174 | } else throw new IllegalArgumentException('''unknown tuple: «tuple»''') | ||
175 | } | ||
176 | def Term transformTypeConstraint( | ||
177 | EClass type, | ||
178 | PVariable variable, | ||
179 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
180 | Map<PVariable, Variable> variable2Variable, | ||
181 | Viatra2LogicTrace viatra2LogicTrace) | ||
182 | { | ||
183 | InstanceOf( | ||
184 | variable.lookup(variable2Variable), | ||
185 | ecore2Logic.TypeofEClass(ecore2LogicTrace.trace,type)) | ||
186 | } | ||
187 | def Term transformPathConstraint( | ||
188 | EStructuralFeature feature, | ||
189 | PVariable src, PVariable trg, | ||
190 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
191 | Map<PVariable, Variable> variable2Variable, | ||
192 | Viatra2LogicTrace viatra2LogicTrace) | ||
193 | { | ||
194 | if(feature instanceof EReference) { | ||
195 | return ecore2Logic.IsInReference(ecore2LogicTrace.trace, | ||
196 | src.lookup(variable2Variable), | ||
197 | trg.lookup(variable2Variable), | ||
198 | feature) | ||
199 | } else if(feature instanceof EAttribute) { | ||
200 | return ecore2Logic.IsAttributeValue(ecore2LogicTrace.trace, | ||
201 | src.lookup(variable2Variable), | ||
202 | trg.lookup(variable2Variable), | ||
203 | feature) | ||
204 | } else { | ||
205 | throw new IllegalArgumentException('''Unsupported path expression: «feature.class.name»''') | ||
206 | } | ||
207 | } | ||
208 | |||
209 | def dispatch Term transformConstraint(PConstraint constraint, | ||
210 | TracedOutput<LogicProblem, Ecore2Logic_Trace> ecore2LogicTrace, | ||
211 | Viatra2LogicTrace viatra2LogicTrace, | ||
212 | Map<PVariable, Variable> variable2Variable, | ||
213 | Viatra2LogicConfiguration config) | ||
214 | { | ||
215 | throw new UnsupportedOperationException('''Unkown constraint type: «constraint.class.name»''') | ||
216 | } | ||
217 | } \ No newline at end of file | ||