rgradisc@1027
|
1 |
/*
|
rgradisc@1027
|
2 |
* Created on Oct 20, 2003
|
rgradisc@1027
|
3 |
*/
|
rgradisc@1027
|
4 |
package isac.bridge;
|
rgradisc@1027
|
5 |
|
wneuper@1976
|
6 |
import isac.util.CalcChanged;
|
rgradisc@1088
|
7 |
import isac.util.Formalization;
|
rgradisc@1191
|
8 |
import isac.util.NotInSpecificationPhaseException;
|
rgradisc@1311
|
9 |
import isac.util.formulae.CalcHead;
|
rgradisc@1311
|
10 |
import isac.util.formulae.CalcHeadCompoundID;
|
rgradisc@1311
|
11 |
import isac.util.interfaces.ICalcElement;
|
rgradisc@1311
|
12 |
import isac.util.interfaces.ICalcIterator;
|
rgradisc@1311
|
13 |
import isac.util.tactics.Tactic;
|
rgradisc@1035
|
14 |
|
rgradisc@1035
|
15 |
import java.io.Serializable;
|
rgradisc@1035
|
16 |
import java.rmi.Naming;
|
rgradisc@1035
|
17 |
import java.rmi.RemoteException;
|
rgradisc@1191
|
18 |
import java.util.HashMap;
|
rgradisc@1191
|
19 |
import java.util.Map;
|
rgradisc@1198
|
20 |
import java.util.Vector;
|
rgradisc@1035
|
21 |
|
wneuper@1863
|
22 |
import org.apache.log4j.Logger;
|
wneuper@1863
|
23 |
|
rgradisc@1027
|
24 |
/**
|
rgradisc@1623
|
25 |
* Class MathEngine: This class is called by the Dialog
|
rgradisc@1623
|
26 |
* and communicates with the Bridge via RMI.
|
rgradisc@1623
|
27 |
* It also delegates the methods of the CalcTree and the
|
rgradisc@1623
|
28 |
* CalcIterator to the Bridge.
|
rgradisc@1027
|
29 |
* @author rgradisc
|
rgradisc@1027
|
30 |
*/
|
rgradisc@1035
|
31 |
public class MathEngine implements Serializable {
|
rgradisc@1027
|
32 |
|
rgradisc@1222
|
33 |
private static MathEngine singleton;
|
rgradisc@1036
|
34 |
|
rgradisc@1035
|
35 |
private IBridgeRMI bridgeRMI;
|
rgradisc@1635
|
36 |
|
wneuper@1742
|
37 |
// Stores the calcTrees after call of startSpecifying //WN00827 startCalculation
|
wneuper@1742
|
38 |
// and hands them out to the dialog after startSolving//WN00827 ???
|
rgradisc@1191
|
39 |
private Map calcTrees;
|
rgradisc@1191
|
40 |
|
wneuper@1863
|
41 |
static Logger logger = Logger.getLogger(MathEngine.class.getName());
|
wneuper@1863
|
42 |
|
rgradisc@1623
|
43 |
// This is a Singleton Class: A private constructor is needed
|
rgradisc@1036
|
44 |
private MathEngine(String hostName) {
|
wneuper@1976
|
45 |
logger.debug("MathEngine("+hostName+")");
|
rgradisc@1035
|
46 |
try {
|
rgradisc@1623
|
47 |
//connect to bridge
|
rgradisc@1586
|
48 |
bridgeRMI = (IBridgeRMI) Naming.lookup("//" + hostName + "/BridgeRMI");
|
rgradisc@1586
|
49 |
System.out.println(
|
rgradisc@1586
|
50 |
"MathEngine Constr: connected to Bridge: " + bridgeRMI.toString());
|
rgradisc@1035
|
51 |
if (bridgeRMI == null)
|
rgradisc@1623
|
52 |
System.out.println("Error occured: The Bridge could not be found!");
|
rgradisc@1035
|
53 |
} catch (Exception e) {
|
rgradisc@1048
|
54 |
System.out.println(
|
rgradisc@1048
|
55 |
"Could not connect to Bridge via RMI\n"
|
rgradisc@1048
|
56 |
+ "Please make sure that the Bridge is running and connected");
|
rgradisc@1035
|
57 |
e.printStackTrace();
|
rgradisc@1035
|
58 |
}
|
rgradisc@1191
|
59 |
calcTrees = new HashMap();
|
rgradisc@1035
|
60 |
}
|
rgradisc@1035
|
61 |
|
wneuper@1875
|
62 |
/*
|
wneuper@1875
|
63 |
* FIXME.WN040924 just to make the old bridge continue work
|
wneuper@1875
|
64 |
**/
|
wneuper@1875
|
65 |
protected IBridgeRMI getBridgeRMI() {
|
wneuper@1875
|
66 |
return bridgeRMI;
|
wneuper@1875
|
67 |
}
|
rgradisc@1623
|
68 |
/**
|
rgradisc@1623
|
69 |
* Initialize the MathEngine
|
rgradisc@1623
|
70 |
* @param hostName: Host name on which the Bridge is running
|
rgradisc@1623
|
71 |
*/
|
rgradisc@1036
|
72 |
public static void init(String hostName) {
|
wneuper@1976
|
73 |
logger.fatal("DG->BR: init("+hostName+")");
|
rgradisc@1304
|
74 |
if (singleton == null) {
|
rgradisc@1220
|
75 |
singleton = new MathEngine(hostName);
|
rgradisc@1304
|
76 |
}
|
rgradisc@1036
|
77 |
}
|
rgradisc@1048
|
78 |
|
rgradisc@1036
|
79 |
public static MathEngine getMathEngine() {
|
wneuper@1976
|
80 |
logger.fatal("DG->BR: getMathEngine()");
|
rgradisc@1036
|
81 |
return singleton;
|
rgradisc@1036
|
82 |
}
|
rgradisc@1036
|
83 |
|
wneuper@1875
|
84 |
/**
|
wneuper@1875
|
85 |
* Start a new calculation
|
wneuper@1875
|
86 |
* @param f Formalization of the new calculation, from expl
|
wneuper@1875
|
87 |
* @return CalcHead: empty for startSpecifying
|
wneuper@1875
|
88 |
* @throws RemoteException WN040924 ???
|
wneuper@1891
|
89 |
*
|
wneuper@1891
|
90 |
* int id = calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead
|
wneuper@1891
|
91 |
* CalcTree calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922 TODO simplify
|
wneuper@1875
|
92 |
*/
|
wneuper@1875
|
93 |
public CalcTree startCalculation(Formalization f) {
|
wneuper@1976
|
94 |
logger.fatal("DG->BR: startCalculation("+f.toSMLString()+")");
|
wneuper@1875
|
95 |
//CalcHead calcHead = null;
|
wneuper@1875
|
96 |
CalcTree calcTree = null;//WN
|
wneuper@1875
|
97 |
ICalcIterator hotSpot = null;//WN
|
rgradisc@1166
|
98 |
try {
|
wneuper@1738
|
99 |
int id = bridgeRMI.startCalculation(f);
|
wneuper@1875
|
100 |
calcTree = new CalcTree(this, id);
|
rgradisc@1191
|
101 |
calcTrees.put(new Integer(id), calcTree);
|
rgradisc@1586
|
102 |
|
wneuper@1875
|
103 |
hotSpot = calcTree.iterator();
|
rgradisc@1191
|
104 |
calcTree.setHotSpot(hotSpot); //first iterator marks the hotSpot
|
wneuper@1884
|
105 |
hotSpot.moveRoot();
|
wneuper@1738
|
106 |
//---------------------------------------------- startSpecifying
|
wneuper@1875
|
107 |
//WN calcHead = (CalcHead) hotSpot.getElement();
|
wneuper@1875
|
108 |
//WN System.out.println("Calchead: " + calcHead.toSMLString());
|
rgradisc@1191
|
109 |
} catch (RemoteException e) {
|
rgradisc@1166
|
110 |
e.printStackTrace();
|
rgradisc@1166
|
111 |
}
|
wneuper@1875
|
112 |
return calcTree;
|
rgradisc@1166
|
113 |
}
|
rgradisc@1166
|
114 |
|
wneuper@1875
|
115 |
/*
|
wneuper@1875
|
116 |
boolean autoCalculate(int id, int scope, int nSteps) throws RemoteException {
|
wneuper@1979
|
117 |
logger.debug("autoCalculate: id="+id+", scope="+scope+", nSteps="+nSteps);
|
wneuper@1875
|
118 |
return bridgeRMI.autoCalculate(id, scope, nSteps);
|
wneuper@1875
|
119 |
}
|
wneuper@1875
|
120 |
*/
|
wneuper@1875
|
121 |
|
rgradisc@1166
|
122 |
/**
|
wneuper@1875
|
123 |
* @see IToCalc#startSolving(), copied to CalcTree
|
wneuper@1875
|
124 |
*
|
wneuper@1875
|
125 |
public void startSolving(CalcHead calcHead) throws Exception {
|
wneuper@1979
|
126 |
logger.debug("startSolving: calcHead="+calcHead);
|
wneuper@1875
|
127 |
int id = calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead
|
wneuper@1875
|
128 |
CalcTree calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922 TODO simplify
|
rgradisc@1623
|
129 |
|
rgradisc@1623
|
130 |
if (calcHead.getCalcHeadStatus() != CalcHead.CALCHEAD_CORRECT) {
|
rgradisc@1623
|
131 |
throw new Exception("Startsolving with incorrect CalcHead");
|
rgradisc@1623
|
132 |
}
|
rgradisc@1618
|
133 |
Tactic t = calcTree.fetchProposedTactic();
|
rgradisc@1618
|
134 |
if (t.getName().compareTo("Apply_Method") != 0) {
|
rgradisc@1623
|
135 |
throw new Exception("Startsolving fetches " + t.getName());
|
rgradisc@1618
|
136 |
}
|
rgradisc@1618
|
137 |
//Next Tactic must always be Apply_Method
|
rgradisc@1618
|
138 |
calcTree.setNextTactic(t);
|
wneuper@1875
|
139 |
//return calcTree;
|
rgradisc@1035
|
140 |
}
|
wneuper@1875
|
141 |
**/
|
wneuper@1875
|
142 |
|
rgradisc@1166
|
143 |
/**
|
wneuper@1875
|
144 |
* @see IToCalc
|
akremp@1257
|
145 |
public void modifyCalcHead(CalcHead calcHead) {
|
wneuper@1979
|
146 |
logger.debug("modifyCalcHead: calcHead="+calcHead);
|
rgradisc@1623
|
147 |
CalcHead newCalcHead = null;
|
rgradisc@1166
|
148 |
try {
|
rgradisc@1615
|
149 |
newCalcHead = bridgeRMI.modifyCalcHead(calcHead);
|
rgradisc@1623
|
150 |
calcHead.fillValuesfrom(newCalcHead);
|
rgradisc@1304
|
151 |
} catch (RemoteException e) {
|
rgradisc@1191
|
152 |
e.printStackTrace();
|
rgradisc@1191
|
153 |
}
|
rgradisc@1191
|
154 |
}
|
wneuper@1875
|
155 |
*/
|
wneuper@1875
|
156 |
|
wneuper@1875
|
157 |
/*
|
wneuper@1875
|
158 |
* @see IToCalc
|
rgradisc@1615
|
159 |
public void completeCalcHead(CalcHead calcHead) {
|
wneuper@1979
|
160 |
logger.debug("completeCalcHead: calcHead="+calcHead);
|
rgradisc@1615
|
161 |
CalcHead newCalcHead = null;
|
rgradisc@1304
|
162 |
try {
|
wneuper@1875
|
163 |
//WN040924 newCalcHead = bridgeRMI.completeCalcHead(calcHead);
|
wneuper@1875
|
164 |
bridgeRMI.completeCalcHead(calcHead);
|
wneuper@1875
|
165 |
//WN040924 calcHead.fillValuesfrom(newCalcHead);
|
rgradisc@1304
|
166 |
} catch (RemoteException e) {
|
rgradisc@1304
|
167 |
e.printStackTrace();
|
rgradisc@1623
|
168 |
}
|
akremp@1256
|
169 |
}
|
rgradisc@1623
|
170 |
*/
|
wneuper@1875
|
171 |
|
wneuper@1875
|
172 |
/*
|
wneuper@1875
|
173 |
* @see IToCalc
|
rgradisc@1615
|
174 |
public void completeCalcHead(CalcHead calcHead, int completeItem) {
|
rgradisc@1615
|
175 |
CalcHead newCalcHead;
|
rgradisc@1586
|
176 |
try {
|
wneuper@1875
|
177 |
//WN040924 newCalcHead = bridgeRMI.completeCalcHead(calcHead, completeItem);
|
wneuper@1875
|
178 |
bridgeRMI.completeCalcHead(calcHead, completeItem);
|
wneuper@1875
|
179 |
//WN040924 calcHead.fillValuesfrom(newCalcHead);
|
rgradisc@1635
|
180 |
//TODO: Remove the following line!!
|
rgradisc@1635
|
181 |
calcHead.setCalcHeadStatus(CalcHead.CALCHEAD_CORRECT);
|
rgradisc@1635
|
182 |
|
rgradisc@1586
|
183 |
} catch (RemoteException e) {
|
rgradisc@1586
|
184 |
e.printStackTrace();
|
rgradisc@1623
|
185 |
}
|
rgradisc@1304
|
186 |
}
|
wneuper@1875
|
187 |
*/
|
rgradisc@1191
|
188 |
|
rgradisc@1191
|
189 |
/**
|
rgradisc@1191
|
190 |
* Refine a given problem for this CalcTree
|
rgradisc@1191
|
191 |
* @param problemID
|
rgradisc@1191
|
192 |
* @return CalcHead, if the operation was successful, else null
|
rgradisc@1191
|
193 |
*/
|
rgradisc@1615
|
194 |
public void tryRefine(CalcHead calcHead, CalcHeadCompoundID problemID)
|
rgradisc@1191
|
195 |
throws NotInSpecificationPhaseException {
|
rgradisc@1623
|
196 |
CalcHead newCalcHead = null;
|
rgradisc@1191
|
197 |
try {
|
wneuper@1875
|
198 |
//WN040924 newCalcHead = bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
|
wneuper@1875
|
199 |
bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
|
rgradisc@1615
|
200 |
if (newCalcHead == null) {
|
rgradisc@1191
|
201 |
throw new NotInSpecificationPhaseException();
|
rgradisc@1191
|
202 |
}
|
rgradisc@1623
|
203 |
calcHead.fillValuesfrom(newCalcHead);
|
rgradisc@1191
|
204 |
} catch (RemoteException e) {
|
rgradisc@1191
|
205 |
// TODO Auto-generated catch block
|
rgradisc@1191
|
206 |
e.printStackTrace();
|
rgradisc@1586
|
207 |
}
|
rgradisc@1191
|
208 |
}
|
rgradisc@1191
|
209 |
|
rgradisc@1191
|
210 |
/**
|
rgradisc@1191
|
211 |
* Match a given problem for this CalcTree
|
rgradisc@1191
|
212 |
* @param problemID
|
rgradisc@1191
|
213 |
* @return CalcHead, if the operation was successful, else null
|
rgradisc@1191
|
214 |
*/
|
rgradisc@1615
|
215 |
public void tryMatch(CalcHead calcHead, CalcHeadCompoundID problemID)
|
rgradisc@1191
|
216 |
throws NotInSpecificationPhaseException {
|
rgradisc@1623
|
217 |
CalcHead newCalcHead = null;
|
rgradisc@1191
|
218 |
try {
|
wneuper@1875
|
219 |
//WN040924 newCalcHead = bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
|
wneuper@1875
|
220 |
bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
|
rgradisc@1615
|
221 |
if (newCalcHead == null) {
|
rgradisc@1191
|
222 |
throw new NotInSpecificationPhaseException();
|
rgradisc@1191
|
223 |
}
|
rgradisc@1623
|
224 |
calcHead.fillValuesfrom(newCalcHead);
|
rgradisc@1191
|
225 |
} catch (RemoteException e) {
|
rgradisc@1191
|
226 |
// TODO Auto-generated catch block
|
rgradisc@1166
|
227 |
e.printStackTrace();
|
rgradisc@1586
|
228 |
}
|
rgradisc@1035
|
229 |
}
|
rgradisc@1035
|
230 |
|
rgradisc@1048
|
231 |
// Delegated methods from CalcTree/Iterator
|
rgradisc@1048
|
232 |
int setNextTactic(int id, Tactic tactic) throws RemoteException {
|
rgradisc@1035
|
233 |
return bridgeRMI.setNextTactic(id, tactic);
|
rgradisc@1035
|
234 |
}
|
rgradisc@1035
|
235 |
|
rgradisc@1048
|
236 |
Tactic fetchProposedTactic(int id) throws RemoteException {
|
rgradisc@1035
|
237 |
return bridgeRMI.fetchProposedTactic(id);
|
rgradisc@1035
|
238 |
}
|
rgradisc@1035
|
239 |
|
rgradisc@1048
|
240 |
Tactic[] fetchAppliableTactics(int id, int scope) throws RemoteException {
|
rgradisc@1035
|
241 |
return bridgeRMI.fetchAppliableTactics(id, scope);
|
rgradisc@1035
|
242 |
}
|
rgradisc@1035
|
243 |
|
wneuper@1976
|
244 |
CalcChanged autoCalculate(int id, int scope, int nSteps) throws RemoteException {
|
wneuper@1979
|
245 |
logger.debug("autoCalculate: id="+id+", scope="+scope+", nSteps="+nSteps);
|
rgradisc@1035
|
246 |
return bridgeRMI.autoCalculate(id, scope, nSteps);
|
rgradisc@1035
|
247 |
}
|
rgradisc@1035
|
248 |
|
rgradisc@1048
|
249 |
int iterator(int id) throws RemoteException {
|
wneuper@1979
|
250 |
logger.debug("iterator: id="+id);
|
rgradisc@1035
|
251 |
return bridgeRMI.iterator(id);
|
rgradisc@1035
|
252 |
}
|
rgradisc@1035
|
253 |
|
rgradisc@1048
|
254 |
boolean moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
|
wneuper@1979
|
255 |
logger.debug("moveRoot: calcTreeID="+calcTreeID+", iteratorID="+iteratorID);
|
wneuper@1863
|
256 |
return bridgeRMI.moveRoot(calcTreeID, iteratorID);
|
rgradisc@1035
|
257 |
}
|
rgradisc@1035
|
258 |
|
rgradisc@1048
|
259 |
boolean moveUp(int calcTreeID, int iteratorID) throws RemoteException {
|
rgradisc@1035
|
260 |
return bridgeRMI.moveUp(calcTreeID, iteratorID);
|
rgradisc@1035
|
261 |
}
|
rgradisc@1035
|
262 |
|
rgradisc@1048
|
263 |
boolean moveDown(int calcTreeID, int iteratorID) throws RemoteException {
|
wneuper@1979
|
264 |
logger.debug("moveDown: calcTreeID="+calcTreeID+", iteratorID="+iteratorID);
|
rgradisc@1035
|
265 |
return bridgeRMI.moveDown(calcTreeID, iteratorID);
|
rgradisc@1035
|
266 |
}
|
rgradisc@1035
|
267 |
|
rgradisc@1048
|
268 |
boolean moveLevelUp(int calcTreeID, int iteratorID) throws RemoteException {
|
rgradisc@1035
|
269 |
return bridgeRMI.moveLevelUp(calcTreeID, iteratorID);
|
rgradisc@1035
|
270 |
}
|
rgradisc@1035
|
271 |
|
rgradisc@1586
|
272 |
boolean moveLevelDown(int calcTreeID, int iteratorID)
|
rgradisc@1586
|
273 |
throws RemoteException {
|
rgradisc@1035
|
274 |
return bridgeRMI.moveLevelDown(calcTreeID, iteratorID);
|
rgradisc@1035
|
275 |
}
|
rgradisc@1035
|
276 |
|
rgradisc@1048
|
277 |
boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
|
rgradisc@1035
|
278 |
return bridgeRMI.moveTactic(calcTreeID, iteratorID);
|
rgradisc@1035
|
279 |
}
|
rgradisc@1035
|
280 |
|
rgradisc@1048
|
281 |
boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
|
rgradisc@1035
|
282 |
return bridgeRMI.moveFormula(calcTreeID, iteratorID);
|
rgradisc@1035
|
283 |
}
|
rgradisc@1035
|
284 |
|
rgradisc@1586
|
285 |
ICalcElement getElement(int calcTreeID, int iteratorID)
|
rgradisc@1586
|
286 |
throws RemoteException {
|
rgradisc@1035
|
287 |
return bridgeRMI.getElement(calcTreeID, iteratorID);
|
rgradisc@1035
|
288 |
}
|
rgradisc@1304
|
289 |
|
rgradisc@1202
|
290 |
/**
|
rgradisc@1202
|
291 |
* Destruct a calcTree: Gives the memory occupied by this calcTree free again.
|
rgradisc@1202
|
292 |
* Call this method when the calcTree is no longer needed. This method is irreversible.
|
rgradisc@1202
|
293 |
* @param calcTreeID id of the calcTree to be destructed
|
rgradisc@1202
|
294 |
*/
|
rgradisc@1304
|
295 |
public boolean destruct(int calcTreeID) throws RemoteException {
|
rgradisc@1304
|
296 |
return bridgeRMI.destruct(calcTreeID);
|
rgradisc@1198
|
297 |
}
|
rgradisc@1304
|
298 |
|
rgradisc@1202
|
299 |
/**
|
rgradisc@1202
|
300 |
* Save a calcTree
|
rgradisc@1202
|
301 |
* @param calcTreeID id of the calcTree to be saved
|
rgradisc@1202
|
302 |
* @return a vector containing strings, representing the status of the calcTree
|
rgradisc@1202
|
303 |
*/
|
rgradisc@1198
|
304 |
public Vector saveCalcTree(int calcTreeID) {
|
rgradisc@1198
|
305 |
try {
|
rgradisc@1198
|
306 |
return bridgeRMI.saveCalcTree(calcTreeID);
|
rgradisc@1198
|
307 |
} catch (RemoteException e) {
|
rgradisc@1198
|
308 |
e.printStackTrace();
|
rgradisc@1198
|
309 |
}
|
rgradisc@1198
|
310 |
return null;
|
rgradisc@1198
|
311 |
}
|
rgradisc@1198
|
312 |
|
rgradisc@1202
|
313 |
/**
|
rgradisc@1224
|
314 |
* Load a previously stored calcTree
|
rgradisc@1202
|
315 |
* @param v Vector: the return value of saveCalcTree
|
rgradisc@1202
|
316 |
* @return a calcTree reference
|
rgradisc@1202
|
317 |
*/
|
rgradisc@1304
|
318 |
public CalcTree loadCalcTree(Vector v) {
|
rgradisc@1198
|
319 |
try {
|
rgradisc@1198
|
320 |
int id = bridgeRMI.loadCalcTree(v);
|
rgradisc@1198
|
321 |
CalcTree calcTree = new CalcTree(this, id);
|
rgradisc@1198
|
322 |
calcTrees.put(new Integer(id), calcTree);
|
rgradisc@1586
|
323 |
ICalcIterator hotSpot = calcTree.iterator();
|
rgradisc@1586
|
324 |
// may be not first iterator (not hotspot)
|
rgradisc@1586
|
325 |
((CalcIterator) hotSpot).makeHotSpot();
|
rgradisc@1586
|
326 |
// make sure it is a hotspot iterator
|
rgradisc@1304
|
327 |
calcTree.setHotSpot(hotSpot);
|
rgradisc@1198
|
328 |
hotSpot.moveRoot();
|
rgradisc@1304
|
329 |
return calcTree;
|
rgradisc@1198
|
330 |
} catch (RemoteException e) {
|
rgradisc@1198
|
331 |
e.printStackTrace();
|
rgradisc@1304
|
332 |
}
|
rgradisc@1198
|
333 |
return null;
|
rgradisc@1139
|
334 |
}
|
rgradisc@1027
|
335 |
}
|