diff options
Diffstat (limited to 'Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend')
-rw-r--r-- | Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend new file mode 100644 index 00000000..f474ded4 --- /dev/null +++ b/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/ExpressionEvaluation2Logic.xtend | |||
@@ -0,0 +1,132 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatra2logic | ||
2 | |||
3 | import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicProblemBuilder | ||
4 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Term | ||
5 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Variable | ||
6 | import java.util.Map | ||
7 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable | ||
8 | import org.eclipse.xtext.xbase.XBinaryOperation | ||
9 | import org.eclipse.xtext.xbase.XExpression | ||
10 | import org.eclipse.xtext.xbase.XFeatureCall | ||
11 | import org.eclipse.xtext.xbase.XMemberFeatureCall | ||
12 | import org.eclipse.xtext.xbase.XNumberLiteral | ||
13 | import org.eclipse.xtext.xbase.XUnaryOperation | ||
14 | |||
15 | import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.* | ||
16 | |||
17 | class ExpressionEvaluation2Logic { | ||
18 | val extension LogicProblemBuilder builder = new LogicProblemBuilder | ||
19 | |||
20 | def Term transformCheck(XExpression expression, Map<PVariable, Variable> variable2Variable) { | ||
21 | return expression.transform(variable2Variable) | ||
22 | } | ||
23 | def Term transformEval(PVariable target, XExpression expression, Map<PVariable, Variable> variable2Variable) { | ||
24 | return target.lookup(variable2Variable) == expression.transform(variable2Variable) | ||
25 | } | ||
26 | |||
27 | static val N_Base = "org.eclipse.xtext.xbase.lib." | ||
28 | |||
29 | static val N_PLUS1 = "operator_plus" | ||
30 | static val N_MINUS1 = "operator_minus" | ||
31 | |||
32 | static val N_MINUS2 = "operator_minus" | ||
33 | static val N_PLUS2 = "operator_plus" | ||
34 | static val N_POWER = "operator_power" | ||
35 | static val N_MULTIPLY = "operator_multiply" | ||
36 | static val N_DIVIDE = "operator_divide" | ||
37 | static val N_MODULO = "operator_modulo" | ||
38 | static val N_LESSTHAN = "operator_lessThan" | ||
39 | static val N_LESSEQUALSTHAN = "operator_lessEqualsThan" | ||
40 | static val N_GREATERTHAN = "operator_greaterThan" | ||
41 | static val N_GREATEREQUALTHAN = "operator_greaterEqualsThan" | ||
42 | static val N_EQUALS = "operator_equals" | ||
43 | static val N_NOTEQUALS = "operator_notEquals" | ||
44 | static val N_EQUALS3 = "operator_tripleEquals" | ||
45 | static val N_NOTEQUALS3 = "operator_tripleNotEquals" | ||
46 | |||
47 | |||
48 | |||
49 | protected def isN(String name, String s) { | ||
50 | val res = name.startsWith(N_Base) && name.endsWith(s) | ||
51 | //println('''[«res»] «name» ?= «N_Base»*«s»''') | ||
52 | return res | ||
53 | } | ||
54 | |||
55 | static val N_POWER2 = "java.lang.Math.pow" | ||
56 | |||
57 | def protected dispatch Term transform(XBinaryOperation e, Map<PVariable, Variable> variable2Variable) { | ||
58 | val left = e.leftOperand.transform(variable2Variable) | ||
59 | val right = e.rightOperand.transform(variable2Variable) | ||
60 | val feature = e.feature.qualifiedName | ||
61 | if(feature.isN(N_MINUS2)) { return Minus(left,right) } | ||
62 | else if(feature.isN(N_PLUS2)) { return Plus(left,right) } | ||
63 | else if(feature.isN(N_POWER)) { return Pow(left,right) } | ||
64 | else if(feature.isN(N_MULTIPLY)) { return Multiply(left,right) } | ||
65 | else if(feature.isN(N_DIVIDE)) { return Divide(left,right) } | ||
66 | else if(feature.isN(N_MODULO)) { return Modulo(left,right) } | ||
67 | else if(feature.isN(N_LESSTHAN)) { return LessThan(left,right) } | ||
68 | else if(feature.isN(N_LESSEQUALSTHAN)) { return LessOrEqual(left,right) } | ||
69 | else if(feature.isN(N_GREATERTHAN)) { return MoreThan(left,right) } | ||
70 | else if(feature.isN(N_GREATEREQUALTHAN)) { return MoreOrEqual(left,right) } | ||
71 | else if(feature.isN(N_EQUALS)) { return Equals(left,right) } | ||
72 | else if(feature.isN(N_NOTEQUALS)) { return Distinct(left,right) } | ||
73 | else if(feature.isN(N_EQUALS3)) { return Equals(left,right) } | ||
74 | else if(feature.isN(N_NOTEQUALS3)) { return Distinct(left,right) } | ||
75 | else { | ||
76 | println("-> " + e.feature+","+e.class) | ||
77 | println("-> " + e.leftOperand) | ||
78 | println("-> " + e.rightOperand) | ||
79 | println("-> " + e.feature.qualifiedName) | ||
80 | throw new UnsupportedOperationException('''Unsupported binary operator feature: "«e.feature.class.simpleName»" - «e»''') | ||
81 | } | ||
82 | } | ||
83 | |||
84 | def protected dispatch Term transform(XUnaryOperation e, Map<PVariable, Variable> variable2Variable) { | ||
85 | val operand = e.operand.transform(variable2Variable) | ||
86 | val feature = e.feature.qualifiedName | ||
87 | if(feature.isN(N_MINUS1)) { return Minus(0.asTerm,operand)} | ||
88 | else if(feature.isN(N_PLUS1)) { return operand} | ||
89 | else{ | ||
90 | println("-> " + e.feature+","+e.class) | ||
91 | println("-> " + e.operand) | ||
92 | println("-> " + e.feature.qualifiedName) | ||
93 | throw new UnsupportedOperationException('''Unsupported unary operator feature: "«e.feature.class.simpleName»" - «e»''') | ||
94 | } | ||
95 | } | ||
96 | |||
97 | def protected dispatch Term transform(XMemberFeatureCall e, Map<PVariable, Variable> variable2Variable) { | ||
98 | val transformedArguments = e.actualArguments.map[transform(variable2Variable)] | ||
99 | val feature = e.feature.qualifiedName | ||
100 | if(feature == N_POWER2) { | ||
101 | return Pow(transformedArguments.get(0),transformedArguments.get(1)) | ||
102 | }else { | ||
103 | println(e.feature+","+e.class) | ||
104 | println(e.actualArguments.join(", ")) | ||
105 | println(e.feature.qualifiedName) | ||
106 | throw new UnsupportedOperationException('''Unsupported feature call: "«e.feature.qualifiedName»" - «e»''') | ||
107 | } | ||
108 | } | ||
109 | |||
110 | def protected dispatch Term transform(XFeatureCall e, Map<PVariable,Variable> variable2Variable) { | ||
111 | val featureName = e.feature.qualifiedName | ||
112 | val entryWithName = variable2Variable.entrySet.filter[it.key.name == featureName].head | ||
113 | if(entryWithName !== null) { | ||
114 | return entryWithName.value.toTerm | ||
115 | } else { | ||
116 | throw new IllegalArgumentException('''Feature call reference to unavailable variable "«featureName»"''') | ||
117 | } | ||
118 | } | ||
119 | |||
120 | def protected dispatch Term transform(XNumberLiteral l, Map<PVariable, Variable> variable2Variable) { | ||
121 | val s = l.value | ||
122 | try{ return Integer.parseInt(s).asTerm } catch(NumberFormatException e){} | ||
123 | try{ return Short.parseShort(s).asTerm } catch(NumberFormatException e){} | ||
124 | try{ return Double.parseDouble(s).asTerm } catch(NumberFormatException e){} | ||
125 | try{ return Float.parseFloat(s).asTerm } catch(NumberFormatException e){} | ||
126 | throw new UnsupportedOperationException('''Unsupported numeric type: "«s»"''') | ||
127 | } | ||
128 | |||
129 | def protected dispatch Term transform(XExpression e, Map<PVariable, Variable> variable2Variable) { | ||
130 | throw new UnsupportedOperationException('''Unsupported expression: "«e.class.simpleName»" - «e»''') | ||
131 | } | ||
132 | } \ No newline at end of file | ||