2 * Created on Oct 20, 2003
6 import isac.util.Formalization;
7 import isac.util.NotInSpecificationPhaseException;
8 import isac.util.formulae.CalcHead;
9 import isac.util.formulae.CalcHeadCompoundID;
10 import isac.util.interfaces.ICalcElement;
11 import isac.util.interfaces.ICalcIterator;
12 import isac.util.tactics.Tactic;
14 import java.io.Serializable;
15 import java.rmi.Naming;
16 import java.rmi.RemoteException;
17 import java.util.HashMap;
19 import java.util.Vector;
21 import org.apache.log4j.Logger;
24 * Class MathEngine: This class is called by the Dialog
25 * and communicates with the Bridge via RMI.
26 * It also delegates the methods of the CalcTree and the
27 * CalcIterator to the Bridge.
30 public class MathEngine implements Serializable {
32 private static MathEngine singleton;
34 private IBridgeRMI bridgeRMI;
36 // Stores the calcTrees after call of startSpecifying //WN00827 startCalculation
37 // and hands them out to the dialog after startSolving//WN00827 ???
38 private Map calcTrees;
40 static Logger logger = Logger.getLogger(MathEngine.class.getName());
42 // This is a Singleton Class: A private constructor is needed
43 private MathEngine(String hostName) {
44 logger.fatal("MathEngine("+hostName+")");
47 bridgeRMI = (IBridgeRMI) Naming.lookup("//" + hostName + "/BridgeRMI");
49 "MathEngine Constr: connected to Bridge: " + bridgeRMI.toString());
50 if (bridgeRMI == null)
51 System.out.println("Error occured: The Bridge could not be found!");
52 } catch (Exception e) {
54 "Could not connect to Bridge via RMI\n"
55 + "Please make sure that the Bridge is running and connected");
58 calcTrees = new HashMap();
62 * FIXME.WN040924 just to make the old bridge continue work
64 protected IBridgeRMI getBridgeRMI() {
68 * Initialize the MathEngine
69 * @param hostName: Host name on which the Bridge is running
71 public static void init(String hostName) {
72 if (singleton == null) {
73 singleton = new MathEngine(hostName);
77 public static MathEngine getMathEngine() {
78 logger.fatal("getMathEngine()");
83 * Start a new calculation
84 * @param f Formalization of the new calculation, from expl
85 * @return CalcHead: empty for startSpecifying
86 * @throws RemoteException WN040924 ???
88 * int id = calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead
89 * CalcTree calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922 TODO simplify
91 public CalcTree startCalculation(Formalization f) {
92 logger.fatal("startCalculation("+f.toSMLString()+")");
93 //CalcHead calcHead = null;
94 CalcTree calcTree = null;//WN
95 ICalcIterator hotSpot = null;//WN
97 int id = bridgeRMI.startCalculation(f);
98 calcTree = new CalcTree(this, id);
99 calcTrees.put(new Integer(id), calcTree);
101 hotSpot = calcTree.iterator();
102 calcTree.setHotSpot(hotSpot); //first iterator marks the hotSpot
104 //---------------------------------------------- startSpecifying
105 //WN calcHead = (CalcHead) hotSpot.getElement();
106 //WN System.out.println("Calchead: " + calcHead.toSMLString());
107 } catch (RemoteException e) {
114 boolean autoCalculate(int id, int scope, int nSteps) throws RemoteException {
115 logger.warn("autoCalculate: id="+id+", scope="+scope+", nSteps="+nSteps);
116 return bridgeRMI.autoCalculate(id, scope, nSteps);
121 * @see IToCalc#startSolving(), copied to CalcTree
123 public void startSolving(CalcHead calcHead) throws Exception {
124 logger.warn("startSolving: calcHead="+calcHead);
125 int id = calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead
126 CalcTree calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922 TODO simplify
128 if (calcHead.getCalcHeadStatus() != CalcHead.CALCHEAD_CORRECT) {
129 throw new Exception("Startsolving with incorrect CalcHead");
131 Tactic t = calcTree.fetchProposedTactic();
132 if (t.getName().compareTo("Apply_Method") != 0) {
133 throw new Exception("Startsolving fetches " + t.getName());
135 //Next Tactic must always be Apply_Method
136 calcTree.setNextTactic(t);
143 public void modifyCalcHead(CalcHead calcHead) {
144 logger.warn("modifyCalcHead: calcHead="+calcHead);
145 CalcHead newCalcHead = null;
147 newCalcHead = bridgeRMI.modifyCalcHead(calcHead);
148 calcHead.fillValuesfrom(newCalcHead);
149 } catch (RemoteException e) {
157 public void completeCalcHead(CalcHead calcHead) {
158 logger.warn("completeCalcHead: calcHead="+calcHead);
159 CalcHead newCalcHead = null;
161 //WN040924 newCalcHead = bridgeRMI.completeCalcHead(calcHead);
162 bridgeRMI.completeCalcHead(calcHead);
163 //WN040924 calcHead.fillValuesfrom(newCalcHead);
164 } catch (RemoteException e) {
172 public void completeCalcHead(CalcHead calcHead, int completeItem) {
173 CalcHead newCalcHead;
175 //WN040924 newCalcHead = bridgeRMI.completeCalcHead(calcHead, completeItem);
176 bridgeRMI.completeCalcHead(calcHead, completeItem);
177 //WN040924 calcHead.fillValuesfrom(newCalcHead);
178 //TODO: Remove the following line!!
179 calcHead.setCalcHeadStatus(CalcHead.CALCHEAD_CORRECT);
181 } catch (RemoteException e) {
188 * Refine a given problem for this CalcTree
190 * @return CalcHead, if the operation was successful, else null
192 public void tryRefine(CalcHead calcHead, CalcHeadCompoundID problemID)
193 throws NotInSpecificationPhaseException {
194 CalcHead newCalcHead = null;
196 //WN040924 newCalcHead = bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
197 bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
198 if (newCalcHead == null) {
199 throw new NotInSpecificationPhaseException();
201 calcHead.fillValuesfrom(newCalcHead);
202 } catch (RemoteException e) {
203 // TODO Auto-generated catch block
209 * Match a given problem for this CalcTree
211 * @return CalcHead, if the operation was successful, else null
213 public void tryMatch(CalcHead calcHead, CalcHeadCompoundID problemID)
214 throws NotInSpecificationPhaseException {
215 CalcHead newCalcHead = null;
217 //WN040924 newCalcHead = bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
218 bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
219 if (newCalcHead == null) {
220 throw new NotInSpecificationPhaseException();
222 calcHead.fillValuesfrom(newCalcHead);
223 } catch (RemoteException e) {
224 // TODO Auto-generated catch block
229 // Delegated methods from CalcTree/Iterator
230 int setNextTactic(int id, Tactic tactic) throws RemoteException {
231 return bridgeRMI.setNextTactic(id, tactic);
234 Tactic fetchProposedTactic(int id) throws RemoteException {
235 return bridgeRMI.fetchProposedTactic(id);
238 Tactic[] fetchAppliableTactics(int id, int scope) throws RemoteException {
239 return bridgeRMI.fetchAppliableTactics(id, scope);
242 boolean autoCalculate(int id, int scope, int nSteps) throws RemoteException {
243 logger.warn("autoCalculate: id="+id+", scope="+scope+", nSteps="+nSteps);
244 return bridgeRMI.autoCalculate(id, scope, nSteps);
247 int iterator(int id) throws RemoteException {
248 logger.warn("iterator: id="+id);
249 return bridgeRMI.iterator(id);
252 boolean moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
253 logger.warn("moveRoot: calcTreeID="+calcTreeID+", iteratorID="+iteratorID);
254 return bridgeRMI.moveRoot(calcTreeID, iteratorID);
257 boolean moveUp(int calcTreeID, int iteratorID) throws RemoteException {
258 return bridgeRMI.moveUp(calcTreeID, iteratorID);
261 boolean moveDown(int calcTreeID, int iteratorID) throws RemoteException {
262 logger.warn("moveDown: calcTreeID="+calcTreeID+", iteratorID="+iteratorID);
263 return bridgeRMI.moveDown(calcTreeID, iteratorID);
266 boolean moveLevelUp(int calcTreeID, int iteratorID) throws RemoteException {
267 return bridgeRMI.moveLevelUp(calcTreeID, iteratorID);
270 boolean moveLevelDown(int calcTreeID, int iteratorID)
271 throws RemoteException {
272 return bridgeRMI.moveLevelDown(calcTreeID, iteratorID);
275 boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
276 return bridgeRMI.moveTactic(calcTreeID, iteratorID);
279 boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
280 return bridgeRMI.moveFormula(calcTreeID, iteratorID);
283 ICalcElement getElement(int calcTreeID, int iteratorID)
284 throws RemoteException {
285 return bridgeRMI.getElement(calcTreeID, iteratorID);
289 * Destruct a calcTree: Gives the memory occupied by this calcTree free again.
290 * Call this method when the calcTree is no longer needed. This method is irreversible.
291 * @param calcTreeID id of the calcTree to be destructed
293 public boolean destruct(int calcTreeID) throws RemoteException {
294 return bridgeRMI.destruct(calcTreeID);
299 * @param calcTreeID id of the calcTree to be saved
300 * @return a vector containing strings, representing the status of the calcTree
302 public Vector saveCalcTree(int calcTreeID) {
304 return bridgeRMI.saveCalcTree(calcTreeID);
305 } catch (RemoteException e) {
312 * Load a previously stored calcTree
313 * @param v Vector: the return value of saveCalcTree
314 * @return a calcTree reference
316 public CalcTree loadCalcTree(Vector v) {
318 int id = bridgeRMI.loadCalcTree(v);
319 CalcTree calcTree = new CalcTree(this, id);
320 calcTrees.put(new Integer(id), calcTree);
321 ICalcIterator hotSpot = calcTree.iterator();
322 // may be not first iterator (not hotspot)
323 ((CalcIterator) hotSpot).makeHotSpot();
324 // make sure it is a hotspot iterator
325 calcTree.setHotSpot(hotSpot);
328 } catch (RemoteException e) {