src/java/isac/bridge/MathEngine.java
author mlang
Tue, 28 Jun 2005 13:14:03 +0200
changeset 2544 631fe7f467b7
parent 2526 f37df79389c0
child 2701 fbd8357974b7
permissions -rw-r--r--
cosmetics part2
     1 /*
     2  * Created on Oct 20, 2003
     3  */
     4 package isac.bridge;
     5 
     6 import isac.util.CalcChanged;
     7 import isac.util.Formalization;
     8 import isac.util.formulae.Assumptions;
     9 import isac.util.formulae.CalcFormula;
    10 import isac.util.formulae.KEStoreID;
    11 import isac.util.formulae.Match;
    12 import isac.util.formulae.Position;
    13 import isac.util.formulae.ProblemID;
    14 import isac.util.interfaces.ICalcElement;
    15 import isac.util.interfaces.ICalcIterator;
    16 import isac.util.tactics.Tactic;
    17 
    18 import java.io.Serializable;
    19 import java.rmi.Naming;
    20 import java.rmi.RemoteException;
    21 import java.util.HashMap;
    22 import java.util.Map;
    23 import java.util.Vector;
    24 
    25 import org.apache.log4j.Logger;
    26 
    27 /**
    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.
    31  * 
    32  * @author rgradisc
    33  */
    34 public class MathEngine implements Serializable {
    35 
    36     private static MathEngine singleton_;
    37 
    38     private IBridgeRMI bridge_rmi_;
    39 
    40     // Stores the calc_trees_ after call of startSpecifying //WN00827
    41     // startCalculation
    42     // and hands them out to the dialog after startSolving//WN00827 ???
    43     private Map calc_trees_;
    44 
    45     static Logger logger_ = Logger.getLogger(MathEngine.class.getName());
    46 
    47     // This is a Singleton Class: A private constructor is needed
    48     private MathEngine(String hostName) {
    49         logger_.debug("MathEngine(" + hostName + ")");
    50         try {
    51             //connect to bridge
    52             bridge_rmi_ = (IBridgeRMI) Naming.lookup("//" + hostName
    53                     + "/BridgeRMI");
    54             System.out.println("MathEngine Constr: connected to Bridge: "
    55                     + bridge_rmi_.toString());
    56             if (bridge_rmi_ == null)
    57                 System.out
    58                         .println("Error occured: The Bridge could not be found!");
    59         } catch (Exception e) {
    60             System.out
    61                     .println("Could not connect to Bridge via RMI\n"
    62                             + "Please make sure that the Bridge is running and connected");
    63             e.printStackTrace();
    64         }
    65         calc_trees_ = new HashMap();
    66     }
    67 
    68     /*
    69      * FIXME.WN040924 just to make the old bridge continue work
    70      */
    71     protected IBridgeRMI getBridgeRMI() {
    72         return bridge_rmi_;
    73     }
    74 
    75     /**
    76      * Initialize the MathEngine
    77      * 
    78      * @param hostName:
    79      *            Host name on which the Bridge is running
    80      */
    81     public static void init(String hostName) {
    82         logger_.fatal("  DG->BR: init(" + hostName + ")");
    83         if (singleton_ == null) {
    84             singleton_ = new MathEngine(hostName);
    85         }
    86     }
    87 
    88     public static MathEngine getMathEngine() {
    89         logger_.fatal("  DG<>BR: getMathEngine()");
    90         return singleton_;
    91     }
    92 
    93     /**
    94      * Start a new calculation
    95      * 
    96      * @param f
    97      *            Formalization of the new calculation, from expl
    98      * @return CalcHead: empty for startSpecifying
    99      * @throws RemoteException
   100      *             WN040924 ???
   101      * 
   102      * int id = calc_head_.getCalcTreeID();//WN040922 TODO simplify CalcHead
   103      * CalcTree calcTree = (CalcTree) calc_trees_.get(new
   104      * Integer(id));//WN040922 TODO simplify
   105      */
   106     public CalcTree startCalculation(Formalization f) {
   107         logger_.fatal("  DG->BR: startCalculation(" + f.toSMLString() + ")");
   108         CalcTree calcTree = null;//WN
   109         try {
   110             int id = bridge_rmi_.startCalculation(f);
   111             calcTree = new CalcTree(this, id);
   112             calc_trees_.put(new Integer(id), calcTree);
   113 
   114             ICalcIterator hotSpot = calcTree.iterator();
   115             calcTree.setHotSpot(hotSpot); //first iterator marks the hotSpot
   116             //hotSpot.moveRoot();
   117             int i = 111;
   118         } catch (RemoteException e) {
   119             e.printStackTrace();
   120         }
   121         logger_.fatal("  DG<-BR: startCalculation <- calcTree=" + calcTree);
   122         return calcTree;
   123     }
   124 
   125     /*
   126      * boolean autoCalculate(int id, int scope, int nSteps) throws
   127      * RemoteException { logger.debug("autoCalculate: id="+id+",
   128      * scope="+scope+", nSteps="+nSteps); return bridge_rmi_.autoCalculate(id,
   129      * scope, nSteps); }
   130      */
   131 
   132     /**
   133      * @see IToCalc#startSolving(), copied to CalcTree
   134      * 
   135      * public void startSolving(CalcHead calc_head_) throws Exception {
   136      * logger.debug("startSolving: calc_head_="+calc_head_); int id =
   137      * calc_head_.getCalcTreeID();//WN040922 TODO simplify CalcHead CalcTree
   138      * calcTree = (CalcTree) calc_trees_.get(new Integer(id));//WN040922 TODO
   139      * simplify
   140      * 
   141      * if (calc_head_.getCalcHeadStatus() != CalcHead.MODEL_ITEM_CORRECT) {
   142      * throw 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
   146      * calcTree; }
   147      */
   148 
   149     /**
   150      * @see IToCalc public void modifyCalcHead(CalcHead calc_head_) {
   151      *      logger.debug("modifyCalcHead: calc_head_="+calc_head_); CalcHead
   152      *      newCalcHead = null; try { newCalcHead =
   153      *      bridge_rmi_.modifyCalcHead(calc_head_);
   154      *      calc_head_.fillValuesfrom(newCalcHead); } catch (RemoteException e) {
   155      *      e.printStackTrace(); } }
   156      */
   157 
   158     /*
   159      * @see IToCalc public void completeCalcHead(CalcHead calc_head_) {
   160      *      logger.debug("completeCalcHead: calc_head_="+calc_head_); CalcHead
   161      *      newCalcHead = null; try { //WN040924 newCalcHead =
   162      *      bridge_rmi_.completeCalcHead(calc_head_);
   163      *      bridge_rmi_.completeCalcHead(calc_head_); //WN040924
   164      *      calc_head_.fillValuesfrom(newCalcHead); } catch (RemoteException e) {
   165      *      e.printStackTrace(); } }
   166      */
   167 
   168     /*
   169      * @see IToCalc public void completeCalcHead(CalcHead calc_head_, int
   170      *      completeItem) { CalcHead newCalcHead; try { //WN040924 newCalcHead =
   171      *      bridge_rmi_.completeCalcHead(calc_head_, completeItem);
   172      *      bridge_rmi_.completeCalcHead(calc_head_, completeItem); //WN040924
   173      *      calc_head_.fillValuesfrom(newCalcHead); //TODO: Remove the following
   174      *      line!! calc_head_.setCalcHeadStatus(CalcHead.MODEL_ITEM_CORRECT); }
   175      *      catch (RemoteException e) { e.printStackTrace(); } }
   176      */
   177 
   178     //	/**
   179     //	 * Refine a given problem for this CalcTree
   180     //	 *
   181     //	 * @param problemID
   182     //	 * @return CalcHead, if the operation was successful, else null
   183     //	 */
   184     //	public void try_refine_(CalcHead calc_head_, CalcHeadCompoundID
   185     // problemID)
   186     //			throws NotInSpecificationPhaseException {
   187     //		CalcHead newCalcHead = null;
   188     //		try {
   189     //			//WN040924 newCalcHead =
   190     //			// bridge_rmi_.try_refine_(calc_head_.getCalcTreeID(), problemID);
   191     //			bridge_rmi_.try_refine_(calc_head_.getCalcTreeID(), problemID);
   192     //			if (newCalcHead == null) {
   193     //				throw new NotInSpecificationPhaseException();
   194     //			}
   195     //			calc_head_.fillValuesfrom(newCalcHead);
   196     //		} catch (RemoteException e) {
   197     //	
   198     //			e.printStackTrace();
   199     //		}
   200     //	}
   201     //WN050610 transferred toCalcTree
   202     //	/**
   203     //	 * Match a given problem for this CalcTree
   204     //	 *
   205     //	 * @param problemID
   206     //	 * @return CalcHead, if the operation was successful, else null
   207     //	 */
   208     //	public void try_match_(CalcHead calc_head_, CalcHeadCompoundID problemID)
   209     //			throws NotInSpecificationPhaseException {
   210     //		CalcHead newCalcHead = null;
   211     //		try {
   212     //			//WN040924 newCalcHead =
   213     //			// bridge_rmi_.try_match_(calc_head_.getCalcTreeID(), problemID);
   214     //			bridge_rmi_.try_match_(calc_head_.getCalcTreeID(), problemID);
   215     //			if (newCalcHead == null) {
   216     //				throw new NotInSpecificationPhaseException();
   217     //			}
   218     //			calc_head_.fillValuesfrom(newCalcHead);
   219     //		} catch (RemoteException e) {
   220     //			
   221     //			e.printStackTrace();
   222     //		}
   223     //	}
   224     // Delegated methods from CalcTree/Iterator
   225     int setNextTactic(int id, Tactic tactic) throws RemoteException {
   226         return bridge_rmi_.setNextTactic(id, tactic);
   227     }
   228 
   229     Tactic fetchProposedTactic(int id) throws RemoteException {
   230         return bridge_rmi_.fetchProposedTactic(id);
   231     }
   232 
   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 bridge_rmi_.autoCalculate(id, scope, nSteps);
   238     }
   239 
   240     CalcChanged replaceFormula(int id, CalcFormula f) throws RemoteException {
   241         return bridge_rmi_.replaceFormula(id, f);
   242     }
   243 
   244     CalcChanged appendFormula(int id, CalcFormula f) throws RemoteException {
   245         return bridge_rmi_.appendFormula(id, f);
   246     }
   247 
   248     CalcChanged intermediateSteps(int id, ICalcIterator ci)
   249             throws RemoteException {
   250         return bridge_rmi_.intermediateSteps(id, ci);
   251     }
   252 
   253     Vector getElementsFromTo(int id, ICalcIterator iterator_from,
   254             ICalcIterator iterator_to, Integer level,
   255             boolean result_includes_tactics) throws RemoteException {
   256         return bridge_rmi_.getElementsFromTo(id, iterator_from, iterator_to,
   257                 level, result_includes_tactics);
   258     }
   259 
   260     Position moveActiveFormula(int calcTreeID, Position p)
   261             throws RemoteException {
   262         return bridge_rmi_.moveActiveFormula(calcTreeID, p);
   263     }
   264 
   265     int iterator(int id) throws RemoteException {
   266         logger_.debug("iterator: id=" + id);
   267         return bridge_rmi_.iterator(id);
   268     }
   269 
   270     Position moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
   271         logger_.debug("moveRoot: calcTreeID=" + calcTreeID + ", iteratorID="
   272                 + iteratorID);
   273         return bridge_rmi_.moveRoot(calcTreeID, iteratorID);
   274     }
   275 
   276     Position moveUp(int calcTreeID, int iteratorID, Position p)
   277             throws RemoteException {
   278         return bridge_rmi_.moveUp(calcTreeID, iteratorID, p);
   279     }
   280 
   281     Position moveDown(int calcTreeID, int iteratorID, Position p)
   282             throws RemoteException {
   283         logger_.debug("moveDown: calcTreeID=" + calcTreeID + ", sml_pos_=" + p);
   284         return bridge_rmi_.moveDown(calcTreeID, iteratorID, p);
   285     }
   286 
   287     Position moveLevelUp(int calcTreeID, int iteratorID, Position p)
   288             throws RemoteException {
   289         return bridge_rmi_.moveLevelUp(calcTreeID, iteratorID, p);
   290     }
   291 
   292     Position moveLevelDown(int calcTreeID, int iteratorID, Position p)
   293             throws RemoteException {
   294         return bridge_rmi_.moveLevelDown(calcTreeID, iteratorID, p);
   295     }
   296 
   297     Position moveCalcHead(int calcTreeID, int iteratorID, Position p)
   298             throws RemoteException {
   299         return bridge_rmi_.moveCalcHead(calcTreeID, iteratorID, p);
   300     }
   301 
   302     boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
   303         return bridge_rmi_.moveTactic(calcTreeID, iteratorID);
   304     }
   305 
   306     boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
   307         return bridge_rmi_.moveFormula(calcTreeID, iteratorID);
   308     }
   309 
   310     ICalcElement getElement(int calcTreeID, Position p) throws RemoteException {
   311         return bridge_rmi_.getElement(calcTreeID, p);
   312     }
   313 
   314     /**
   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.
   318      * 
   319      * @param calcTreeID
   320      *            id of the calcTree to be destructed
   321      */
   322     public boolean destruct(int calcTreeID) throws RemoteException {
   323         return bridge_rmi_.destruct(calcTreeID);
   324     }
   325 
   326     /**
   327      * Save a calcTree
   328      * 
   329      * @param calcTreeID
   330      *            id of the calcTree to be saved
   331      * @return a vector containing strings, representing the status of the
   332      *         calcTree
   333      */
   334     public Vector saveCalcTree(int calcTreeID) {
   335         try {
   336             return bridge_rmi_.saveCalcTree(calcTreeID);
   337         } catch (RemoteException e) {
   338             e.printStackTrace();
   339         }
   340         return null;
   341     }
   342 
   343     /**
   344      * Load a previously stored calcTree
   345      * 
   346      * @param v
   347      *            Vector: the return value of saveCalcTree
   348      * @return a calcTree reference
   349      */
   350     public CalcTree loadCalcTree(Vector v) {
   351         try {
   352             int id = bridge_rmi_.loadCalcTree(v);
   353             CalcTree calcTree = new CalcTree(this, id);
   354             calc_trees_.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);
   360             hotSpot.moveRoot();
   361             return calcTree;
   362         } catch (RemoteException e) {
   363             e.printStackTrace();
   364         }
   365         return null;
   366     }
   367 
   368     /**
   369      * @see isac.util.interfaces.ICalcIterator#getTactic()
   370      */
   371     public Tactic getTactic(int id, Position pos) throws RemoteException {
   372         return bridge_rmi_.getTactic(id, pos);
   373     }
   374 
   375     public Vector getApplicableTactics(int calcTreeID, int scope, Position pos)
   376             throws RemoteException {
   377         return bridge_rmi_.getAppliableTactics(calcTreeID, scope, pos);
   378     }
   379 
   380     /**
   381      * @see isac.util.interfaces.ICalcIterator#getAssumptions()
   382      */
   383     public Assumptions getAssumptions(int id, Position pos)
   384             throws RemoteException {
   385         return bridge_rmi_.getAssumptions(id, pos);
   386     }
   387 
   388     /**
   389      * @see isac.util.interfaces.ICalcIterator#getAssumptions()
   390      */
   391     public Assumptions getAccumulatedAssumptions(int id, Position pos)
   392             throws RemoteException {
   393         return bridge_rmi_.getAccumulatedAssumptions(id, pos);
   394     }
   395 
   396     /**
   397      * @see isac.util.interfaces.ICalcIterator#tryMatchProblem
   398      */
   399     public Match tryMatchProblem(int calcTreeID, ProblemID pblID)
   400             throws RemoteException {
   401         return bridge_rmi_.tryMatchProblem(calcTreeID, pblID);
   402     }
   403 
   404     /**
   405      * @see isac.util.interfaces.ICalcIterator#tryRefineProblem
   406      */
   407     public Match tryRefineProblem(int calcTreeID, ProblemID pblID)
   408             throws RemoteException {
   409         return bridge_rmi_.tryRefineProblem(calcTreeID, pblID);
   410     }
   411 
   412     /**
   413      * @see isac.util.interfaces.ICalcIterator#tryMatchMethod
   414      */
   415     public Match tryMatchMethod(int calcTreeID, KEStoreID pblID)
   416             throws RemoteException {
   417         return bridge_rmi_.tryMatchMethod(calcTreeID, pblID);
   418     }
   419 
   420     /**
   421      * @see isac.util.interfaces.ICalcIterator#tryRefineMethod
   422      */
   423     public Match tryRefineMethod(int calcTreeID, KEStoreID pblID)
   424             throws RemoteException {
   425         return bridge_rmi_.tryRefineMethod(calcTreeID, pblID);
   426     }
   427 }