rgradisc@1027: /* rgradisc@1027: * Created on Oct 20, 2003 rgradisc@1027: */ rgradisc@1027: package isac.bridge; rgradisc@1027: wneuper@1976: import isac.util.CalcChanged; rgradisc@1088: import isac.util.Formalization; wneuper@2106: import isac.util.formulae.Assumptions; wneuper@2051: import isac.util.formulae.CalcFormula; wneuper@2526: import isac.util.formulae.KEStoreID; wneuper@2526: import isac.util.formulae.Match; wneuper@1989: import isac.util.formulae.Position; wneuper@2526: import isac.util.formulae.ProblemID; rgradisc@1311: import isac.util.interfaces.ICalcElement; rgradisc@1311: import isac.util.interfaces.ICalcIterator; rgradisc@1311: import isac.util.tactics.Tactic; rgradisc@1035: rgradisc@1035: import java.io.Serializable; rgradisc@1035: import java.rmi.Naming; rgradisc@1035: import java.rmi.RemoteException; rgradisc@1191: import java.util.HashMap; rgradisc@1191: import java.util.Map; rgradisc@1198: import java.util.Vector; rgradisc@1035: wneuper@1863: import org.apache.log4j.Logger; wneuper@1863: rgradisc@1027: /** wneuper@2065: * Class MathEngine: This class is called by the Dialog and communicates with wneuper@2065: * the Bridge via RMI. It also delegates the methods of the CalcTree and the wneuper@2065: * CalcIterator to the Bridge. wneuper@2065: * rgradisc@1027: * @author rgradisc rgradisc@1027: */ rgradisc@1035: public class MathEngine implements Serializable { rgradisc@1027: mlang@2450: private static MathEngine singleton_; rgradisc@1036: mlang@2450: private IBridgeRMI bridge_rmi_; rgradisc@1635: mlang@2450: // Stores the calc_trees_ after call of startSpecifying //WN00827 mlang@2450: // startCalculation mlang@2450: // and hands them out to the dialog after startSolving//WN00827 ??? mlang@2450: private Map calc_trees_; rgradisc@1191: mlang@2544: static Logger logger_ = Logger.getLogger(MathEngine.class.getName()); wneuper@1863: mlang@2450: // This is a Singleton Class: A private constructor is needed mlang@2450: private MathEngine(String hostName) { mlang@2544: logger_.debug("MathEngine(" + hostName + ")"); mlang@2450: try { mlang@2450: //connect to bridge mlang@2450: bridge_rmi_ = (IBridgeRMI) Naming.lookup("//" + hostName mlang@2450: + "/BridgeRMI"); mlang@2450: System.out.println("MathEngine Constr: connected to Bridge: " mlang@2450: + bridge_rmi_.toString()); mlang@2450: if (bridge_rmi_ == null) wneuper@2526: System.out wneuper@2526: .println("Error occured: The Bridge could not be found!"); mlang@2450: } catch (Exception e) { mlang@2450: System.out mlang@2450: .println("Could not connect to Bridge via RMI\n" mlang@2450: + "Please make sure that the Bridge is running and connected"); mlang@2450: e.printStackTrace(); mlang@2450: } mlang@2450: calc_trees_ = new HashMap(); mlang@2450: } rgradisc@1035: mlang@2450: /* mlang@2450: * FIXME.WN040924 just to make the old bridge continue work mlang@2450: */ mlang@2450: protected IBridgeRMI getBridgeRMI() { mlang@2450: return bridge_rmi_; mlang@2450: } rgradisc@1048: mlang@2450: /** mlang@2450: * Initialize the MathEngine mlang@2450: * mlang@2450: * @param hostName: mlang@2450: * Host name on which the Bridge is running mlang@2450: */ mlang@2450: public static void init(String hostName) { mlang@2544: logger_.fatal(" DG->BR: init(" + hostName + ")"); mlang@2450: if (singleton_ == null) { mlang@2450: singleton_ = new MathEngine(hostName); mlang@2450: } mlang@2450: } rgradisc@1036: mlang@2450: public static MathEngine getMathEngine() { mlang@2544: logger_.fatal(" DG<>BR: getMathEngine()"); mlang@2450: return singleton_; mlang@2450: } rgradisc@1586: mlang@2450: /** mlang@2450: * Start a new calculation mlang@2450: * mlang@2450: * @param f mlang@2450: * Formalization of the new calculation, from expl mlang@2450: * @return CalcHead: empty for startSpecifying mlang@2450: * @throws RemoteException mlang@2450: * WN040924 ??? mlang@2450: * mlang@2450: * int id = calc_head_.getCalcTreeID();//WN040922 TODO simplify CalcHead mlang@2450: * CalcTree calcTree = (CalcTree) calc_trees_.get(new mlang@2450: * Integer(id));//WN040922 TODO simplify mlang@2450: */ mlang@2450: public CalcTree startCalculation(Formalization f) { mlang@2544: logger_.fatal(" DG->BR: startCalculation(" + f.toSMLString() + ")"); mlang@2450: CalcTree calcTree = null;//WN mlang@2450: try { mlang@2450: int id = bridge_rmi_.startCalculation(f); mlang@2450: calcTree = new CalcTree(this, id); mlang@2450: calc_trees_.put(new Integer(id), calcTree); rgradisc@1166: mlang@2450: ICalcIterator hotSpot = calcTree.iterator(); mlang@2450: calcTree.setHotSpot(hotSpot); //first iterator marks the hotSpot mlang@2450: //hotSpot.moveRoot(); mlang@2450: int i = 111; mlang@2450: } catch (RemoteException e) { mlang@2450: e.printStackTrace(); mlang@2450: } mlang@2544: logger_.fatal(" DG<-BR: startCalculation <- calcTree=" + calcTree); mlang@2450: return calcTree; mlang@2450: } rgradisc@1623: mlang@2450: /* mlang@2450: * boolean autoCalculate(int id, int scope, int nSteps) throws mlang@2450: * RemoteException { logger.debug("autoCalculate: id="+id+", mlang@2450: * scope="+scope+", nSteps="+nSteps); return bridge_rmi_.autoCalculate(id, mlang@2450: * scope, nSteps); } mlang@2450: */ rgradisc@1635: mlang@2450: /** mlang@2450: * @see IToCalc#startSolving(), copied to CalcTree mlang@2450: * mlang@2450: * public void startSolving(CalcHead calc_head_) throws Exception { mlang@2450: * logger.debug("startSolving: calc_head_="+calc_head_); int id = mlang@2450: * calc_head_.getCalcTreeID();//WN040922 TODO simplify CalcHead CalcTree mlang@2450: * calcTree = (CalcTree) calc_trees_.get(new Integer(id));//WN040922 TODO mlang@2450: * simplify mlang@2450: * wneuper@2526: * if (calc_head_.getCalcHeadStatus() != CalcHead.MODEL_ITEM_CORRECT) { wneuper@2526: * throw new Exception("Startsolving with incorrect CalcHead"); } Tactic t = mlang@2450: * calcTree.fetchProposedTactic(); if (t.getName().compareTo("Apply_Method") != mlang@2450: * 0) { throw new Exception("Startsolving fetches " + t.getName()); } //Next mlang@2450: * Tactic must always be Apply_Method calcTree.setNextTactic(t); //return mlang@2450: * calcTree; } mlang@2450: */ rgradisc@1191: mlang@2450: /** mlang@2450: * @see IToCalc public void modifyCalcHead(CalcHead calc_head_) { mlang@2450: * logger.debug("modifyCalcHead: calc_head_="+calc_head_); CalcHead mlang@2450: * newCalcHead = null; try { newCalcHead = mlang@2450: * bridge_rmi_.modifyCalcHead(calc_head_); mlang@2450: * calc_head_.fillValuesfrom(newCalcHead); } catch (RemoteException e) { mlang@2450: * e.printStackTrace(); } } mlang@2450: */ rgradisc@1191: mlang@2450: /* mlang@2450: * @see IToCalc public void completeCalcHead(CalcHead calc_head_) { mlang@2450: * logger.debug("completeCalcHead: calc_head_="+calc_head_); CalcHead mlang@2450: * newCalcHead = null; try { //WN040924 newCalcHead = mlang@2450: * bridge_rmi_.completeCalcHead(calc_head_); mlang@2450: * bridge_rmi_.completeCalcHead(calc_head_); //WN040924 mlang@2450: * calc_head_.fillValuesfrom(newCalcHead); } catch (RemoteException e) { mlang@2450: * e.printStackTrace(); } } mlang@2450: */ rgradisc@1035: mlang@2450: /* mlang@2450: * @see IToCalc public void completeCalcHead(CalcHead calc_head_, int mlang@2450: * completeItem) { CalcHead newCalcHead; try { //WN040924 newCalcHead = mlang@2450: * bridge_rmi_.completeCalcHead(calc_head_, completeItem); mlang@2450: * bridge_rmi_.completeCalcHead(calc_head_, completeItem); //WN040924 mlang@2450: * calc_head_.fillValuesfrom(newCalcHead); //TODO: Remove the following mlang@2450: * line!! calc_head_.setCalcHeadStatus(CalcHead.MODEL_ITEM_CORRECT); } mlang@2450: * catch (RemoteException e) { e.printStackTrace(); } } mlang@2450: */ rgradisc@1035: mlang@2450: // /** mlang@2450: // * Refine a given problem for this CalcTree mlang@2450: // * mlang@2450: // * @param problemID mlang@2450: // * @return CalcHead, if the operation was successful, else null mlang@2450: // */ wneuper@2526: // public void try_refine_(CalcHead calc_head_, CalcHeadCompoundID wneuper@2526: // problemID) mlang@2450: // throws NotInSpecificationPhaseException { mlang@2450: // CalcHead newCalcHead = null; mlang@2450: // try { mlang@2450: // //WN040924 newCalcHead = mlang@2450: // // bridge_rmi_.try_refine_(calc_head_.getCalcTreeID(), problemID); mlang@2450: // bridge_rmi_.try_refine_(calc_head_.getCalcTreeID(), problemID); mlang@2450: // if (newCalcHead == null) { mlang@2450: // throw new NotInSpecificationPhaseException(); mlang@2450: // } mlang@2450: // calc_head_.fillValuesfrom(newCalcHead); mlang@2450: // } catch (RemoteException e) { mlang@2450: // mlang@2450: // e.printStackTrace(); mlang@2450: // } mlang@2450: // } mlang@2450: //WN050610 transferred toCalcTree mlang@2450: // /** mlang@2450: // * Match a given problem for this CalcTree mlang@2450: // * mlang@2450: // * @param problemID mlang@2450: // * @return CalcHead, if the operation was successful, else null mlang@2450: // */ mlang@2450: // public void try_match_(CalcHead calc_head_, CalcHeadCompoundID problemID) mlang@2450: // throws NotInSpecificationPhaseException { mlang@2450: // CalcHead newCalcHead = null; mlang@2450: // try { mlang@2450: // //WN040924 newCalcHead = mlang@2450: // // bridge_rmi_.try_match_(calc_head_.getCalcTreeID(), problemID); mlang@2450: // bridge_rmi_.try_match_(calc_head_.getCalcTreeID(), problemID); mlang@2450: // if (newCalcHead == null) { mlang@2450: // throw new NotInSpecificationPhaseException(); mlang@2450: // } mlang@2450: // calc_head_.fillValuesfrom(newCalcHead); mlang@2450: // } catch (RemoteException e) { mlang@2450: // mlang@2450: // e.printStackTrace(); mlang@2450: // } mlang@2450: // } mlang@2450: // Delegated methods from CalcTree/Iterator mlang@2450: int setNextTactic(int id, Tactic tactic) throws RemoteException { mlang@2450: return bridge_rmi_.setNextTactic(id, tactic); mlang@2450: } rgradisc@1035: mlang@2450: Tactic fetchProposedTactic(int id) throws RemoteException { mlang@2450: return bridge_rmi_.fetchProposedTactic(id); mlang@2450: } rgradisc@1035: mlang@2450: CalcChanged autoCalculate(int id, int scope, int nSteps) mlang@2450: throws RemoteException { mlang@2544: logger_.debug("autoCalculate: id=" + id + ", scope=" + scope mlang@2450: + ", nSteps=" + nSteps); mlang@2450: return bridge_rmi_.autoCalculate(id, scope, nSteps); mlang@2450: } rgradisc@1035: mlang@2450: CalcChanged replaceFormula(int id, CalcFormula f) throws RemoteException { mlang@2450: return bridge_rmi_.replaceFormula(id, f); mlang@2450: } wneuper@1997: mlang@2450: CalcChanged appendFormula(int id, CalcFormula f) throws RemoteException { mlang@2450: return bridge_rmi_.appendFormula(id, f); mlang@2450: } wneuper@2002: mlang@2450: CalcChanged intermediateSteps(int id, ICalcIterator ci) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.intermediateSteps(id, ci); mlang@2450: } rgradisc@1035: mlang@2450: Vector getElementsFromTo(int id, ICalcIterator iterator_from, mlang@2450: ICalcIterator iterator_to, Integer level, mlang@2450: boolean result_includes_tactics) throws RemoteException { mlang@2450: return bridge_rmi_.getElementsFromTo(id, iterator_from, iterator_to, mlang@2450: level, result_includes_tactics); mlang@2450: } rgradisc@1035: mlang@2450: Position moveActiveFormula(int calcTreeID, Position p) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.moveActiveFormula(calcTreeID, p); mlang@2450: } wneuper@2082: mlang@2450: int iterator(int id) throws RemoteException { mlang@2544: logger_.debug("iterator: id=" + id); mlang@2450: return bridge_rmi_.iterator(id); mlang@2450: } rgradisc@1035: mlang@2450: Position moveRoot(int calcTreeID, int iteratorID) throws RemoteException { mlang@2544: logger_.debug("moveRoot: calcTreeID=" + calcTreeID + ", iteratorID=" mlang@2450: + iteratorID); mlang@2450: return bridge_rmi_.moveRoot(calcTreeID, iteratorID); mlang@2450: } rgradisc@1035: mlang@2450: Position moveUp(int calcTreeID, int iteratorID, Position p) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.moveUp(calcTreeID, iteratorID, p); mlang@2450: } rgradisc@1035: mlang@2450: Position moveDown(int calcTreeID, int iteratorID, Position p) mlang@2450: throws RemoteException { mlang@2544: logger_.debug("moveDown: calcTreeID=" + calcTreeID + ", sml_pos_=" + p); mlang@2450: return bridge_rmi_.moveDown(calcTreeID, iteratorID, p); mlang@2450: } rgradisc@1035: mlang@2450: Position moveLevelUp(int calcTreeID, int iteratorID, Position p) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.moveLevelUp(calcTreeID, iteratorID, p); mlang@2450: } wneuper@1989: mlang@2450: Position moveLevelDown(int calcTreeID, int iteratorID, Position p) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.moveLevelDown(calcTreeID, iteratorID, p); mlang@2450: } rgradisc@1035: mlang@2450: Position moveCalcHead(int calcTreeID, int iteratorID, Position p) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.moveCalcHead(calcTreeID, iteratorID, p); mlang@2450: } rgradisc@1035: mlang@2450: boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException { mlang@2450: return bridge_rmi_.moveTactic(calcTreeID, iteratorID); mlang@2450: } rgradisc@1304: mlang@2450: boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException { mlang@2450: return bridge_rmi_.moveFormula(calcTreeID, iteratorID); mlang@2450: } rgradisc@1304: mlang@2450: ICalcElement getElement(int calcTreeID, Position p) throws RemoteException { mlang@2450: return bridge_rmi_.getElement(calcTreeID, p); mlang@2450: } rgradisc@1198: mlang@2450: /** mlang@2450: * Destruct a calcTree: Gives the memory occupied by this calcTree free mlang@2450: * again. Call this method when the calcTree is no longer needed. This mlang@2450: * method is irreversible. mlang@2450: * mlang@2450: * @param calcTreeID mlang@2450: * id of the calcTree to be destructed mlang@2450: */ mlang@2450: public boolean destruct(int calcTreeID) throws RemoteException { mlang@2450: return bridge_rmi_.destruct(calcTreeID); mlang@2450: } wneuper@2065: mlang@2450: /** mlang@2450: * Save a calcTree mlang@2450: * mlang@2450: * @param calcTreeID mlang@2450: * id of the calcTree to be saved mlang@2450: * @return a vector containing strings, representing the status of the mlang@2450: * calcTree mlang@2450: */ mlang@2450: public Vector saveCalcTree(int calcTreeID) { mlang@2450: try { mlang@2450: return bridge_rmi_.saveCalcTree(calcTreeID); mlang@2450: } catch (RemoteException e) { mlang@2450: e.printStackTrace(); mlang@2450: } mlang@2450: return null; mlang@2450: } wneuper@2065: mlang@2450: /** mlang@2450: * Load a previously stored calcTree mlang@2450: * mlang@2450: * @param v mlang@2450: * Vector: the return value of saveCalcTree mlang@2450: * @return a calcTree reference mlang@2450: */ mlang@2450: public CalcTree loadCalcTree(Vector v) { mlang@2450: try { mlang@2450: int id = bridge_rmi_.loadCalcTree(v); mlang@2450: CalcTree calcTree = new CalcTree(this, id); mlang@2450: calc_trees_.put(new Integer(id), calcTree); mlang@2450: ICalcIterator hotSpot = calcTree.iterator(); mlang@2450: // may be not first iterator (not hotspot) mlang@2450: ((CalcIterator) hotSpot).makeHotSpot(); mlang@2450: // make sure it is a hotspot iterator mlang@2450: calcTree.setHotSpot(hotSpot); mlang@2450: hotSpot.moveRoot(); mlang@2450: return calcTree; mlang@2450: } catch (RemoteException e) { mlang@2450: e.printStackTrace(); mlang@2450: } mlang@2450: return null; mlang@2450: } wneuper@2065: mlang@2450: /** mlang@2450: * @see isac.util.interfaces.ICalcIterator#getTactic() mlang@2450: */ mlang@2450: public Tactic getTactic(int id, Position pos) throws RemoteException { mlang@2450: return bridge_rmi_.getTactic(id, pos); mlang@2450: } wneuper@2065: mlang@2450: public Vector getApplicableTactics(int calcTreeID, int scope, Position pos) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.getAppliableTactics(calcTreeID, scope, pos); mlang@2450: } wneuper@2106: mlang@2450: /** mlang@2450: * @see isac.util.interfaces.ICalcIterator#getAssumptions() mlang@2450: */ mlang@2450: public Assumptions getAssumptions(int id, Position pos) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.getAssumptions(id, pos); mlang@2450: } wneuper@2106: mlang@2450: /** mlang@2450: * @see isac.util.interfaces.ICalcIterator#getAssumptions() mlang@2450: */ mlang@2450: public Assumptions getAccumulatedAssumptions(int id, Position pos) mlang@2450: throws RemoteException { mlang@2450: return bridge_rmi_.getAccumulatedAssumptions(id, pos); mlang@2450: } wneuper@2106: wneuper@2526: /** wneuper@2526: * @see isac.util.interfaces.ICalcIterator#tryMatchProblem wneuper@2526: */ wneuper@2526: public Match tryMatchProblem(int calcTreeID, ProblemID pblID) wneuper@2526: throws RemoteException { wneuper@2526: return bridge_rmi_.tryMatchProblem(calcTreeID, pblID); wneuper@2526: } wneuper@2526: wneuper@2526: /** wneuper@2526: * @see isac.util.interfaces.ICalcIterator#tryRefineProblem wneuper@2526: */ wneuper@2526: public Match tryRefineProblem(int calcTreeID, ProblemID pblID) wneuper@2526: throws RemoteException { wneuper@2526: return bridge_rmi_.tryRefineProblem(calcTreeID, pblID); wneuper@2526: } wneuper@2526: wneuper@2526: /** wneuper@2526: * @see isac.util.interfaces.ICalcIterator#tryMatchMethod wneuper@2526: */ wneuper@2526: public Match tryMatchMethod(int calcTreeID, KEStoreID pblID) wneuper@2526: throws RemoteException { wneuper@2526: return bridge_rmi_.tryMatchMethod(calcTreeID, pblID); wneuper@2526: } wneuper@2526: wneuper@2526: /** wneuper@2526: * @see isac.util.interfaces.ICalcIterator#tryRefineMethod wneuper@2526: */ wneuper@2526: public Match tryRefineMethod(int calcTreeID, KEStoreID pblID) wneuper@2526: throws RemoteException { wneuper@2526: return bridge_rmi_.tryRefineMethod(calcTreeID, pblID); wneuper@2526: } wneuper@2065: }