src/java/isac/wsdialog/DialogGuide.java
author wneuper
Wed, 15 Jun 2005 18:31:02 +0200
branchjava_rename_format
changeset 3922 fcd5648113ca
parent 960 30baa0cd35f8
permissions -rw-r--r--
java: isac.util.tactics .. end renamed, formatted, inst_var_, import cleaned
     1 package isac.wsdialog;
     2 
     3 import isac.bridge.*;
     4 import isac.session.SDialog;
     5 import isac.util.*;
     6 import isac.util.formulae.*;
     7 import isac.util.interfaces.*;
     8 import isac.util.tactics.*;
     9 import isac.util.users.IUserSettings;
    10 import isac.util.users.User;
    11 import isac.util.users.UserSettings;
    12 
    13 import java.io.Serializable;
    14 import java.net.MalformedURLException;
    15 import java.rmi.*;
    16 import java.rmi.registry.LocateRegistry;
    17 import java.rmi.server.UnicastRemoteObject;
    18 import java.util.Vector;
    19 
    20 import org.apache.log4j.Logger;
    21 
    22 /**
    23  * The DialogGuide moderates the communication between two instances working on
    24  * the same CalcTree object. One of these instances is the user, the other a
    25  * math engine. Most probably, the user's GUI, the DialogGuide and the math
    26  * engine reside on different machines and communicate with each other by means
    27  * of RMI.
    28  * 
    29  * @author Alan Krempler
    30  */
    31 public class DialogGuide extends UnicastRemoteObject implements DGuide,
    32         IToUser, Serializable {
    33 
    34     static Logger logger_ = Logger.getLogger(BridgeRMI.class.getName());
    35 
    36     protected MathEngine math_engine_;
    37 
    38     //the calc_tree_ is also known by the DialogIterators -- remove AK?LK?
    39     //WN0505 only needed by getElementsFromTo !! move method to Iterator?
    40     protected CalcTree calc_tree_;
    41 
    42     //protected CalcHead calc_head_;
    43 
    44     //protected CalcHead calc_head_sub_;
    45 
    46     protected int phase_;
    47 
    48     private Vector datachange_listeners_;
    49 
    50     private IToWorksheet ui_control_listener_;
    51 
    52     //protected String session_id_;WN0503
    53 
    54     protected UserSettings user_settings_;//hold whole hashmap locally
    55 
    56     /**
    57      * @param ME_path
    58      *            URL of the math engine to be used
    59      * @param user_path
    60      *            path(without URL) to UserSettings, UserModel
    61      * @param session_dialog
    62      * @param session_id
    63      *            which create the DialogGuide and Worksheet
    64      * @throws RemoteException
    65      */
    66     public DialogGuide(String ME_path, String user_path,
    67             SDialog session_dialog, String session_id) throws RemoteException {
    68         super();
    69         this.rmiBind();
    70         datachange_listeners_ = new Vector();
    71         MathEngine.init(ME_path);
    72         math_engine_ = MathEngine.getMathEngine();
    73         phase_ = DIALOGPHASE_IDLE;
    74 
    75         User user = null;
    76         try {
    77             user = session_dialog.getUser(session_id);
    78         } catch (RemoteException e1) {
    79             e1.printStackTrace();
    80         }
    81         user_settings_ = new UserSettings(user_path, user.getUsername());
    82     }
    83 
    84     /**
    85      * start a calculation
    86      */
    87     public CalcTree startCalculation(int user_id, Formalization f,
    88             int start_from/* WN050421 , int requested_calchead_view */) {
    89 
    90         phase_ = DIALOGPHASE_SOLVE;
    91         calc_tree_ = math_engine_.startCalculation(f);
    92         calc_tree_.addDataChangeListener(this);
    93         return calc_tree_;
    94     }
    95 
    96     /**
    97      * at the beginning of a calculation as well as of a subproblem;
    98      * 
    99      * @return CalcHead eg. for providing CalcHead#VIEWSTYLE_*
   100      * 
   101      * @see isac.wsdialog.DGuide#startSpecifying()
   102      */
   103     public CalcHead startSpecifying() {
   104         ICalcIterator it = null;
   105         CalcHead calc_head = null;
   106         int calchead_view;
   107         try {
   108             it = calc_tree_.iterator();
   109             //			WN0504DI it = this.iterator();//WN050421 NEW...
   110             it.moveRoot();
   111             //WN0504DI ...error DialogGuide_Stub.startSpecifying(Unknown
   112             //WN0504DI Source) with ...NEW
   113             calc_head = (CalcHead) it.getFormula();
   114         } catch (RemoteException e) {
   115             e.printStackTrace();
   116         }
   117 
   118         //AK set viewstyle according to user settings
   119         switch (user_settings_.getValue(IUserSettings.KEY1_START_SPECIFY)) {
   120         //________________________________________________________
   121         case IUserSettings.VAL1_SKIP_SPECIFY_TO_START_SOLVE:
   122             calc_tree_.completeCalcHead();
   123             calchead_view = CalcHead.VIEWSTYLE_SINGLELINE;
   124             phase_ = DIALOGPHASE_SOLVE;
   125             break;
   126         //________________________________________________________
   127         case IUserSettings.VAL1_POP_CALCHEAD_WITH_DESCRIPTIONS:
   128             calc_tree_.autoCalculate(IToCalc.SCOPE_CALCULATION, 1);
   129             //...applies the Tactic Model_Problem which generates descriptions
   130             calchead_view = CalcHead.VIEWSTYLE_FULL;
   131             phase_ = DIALOGPHASE_SPECIFY;
   132             break;
   133         //________________________________________________________
   134         default:
   135             //=case IUserSettings.VAL1_SKIP_SPECIFY_TO_START_SOLVE:
   136             calc_tree_.completeCalcHead();
   137             calchead_view = CalcHead.VIEWSTYLE_SINGLELINE;
   138             phase_ = DIALOGPHASE_SOLVE;
   139             break;
   140         }
   141         calc_head.setViewStyle(calchead_view);
   142         return calc_head;
   143     }
   144 
   145     //	/**
   146     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   147     //	 */
   148     //	public void modifyCalcHead(CalcHead calc_head) {
   149     //		// MathEngine.getMathEngine().modifyCalcHead(calc_head);
   150     //		calc_tree_.modifyCalcHead(calc_head);
   151     //	}
   152 
   153     /**
   154      * dismiss the CalcHeadPanel and start DGuide#DIALOGPHASE_SOLVE
   155      * 
   156      * @see isac.util.interfaces.IToCalc#startSolving()
   157      */
   158     public void startSolving() throws Exception {
   159         calc_tree_.startSolving();
   160 
   161     }
   162 
   163     /**
   164      * accept the changes in a calculation reported by the bridge ..............
   165      * TODO handle these changes according to UserSettings and UserModel
   166      * 
   167      * ... and pass them to the Worksheet
   168      * 
   169      * @see isac.wsdialog.IToUser#calcChanged(isac.wsdialog.CalcChangedEvent)
   170      */
   171     public void calcChanged(CalcChangedEvent event) throws RemoteException {
   172         logger_.fatal(" WS<-. . : calcChanged (unc="
   173                 + event.getLastUnchangedFormula().toSMLString() + ", del="
   174                 + event.getLastDeletedFormula().toSMLString() + ", gen="
   175                 + event.getLastGeneratedFormula().toSMLString());
   176         try {
   177             for (int i = 0; i < datachange_listeners_.size(); i++) {
   178                 ((IToUser) datachange_listeners_.elementAt(i))
   179                         .calcChanged(event);//WN050513GOON
   180                 // .calcChanged(event.createDialogIterators(this));
   181             }
   182         } catch (Exception e) {
   183             e.printStackTrace();
   184         }
   185     }
   186 
   187     /**
   188      * @see isac.wsdialog.DGuide#iterator(CalcIterator, DGuide)
   189      * @see isac.util.interfaces.IToCalc#iterator()
   190      */
   191     public ICalcIterator iterator(CalcIterator it, DGuide dg) {
   192         //WN050512...was before:
   193         //public ICalcIterator iterator() {
   194         //		try {
   195         //			return new DialogIterator((CalcIterator) calc_tree_.iterator(),
   196         //					this);
   197         //		} catch (Exception e) {
   198         //			e.printStackTrace();
   199         //		}
   200         //		return null;
   201         try {
   202             return new DialogIterator(it, dg);
   203         } catch (RemoteException e) {
   204             e.printStackTrace();
   205         }
   206         return null;
   207     }
   208 
   209     /**
   210      * @see isac.wsdialog.IToCalc#addListener(isac.wsdialog.IToUser)
   211      */
   212     public boolean addDataChangeListener(IToUser listener)
   213             throws RemoteException {
   214         if (datachange_listeners_.contains(listener)) {
   215             return false;
   216         } else {
   217             datachange_listeners_.add(listener);
   218         }
   219         return true;
   220     }
   221 
   222     public boolean registerUIControlListener(IToWorksheet listener)
   223             throws RemoteException {
   224         //WN0504 ???
   225         //		if (ui_control_listener_ != null) {
   226         //			ui_control_listener_.doUIAction(new UserAction(UI_DO_DETACH));
   227         //		}
   228         ui_control_listener_ = listener;
   229         return true;
   230     }
   231 
   232     //	/**
   233     //	 * @see isac.wsdialog.IToCalc#fetchProposedTactic
   234     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   235     //	 */
   236     //	public Tactic fetchProposedTactic() {
   237     //		return calc_tree_.fetchProposedTactic();
   238     //	}
   239     //
   240     //
   241     //	/**
   242     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   243     //	 */
   244     //	public int setNextTactic(Tactic tactic) {
   245     //		return calc_tree_.setNextTactic(tactic);
   246     //	}
   247     //
   248     //	/**
   249     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   250     //	 */
   251     //	public int autoCalculate(int scope, int nSteps) {
   252     //		return calc_tree_.autoCalculate(scope, nSteps);
   253     //	}
   254     //
   255     //	/**
   256     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   257     //	 */
   258     //	public void tryMatch(CalcHead ch, CalcHeadCompoundID problemID)
   259     //			throws NotInSpecificationPhaseException {
   260     //
   261     //		// MathEngine.getMathEngine().tryMatch(ch, problemID);
   262     //		math_engine_.tryMatch(ch, problemID);
   263     //	}
   264     //
   265     //	/**
   266     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   267     //	 */
   268     //	public void tryRefine(CalcHead ch, CalcHeadCompoundID problemID)
   269     //			throws NotInSpecificationPhaseException {
   270     //
   271     //		// MathEngine.getMathEngine().tryRefine(ch , problemID);
   272     //		math_engine_.tryRefine(ch, problemID);
   273     //	}
   274     //
   275     //	/**
   276     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   277     //	 */
   278     //	public ICalcIterator getActiveFormula() {
   279     //		return calc_tree_.getActiveFormula();
   280     //	}
   281     //
   282     //	/**
   283     //	 * @throws RemoteException
   284     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   285     //	 */
   286     //	public int replaceFormula(CalcFormula newFormula) throws RemoteException
   287     // {
   288     //		return calc_tree_.replaceFormula(newFormula);
   289     //	}
   290     //
   291     //	/**
   292     //	 * @throws RemoteException
   293     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   294     //	 */
   295     //	public int appendFormula(CalcFormula newFormula) throws RemoteException {
   296     //		return calc_tree_.appendFormula(newFormula);
   297     //	}
   298     //
   299     //	/**
   300     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   301     //	 */
   302     //	public void moveActiveFormula(ICalcIterator newActiveFormula) {
   303     //		calc_tree_.moveActiveFormula(newActiveFormula);
   304     //	}
   305     //
   306     /**
   307      * Notify the Dialog Guide about interaction from the User
   308      * 
   309      * @param action
   310      * @return true, if the action has been processed, false if the processing
   311      *         the action has been denied
   312      * @throws RemoteException
   313      *  
   314      */
   315     public boolean notifyUserAction(IUserAction action)
   316             throws DialogProtocolException, RemoteException {
   317         int request = action.getActionID();
   318         logger_.fatal(" WS->DG: notifyUserAction request= " + request);
   319 
   320         // two userActions dont care about the dialogphase
   321         //WN050420 would be nicer, if DialogGuide would set the Buttons
   322         switch (request) {
   323         case (UI_CALCULATE_1):
   324             if (phase_ == DIALOGPHASE_SPECIFY)
   325                 request = UI_SPECIFY_TO_SOLVE;//WN#####################
   326             else
   327                 request = UI_SOLVE_CALCULATE_1;
   328             break;
   329         case (UI_CALCULATE_ALL):
   330             if (phase_ == DIALOGPHASE_SPECIFY)
   331                 request = UI_SPECIFY_CALCULATE_ALL;
   332             else
   333                 request = UI_SOLVE_CALCULATE_ALL;
   334         }
   335         // all other userActions belong to a certain dialogphase
   336 
   337         switch (phase_) {
   338         case DIALOGPHASE_IDLE:
   339             throw new DialogProtocolException(request, phase_);
   340         case DIALOGPHASE_SPECIFY:
   341             if (request < UI_DUMMY_FIRST_SPECIFY
   342                     || request > UI_DUMMY_LAST_SPECIFY)
   343                 throw new DialogProtocolException(request, phase_);
   344             break;
   345         case DIALOGPHASE_SOLVE:
   346             if (request < UI_DUMMY_FIRST_SOLVE || request > UI_DUMMY_LAST_SOLVE)
   347                 throw new DialogProtocolException(request, phase_);
   348             break;
   349         }
   350 
   351         //*********** handle IUserAction's **********************
   352         CalcHead calc_head = new CalcHead();//WN0502023?????????
   353         try {
   354             calc_head = (CalcHead) calc_tree_.iterator().getFormula();
   355         } catch (Exception e) {
   356             e.printStackTrace();
   357         }
   358         switch (request) {
   359         case UI_SPECIFY_TRY_MATCH:
   360             throw new DialogNotImplementedException(request, phase_);
   361         //			try {
   362         //				tryMatch(calc_head, ((UserActionOnCalcHeadCompoundID) action)
   363         //						.getObjectID());
   364         //			} catch (NotInSpecificationPhaseException e) {
   365         //				throw new DialogMathException(request, phase_, e);
   366         //			}
   367         //			break;
   368 
   369         case UI_SPECIFY_TRY_REFINE:
   370             throw new DialogNotImplementedException(request, phase_);
   371         //			try {
   372         //				tryRefine(calc_head, ((UserActionOnCalcHeadCompoundID) action)
   373         //						.getObjectID());
   374         //			} catch (NotInSpecificationPhaseException e) {
   375         //				throw new DialogMathException(request, phase_, e);
   376         //			}
   377         //			break;
   378 
   379         case UI_SPECIFY_CHANGE_VIEW:
   380             calc_head.setViewStyle(((UserActionOnInt) action).getInt());
   381             break;
   382 
   383         case UI_SPECIFY_COMPLETE_CALCHEAD:
   384             calc_tree_.completeCalcHead();
   385             // old-style filling the fields of the calc-head by setting
   386             // the HELP_ME flag
   387             // calc_head_.setCalcHeadStatus(CalcHead.MODEL_ITEM_HELP_ME);
   388             // modifyCalcHead(calc_head_);
   389             break;
   390         case UI_SPECIFY_COMPLETE_METHOD:
   391         case UI_SPECIFY_COMPLETE_THEORY:
   392         case UI_SPECIFY_COMPLETE_PROBLEM:
   393         case UI_SPECIFY_COMPLETE_GIVEN:
   394         case UI_SPECIFY_COMPLETE_FIND:
   395         case UI_SPECIFY_COMPLETE_RELATE:
   396             throw new DialogNotImplementedException(request, phase_);
   397 
   398         case UI_SPECIFY_CHECK_CALCHEAD:
   399             calc_tree_.modifyCalcHead(calc_head);
   400             break;
   401 
   402         // was: startCalculation() WN050428 ? really ?
   403         case UI_SPECIFY_TO_SOLVE:
   404         case UI_SPECIFY_CALCULATE_ALL:
   405             //if (user_settings_.getValue(""))
   406             if (true)
   407                 calc_tree_.completeCalcHead();
   408             else
   409                 calc_tree_.modifyCalcHead(calc_head);
   410 
   411             if (calc_head.getCalcHeadStatus() != CalcHead.MODEL_ITEM_CORRECT)
   412                 return false;
   413 
   414             try {
   415                 calc_tree_.startSolving();
   416             } catch (Exception e) {
   417                 // TODO Auto-generated catch block
   418                 e.printStackTrace();
   419             }
   420             // calc_tree_ = MathEngine.getMathEngine().startSolving(calcHead);
   421             // FIXME: AK020224: do not register with the calc_tree_ if finished
   422             // specifying a SUBPROBLEM
   423             //calc_tree_.addDataChangeListener(this);//WN050420-->startCalculation
   424 
   425             phase_ = DIALOGPHASE_SOLVE;//FIXXXME.WN050428
   426         // fall through to calculate
   427 
   428         case UI_SOLVE_CALCULATE_1:
   429         //WN050420 why not at (*) here ?
   430         case UI_SOLVE_CALCULATE_ALL:
   431             switch (request) {
   432             case UI_SPECIFY_CALCULATE_ALL:
   433             case UI_SOLVE_CALCULATE_ALL:
   434                 calc_tree_.autoCalculate(IToCalc.SCOPE_CALCULATION, 0);
   435                 break;
   436             case UI_SPECIFY_TO_SOLVE://WN050420 ???
   437             // break;
   438             default:
   439                 calc_tree_.autoCalculate(IToCalc.SCOPE_CALCULATION, 1);
   440                 //why not at (*) above ?
   441                 break;
   442             }
   443             break;
   444 
   445         case UI_SOLVE_CALCULATE_SUBPROBLEM:
   446             calc_tree_.autoCalculate(IToCalc.SCOPE_SUBPROBLEM, 0);
   447             break;
   448 
   449         case UI_SOLVE_EDIT_ACTIVE_FORMULA:
   450             ui_control_listener_.doUIAction(new UserAction(UI_DO_EDIT_FORMULA));
   451             break;
   452 
   453         case UI_SOLVE_EDIT_ACTIVE_FORMULA_COMPLETE:
   454             CalcFormula formula = (CalcFormula) ((UserActionOnCalcElement) action)
   455                     .getCalcElement();
   456             // if the position is null, the formula did not exist, so we try to
   457             // append a new formula
   458             if (formula.getPosition() == null) {
   459                 calc_tree_.appendFormula(formula);
   460             } else {
   461                 calc_tree_.replaceFormula(formula);
   462             }
   463             break;
   464 
   465         case UI_SOLVE_APPEND_USER_FORMULA:
   466             // appendFormula((Formula) ((UserActionOnCalcElement)
   467             // action).getCalcElement());
   468             ui_control_listener_
   469                     .doUIAction(new UserAction(UI_DO_APPEND_FORMULA));
   470             break;
   471 
   472         case UI_SOLVE_MOVE_ACTIVE_FORMULA:
   473             calc_tree_.moveActiveFormula(((UserActionOnIterator) action)
   474                     .getPosition());
   475             break;
   476 
   477         case UI_SOLVE_GET_PROPOSED_TACTIC:
   478             throw new DialogNotImplementedException(request, phase_);
   479 
   480         case UI_SOLVE_GET_APPLICABLE_TACTICS:
   481             throw new DialogNotImplementedException(request, phase_);
   482 
   483         case UI_SOLVE_SET_NEXT_TACTIC:
   484             calc_tree_
   485                     .setNextTactic((Tactic) ((UserActionOnCalcElement) action)
   486                             .getCalcElement());
   487             //WN041126 extracted from isac.bridge.CalcTree#setNextTactic @@@
   488             //autoCalculate(SCOPE_CALCULATION, 1);
   489             break;
   490 
   491         case UI_SOLVE_HELP_ENTERING_FORMULA:
   492             throw new DialogNotImplementedException(request, phase_);
   493 
   494         case UI_SOLVE_SHOW_ASSUMPTIONS:
   495             throw new DialogNotImplementedException(request, phase_);
   496 
   497         case UI_SOLVE_SHOW_DETAILS:
   498             throw new DialogNotImplementedException(request, phase_);
   499 
   500         default:
   501             throw new DialogUnknownActionException(request, phase_);
   502         }
   503         return true;
   504     }
   505 
   506     private void rmiBind() {
   507         if (System.getSecurityManager() == null) {
   508             System.setSecurityManager(new RMISecurityManager());
   509         }
   510 
   511         try {
   512             LocateRegistry.createRegistry(1099);
   513         } catch (java.rmi.RemoteException exc2) {
   514             System.err.println("can not create registry: " + exc2.getMessage());
   515         }
   516 
   517         String name = "//localhost/isac-DialogGuide";
   518         try {
   519             System.out.println("try to bind as " + name);
   520             Naming.rebind(name, this);
   521             System.out.println("Dialog Guide bound to " + name);
   522             //WN040906 was Object Manager ...
   523         } catch (java.rmi.ConnectException e) {
   524             System.err.println("failed to contact as " + name
   525                     + " (creating RMI-Server on localhost: 1099)");
   526         } catch (RemoteException e) {
   527             // TODO Auto-generated catch block
   528             e.printStackTrace();
   529         } catch (MalformedURLException e) {
   530             // TODO Auto-generated catch block
   531             e.printStackTrace();
   532         }
   533     }
   534 
   535     //	public static void main(String[] args) {
   536     //		try {
   537     //			new DialogGuide(args[0]);
   538     //
   539     //		} catch (RemoteException e) {
   540     //			e.printStackTrace();
   541     //		}
   542     //	}
   543 
   544     //	/**
   545     //	 * @see
   546     // isac.util.interfaces.IToCalc#completeCalcHead(isac.util.formulae.CalcHead)
   547     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   548     //	 */
   549     //	public void completeCalcHead(CalcHead calcHead) {
   550     //		// TODO Auto-generated method stub
   551     //
   552     //	}
   553     //
   554     //	/**
   555     //	 * @see
   556     // isac.util.interfaces.IToCalc#completeCalcHead(isac.util.formulae.CalcHead,
   557     //	 * int)
   558     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   559     //	 */
   560     //	public void completeCalcHead(CalcHead calcHead, int completeItem) {
   561     //		// TODO Auto-generated method stub
   562     //
   563     //	}
   564     //
   565     //	/**
   566     //	 * (non-Javadoc)
   567     //	 *
   568     //	 * @see isac.util.interfaces.IToCalc#completeCalcHead()
   569     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   570     //	 */
   571     //	public void completeCalcHead() throws RemoteException {
   572     //		// TODO Auto-generated method stub WN050223
   573     //
   574     //	}
   575     //
   576     //	/**
   577     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   578     //	 *
   579     //	 * @see
   580     // isac.util.interfaces.IToCalc#intermediateSteps(isac.util.interfaces.ICalcIterator)
   581     //	 */
   582     //	public int intermediateSteps(ICalcIterator i) throws RemoteException {
   583     //		// TODO Auto-generated method stub
   584     //		return 0;
   585     //	}
   586     //
   587     //	/**
   588     //	 * @see
   589     // isac.util.interfaces.IToCalc#getElementsFromTo(isac.util.interfaces.ICalcIterator,
   590     //	 * isac.util.interfaces.ICalcIterator, java.lang.Integer, boolean)
   591     //	 * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
   592     //	 */
   593     //	public Vector getElementsFromTo(ICalcIterator iterator_from,
   594     //			ICalcIterator iterator_to, Integer level,
   595     //			boolean result_includes_tactics) throws RemoteException {
   596     //		// TODO Auto-generated method stub
   597     //		return null;
   598     //	}
   599 
   600 }