src/java/isac/bridge/MathEngine.java
author rgradisc
Tue, 25 Nov 2003 12:47:50 +0100
changeset 1202 e826c0eccaa6
parent 1198 308284023a56
child 1220 68529c86cd5a
permissions -rw-r--r--
*** empty log message ***
     1 /*
     2  * Created on Oct 20, 2003
     3  */
     4 package isac.bridge;
     5 
     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;
    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 /**
    22  * @author rgradisc
    23  */
    24 public class MathEngine implements Serializable {
    25 
    26   private static MathEngine singleton;
    27 
    28   private IBridgeRMI bridgeRMI;
    29 
    30   private Map calcTrees;
    31 
    32   // Singleton Class: private constructor
    33   private MathEngine(String hostName) {
    34     //connect to bridge    
    35     try {
    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) {
    41       System.out.println(
    42         "Could not connect to Bridge via RMI\n"
    43           + "Please make sure that the Bridge is running and connected");
    44       e.printStackTrace();
    45     }
    46     calcTrees = new HashMap();
    47     //states:= []; // only for testing: reset states for every new CalcTree
    48   }
    49 
    50   public static void init(String hostName) {
    51     singleton = new MathEngine(hostName);
    52   }
    53 
    54   public static MathEngine getMathEngine() {
    55     return singleton;
    56   }
    57 
    58   /**
    59   * Start a new calculation
    60   * @param f Formalization of the new calculation
    61   * @return CalcHead: Contains Specification part of Formalization
    62   * @throws RemoteException
    63   */
    64   public CalcHead startSpecifying(Formalization f) {
    65     CalcHead calcHead = null;
    66     try {
    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
    72       hotSpot.moveRoot();
    73       calcHead = (CalcHead)hotSpot.getElement();
    74       // calcHead.setCalcTreeID(javaCalcID);
    75     } catch (RemoteException e) {
    76       e.printStackTrace();
    77     }
    78     return calcHead;
    79   }
    80 
    81   /**
    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
    87   */
    88   public CalcTree startSolving(CalcHead calcHead) {
    89     /*CalcTree calcTree = null;
    90     System.out.println("MathEngine.startSolving: start");
    91     try {
    92       int id = bridgeRMI.startSolving(calcHead);
    93       calcTree = new CalcTree(this, id);
    94     } catch (RemoteException e) {
    95       e.printStackTrace();
    96     }
    97     System.out.println("MathEngine.startSolving: end");
    98     return calcTree;*/
    99     int id = calcHead.getCalcTreeID();
   100     CalcTree calcTree = (CalcTree)calcTrees.get(new Integer(id));
   101     return calcTree;
   102   }
   103 
   104   /**
   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   
   110    */
   111   public CalcHead modifyCalcHead(CalcHead calcHead) {
   112     try {
   113       return bridgeRMI.modifyCalcHead(calcHead);
   114     } catch (RemoteException e) {
   115       e.printStackTrace();
   116     }
   117     return null;
   118   }
   119 
   120   /**
   121   * Refine a given problem for this CalcTree
   122   * @param problemID
   123   * @return CalcHead, if the operation was successful, else null 
   124   */
   125   public CalcHead tryRefine(CalcHead owner, CalcHeadCompoundID problemID)
   126     throws NotInSpecificationPhaseException {
   127 
   128     try {
   129       CalcHead result = bridgeRMI.tryRefine(owner.getCalcTreeID(), problemID);
   130       if (result == null) {
   131         throw new NotInSpecificationPhaseException();
   132       }
   133       return result;
   134     } catch (RemoteException e) {
   135       // TODO Auto-generated catch block
   136       e.printStackTrace();
   137     }
   138     return null;
   139   }
   140 
   141   /**
   142   * Match a given problem for this CalcTree
   143   * @param problemID
   144   * @return CalcHead, if the operation was successful, else null 
   145   */
   146   public CalcHead tryMatch(CalcHead owner, CalcHeadCompoundID problemID)
   147     throws NotInSpecificationPhaseException {
   148     try {
   149       CalcHead result = bridgeRMI.tryMatch(owner.getCalcTreeID(), problemID);
   150       if (result == null) {
   151         throw new NotInSpecificationPhaseException();
   152       }
   153       return result;
   154     } catch (RemoteException e) {
   155       // TODO Auto-generated catch block
   156       e.printStackTrace();
   157     }
   158     return null;
   159   }
   160 
   161   // Delegated methods from CalcTree/Iterator
   162   int setNextTactic(int id, Tactic tactic) throws RemoteException {
   163     return bridgeRMI.setNextTactic(id, tactic);
   164   }
   165 
   166   Tactic fetchProposedTactic(int id) throws RemoteException {
   167     return bridgeRMI.fetchProposedTactic(id);
   168   }
   169 
   170   Tactic[] fetchAppliableTactics(int id, int scope) throws RemoteException {
   171     return bridgeRMI.fetchAppliableTactics(id, scope);
   172   }
   173 
   174   int autoCalculate(int id, int scope, int nSteps) throws RemoteException {
   175     return bridgeRMI.autoCalculate(id, scope, nSteps);
   176   }
   177 
   178   int iterator(int id) throws RemoteException {
   179     return bridgeRMI.iterator(id);
   180   }
   181 
   182   boolean moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
   183     return bridgeRMI.moveRoot(calcTreeID, iteratorID);
   184   }
   185 
   186   boolean moveUp(int calcTreeID, int iteratorID) throws RemoteException {
   187     return bridgeRMI.moveUp(calcTreeID, iteratorID);
   188   }
   189 
   190   boolean moveDown(int calcTreeID, int iteratorID) throws RemoteException {
   191     return bridgeRMI.moveDown(calcTreeID, iteratorID);
   192   }
   193 
   194   boolean moveLevelUp(int calcTreeID, int iteratorID) throws RemoteException {
   195     return bridgeRMI.moveLevelUp(calcTreeID, iteratorID);
   196   }
   197 
   198   boolean moveLevelDown(int calcTreeID, int iteratorID) throws RemoteException {
   199     return bridgeRMI.moveLevelDown(calcTreeID, iteratorID);
   200   }
   201 
   202   boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
   203     return bridgeRMI.moveTactic(calcTreeID, iteratorID);
   204   }
   205 
   206   boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
   207     return bridgeRMI.moveFormula(calcTreeID, iteratorID);
   208   }
   209 
   210   CalcElement getElement(int calcTreeID, int iteratorID) throws RemoteException {
   211     return bridgeRMI.getElement(calcTreeID, iteratorID);
   212   }
   213   /*
   214     public CalcHead tryRefine(int calcTreeID, CalcHeadCompoundID problemID) throws RemoteException {
   215       return bridgeRMI.tryRefine(calcTreeID, problemID);
   216     }
   217   
   218     public CalcHead tryMatch(int calcTreeID, CalcHeadCompoundID problemID) throws RemoteException {
   219       return bridgeRMI.tryMatch(calcTreeID, problemID);
   220     }
   221   */
   222   
   223   /**
   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
   227    */
   228   public boolean destruct(int calcTreeID) throws RemoteException {    
   229     return bridgeRMI.destruct(calcTreeID);    
   230   }
   231  
   232   /**
   233    * Save a calcTree
   234    * @param calcTreeID id of the calcTree to be saved
   235    * @return a vector containing strings, representing the status of the calcTree
   236    */
   237   public Vector saveCalcTree(int calcTreeID) {
   238     try {
   239       return bridgeRMI.saveCalcTree(calcTreeID);
   240     } catch (RemoteException e) {
   241       e.printStackTrace();
   242     }
   243     return null;
   244   }
   245 
   246   /**
   247    * Load a stored calcTree
   248    * @param v Vector: the return value of saveCalcTree
   249    * @return a calcTree reference
   250    */
   251   public CalcTree loadCalcTree(Vector v) {    
   252     try {
   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); 
   259       hotSpot.moveRoot();
   260       return calcTree; 
   261     } catch (RemoteException e) {
   262       e.printStackTrace();
   263     }  
   264     return null;
   265   }
   266 }