src/java/isac/bridge/MathEngine.java
author wneuper
Tue, 14 Jun 2005 17:22:50 +0200
changeset 2391 59a60ef2a2c8
parent 2293 aedf91a318c5
child 2443 8e3991a49d1f
permissions -rw-r--r--
java: minor changes preparing tryMatch
     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.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;
    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 bridgeRMI;
    39 
    40 	// Stores the calcTrees after call of startSpecifying //WN00827
    41 	// startCalculation
    42 	// and hands them out to the dialog after startSolving//WN00827 ???
    43 	private Map calcTrees;
    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 			bridgeRMI = (IBridgeRMI) Naming.lookup("//" + hostName
    53 					+ "/BridgeRMI");
    54 			System.out.println("MathEngine Constr: connected to Bridge: "
    55 					+ bridgeRMI.toString());
    56 			if (bridgeRMI == 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 		calcTrees = new HashMap();
    66 	}
    67 
    68 	/*
    69 	 * FIXME.WN040924 just to make the old bridge continue work
    70 	 */
    71 	protected IBridgeRMI getBridgeRMI() {
    72 		return bridgeRMI;
    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 = calcHead.getCalcTreeID();//WN040922 TODO simplify CalcHead
   103 	 * CalcTree calcTree = (CalcTree) calcTrees.get(new Integer(id));//WN040922
   104 	 * 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 = bridgeRMI.startCalculation(f);
   111 			calcTree = new CalcTree(this, id);
   112 			calcTrees.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 bridgeRMI.autoCalculate(id,
   129 	 * scope, nSteps); }
   130 	 */
   131 
   132 	/**
   133 	 * @see IToCalc#startSolving(), copied to CalcTree
   134 	 * 
   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
   139 	 * simplify
   140 	 * 
   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
   146 	 * calcTree; }
   147 	 */
   148 
   149 	/**
   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(); } }
   156 	 */
   157 
   158 	/*
   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(); } }
   166 	 */
   167 
   168 	/*
   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(); } }
   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 tryRefine(CalcHead calcHead, CalcHeadCompoundID problemID)
   185 //			throws NotInSpecificationPhaseException {
   186 //		CalcHead newCalcHead = null;
   187 //		try {
   188 //			//WN040924 newCalcHead =
   189 //			// bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
   190 //			bridgeRMI.tryRefine(calcHead.getCalcTreeID(), problemID);
   191 //			if (newCalcHead == null) {
   192 //				throw new NotInSpecificationPhaseException();
   193 //			}
   194 //			calcHead.fillValuesfrom(newCalcHead);
   195 //		} catch (RemoteException e) {
   196 //			// TODO Auto-generated catch block
   197 //			e.printStackTrace();
   198 //		}
   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 tryMatch(CalcHead calcHead, CalcHeadCompoundID problemID)
   209 //			throws NotInSpecificationPhaseException {
   210 //		CalcHead newCalcHead = null;
   211 //		try {
   212 //			//WN040924 newCalcHead =
   213 //			// bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
   214 //			bridgeRMI.tryMatch(calcHead.getCalcTreeID(), problemID);
   215 //			if (newCalcHead == null) {
   216 //				throw new NotInSpecificationPhaseException();
   217 //			}
   218 //			calcHead.fillValuesfrom(newCalcHead);
   219 //		} catch (RemoteException e) {
   220 //			// TODO Auto-generated catch block
   221 //			e.printStackTrace();
   222 //		}
   223 //	}
   224 
   225 	// Delegated methods from CalcTree/Iterator
   226 	int setNextTactic(int id, Tactic tactic) throws RemoteException {
   227 		return bridgeRMI.setNextTactic(id, tactic);
   228 	}
   229 
   230 	Tactic fetchProposedTactic(int id) throws RemoteException {
   231 		return bridgeRMI.fetchProposedTactic(id);
   232 	}
   233 
   234 	CalcChanged autoCalculate(int id, int scope, int nSteps)
   235 			throws RemoteException {
   236 		logger.debug("autoCalculate: id=" + id + ", scope=" + scope
   237 				+ ", nSteps=" + nSteps);
   238 		return bridgeRMI.autoCalculate(id, scope, nSteps);
   239 	}
   240 
   241 	CalcChanged replaceFormula(int id, CalcFormula f) throws RemoteException {
   242 		return bridgeRMI.replaceFormula(id, f);
   243 	}
   244 
   245 	CalcChanged appendFormula(int id, CalcFormula f) throws RemoteException {
   246 		return bridgeRMI.appendFormula(id, f);
   247 	}
   248 
   249 	CalcChanged intermediateSteps(int id, ICalcIterator ci)
   250 			throws RemoteException {
   251 		return bridgeRMI.intermediateSteps(id, ci);
   252 	}
   253 
   254 	Vector getElementsFromTo(int id, ICalcIterator iterator_from,
   255 			ICalcIterator iterator_to, Integer level,
   256 			boolean result_includes_tactics) throws RemoteException {
   257 		return bridgeRMI.getElementsFromTo(id, iterator_from, iterator_to,
   258 				level, result_includes_tactics);
   259 	}
   260 
   261 	Position moveActiveFormula(int calcTreeID, Position p)
   262 			throws RemoteException {
   263 		return bridgeRMI.moveActiveFormula(calcTreeID, p);
   264 	}
   265 
   266 	int iterator(int id) throws RemoteException {
   267 		logger.debug("iterator: id=" + id);
   268 		return bridgeRMI.iterator(id);
   269 	}
   270 
   271 	Position moveRoot(int calcTreeID, int iteratorID) throws RemoteException {
   272 		logger.debug("moveRoot: calcTreeID=" + calcTreeID + ", iteratorID="
   273 				+ iteratorID);
   274 		return bridgeRMI.moveRoot(calcTreeID, iteratorID);
   275 	}
   276 
   277 	Position moveUp(int calcTreeID, int iteratorID, Position p)
   278 			throws RemoteException {
   279 		return bridgeRMI.moveUp(calcTreeID, iteratorID, p);
   280 	}
   281 
   282 	Position moveDown(int calcTreeID, int iteratorID, Position p)
   283 			throws RemoteException {
   284 		logger.debug("moveDown: calcTreeID=" + calcTreeID + ", sml_pos_=" + p);
   285 		return bridgeRMI.moveDown(calcTreeID, iteratorID, p);
   286 	}
   287 
   288 	Position moveLevelUp(int calcTreeID, int iteratorID, Position p)
   289 			throws RemoteException {
   290 		return bridgeRMI.moveLevelUp(calcTreeID, iteratorID, p);
   291 	}
   292 
   293 	Position moveLevelDown(int calcTreeID, int iteratorID, Position p)
   294 			throws RemoteException {
   295 		return bridgeRMI.moveLevelDown(calcTreeID, iteratorID, p);
   296 	}
   297 
   298 	Position moveCalcHead(int calcTreeID, int iteratorID, Position p)
   299 			throws RemoteException {
   300 		return bridgeRMI.moveCalcHead(calcTreeID, iteratorID, p);
   301 	}
   302 
   303 	boolean moveTactic(int calcTreeID, int iteratorID) throws RemoteException {
   304 		return bridgeRMI.moveTactic(calcTreeID, iteratorID);
   305 	}
   306 
   307 	boolean moveFormula(int calcTreeID, int iteratorID) throws RemoteException {
   308 		return bridgeRMI.moveFormula(calcTreeID, iteratorID);
   309 	}
   310 
   311 	ICalcElement getElement(int calcTreeID, Position p) throws RemoteException {
   312 		return bridgeRMI.getElement(calcTreeID, p);
   313 	}
   314 
   315 	/**
   316 	 * Destruct a calcTree: Gives the memory occupied by this calcTree free
   317 	 * again. Call this method when the calcTree is no longer needed. This
   318 	 * method is irreversible.
   319 	 * 
   320 	 * @param calcTreeID
   321 	 *            id of the calcTree to be destructed
   322 	 */
   323 	public boolean destruct(int calcTreeID) throws RemoteException {
   324 		return bridgeRMI.destruct(calcTreeID);
   325 	}
   326 
   327 	/**
   328 	 * Save a calcTree
   329 	 * 
   330 	 * @param calcTreeID
   331 	 *            id of the calcTree to be saved
   332 	 * @return a vector containing strings, representing the status of the
   333 	 *         calcTree
   334 	 */
   335 	public Vector saveCalcTree(int calcTreeID) {
   336 		try {
   337 			return bridgeRMI.saveCalcTree(calcTreeID);
   338 		} catch (RemoteException e) {
   339 			e.printStackTrace();
   340 		}
   341 		return null;
   342 	}
   343 
   344 	/**
   345 	 * Load a previously stored calcTree
   346 	 * 
   347 	 * @param v
   348 	 *            Vector: the return value of saveCalcTree
   349 	 * @return a calcTree reference
   350 	 */
   351 	public CalcTree loadCalcTree(Vector v) {
   352 		try {
   353 			int id = bridgeRMI.loadCalcTree(v);
   354 			CalcTree calcTree = new CalcTree(this, id);
   355 			calcTrees.put(new Integer(id), calcTree);
   356 			ICalcIterator hotSpot = calcTree.iterator();
   357 			// may be not first iterator (not hotspot)
   358 			((CalcIterator) hotSpot).makeHotSpot();
   359 			// make sure it is a hotspot iterator
   360 			calcTree.setHotSpot(hotSpot);
   361 			hotSpot.moveRoot();
   362 			return calcTree;
   363 		} catch (RemoteException e) {
   364 			e.printStackTrace();
   365 		}
   366 		return null;
   367 	}
   368 
   369 	/**
   370 	 * @see isac.util.interfaces.ICalcIterator#getTactic()
   371 	 */
   372 	public Tactic getTactic(int id, Position pos) throws RemoteException {
   373 		return bridgeRMI.getTactic(id, pos);
   374 	}
   375 
   376 	public Vector getApplicableTactics(int calcTreeID, int scope, Position pos)
   377 			throws RemoteException {
   378 		return bridgeRMI.getAppliableTactics(calcTreeID, scope, pos);
   379 	}
   380 
   381 	/**
   382 	 * @see isac.util.interfaces.ICalcIterator#getAssumptions()
   383 	 */
   384 	public Assumptions getAssumptions(int id, Position pos) throws RemoteException {
   385 		return bridgeRMI.getAssumptions(id, pos);
   386 	}
   387 
   388 	/**
   389 	 * @see isac.util.interfaces.ICalcIterator#getAssumptions()
   390 	 */
   391 	public Assumptions getAccumulatedAssumptions(int id, Position pos) throws RemoteException {
   392 		return bridgeRMI.getAccumulatedAssumptions(id, pos);
   393 	}
   394 
   395 
   396 }