src/java/isac/bridge/MathEngine.java
author wneuper
Wed, 29 Sep 2004 16:42:42 +0200
changeset 1891 129e2290eb4e
parent 1888 04080b61a45e
child 1976 b7294e0e8b4b
permissions -rw-r--r--
search error 2 integration specify-solve-phase, testMinisubpblAutoCalc OK
     1 /*
     2  * Created on Oct 20, 2003
     3  */
     4 package isac.bridge;
     5 
     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;
    13 
    14 import java.io.Serializable;
    15 import java.rmi.Naming;
    16 import java.rmi.RemoteException;
    17 import java.util.HashMap;
    18 import java.util.Map;
    19 import java.util.Vector;
    20 
    21 import org.apache.log4j.Logger;
    22 
    23 /**
    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. 
    28  * @author rgradisc
    29  */
    30 public class MathEngine implements Serializable {
    31 
    32   private static MathEngine singleton;
    33 
    34   private IBridgeRMI bridgeRMI;
    35 
    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;
    39 
    40   static Logger logger = Logger.getLogger(MathEngine.class.getName());
    41 
    42   // This is a Singleton Class: A private constructor is needed
    43   private MathEngine(String hostName) {
    44   	logger.fatal("MathEngine("+hostName+")");
    45     try {
    46       //connect to bridge  
    47       bridgeRMI = (IBridgeRMI) Naming.lookup("//" + hostName + "/BridgeRMI");
    48       System.out.println(
    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) {
    53       System.out.println(
    54         "Could not connect to Bridge via RMI\n"
    55           + "Please make sure that the Bridge is running and connected");
    56       e.printStackTrace();
    57     }
    58     calcTrees = new HashMap();
    59   }
    60 
    61   /*
    62    * FIXME.WN040924 just to make the old bridge continue work 
    63    **/
    64   protected IBridgeRMI getBridgeRMI() {
    65 	return bridgeRMI; 	
    66   }
    67   /**
    68    * Initialize the MathEngine
    69    * @param hostName: Host name on which the Bridge is running
    70    */
    71   public static void init(String hostName) {
    72     if (singleton == null) {
    73       singleton = new MathEngine(hostName);
    74     }
    75   }
    76 
    77   public static MathEngine getMathEngine() {
    78   	logger.fatal("getMathEngine()");
    79     return singleton;
    80   }
    81 
    82 	 /**
    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 ???
    87 	  *     
    88 	  * int id = calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead
    89 	  * CalcTree calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922 TODO simplify
    90 	  */
    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
    96     try {
    97       int id = bridgeRMI.startCalculation(f);
    98       calcTree = new CalcTree(this, id);
    99       calcTrees.put(new Integer(id), calcTree);
   100 
   101       hotSpot = calcTree.iterator();
   102       calcTree.setHotSpot(hotSpot); //first iterator marks the hotSpot
   103       hotSpot.moveRoot();
   104       //---------------------------------------------- startSpecifying
   105       //WN calcHead = (CalcHead) hotSpot.getElement();
   106       //WN System.out.println("Calchead: " + calcHead.toSMLString());
   107     } catch (RemoteException e) {
   108       e.printStackTrace();
   109     }
   110     return calcTree;
   111   }
   112 
   113 /*
   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);
   117    }
   118 */
   119    
   120   /**
   121    * @see IToCalc#startSolving(), copied to CalcTree
   122    *
   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
   127 
   128     if (calcHead.getCalcHeadStatus() != CalcHead.CALCHEAD_CORRECT) {
   129       throw new Exception("Startsolving with incorrect CalcHead");
   130     }
   131     Tactic t = calcTree.fetchProposedTactic();
   132     if (t.getName().compareTo("Apply_Method") != 0) {
   133       throw new Exception("Startsolving fetches " + t.getName());
   134     }
   135     //Next Tactic must always be Apply_Method
   136     calcTree.setNextTactic(t);
   137     //return calcTree;
   138   }
   139   **/
   140    
   141   /**
   142    * @see IToCalc   
   143   public void modifyCalcHead(CalcHead calcHead) {
   144   	logger.warn("modifyCalcHead: calcHead="+calcHead);
   145     CalcHead newCalcHead = null;
   146     try {
   147       newCalcHead = bridgeRMI.modifyCalcHead(calcHead);
   148       calcHead.fillValuesfrom(newCalcHead);
   149     } catch (RemoteException e) {
   150       e.printStackTrace();
   151     }
   152   }
   153   */
   154    
   155   /*
   156    * @see IToCalc
   157   public void completeCalcHead(CalcHead calcHead) {
   158   	logger.warn("completeCalcHead: calcHead="+calcHead);
   159     CalcHead newCalcHead = null;
   160     try {
   161     	//WN040924 newCalcHead = bridgeRMI.completeCalcHead(calcHead);
   162         bridgeRMI.completeCalcHead(calcHead);
   163         //WN040924 calcHead.fillValuesfrom(newCalcHead);
   164     } catch (RemoteException e) {
   165       e.printStackTrace();
   166     }
   167   }
   168   */
   169  
   170   /*
   171    * @see IToCalc
   172   public void completeCalcHead(CalcHead calcHead, int completeItem) {
   173     CalcHead newCalcHead;
   174     try {
   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);
   180 
   181     } catch (RemoteException e) {
   182       e.printStackTrace();
   183     }
   184   }
   185    */
   186 
   187   /**
   188   * Refine a given problem for this CalcTree
   189   * @param problemID
   190   * @return CalcHead, if the operation was successful, else null 
   191   */
   192   public void tryRefine(CalcHead calcHead, CalcHeadCompoundID problemID)
   193     throws NotInSpecificationPhaseException {
   194     CalcHead newCalcHead = null;
   195     try {
   196     	//WN040924 newCalcHead = bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
   197         bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
   198       if (newCalcHead == null) {
   199         throw new NotInSpecificationPhaseException();
   200       }
   201       calcHead.fillValuesfrom(newCalcHead);
   202     } catch (RemoteException e) {
   203       // TODO Auto-generated catch block
   204       e.printStackTrace();
   205     }
   206   }
   207 
   208   /**
   209   * Match a given problem for this CalcTree
   210   * @param problemID
   211   * @return CalcHead, if the operation was successful, else null 
   212   */
   213   public void tryMatch(CalcHead calcHead, CalcHeadCompoundID problemID)
   214     throws NotInSpecificationPhaseException {
   215     CalcHead newCalcHead = null;
   216     try {
   217     	//WN040924 newCalcHead = bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
   218         bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
   219       if (newCalcHead == null) {
   220         throw new NotInSpecificationPhaseException();
   221       }
   222       calcHead.fillValuesfrom(newCalcHead);
   223     } catch (RemoteException e) {
   224       // TODO Auto-generated catch block
   225       e.printStackTrace();
   226     }
   227   }
   228 
   229   // Delegated methods from CalcTree/Iterator
   230   int setNextTactic(int id, Tactic tactic) throws RemoteException {
   231     return bridgeRMI.setNextTactic(id, tactic);
   232   }
   233 
   234   Tactic fetchProposedTactic(int id) throws RemoteException {
   235     return bridgeRMI.fetchProposedTactic(id);
   236   }
   237 
   238   Tactic[] fetchAppliableTactics(int id, int scope) throws RemoteException {
   239     return bridgeRMI.fetchAppliableTactics(id, scope);
   240   }
   241 
   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);
   245   }
   246 
   247   int iterator(int id) throws RemoteException {
   248   	logger.warn("iterator: id="+id);
   249     return bridgeRMI.iterator(id);
   250   }
   251 
   252   boolean moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
   253   	logger.warn("moveRoot: calcTreeID="+calcTreeID+", iteratorID="+iteratorID);
   254   	return bridgeRMI.moveRoot(calcTreeID, iteratorID);
   255   }
   256 
   257   boolean moveUp(int calcTreeID, int iteratorID) throws RemoteException {
   258     return bridgeRMI.moveUp(calcTreeID, iteratorID);
   259   }
   260 
   261   boolean moveDown(int calcTreeID, int iteratorID) throws RemoteException {
   262   	logger.warn("moveDown: calcTreeID="+calcTreeID+", iteratorID="+iteratorID);
   263     return bridgeRMI.moveDown(calcTreeID, iteratorID);
   264   }
   265 
   266   boolean moveLevelUp(int calcTreeID, int iteratorID) throws RemoteException {
   267     return bridgeRMI.moveLevelUp(calcTreeID, iteratorID);
   268   }
   269 
   270   boolean moveLevelDown(int calcTreeID, int iteratorID)
   271     throws RemoteException {
   272     return bridgeRMI.moveLevelDown(calcTreeID, iteratorID);
   273   }
   274 
   275   boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
   276     return bridgeRMI.moveTactic(calcTreeID, iteratorID);
   277   }
   278 
   279   boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
   280     return bridgeRMI.moveFormula(calcTreeID, iteratorID);
   281   }
   282 
   283   ICalcElement getElement(int calcTreeID, int iteratorID)
   284     throws RemoteException {
   285     return bridgeRMI.getElement(calcTreeID, iteratorID);
   286   }
   287 
   288   /**
   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
   292    */
   293   public boolean destruct(int calcTreeID) throws RemoteException {
   294     return bridgeRMI.destruct(calcTreeID);
   295   }
   296 
   297   /**
   298    * Save a calcTree
   299    * @param calcTreeID id of the calcTree to be saved
   300    * @return a vector containing strings, representing the status of the calcTree
   301    */
   302   public Vector saveCalcTree(int calcTreeID) {
   303     try {
   304       return bridgeRMI.saveCalcTree(calcTreeID);
   305     } catch (RemoteException e) {
   306       e.printStackTrace();
   307     }
   308     return null;
   309   }
   310 
   311   /**
   312    * Load a previously stored calcTree
   313    * @param v Vector: the return value of saveCalcTree
   314    * @return a calcTree reference
   315    */
   316   public CalcTree loadCalcTree(Vector v) {
   317     try {
   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);
   326       hotSpot.moveRoot();
   327       return calcTree;
   328     } catch (RemoteException e) {
   329       e.printStackTrace();
   330     }
   331     return null;
   332   }
   333 }