2 * Created on Oct 20, 2003
6 import isac.util.CalcElement;
7 import isac.util.CalcHead;
8 import isac.util.CalcHeadCompoundID;
9 import isac.util.Formalization;
10 import isac.util.ICalcIterator;
11 import isac.util.NotInSpecificationPhaseException;
12 import isac.util.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;
24 public class MathEngine implements Serializable {
26 private static MathEngine singleton;
28 private IBridgeRMI bridgeRMI;
30 private Map calcTrees;
32 // Singleton Class: private constructor
33 private MathEngine(String hostName) {
36 bridgeRMI = (IBridgeRMI)Naming.lookup("//" + hostName + "/BridgeRMI");
37 System.out.println("MathEngine Constr: connected to Bridge: " + bridgeRMI.toString());
38 if (bridgeRMI == null)
39 System.out.println("Bridge null!");
40 } catch (Exception e) {
42 "Could not connect to Bridge via RMI\n"
43 + "Please make sure that the Bridge is running and connected");
46 calcTrees = new HashMap();
47 //states:= []; // only for testing: reset states for every new CalcTree
50 public static void init(String hostName) {
51 singleton = new MathEngine(hostName);
54 public static MathEngine getMathEngine() {
59 * Start a new calculation
60 * @param f Formalization of the new calculation
61 * @return CalcHead: Contains Specification part of Formalization
62 * @throws RemoteException
64 public CalcHead startSpecifying(Formalization f) {
65 CalcHead calcHead = null;
67 int id = bridgeRMI.startSpecifying(f);
68 CalcTree calcTree = new CalcTree(this, id);
69 calcTrees.put(new Integer(id), calcTree);
70 ICalcIterator hotSpot = calcTree.iterator();
71 calcTree.setHotSpot(hotSpot); //first iterator marks the hotSpot
73 calcHead = (CalcHead)hotSpot.getElement();
74 // calcHead.setCalcTreeID(javaCalcID);
75 } catch (RemoteException e) {
82 * Enter the solving phase of the calculation.
83 * StartSpecifying must be called first
84 * Only possible with a complete and correct calcHead
85 * @param calcHead CalcHead for this calculation
86 * @return CalcTree represents the calculation on the java side
88 public CalcTree startSolving(CalcHead calcHead) {
89 /*CalcTree calcTree = null;
90 System.out.println("MathEngine.startSolving: start");
92 int id = bridgeRMI.startSolving(calcHead);
93 calcTree = new CalcTree(this, id);
94 } catch (RemoteException e) {
97 System.out.println("MathEngine.startSolving: end");
99 int id = calcHead.getCalcTreeID();
100 CalcTree calcTree = (CalcTree)calcTrees.get(new Integer(id));
105 * Modify a given CalcHead
106 * The result is the same CalcHead with status flags for each item
107 * @see CalcHead for possible status flags
108 * @param calcHead CalcHead to be modified
109 * @return new CalcHead with a status for each item
111 public CalcHead modifyCalcHead(CalcHead calcHead) {
113 return bridgeRMI.modifyCalcHead(calcHead);
114 } catch (RemoteException e) {
121 * Refine a given problem for this CalcTree
123 * @return CalcHead, if the operation was successful, else null
125 public CalcHead tryRefine(CalcHead owner, CalcHeadCompoundID problemID)
126 throws NotInSpecificationPhaseException {
129 CalcHead result = bridgeRMI.tryRefine(owner.getCalcTreeID(), problemID);
130 if (result == null) {
131 throw new NotInSpecificationPhaseException();
134 } catch (RemoteException e) {
135 // TODO Auto-generated catch block
142 * Match a given problem for this CalcTree
144 * @return CalcHead, if the operation was successful, else null
146 public CalcHead tryMatch(CalcHead owner, CalcHeadCompoundID problemID)
147 throws NotInSpecificationPhaseException {
149 CalcHead result = bridgeRMI.tryMatch(owner.getCalcTreeID(), problemID);
150 if (result == null) {
151 throw new NotInSpecificationPhaseException();
154 } catch (RemoteException e) {
155 // TODO Auto-generated catch block
161 // Delegated methods from CalcTree/Iterator
162 int setNextTactic(int id, Tactic tactic) throws RemoteException {
163 return bridgeRMI.setNextTactic(id, tactic);
166 Tactic fetchProposedTactic(int id) throws RemoteException {
167 return bridgeRMI.fetchProposedTactic(id);
170 Tactic[] fetchAppliableTactics(int id, int scope) throws RemoteException {
171 return bridgeRMI.fetchAppliableTactics(id, scope);
174 int autoCalculate(int id, int scope, int nSteps) throws RemoteException {
175 return bridgeRMI.autoCalculate(id, scope, nSteps);
178 int iterator(int id) throws RemoteException {
179 return bridgeRMI.iterator(id);
182 boolean moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
183 return bridgeRMI.moveRoot(calcTreeID, iteratorID);
186 boolean moveUp(int calcTreeID, int iteratorID) throws RemoteException {
187 return bridgeRMI.moveUp(calcTreeID, iteratorID);
190 boolean moveDown(int calcTreeID, int iteratorID) throws RemoteException {
191 return bridgeRMI.moveDown(calcTreeID, iteratorID);
194 boolean moveLevelUp(int calcTreeID, int iteratorID) throws RemoteException {
195 return bridgeRMI.moveLevelUp(calcTreeID, iteratorID);
198 boolean moveLevelDown(int calcTreeID, int iteratorID) throws RemoteException {
199 return bridgeRMI.moveLevelDown(calcTreeID, iteratorID);
202 boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
203 return bridgeRMI.moveTactic(calcTreeID, iteratorID);
206 boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
207 return bridgeRMI.moveFormula(calcTreeID, iteratorID);
210 CalcElement getElement(int calcTreeID, int iteratorID) throws RemoteException {
211 return bridgeRMI.getElement(calcTreeID, iteratorID);
214 public CalcHead tryRefine(int calcTreeID, CalcHeadCompoundID problemID) throws RemoteException {
215 return bridgeRMI.tryRefine(calcTreeID, problemID);
218 public CalcHead tryMatch(int calcTreeID, CalcHeadCompoundID problemID) throws RemoteException {
219 return bridgeRMI.tryMatch(calcTreeID, problemID);
224 * Destruct a calcTree: Gives the memory occupied by this calcTree free again.
225 * Call this method when the calcTree is no longer needed. This method is irreversible.
226 * @param calcTreeID id of the calcTree to be destructed
228 public boolean destruct(int calcTreeID) throws RemoteException {
229 return bridgeRMI.destruct(calcTreeID);
234 * @param calcTreeID id of the calcTree to be saved
235 * @return a vector containing strings, representing the status of the calcTree
237 public Vector saveCalcTree(int calcTreeID) {
239 return bridgeRMI.saveCalcTree(calcTreeID);
240 } catch (RemoteException e) {
247 * Load a stored calcTree
248 * @param v Vector: the return value of saveCalcTree
249 * @return a calcTree reference
251 public CalcTree loadCalcTree(Vector v) {
253 int id = bridgeRMI.loadCalcTree(v);
254 CalcTree calcTree = new CalcTree(this, id);
255 calcTrees.put(new Integer(id), calcTree);
256 ICalcIterator hotSpot = calcTree.iterator(); // may be not first iterator (not hotspot)
257 ((CalcIterator)hotSpot).makeHotSpot(); // make sure it is a hotspot iterator
258 calcTree.setHotSpot(hotSpot);
261 } catch (RemoteException e) {