-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp.java
More file actions
173 lines (151 loc) · 3.63 KB
/
Exp.java
File metadata and controls
173 lines (151 loc) · 3.63 KB
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
package simulator;
import java.util.ArrayList;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Exp {
private String exp;// stores expression (f(x)) which is in terms of y = f(x)
// and objective fun (f(x,y)) in terms of z = f(x,y)
// if expression is y=f(x), isfx flag is set true
// if expression is x=f(y), isfx flag is set false
private Boolean isfx = true;
// if expression is objective fun, then isObj flag is set true
// otherwise false
private Boolean isObjFun = false;
private String operator;
// get expression
public String getExp() {
return exp;
}
// initialize exp and isfx
public void setExp(String exp, Boolean isfx, Boolean isObjFun, String operator) {
this.exp = exp;
this.isfx = isfx;
this.isObjFun = isObjFun;
if (isObjFun)
this.operator = "";
else
this.operator = operator;
}
@Override
public String toString() {
return this.exp;
}
/*
* Expression is a String. To evaluate such an expression we use javascript
* engine from ScriptEngine Library
*/
private ScriptEngineManager mgr = new ScriptEngineManager();
private ScriptEngine engine = mgr.getEngineByName("JavaScript");
// addValue() gives value to a variable in the expression
// key = variable in expression
// value = its value
public void addValue(String key, Object value) {
engine.put(key, value);
}
public Boolean getisfxFlag() {
return this.isfx;
}
public Boolean followsOperator(double val1, String op, double val2) {
Boolean ret = false;
switch (op) {
case "<":
if (val1 < val2)
ret = true;
break;
case "<=":
if (val1 <= val2)
ret = true;
break;
case ">":
if (val1 > val2)
ret = true;
break;
case ">=":
if (val1 >= val2)
ret = true;
break;
case "==":
if (val1 == val2)
ret = true;
break;
case "!=":
if (val1 != val2)
ret = true;
break;
case "=":
if (val1 == val2)
ret = true;
break;
default:
ret = false;
}
return ret;
}
public Boolean getisObjFunFlag() {
return this.isObjFun;
}
public Boolean satisfy(double x, double y) {
if (isfx) {
this.addValue("x", x);
if (followsOperator(y, operator, this.solve()))
return true;
else
return false;
} else {
this.addValue("y", y);
// System.out.println(this.exp+":"+x+","+y+"="+this.solve());
if (followsOperator(x, operator, this.solve()))
return true;
else
return false;
}
}
public ArrayList<SimplePoint> getCoordsSet(double limitX, double limitY, double step) {
if (!isObjFun) {
ArrayList<SimplePoint> coordsSet = new ArrayList<SimplePoint>();
for (double i = 0; i < limitX; i += step) {
if (isfx) {
this.addValue("x", i);
SimplePoint sp = new SimplePoint();
sp.x = i;
sp.y = this.solve();
if (sp.y <= limitY)
coordsSet.add(sp);
} else {
this.addValue("y", i);
SimplePoint sp = new SimplePoint();
sp.x = this.solve();
sp.y = i;
if (sp.x <= limitX)
coordsSet.add(sp);
}
}
return coordsSet;
} else
return null;
}
public double solve(double x, double y) {
if (isObjFun) {
this.addValue("x", x);
this.addValue("y", y);
return this.solve();
} else
return 0;
}
// return double value of expression after substituting all key with value
// obtained from addValue() method
public double solve() {
try {
Object res = engine.eval(exp);
if (res instanceof Integer) {
return (double) ((Integer) res).intValue();
} else
return (double) res;
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return 0;
}
}
}