diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PConstraintTransformer.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PConstraintTransformer.xtend | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PConstraintTransformer.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PConstraintTransformer.xtend new file mode 100644 index 00000000..64fbb2f1 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PConstraintTransformer.xtend | |||
@@ -0,0 +1,192 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.viatra2logic.XExpressionExtractor | ||
4 | import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.Modality | ||
5 | import org.eclipse.emf.ecore.EAttribute | ||
6 | import org.eclipse.emf.ecore.EEnumLiteral | ||
7 | import org.eclipse.emf.ecore.EReference | ||
8 | import org.eclipse.viatra.query.runtime.emf.types.EClassTransitiveInstancesKey | ||
9 | import org.eclipse.viatra.query.runtime.emf.types.EDataTypeInSlotsKey | ||
10 | import org.eclipse.viatra.query.runtime.emf.types.EStructuralFeatureInstancesKey | ||
11 | import org.eclipse.viatra.query.runtime.matchers.psystem.PConstraint | ||
12 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable | ||
13 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Equality | ||
14 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExportedParameter | ||
15 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.ExpressionEvaluation | ||
16 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.Inequality | ||
17 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.NegativePatternCall | ||
18 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicdeferred.TypeFilterConstraint | ||
19 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.BinaryTransitiveClosure | ||
20 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.ConstantValue | ||
21 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.PositivePatternCall | ||
22 | import org.eclipse.viatra.query.runtime.matchers.psystem.basicenumerables.TypeConstraint | ||
23 | |||
24 | class PConstraintTransformer { | ||
25 | val extension RelationDefinitionIndexer relationDefinitionIndexer; | ||
26 | val expressionExtractor = new XExpressionExtractor | ||
27 | |||
28 | new(RelationDefinitionIndexer relationDefinitionIndexer) { | ||
29 | this.relationDefinitionIndexer = relationDefinitionIndexer | ||
30 | } | ||
31 | |||
32 | dispatch def transformConstraint(TypeConstraint constraint, Modality modality) { | ||
33 | val touple = constraint.variablesTuple | ||
34 | if(touple.size == 1) { | ||
35 | val inputKey = constraint.equivalentJudgement.inputKey | ||
36 | if(inputKey instanceof EClassTransitiveInstancesKey) { | ||
37 | return relationDefinitionIndexer.base.typeIndexer.referInstanceOf(inputKey.emfKey,modality.toMustMay, | ||
38 | constraint.getVariableInTuple(0).canonizeName) | ||
39 | } else if(inputKey instanceof EDataTypeInSlotsKey){ | ||
40 | return '''// type constraint is enforced by construction''' | ||
41 | } | ||
42 | |||
43 | } else if(touple.size == 2){ | ||
44 | val key = (constraint.equivalentJudgement.inputKey as EStructuralFeatureInstancesKey).emfKey | ||
45 | if(key instanceof EReference) { | ||
46 | return base.referRelationByName( | ||
47 | key, | ||
48 | constraint.getVariableInTuple(0).canonizeName, | ||
49 | constraint.getVariableInTuple(1).canonizeName, | ||
50 | modality.toMustMay) | ||
51 | } else if (key instanceof EAttribute) { | ||
52 | return base.referAttributeByName(key, | ||
53 | constraint.getVariableInTuple(0).canonizeName, | ||
54 | constraint.getVariableInTuple(1).canonizeName, | ||
55 | modality.toMustMay) | ||
56 | } else throw new UnsupportedOperationException('''unknown key: «key.class»''') | ||
57 | } else { | ||
58 | throw new UnsupportedOperationException('''Unsupported touple size: «touple.size»''') | ||
59 | } | ||
60 | } | ||
61 | dispatch def transformConstraint(TypeFilterConstraint constraint, Modality modality) { | ||
62 | val touple = constraint.variablesTuple | ||
63 | if(touple.size == 1) { | ||
64 | val inputKey = constraint.equivalentJudgement.inputKey | ||
65 | if(inputKey instanceof EClassTransitiveInstancesKey) { | ||
66 | return base.typeIndexer.referInstanceOf(inputKey.emfKey,modality.toMustMay, | ||
67 | (constraint.getVariablesTuple.get(0) as PVariable).canonizeName) | ||
68 | } else if(inputKey instanceof EDataTypeInSlotsKey){ | ||
69 | return '''// type constraint is enforced by construction''' | ||
70 | } | ||
71 | |||
72 | } else if(touple.size == 2){ | ||
73 | val key = (constraint.equivalentJudgement.inputKey as EStructuralFeatureInstancesKey).emfKey | ||
74 | if(key instanceof EReference) { | ||
75 | return base.referRelationByName( | ||
76 | key, | ||
77 | (constraint.getVariablesTuple.get(0) as PVariable).canonizeName, | ||
78 | (constraint.getVariablesTuple.get(1) as PVariable).canonizeName, | ||
79 | modality.toMustMay) | ||
80 | } else if (key instanceof EAttribute) { | ||
81 | return base.referAttributeByName(key, | ||
82 | (constraint.getVariablesTuple.get(0) as PVariable).canonizeName, | ||
83 | (constraint.getVariablesTuple.get(1) as PVariable).canonizeName, | ||
84 | modality.toMustMay) | ||
85 | } else throw new UnsupportedOperationException('''unknown key: «key.class»''') | ||
86 | } else { | ||
87 | throw new UnsupportedOperationException('''Unsupported touple size: «touple.size»''') | ||
88 | } | ||
89 | } | ||
90 | |||
91 | dispatch def transformConstraint(Equality equality, Modality modality) { | ||
92 | val a = equality.who | ||
93 | val b = equality.withWhom | ||
94 | transformEquality(modality.toMustMay, a, b) | ||
95 | } | ||
96 | |||
97 | private def CharSequence transformEquality(Modality modality, PVariable a, PVariable b) { | ||
98 | if(modality.isMustOrCurrent) '''find mustEquivalent(problem, interpretation, «a.canonizeName», «b.canonizeName»);''' | ||
99 | else '''find mayEquivalent(problem, interpretation, «a.canonizeName», «b.canonizeName»);''' | ||
100 | } | ||
101 | |||
102 | dispatch def transformConstraint(Inequality inequality, Modality modality) { | ||
103 | val a = inequality.who | ||
104 | val b = inequality.withWhom | ||
105 | if(modality.isCurrent) { | ||
106 | return '''neg find mustEquivalent(problem, interpretation, «a.canonizeName», «b.canonizeName»);''' | ||
107 | } else if(modality.isMust) { | ||
108 | return '''neg find mayEquivalent(problem, interpretation, «a.canonizeName», «b.canonizeName»);''' | ||
109 | } else { // modality.isMay | ||
110 | return '''neg find mustEquivalent(problem, interpretation, «a.canonizeName», «b.canonizeName»);''' | ||
111 | } | ||
112 | } | ||
113 | |||
114 | dispatch def transformConstraint(NegativePatternCall pcall, Modality modality) { | ||
115 | val params = (0..<pcall.actualParametersTuple.size).map[index | | ||
116 | val variable = pcall.actualParametersTuple.get(index) as PVariable | ||
117 | return variable.canonizeName | ||
118 | ] | ||
119 | return referPattern(pcall.referredQuery,params,modality.dual,false,false) | ||
120 | } | ||
121 | |||
122 | dispatch def transformConstraint(PositivePatternCall pcall, Modality modality) { | ||
123 | val params = (0..<pcall.variablesTuple.size).map[index | | ||
124 | val variable = pcall.variablesTuple.get(index) as PVariable | ||
125 | return variable.canonizeName | ||
126 | ] | ||
127 | return referPattern(pcall.referredQuery,params,modality,true,false) | ||
128 | } | ||
129 | dispatch def transformConstraint(BinaryTransitiveClosure pcall, Modality modality) { | ||
130 | val params = (0..1).map[index | | ||
131 | val variable = pcall.getVariableInTuple(index) as PVariable | ||
132 | return variable.canonizeName | ||
133 | ] | ||
134 | return referPattern(pcall.referredQuery,params,modality,true,true) | ||
135 | } | ||
136 | dispatch def transformConstraint(ExportedParameter e, Modality modality) { | ||
137 | return '''// «e.parameterName» is exported''' | ||
138 | } | ||
139 | dispatch def transformConstraint(ConstantValue c, Modality modality) { | ||
140 | val target = c.supplierKey | ||
141 | |||
142 | var String targetString; | ||
143 | var String additionalDefinition; | ||
144 | if(target instanceof EEnumLiteral) { | ||
145 | targetString = '''const_«target.name»_«target.EEnum.name»''' | ||
146 | additionalDefinition = '''DefinedElement.name(«targetString»,"«target.name» «target.EEnum.name»"); //LogicProblem.elements(problem,«targetString»);''' | ||
147 | } else if(target instanceof Integer) { | ||
148 | targetString = '''const_«target»_Integer''' | ||
149 | additionalDefinition = '''IntegerElement.value(«targetString»,«target»);''' | ||
150 | } else if(target instanceof Boolean) { | ||
151 | targetString = '''const_«target»_Boolean''' | ||
152 | additionalDefinition = '''BooleanElement.value(«targetString»,«target»);''' | ||
153 | } else if(target instanceof String) { | ||
154 | targetString = '''const_«target»_String''' | ||
155 | additionalDefinition = '''StringElement.value(«targetString»,"«target»");''' | ||
156 | } else if(target instanceof Double) { | ||
157 | targetString = '''const_«target»_Number''' | ||
158 | additionalDefinition = '''RealElement.value(«targetString»,"«target»");''' | ||
159 | } else if(target instanceof Float) { | ||
160 | targetString = '''const_«target»_Number''' | ||
161 | additionalDefinition = '''RealElement.value(«targetString»,"«target»");''' | ||
162 | } else { | ||
163 | throw new UnsupportedOperationException('''Unknown constant type: «target.class»''') | ||
164 | } | ||
165 | |||
166 | val source = c.variablesTuple | ||
167 | var String sourceName | ||
168 | if(source.size == 1) | ||
169 | sourceName = (source.get(0) as PVariable).canonizeName | ||
170 | else throw new UnsupportedOperationException("unknown source") | ||
171 | return '''«sourceName» == «targetString»;«additionalDefinition»'''; | ||
172 | } | ||
173 | |||
174 | |||
175 | |||
176 | dispatch def transformConstrait(ExpressionEvaluation e, Modality modality) { | ||
177 | if(e.outputVariable!==null) { | ||
178 | throw new UnsupportedOperationException('''Only check expressions are supported "«e.class.name»"!''') | ||
179 | } else { | ||
180 | val expression = expressionExtractor.extractExpression(e.evaluator) | ||
181 | if(modality.isMust) { | ||
182 | return '''''' | ||
183 | } else if(modality.isMay) { | ||
184 | return '''''' | ||
185 | } | ||
186 | } | ||
187 | } | ||
188 | |||
189 | dispatch def transformConstraint(PConstraint c, Modality modality) { | ||
190 | throw new UnsupportedOperationException('''Unknown constraint type: "«c.class.name»"!''') | ||
191 | } | ||
192 | } \ No newline at end of file | ||