aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/hu.bme.mit.inf.dslreasoner.viatra2logic/src/hu/bme/mit/inf/dslreasoner/viatra2logic/NumericProblemSolver.java
blob: 2372177a36760ea4d1099f025ceb658119b456e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package hu.bme.mit.inf.dslreasoner.viatra2logic;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.eclipse.xtext.xbase.XBinaryOperation;
import org.eclipse.xtext.xbase.XExpression;
import org.eclipse.xtext.xbase.XFeatureCall;
import org.eclipse.xtext.xbase.XNumberLiteral;

import com.microsoft.z3.ArithExpr;
import com.microsoft.z3.BoolExpr;
import com.microsoft.z3.Context;
import com.microsoft.z3.Expr;
import com.microsoft.z3.IntExpr;
import com.microsoft.z3.Solver;
import com.microsoft.z3.Status;


public class NumericProblemSolver {
	private static final String N_Base = "org.eclipse.xtext.xbase.lib.";
	private static final String N_PLUS = "operator_plus";
	private static final String N_MINUS = "operator_minus";
	private static final String N_POWER = "operator_power";
	private static final String N_MULTIPLY = "operator_multiply";
	private static final String N_DIVIDE = "operator_divide";
	private static final String N_MODULO = "operator_modulo";
	private static final String N_LESSTHAN = "operator_lessThan";
	private static final String N_LESSEQUALSTHAN = "operator_lessEqualsThan";
	private static final String N_GREATERTHAN = "operator_greaterThan";
	private static final String N_GREATEREQUALTHAN = "operator_greaterEqualsThan";
	private static final String N_EQUALS = "operator_equals";
	private static final String N_NOTEQUALS = "operator_notEquals";
	private static final String N_EQUALS3 = "operator_tripleEquals";
	private static final String N_NOTEQUALS3 = "operator_tripleNotEquals";

	private Context ctx;
	private Solver s;
	private Map<XExpression, BoolExpr> templates;
	private Map<XExpression, Map<Object, Expr>> bindings;
	private BoolExpr test;

	public NumericProblemSolver() {
		HashMap<String, String> cfg = new HashMap<String, String>();
		cfg.put("model", "true");
		ctx = new Context(cfg);	
		s = ctx.mkSolver();
		templates = new HashMap<XExpression, BoolExpr>();
		bindings = new HashMap<XExpression, Map<Object, Expr>>();
	}

	public Context getNumericProblemContext() {
		return ctx;
	}

	public void formNumericProblemTemplate(XExpression e) throws Exception {
		if (!(e instanceof XBinaryOperation)) {
			throw new Exception ("error in check expression!!!");
		}

		String name = ((XBinaryOperation) e).getFeature().getQualifiedName();

		BoolExpr constraint = null;
				
		ArithExpr left_operand = formNumericProblemTemplateHelper(((XBinaryOperation) e).getLeftOperand());
		ArithExpr right_operand = formNumericProblemTemplateHelper(((XBinaryOperation) e).getRightOperand());

		if (nameEndsWith(name, N_LESSTHAN)) {
			constraint = ctx.mkLt(left_operand, right_operand);
		} else if (nameEndsWith(name, N_LESSEQUALSTHAN)) {
			constraint = ctx.mkLe(left_operand, right_operand);
		} else if (nameEndsWith(name, N_GREATERTHAN)) {
			constraint = ctx.mkGt(left_operand, right_operand);
		} else if (nameEndsWith(name, N_GREATEREQUALTHAN)) {
			constraint = ctx.mkGe(left_operand, right_operand);
		} else if (nameEndsWith(name, N_EQUALS)) {
			constraint = ctx.mkEq(left_operand, right_operand);
		} else if (nameEndsWith(name, N_NOTEQUALS)) {
			constraint = ctx.mkDistinct(left_operand, right_operand);
		} else if (nameEndsWith(name, N_EQUALS3)) {
			constraint = ctx.mkGe(left_operand, right_operand);	// ???
		} else if (nameEndsWith(name, N_NOTEQUALS3)) {
			constraint = ctx.mkGe(left_operand, right_operand);	// ???
		} else {
			throw new Exception ("Unsupported binary operation " + name);
		}
		
		templates.put(e, constraint);
		HashMap<XExpression, Set<List<Object>>> matches = new HashMap<XExpression, Set<List<Object>>>();
		Object o1 = new Object();
		Object o2 = new Object();
		List<Object> list = new ArrayList<Object>();
		list.add(o1);
		list.add(o2);
		Set<List<Object>> set = new HashSet<List<Object>>();
		set.add(list);
		matches.put(e, set);	
		System.out.println("************************");
		System.out.println(this.isSatisfiable(matches));
	}

	// TODO: add variable: state of the solver
	private ArithExpr formNumericProblemTemplateHelper(XExpression e) throws Exception {
		ArithExpr expr = null;
		// Variables
		if (e instanceof XFeatureCall) {
			String var_name = ((XFeatureCall) e).getFeature().getQualifiedName();
			expr = (ArithExpr) ctx.mkConst(ctx.mkSymbol(var_name), ctx.getIntSort());
		} 
		// Constants
		else if (e instanceof XNumberLiteral) {
			String value = ((XNumberLiteral) e).getValue();
			try{ int val = Integer.parseInt(value);  expr = (ArithExpr) ctx.mkInt(val);}  catch(NumberFormatException err){}
		} 
		// Expressions with operators
		else if (e instanceof XBinaryOperation) {
			String name = ((XBinaryOperation) e).getFeature().getQualifiedName();
			ArithExpr left_operand = formNumericProblemTemplateHelper(((XBinaryOperation) e).getLeftOperand());
			ArithExpr right_operand = formNumericProblemTemplateHelper(((XBinaryOperation) e).getRightOperand());

			if (nameEndsWith(name, N_PLUS)) {
				expr = ctx.mkAdd(left_operand, right_operand);
			} else if (nameEndsWith(name, N_MINUS)) {
				expr = ctx.mkAdd(left_operand, ctx.mkUnaryMinus(right_operand));
			} else if (nameEndsWith(name, N_POWER)) {
				expr = ctx.mkPower(left_operand, right_operand);
			} else if (nameEndsWith(name, N_MULTIPLY)) {
				expr = ctx.mkMul(left_operand, right_operand);
			} else if (nameEndsWith(name, N_DIVIDE)) {
				expr = ctx.mkDiv(left_operand, right_operand);
			} else if (nameEndsWith(name, N_MODULO)) {
				expr = ctx.mkMod((IntExpr)left_operand, (IntExpr)right_operand);
			} else {
				throw new Exception ("Unsupported binary operation " + name);
			}
		} else {
			throw new Exception ("Unsupported expression " + e.getClass().getSimpleName());
		}
		return expr;

	}

	private boolean nameEndsWith(String name, String end) {
		return name.startsWith(N_Base) && name.endsWith(end);
	}

	public boolean isSatisfiable(Map<XExpression, Set<List<Object>>> matches) throws Exception {
		boolean sat = false;
		BoolExpr problemInstance = formNumericProblemInstance(matches);
		s.add(problemInstance);
		if (s.check() == Status.SATISFIABLE) {
			sat = true;
		} else {
			sat = false;
		}
		return sat;
	}
	
//	public Map<Object,Integer> getOneSolution(List<Object>, Map<XExpression, Set<List<Object>>> matches) {
//		Map<Object,Integer> sol = new HashMap<Object, Integer>();
//		BoolExpr problemInstance = formNumericProblemInstance(matches);
//		s.add(problemInstance);
//		if (s.check() == Status.SATISFIABLE) {
//			//TODO:  Form the map here
//		}
//		return sol;
//	}
	
	private BoolExpr formNumericProblemInstance(Map<XExpression, Set<List<Object>>> matches) throws Exception {
		BoolExpr constraintInstances = ctx.mkTrue();
		for (XExpression e: matches.keySet()) {
			BoolExpr template = templates.get(e);
			Set<List<Object>> matchSets = matches.get(e);
			Map<Object, Expr> varMap = bindings.get(e);

			if (varMap == null) {
				varMap = new HashMap<Object, Expr>();
				bindings.put(e, varMap);
			}

			for (List<Object> aMatch: matchSets) {
				Expr[] expressions = template.getArgs().clone();
				formNumericProblemInstance(expressions, aMatch, varMap, 0);
				constraintInstances = ctx.mkAnd(constraintInstances, formConstraintInstance(template, expressions));
			}
		}
		return constraintInstances;
	}

	// TODO: Create a new exception class??
	private BoolExpr formConstraintInstance(BoolExpr template, Expr[] expressions) throws Exception {
		if (expressions.length != 2) {
			throw new Exception("Wrong format");
		}
		BoolExpr inst = ctx.mkTrue();
		ArithExpr leftOperand = (ArithExpr) expressions[0];
		ArithExpr rightOperand = (ArithExpr) expressions[1];
		if (template.isLE()) {
			inst = ctx.mkLe(leftOperand, rightOperand);
		} else if (template.isLT()) {
			inst = ctx.mkLt(leftOperand, rightOperand);
		} else if (template.isGE()) {
			inst = ctx.mkGe(leftOperand, rightOperand);
		} else if (template.isGT()) {
			inst = ctx.mkGt(leftOperand, rightOperand);
		} else if (template.isEq()) {
			inst = ctx.mkEq(leftOperand, rightOperand);
		} else if (template.isDistinct()) {
			inst = ctx.mkDistinct(leftOperand, rightOperand);
		} else {
			throw new Exception("Something went wrong");
		}
		return inst;
	}
	
	// TODO: This is not gonna work....How to plug in matched objects to the template???
	private void formNumericProblemInstance(Expr[] expr, List<Object> match, Map<Object, Expr> varMap, int ind) {
		for (int i = 0; i < expr.length; i++) {
			if (expr[i].isConst()) {
				expr[i] = ctx.mkConst(expr[i].toString(), ctx.getIntSort());
			} else if (expr[i].getArgs().length > 0) {
				formNumericProblemInstance(expr[i].getArgs(), match, varMap, ind);
			}
		}
	}
}