diff options
Diffstat (limited to 'Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PExpressionGenerator.xtend')
-rw-r--r-- | Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PExpressionGenerator.xtend | 116 |
1 files changed, 116 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/PExpressionGenerator.xtend b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PExpressionGenerator.xtend new file mode 100644 index 00000000..62ff92b2 --- /dev/null +++ b/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PExpressionGenerator.xtend | |||
@@ -0,0 +1,116 @@ | |||
1 | package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.patterns | ||
2 | |||
3 | import java.util.Map | ||
4 | import org.eclipse.viatra.query.runtime.matchers.psystem.PVariable | ||
5 | import org.eclipse.xtext.xbase.XBinaryOperation | ||
6 | import org.eclipse.xtext.xbase.XExpression | ||
7 | import org.eclipse.xtext.xbase.XFeatureCall | ||
8 | import org.eclipse.xtext.xbase.XMemberFeatureCall | ||
9 | import org.eclipse.xtext.xbase.XNumberLiteral | ||
10 | import org.eclipse.xtext.xbase.XUnaryOperation | ||
11 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.PrimitiveTypeReference | ||
12 | import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.RealTypeReference | ||
13 | |||
14 | class PExpressionGenerator { | ||
15 | static val N_Base = "org.eclipse.xtext.xbase.lib." | ||
16 | |||
17 | static val N_PLUS1 = "operator_plus" | ||
18 | static val N_MINUS1 = "operator_minus" | ||
19 | |||
20 | static val N_MINUS2 = "operator_minus" | ||
21 | static val N_PLUS2 = "operator_plus" | ||
22 | static val N_POWER = "operator_power" | ||
23 | static val N_MULTIPLY = "operator_multiply" | ||
24 | static val N_DIVIDE = "operator_divide" | ||
25 | static val N_MODULO = "operator_modulo" | ||
26 | static val N_LESSTHAN = "operator_lessThan" | ||
27 | static val N_LESSEQUALSTHAN = "operator_lessEqualsThan" | ||
28 | static val N_GREATERTHAN = "operator_greaterThan" | ||
29 | static val N_GREATEREQUALTHAN = "operator_greaterEqualsThan" | ||
30 | static val N_EQUALS = "operator_equals" | ||
31 | static val N_NOTEQUALS = "operator_notEquals" | ||
32 | static val N_EQUALS3 = "operator_tripleEquals" | ||
33 | static val N_NOTEQUALS3 = "operator_tripleNotEquals" | ||
34 | |||
35 | protected def isN(String name, String s) { | ||
36 | val res = name.startsWith(N_Base) && name.endsWith(s) | ||
37 | //println('''[«res»] «name» ?= «N_Base»*«s»''') | ||
38 | return res | ||
39 | } | ||
40 | |||
41 | static val N_POWER2 = "java.lang.Math.pow" | ||
42 | |||
43 | def dispatch CharSequence translateExpression(XBinaryOperation e, Map<PVariable,String> valueName, Map<PVariable, PrimitiveTypeReference> variable2Type) { | ||
44 | val left = e.leftOperand.translateExpression(valueName,variable2Type) | ||
45 | val right = e.rightOperand.translateExpression(valueName,variable2Type) | ||
46 | val feature = e.feature.qualifiedName | ||
47 | if(feature.isN(N_MINUS2)) { return '''(«left»-«right»)'''} | ||
48 | else if(feature.isN(N_PLUS2)) { return '''(«left»+«right»)''' } | ||
49 | else if(feature.isN(N_POWER)) { return '''(«left»^«right»)''' } | ||
50 | else if(feature.isN(N_MULTIPLY)) { return '''(«left»*«right»)''' } | ||
51 | else if(feature.isN(N_DIVIDE)) { return '''(«left»/«right»)''' } | ||
52 | else if(feature.isN(N_MODULO)) { return '''(«left»%«right»)''' } | ||
53 | else if(feature.isN(N_LESSTHAN)) { return '''(«left»<«right»)''' } | ||
54 | else if(feature.isN(N_LESSEQUALSTHAN)) { return '''(«left»<=«right»)''' } | ||
55 | else if(feature.isN(N_GREATERTHAN)) { return '''(«left»>«right»)''' } | ||
56 | else if(feature.isN(N_GREATEREQUALTHAN)) { return '''(«left»>=«right»)''' } | ||
57 | else if(feature.isN(N_EQUALS)) { return '''(«left»==«right»)''' } | ||
58 | else if(feature.isN(N_NOTEQUALS)) { return '''(«left»!=«right»)''' } | ||
59 | else if(feature.isN(N_EQUALS3)) { return '''(«left»===«right»)''' } | ||
60 | else if(feature.isN(N_NOTEQUALS3)) { return '''(«left»!==«right»)''' } | ||
61 | else { | ||
62 | println("-> " + e.feature+","+e.class) | ||
63 | println("-> " + e.leftOperand) | ||
64 | println("-> " + e.rightOperand) | ||
65 | println("-> " + e.feature.qualifiedName) | ||
66 | throw new UnsupportedOperationException('''Unsupported binary operator feature: "«e.feature.class.simpleName»" - «e»''') | ||
67 | } | ||
68 | } | ||
69 | |||
70 | def dispatch CharSequence translateExpression(XUnaryOperation e, Map<PVariable,String> valueName, Map<PVariable, PrimitiveTypeReference> variable2Type) { | ||
71 | val operand = e.operand.translateExpression(valueName,variable2Type) | ||
72 | val feature = e.feature.qualifiedName | ||
73 | if(feature.isN(N_MINUS1)) { return '''(-«operand»)'''} | ||
74 | else if(feature.isN(N_PLUS1)) { return '''(+«operand»)'''} | ||
75 | else{ | ||
76 | println("-> " + e.feature+","+e.class) | ||
77 | println("-> " + e.operand) | ||
78 | println("-> " + e.feature.qualifiedName) | ||
79 | throw new UnsupportedOperationException('''Unsupported unary operator feature: "«e.feature.class.simpleName»" - «e»''') | ||
80 | } | ||
81 | } | ||
82 | |||
83 | def dispatch CharSequence translateExpression(XMemberFeatureCall e, Map<PVariable,String> valueName, Map<PVariable, PrimitiveTypeReference> variable2Type) { | ||
84 | val transformedArguments = e.actualArguments.map[translateExpression(valueName,variable2Type)] | ||
85 | val feature = e.feature.qualifiedName | ||
86 | if(feature == N_POWER2) { | ||
87 | return '''Math.pow(«transformedArguments.get(0)»,«transformedArguments.get(1)»)''' | ||
88 | }else { | ||
89 | println(e.feature+","+e.class) | ||
90 | println(e.actualArguments.join(", ")) | ||
91 | println(e.feature.qualifiedName) | ||
92 | throw new UnsupportedOperationException('''Unsupported feature call: "«e.feature.qualifiedName»" - «e»''') | ||
93 | } | ||
94 | } | ||
95 | |||
96 | def dispatch CharSequence translateExpression(XFeatureCall e, Map<PVariable,String> valueName, Map<PVariable, PrimitiveTypeReference> variable2Type) { | ||
97 | val featureName = e.feature.qualifiedName | ||
98 | val type = variable2Type.entrySet.filter[it.key.name===featureName].head.value | ||
99 | val entryWithName = valueName.entrySet.filter[it.key.name == featureName].head | ||
100 | if(entryWithName !== null) { | ||
101 | if(type instanceof RealTypeReference) { | ||
102 | return '''(«entryWithName.value».doubleValue)''' | ||
103 | } else { | ||
104 | return entryWithName.value | ||
105 | } | ||
106 | } else { | ||
107 | throw new IllegalArgumentException('''Feature call reference to unavailable variable "«featureName»"''') | ||
108 | } | ||
109 | } | ||
110 | |||
111 | def dispatch CharSequence translateExpression(XNumberLiteral l, Map<PVariable,String> valueName, Map<PVariable, PrimitiveTypeReference> variable2Type) '''«l.value»''' | ||
112 | |||
113 | def dispatch CharSequence translateExpression(XExpression expression, Map<PVariable,String> valueName, Map<PVariable, PrimitiveTypeReference> variable2Type) { | ||
114 | throw new UnsupportedOperationException('''Unsupported expression in check or eval: «expression.class.name», «expression»"''') | ||
115 | } | ||
116 | } \ No newline at end of file | ||