diff options
author | 2020-04-17 00:17:45 +0200 | |
---|---|---|
committer | 2020-04-17 00:17:45 +0200 | |
commit | 6e56760734f62857d437cbb54746e9ff487f46d3 (patch) | |
tree | 541f68b6e9da4c4badbf42d4a9e0c191dfcd1978 /Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/patterns/PExpressionGenerator.xtend | |
parent | variable mapping trace is created after mapping (diff) | |
download | VIATRA-Generator-6e56760734f62857d437cbb54746e9ff487f46d3.tar.gz VIATRA-Generator-6e56760734f62857d437cbb54746e9ff487f46d3.tar.zst VIATRA-Generator-6e56760734f62857d437cbb54746e9ff487f46d3.zip |
check expressions are mapped to WF constraints
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 | 109 |
1 files changed, 109 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..303c87b9 --- /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,109 @@ | |||
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 | |||
12 | class PExpressionGenerator { | ||
13 | static val N_Base = "org.eclipse.xtext.xbase.lib." | ||
14 | |||
15 | static val N_PLUS1 = "operator_plus" | ||
16 | static val N_MINUS1 = "operator_minus" | ||
17 | |||
18 | static val N_MINUS2 = "operator_minus" | ||
19 | static val N_PLUS2 = "operator_plus" | ||
20 | static val N_POWER = "operator_power" | ||
21 | static val N_MULTIPLY = "operator_multiply" | ||
22 | static val N_DIVIDE = "operator_divide" | ||
23 | static val N_MODULO = "operator_modulo" | ||
24 | static val N_LESSTHAN = "operator_lessThan" | ||
25 | static val N_LESSEQUALSTHAN = "operator_lessEqualsThan" | ||
26 | static val N_GREATERTHAN = "operator_greaterThan" | ||
27 | static val N_GREATEREQUALTHAN = "operator_greaterEqualsThan" | ||
28 | static val N_EQUALS = "operator_equals" | ||
29 | static val N_NOTEQUALS = "operator_notEquals" | ||
30 | static val N_EQUALS3 = "operator_tripleEquals" | ||
31 | static val N_NOTEQUALS3 = "operator_tripleNotEquals" | ||
32 | |||
33 | protected def isN(String name, String s) { | ||
34 | val res = name.startsWith(N_Base) && name.endsWith(s) | ||
35 | //println('''[«res»] «name» ?= «N_Base»*«s»''') | ||
36 | return res | ||
37 | } | ||
38 | |||
39 | static val N_POWER2 = "java.lang.Math.pow" | ||
40 | |||
41 | def dispatch CharSequence translateExpression(XBinaryOperation e, Map<PVariable,String> valueName) { | ||
42 | val left = e.leftOperand.translateExpression(valueName) | ||
43 | val right = e.rightOperand.translateExpression(valueName) | ||
44 | val feature = e.feature.qualifiedName | ||
45 | if(feature.isN(N_MINUS2)) { return '''(«left»-«right»)'''} | ||
46 | else if(feature.isN(N_PLUS2)) { return '''(«left»+«right»)''' } | ||
47 | else if(feature.isN(N_POWER)) { return '''(«left»^«right»)''' } | ||
48 | else if(feature.isN(N_MULTIPLY)) { return '''(«left»*«right»)''' } | ||
49 | else if(feature.isN(N_DIVIDE)) { return '''(«left»/«right»)''' } | ||
50 | else if(feature.isN(N_MODULO)) { return '''(«left»%«right»)''' } | ||
51 | else if(feature.isN(N_LESSTHAN)) { return '''(«left»<«right»)''' } | ||
52 | else if(feature.isN(N_LESSEQUALSTHAN)) { return '''(«left»<=«right»)''' } | ||
53 | else if(feature.isN(N_GREATERTHAN)) { return '''(«left»>«right»)''' } | ||
54 | else if(feature.isN(N_GREATEREQUALTHAN)) { return '''(«left»>=«right»)''' } | ||
55 | else if(feature.isN(N_EQUALS)) { return '''(«left»==«right»)''' } | ||
56 | else if(feature.isN(N_NOTEQUALS)) { return '''(«left»!=«right»)''' } | ||
57 | else if(feature.isN(N_EQUALS3)) { return '''(«left»===«right»)''' } | ||
58 | else if(feature.isN(N_NOTEQUALS3)) { return '''(«left»!==«right»)''' } | ||
59 | else { | ||
60 | println("-> " + e.feature+","+e.class) | ||
61 | println("-> " + e.leftOperand) | ||
62 | println("-> " + e.rightOperand) | ||
63 | println("-> " + e.feature.qualifiedName) | ||
64 | throw new UnsupportedOperationException('''Unsupported binary operator feature: "«e.feature.class.simpleName»" - «e»''') | ||
65 | } | ||
66 | } | ||
67 | |||
68 | def dispatch CharSequence translateExpression(XUnaryOperation e, Map<PVariable,String> valueName) { | ||
69 | val operand = e.operand.translateExpression(valueName) | ||
70 | val feature = e.feature.qualifiedName | ||
71 | if(feature.isN(N_MINUS1)) { return '''(-«operand»)'''} | ||
72 | else if(feature.isN(N_PLUS1)) { return '''(+«operand»)'''} | ||
73 | else{ | ||
74 | println("-> " + e.feature+","+e.class) | ||
75 | println("-> " + e.operand) | ||
76 | println("-> " + e.feature.qualifiedName) | ||
77 | throw new UnsupportedOperationException('''Unsupported unary operator feature: "«e.feature.class.simpleName»" - «e»''') | ||
78 | } | ||
79 | } | ||
80 | |||
81 | def dispatch CharSequence translateExpression(XMemberFeatureCall e, Map<PVariable,String> valueName) { | ||
82 | val transformedArguments = e.actualArguments.map[translateExpression(valueName)] | ||
83 | val feature = e.feature.qualifiedName | ||
84 | if(feature == N_POWER2) { | ||
85 | return '''Math.pow(«transformedArguments.get(0)»,«transformedArguments.get(1)»)''' | ||
86 | }else { | ||
87 | println(e.feature+","+e.class) | ||
88 | println(e.actualArguments.join(", ")) | ||
89 | println(e.feature.qualifiedName) | ||
90 | throw new UnsupportedOperationException('''Unsupported feature call: "«e.feature.qualifiedName»" - «e»''') | ||
91 | } | ||
92 | } | ||
93 | |||
94 | def dispatch CharSequence translateExpression(XFeatureCall e, Map<PVariable,String> valueName) { | ||
95 | val featureName = e.feature.qualifiedName | ||
96 | val entryWithName = valueName.entrySet.filter[it.key.name == featureName].head | ||
97 | if(entryWithName !== null) { | ||
98 | return entryWithName.value | ||
99 | } else { | ||
100 | throw new IllegalArgumentException('''Feature call reference to unavailable variable "«featureName»"''') | ||
101 | } | ||
102 | } | ||
103 | |||
104 | def dispatch CharSequence translateExpression(XNumberLiteral l, Map<PVariable,String> valueName) '''«l.value»''' | ||
105 | |||
106 | def dispatch CharSequence translateExpression(XExpression expression, Map<PVariable,String> valueName) { | ||
107 | throw new UnsupportedOperationException('''Unsupported expression in check or eval: «expression.class.name», «expression»"''') | ||
108 | } | ||
109 | } \ No newline at end of file | ||