java with part. login-triggered startCalculation
2 * Created on Oct 20, 2003
6 import isac.util.CalcChanged;
7 import isac.util.Formalization;
8 import isac.util.NotInSpecificationPhaseException;
9 import isac.util.formulae.Assumptions;
10 import isac.util.formulae.CalcHead;
11 import isac.util.formulae.CalcHeadCompoundID;
12 import isac.util.formulae.CalcFormula;
13 import isac.util.formulae.Position;
14 import isac.util.interfaces.ICalcElement;
15 import isac.util.interfaces.ICalcIterator;
16 import isac.util.tactics.Tactic;
18 import java.io.Serializable;
19 import java.rmi.Naming;
20 import java.rmi.RemoteException;
21 import java.util.HashMap;
23 import java.util.Vector;
25 import org.apache.log4j.Logger;
28 * Class MathEngine: This class is called by the Dialog and communicates with
29 * the Bridge via RMI. It also delegates the methods of the CalcTree and the
30 * CalcIterator to the Bridge.
34 public class MathEngine implements Serializable {
36 private static MathEngine singleton;
38 private IBridgeRMI bridgeRMI;
40 // Stores the calcTrees after call of startSpecifying //WN00827
42 // and hands them out to the dialog after startSolving//WN00827 ???
43 private Map calcTrees;
45 static Logger logger = Logger.getLogger(MathEngine.class.getName());
47 // This is a Singleton Class: A private constructor is needed
48 private MathEngine(String hostName) {
49 logger.debug("MathEngine(" + hostName + ")");
52 bridgeRMI = (IBridgeRMI) Naming.lookup("//" + hostName
54 System.out.println("MathEngine Constr: connected to Bridge: "
55 + bridgeRMI.toString());
56 if (bridgeRMI == null)
58 .println("Error occured: The Bridge could not be found!");
59 } catch (Exception e) {
61 .println("Could not connect to Bridge via RMI\n"
62 + "Please make sure that the Bridge is running and connected");
65 calcTrees = new HashMap();
69 * FIXME.WN040924 just to make the old bridge continue work
71 protected IBridgeRMI getBridgeRMI() {
76 * Initialize the MathEngine
79 * Host name on which the Bridge is running
81 public static void init(String hostName) {
82 logger.fatal(" DG->BR: init(" + hostName + ")");
83 if (singleton == null) {
84 singleton = new MathEngine(hostName);
88 public static MathEngine getMathEngine() {
89 logger.fatal(" DG<>BR: getMathEngine()");
94 * Start a new calculation
97 * Formalization of the new calculation, from expl
98 * @return CalcHead: empty for startSpecifying
99 * @throws RemoteException
102 * int id = calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead
103 * CalcTree calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922
106 public CalcTree startCalculation(Formalization f) {
107 logger.fatal(" DG->BR: startCalculation(" + f.toSMLString() + ")");
108 CalcTree calcTree = null;//WN
110 int id = bridgeRMI.startCalculation(f);
111 calcTree = new CalcTree(this, id);
112 calcTrees.put(new Integer(id), calcTree);
114 ICalcIterator hotSpot = calcTree.iterator();
115 calcTree.setHotSpot(hotSpot); //first iterator marks the hotSpot
116 //hotSpot.moveRoot();
118 } catch (RemoteException e) {
121 logger.fatal(" DG<-BR: startCalculation <- calcTree=" + calcTree);
126 * boolean autoCalculate(int id, int scope, int nSteps) throws
127 * RemoteException { logger.debug("autoCalculate: id="+id+",
128 * scope="+scope+", nSteps="+nSteps); return bridgeRMI.autoCalculate(id,
133 * @see IToCalc#startSolving(), copied to CalcTree
135 * public void startSolving(CalcHead calcHead) throws Exception {
136 * logger.debug("startSolving: calcHead="+calcHead); int id =
137 * calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead CalcTree
138 * calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922 TODO
141 * if (calcHead.getCalcHeadStatus() != CalcHead.MODEL_ITEM_CORRECT) { throw
142 * new Exception("Startsolving with incorrect CalcHead"); } Tactic t =
143 * calcTree.fetchProposedTactic(); if (t.getName().compareTo("Apply_Method") !=
144 * 0) { throw new Exception("Startsolving fetches " + t.getName()); } //Next
145 * Tactic must always be Apply_Method calcTree.setNextTactic(t); //return
150 * @see IToCalc public void modifyCalcHead(CalcHead calcHead) {
151 * logger.debug("modifyCalcHead: calcHead="+calcHead); CalcHead
152 * newCalcHead = null; try { newCalcHead =
153 * bridgeRMI.modifyCalcHead(calcHead);
154 * calcHead.fillValuesfrom(newCalcHead); } catch (RemoteException e) {
155 * e.printStackTrace(); } }
159 * @see IToCalc public void completeCalcHead(CalcHead calcHead) {
160 * logger.debug("completeCalcHead: calcHead="+calcHead); CalcHead
161 * newCalcHead = null; try { //WN040924 newCalcHead =
162 * bridgeRMI.completeCalcHead(calcHead);
163 * bridgeRMI.completeCalcHead(calcHead); //WN040924
164 * calcHead.fillValuesfrom(newCalcHead); } catch (RemoteException e) {
165 * e.printStackTrace(); } }
169 * @see IToCalc public void completeCalcHead(CalcHead calcHead, int
170 * completeItem) { CalcHead newCalcHead; try { //WN040924 newCalcHead =
171 * bridgeRMI.completeCalcHead(calcHead, completeItem);
172 * bridgeRMI.completeCalcHead(calcHead, completeItem); //WN040924
173 * calcHead.fillValuesfrom(newCalcHead); //TODO: Remove the following
174 * line!! calcHead.setCalcHeadStatus(CalcHead.MODEL_ITEM_CORRECT); }
175 * catch (RemoteException e) { e.printStackTrace(); } }
179 * Refine a given problem for this CalcTree
182 * @return CalcHead, if the operation was successful, else null
184 public void tryRefine(CalcHead calcHead, CalcHeadCompoundID problemID)
185 throws NotInSpecificationPhaseException {
186 CalcHead newCalcHead = null;
188 //WN040924 newCalcHead =
189 // bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
190 bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
191 if (newCalcHead == null) {
192 throw new NotInSpecificationPhaseException();
194 calcHead.fillValuesfrom(newCalcHead);
195 } catch (RemoteException e) {
196 // TODO Auto-generated catch block
202 * Match a given problem for this CalcTree
205 * @return CalcHead, if the operation was successful, else null
207 public void tryMatch(CalcHead calcHead, CalcHeadCompoundID problemID)
208 throws NotInSpecificationPhaseException {
209 CalcHead newCalcHead = null;
211 //WN040924 newCalcHead =
212 // bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
213 bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
214 if (newCalcHead == null) {
215 throw new NotInSpecificationPhaseException();
217 calcHead.fillValuesfrom(newCalcHead);
218 } catch (RemoteException e) {
219 // TODO Auto-generated catch block
224 // Delegated methods from CalcTree/Iterator
225 int setNextTactic(int id, Tactic tactic) throws RemoteException {
226 return bridgeRMI.setNextTactic(id, tactic);
229 Tactic fetchProposedTactic(int id) throws RemoteException {
230 return bridgeRMI.fetchProposedTactic(id);
233 CalcChanged autoCalculate(int id, int scope, int nSteps)
234 throws RemoteException {
235 logger.debug("autoCalculate: id=" + id + ", scope=" + scope
236 + ", nSteps=" + nSteps);
237 return bridgeRMI.autoCalculate(id, scope, nSteps);
240 CalcChanged replaceFormula(int id, CalcFormula f) throws RemoteException {
241 return bridgeRMI.replaceFormula(id, f);
244 CalcChanged appendFormula(int id, CalcFormula f) throws RemoteException {
245 return bridgeRMI.appendFormula(id, f);
248 CalcChanged intermediateSteps(int id, ICalcIterator ci)
249 throws RemoteException {
250 return bridgeRMI.intermediateSteps(id, ci);
253 Vector getElementsFromTo(int id, ICalcIterator iterator_from,
254 ICalcIterator iterator_to, Integer level,
255 boolean result_includes_tactics) throws RemoteException {
256 return bridgeRMI.getElementsFromTo(id, iterator_from, iterator_to,
257 level, result_includes_tactics);
260 Position moveActiveFormula(int calcTreeID, Position p)
261 throws RemoteException {
262 return bridgeRMI.moveActiveFormula(calcTreeID, p);
265 int iterator(int id) throws RemoteException {
266 logger.debug("iterator: id=" + id);
267 return bridgeRMI.iterator(id);
270 Position moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
271 logger.debug("moveRoot: calcTreeID=" + calcTreeID + ", iteratorID="
273 return bridgeRMI.moveRoot(calcTreeID, iteratorID);
276 Position moveUp(int calcTreeID, int iteratorID, Position p)
277 throws RemoteException {
278 return bridgeRMI.moveUp(calcTreeID, iteratorID, p);
281 Position moveDown(int calcTreeID, int iteratorID, Position p)
282 throws RemoteException {
283 logger.debug("moveDown: calcTreeID=" + calcTreeID + ", sml_pos_=" + p);
284 return bridgeRMI.moveDown(calcTreeID, iteratorID, p);
287 Position moveLevelUp(int calcTreeID, int iteratorID, Position p)
288 throws RemoteException {
289 return bridgeRMI.moveLevelUp(calcTreeID, iteratorID, p);
292 Position moveLevelDown(int calcTreeID, int iteratorID, Position p)
293 throws RemoteException {
294 return bridgeRMI.moveLevelDown(calcTreeID, iteratorID, p);
297 Position moveCalcHead(int calcTreeID, int iteratorID, Position p)
298 throws RemoteException {
299 return bridgeRMI.moveCalcHead(calcTreeID, iteratorID, p);
302 boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
303 return bridgeRMI.moveTactic(calcTreeID, iteratorID);
306 boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
307 return bridgeRMI.moveFormula(calcTreeID, iteratorID);
310 ICalcElement getElement(int calcTreeID, Position p) throws RemoteException {
311 return bridgeRMI.getElement(calcTreeID, p);
315 * Destruct a calcTree: Gives the memory occupied by this calcTree free
316 * again. Call this method when the calcTree is no longer needed. This
317 * method is irreversible.
320 * id of the calcTree to be destructed
322 public boolean destruct(int calcTreeID) throws RemoteException {
323 return bridgeRMI.destruct(calcTreeID);
330 * id of the calcTree to be saved
331 * @return a vector containing strings, representing the status of the
334 public Vector saveCalcTree(int calcTreeID) {
336 return bridgeRMI.saveCalcTree(calcTreeID);
337 } catch (RemoteException e) {
344 * Load a previously stored calcTree
347 * Vector: the return value of saveCalcTree
348 * @return a calcTree reference
350 public CalcTree loadCalcTree(Vector v) {
352 int id = bridgeRMI.loadCalcTree(v);
353 CalcTree calcTree = new CalcTree(this, id);
354 calcTrees.put(new Integer(id), calcTree);
355 ICalcIterator hotSpot = calcTree.iterator();
356 // may be not first iterator (not hotspot)
357 ((CalcIterator) hotSpot).makeHotSpot();
358 // make sure it is a hotspot iterator
359 calcTree.setHotSpot(hotSpot);
362 } catch (RemoteException e) {
369 * @see isac.util.interfaces.ICalcIterator#getTactic()
371 public Tactic getTactic(int id, Position pos) throws RemoteException {
372 return bridgeRMI.getTactic(id, pos);
375 public Vector getApplicableTactics(int calcTreeID, int scope, Position pos)
376 throws RemoteException {
377 return bridgeRMI.getAppliableTactics(calcTreeID, scope, pos);
381 * @see isac.util.interfaces.ICalcIterator#getAssumptions()
383 public Assumptions getAssumptions(int id, Position pos) throws RemoteException {
384 return bridgeRMI.getAssumptions(id, pos);
388 * @see isac.util.interfaces.ICalcIterator#getAssumptions()
390 public Assumptions getAccumulatedAssumptions(int id, Position pos) throws RemoteException {
391 return bridgeRMI.getAccumulatedAssumptions(id, pos);