4 import isac.session.SDialog;
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;
13 import java.io.Serializable;
14 import java.net.MalformedURLException;
16 import java.rmi.registry.LocateRegistry;
17 import java.rmi.server.UnicastRemoteObject;
18 import java.util.Vector;
20 import org.apache.log4j.Logger;
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
29 * @author Alan Krempler
31 public class DialogGuide extends UnicastRemoteObject implements DGuide,
32 IToUser, Serializable {
34 static Logger logger_ = Logger.getLogger(BridgeRMI.class.getName());
36 protected MathEngine math_engine_;
38 //the DialogGuide knowing the CalcTree is the price for closing off
39 //the CalcTree from the Worksheet (before DGuide implemented IToCalc!)
40 protected CalcTree calc_tree_;
42 //protected CalcHead calc_head_;
44 //protected CalcHead calc_head_sub_;
48 private Vector datachange_listeners_;
50 private IToWorksheet ui_control_listener_;
52 //protected String session_id_;WN0503
54 protected UserSettings user_settings_;//hold whole hashmap locally
58 * URL of the math engine to be used
60 * path(without URL) to UserSettings, UserModel
61 * @param session_dialog
63 * which create the DialogGuide and Worksheet
64 * @throws RemoteException
66 public DialogGuide(String ME_path, String user_path,
67 SDialog session_dialog, String session_id) throws RemoteException {
70 datachange_listeners_ = new Vector();
71 MathEngine.init(ME_path);
72 math_engine_ = MathEngine.getMathEngine();
73 phase_ = DIALOGPHASE_IDLE;
77 user = session_dialog.getUser(session_id);
78 } catch (RemoteException e1) {
81 user_settings_ = new UserSettings(user_path, user.getUsername());
87 public CalcTree startCalculation(int user_id, Formalization f,
88 int start_from/* WN050421 , int requested_calchead_view */) {
90 phase_ = DIALOGPHASE_SOLVE;
91 calc_tree_ = math_engine_.startCalculation(f);
92 calc_tree_.addDataChangeListener(this);
97 * at the beginning of a calculation as well as of a subproblem;
99 * @return CalcHead eg. for providing CalcHead#VIEWSTYLE_*
101 * @see isac.wsdialog.DGuide#startSpecifying()
103 public CalcHead startSpecifying() {
104 ICalcIterator it = null;
105 CalcHead calc_head = null;
108 it = calc_tree_.iterator();
109 // WN0504DI it = this.iterator();//WN050421 NEW...
111 //WN0504DI ...error DialogGuide_Stub.startSpecifying(Unknown
112 //WN0504DI Source) with ...NEW
113 calc_head = (CalcHead) it.getFormula();
114 } catch (RemoteException e) {
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;
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;
133 //________________________________________________________
135 //=case IUserSettings.VAL1_SKIP_SPECIFY_TO_START_SOLVE:
136 calc_tree_.completeCalcHead();
137 calchead_view = CalcHead.VIEWSTYLE_SINGLELINE;
138 phase_ = DIALOGPHASE_SOLVE;
141 calc_head.setViewStyle(calchead_view);
146 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
148 // public void modifyCalcHead(CalcHead calc_head) {
149 // // MathEngine.getMathEngine().modifyCalcHead(calc_head);
150 // calc_tree_.modifyCalcHead(calc_head);
154 * dismiss the CalcHeadPanel and start DGuide#DIALOGPHASE_SOLVE
156 * @see isac.util.interfaces.IToCalc#startSolving()
158 public void startSolving() throws Exception {
159 calc_tree_.startSolving();
163 private CalcChangedEvent toDialogIterators(CalcChangedEvent event) {
164 event.getLastUnchangedFormula();
169 * accept the changes in a calculation reported by the bridge ..............
170 * TODO handle these changes according to UserSettings and UserModel
172 * ... and pass them to the WorkSheet
174 * @see isac.wsdialog.IToUser#calcChanged(isac.wsdialog.CalcChangedEvent)
176 public void calcChanged(CalcChangedEvent event) throws RemoteException {
177 logger_.fatal(" WS<-. . : calcChanged (unc="
178 + event.getLastUnchangedFormula().toSMLString() + ", del="
179 + event.getLastDeletedFormula().toSMLString() + ", gen="
180 + event.getLastGeneratedFormula().toSMLString());
182 for (int i = 0; i < datachange_listeners_.size(); i++) {
183 ((IToUser) datachange_listeners_.elementAt(i))
185 //WN050511->AK?LK? ^^^^^^^^^ cast to DialogIterators ?
187 } catch (Exception e) {
193 * the <code>Dialog#iterator</code> calls methods upon the
194 * <code>CalcTree#iterator</code> eg. in <code>getFormula</code>.
196 * @param CalcIterator
197 * which causes the construction
199 * @see isac.wsdialog.IToCalc#iterator()
201 public ICalcIterator iterator(ICalcIterator it) {
204 // return new DialogIterator((CalcIterator) calc_tree_.iterator(),
205 // //.......................(it, this);
207 // } catch (Exception e) {
208 // e.printStackTrace();
212 return new DialogIterator((CalcIterator) it, (DGuide) this);
213 } catch (RemoteException e) {
214 // TODO Auto-generated catch block
221 * @see isac.wsdialog.IToCalc#addListener(isac.wsdialog.IToUser)
223 public boolean addDataChangeListener(IToUser listener)
224 throws RemoteException {
225 if (datachange_listeners_.contains(listener)) {
228 datachange_listeners_.add(listener);
233 public boolean registerUIControlListener(IToWorksheet listener)
234 throws RemoteException {
236 // if (ui_control_listener_ != null) {
237 // ui_control_listener_.doUIAction(new UserAction(UI_DO_DETACH));
239 ui_control_listener_ = listener;
244 // * @see isac.wsdialog.IToCalc#fetchProposedTactic
245 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
247 // public Tactic fetchProposedTactic() {
248 // return calc_tree_.fetchProposedTactic();
253 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
255 // public int setNextTactic(Tactic tactic) {
256 // return calc_tree_.setNextTactic(tactic);
260 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
262 // public int autoCalculate(int scope, int nSteps) {
263 // return calc_tree_.autoCalculate(scope, nSteps);
267 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
269 // public void tryMatch(CalcHead ch, CalcHeadCompoundID problemID)
270 // throws NotInSpecificationPhaseException {
272 // // MathEngine.getMathEngine().tryMatch(ch, problemID);
273 // math_engine_.tryMatch(ch, problemID);
277 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
279 // public void tryRefine(CalcHead ch, CalcHeadCompoundID problemID)
280 // throws NotInSpecificationPhaseException {
282 // // MathEngine.getMathEngine().tryRefine(ch , problemID);
283 // math_engine_.tryRefine(ch, problemID);
287 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
289 // public ICalcIterator getActiveFormula() {
290 // return calc_tree_.getActiveFormula();
294 // * @throws RemoteException
295 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
297 // public int replaceFormula(CalcFormula newFormula) throws RemoteException
299 // return calc_tree_.replaceFormula(newFormula);
303 // * @throws RemoteException
304 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
306 // public int appendFormula(CalcFormula newFormula) throws RemoteException {
307 // return calc_tree_.appendFormula(newFormula);
311 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
313 // public void moveActiveFormula(ICalcIterator newActiveFormula) {
314 // calc_tree_.moveActiveFormula(newActiveFormula);
318 * Notify the Dialog Guide about interaction from the User
321 * @return true, if the action has been processed, false if the processing
322 * the action has been denied
323 * @throws RemoteException
326 public boolean notifyUserAction(IUserAction action)
327 throws DialogProtocolException, RemoteException {
328 int request = action.getActionID();
329 logger_.fatal(" WS->DG: notifyUserAction request= " + request);
331 // two userActions dont care about the dialogphase
332 //WN050420 would be nicer, if DialogGuide would set the Buttons
334 case (UI_CALCULATE_1):
335 if (phase_ == DIALOGPHASE_SPECIFY)
336 request = UI_SPECIFY_TO_SOLVE;//WN#####################
338 request = UI_SOLVE_CALCULATE_1;
340 case (UI_CALCULATE_ALL):
341 if (phase_ == DIALOGPHASE_SPECIFY)
342 request = UI_SPECIFY_CALCULATE_ALL;
344 request = UI_SOLVE_CALCULATE_ALL;
346 // all other userActions belong to a certain dialogphase
349 case DIALOGPHASE_IDLE:
350 throw new DialogProtocolException(request, phase_);
351 case DIALOGPHASE_SPECIFY:
352 if (request < UI_DUMMY_FIRST_SPECIFY
353 || request > UI_DUMMY_LAST_SPECIFY)
354 throw new DialogProtocolException(request, phase_);
356 case DIALOGPHASE_SOLVE:
357 if (request < UI_DUMMY_FIRST_SOLVE || request > UI_DUMMY_LAST_SOLVE)
358 throw new DialogProtocolException(request, phase_);
362 //*********** handle IUserAction's **********************
363 CalcHead calc_head = new CalcHead();//WN0502023?????????
365 calc_head = (CalcHead) calc_tree_.iterator().getFormula();
366 } catch (Exception e) {
370 case UI_SPECIFY_TRY_MATCH:
371 throw new DialogNotImplementedException(request, phase_);
373 // tryMatch(calc_head, ((UserActionOnCalcHeadCompoundID) action)
375 // } catch (NotInSpecificationPhaseException e) {
376 // throw new DialogMathException(request, phase_, e);
380 case UI_SPECIFY_TRY_REFINE:
381 throw new DialogNotImplementedException(request, phase_);
383 // tryRefine(calc_head, ((UserActionOnCalcHeadCompoundID) action)
385 // } catch (NotInSpecificationPhaseException e) {
386 // throw new DialogMathException(request, phase_, e);
390 case UI_SPECIFY_CHANGE_VIEW:
391 calc_head.setViewStyle(((UserActionOnInt) action).getInt());
394 case UI_SPECIFY_COMPLETE_CALCHEAD:
395 calc_tree_.completeCalcHead();
396 // old-style filling the fields of the calc-head by setting
398 // calc_head_.setCalcHeadStatus(CalcHead.MODEL_ITEM_HELP_ME);
399 // modifyCalcHead(calc_head_);
401 case UI_SPECIFY_COMPLETE_METHOD:
402 case UI_SPECIFY_COMPLETE_THEORY:
403 case UI_SPECIFY_COMPLETE_PROBLEM:
404 case UI_SPECIFY_COMPLETE_GIVEN:
405 case UI_SPECIFY_COMPLETE_FIND:
406 case UI_SPECIFY_COMPLETE_RELATE:
407 throw new DialogNotImplementedException(request, phase_);
409 case UI_SPECIFY_CHECK_CALCHEAD:
410 calc_tree_.modifyCalcHead(calc_head);
413 // was: startCalculation() WN050428 ? really ?
414 case UI_SPECIFY_TO_SOLVE:
415 case UI_SPECIFY_CALCULATE_ALL:
416 //if (user_settings_.getValue(""))
418 calc_tree_.completeCalcHead();
420 calc_tree_.modifyCalcHead(calc_head);
422 if (calc_head.getCalcHeadStatus() != CalcHead.MODEL_ITEM_CORRECT)
426 calc_tree_.startSolving();
427 } catch (Exception e) {
428 // TODO Auto-generated catch block
431 // calc_tree_ = MathEngine.getMathEngine().startSolving(calcHead);
432 // FIXME: AK020224: do not register with the calc_tree_ if finished
433 // specifying a SUBPROBLEM
434 //calc_tree_.addDataChangeListener(this);//WN050420-->startCalculation
436 phase_ = DIALOGPHASE_SOLVE;//FIXXXME.WN050428
437 // fall through to calculate
439 case UI_SOLVE_CALCULATE_1:
440 //WN050420 why not at (*) here ?
441 case UI_SOLVE_CALCULATE_ALL:
443 case UI_SPECIFY_CALCULATE_ALL:
444 case UI_SOLVE_CALCULATE_ALL:
445 calc_tree_.autoCalculate(IToCalc.SCOPE_CALCULATION, 0);
447 case UI_SPECIFY_TO_SOLVE://WN050420 ???
450 calc_tree_.autoCalculate(IToCalc.SCOPE_CALCULATION, 1);
451 //why not at (*) above ?
456 case UI_SOLVE_CALCULATE_SUBPROBLEM:
457 calc_tree_.autoCalculate(IToCalc.SCOPE_SUBPROBLEM, 0);
460 case UI_SOLVE_EDIT_ACTIVE_FORMULA:
461 ui_control_listener_.doUIAction(new UserAction(UI_DO_EDIT_FORMULA));
464 case UI_SOLVE_EDIT_ACTIVE_FORMULA_COMPLETE:
465 CalcFormula formula = (CalcFormula) ((UserActionOnCalcElement) action)
467 // if the position is null, the formula did not exist, so we try to
468 // append a new formula
469 if (formula.getPosition() == null) {
470 calc_tree_.appendFormula(formula);
472 calc_tree_.replaceFormula(formula);
476 case UI_SOLVE_APPEND_USER_FORMULA:
477 // appendFormula((Formula) ((UserActionOnCalcElement)
478 // action).getCalcElement());
480 .doUIAction(new UserAction(UI_DO_APPEND_FORMULA));
483 case UI_SOLVE_MOVE_ACTIVE_FORMULA:
484 calc_tree_.moveActiveFormula(((UserActionOnIterator) action)
488 case UI_SOLVE_GET_PROPOSED_TACTIC:
489 throw new DialogNotImplementedException(request, phase_);
491 case UI_SOLVE_GET_APPLICABLE_TACTICS:
492 throw new DialogNotImplementedException(request, phase_);
494 case UI_SOLVE_SET_NEXT_TACTIC:
496 .setNextTactic((Tactic) ((UserActionOnCalcElement) action)
498 //WN041126 extracted from isac.bridge.CalcTree#setNextTactic @@@
499 //autoCalculate(SCOPE_CALCULATION, 1);
502 case UI_SOLVE_HELP_ENTERING_FORMULA:
503 throw new DialogNotImplementedException(request, phase_);
505 case UI_SOLVE_SHOW_ASSUMPTIONS:
506 throw new DialogNotImplementedException(request, phase_);
508 case UI_SOLVE_SHOW_DETAILS:
509 throw new DialogNotImplementedException(request, phase_);
512 throw new DialogUnknownActionException(request, phase_);
517 private void rmiBind() {
518 if (System.getSecurityManager() == null) {
519 System.setSecurityManager(new RMISecurityManager());
523 LocateRegistry.createRegistry(1099);
524 } catch (java.rmi.RemoteException exc2) {
525 System.err.println("can not create registry: " + exc2.getMessage());
528 String name = "//localhost/isac-DialogGuide";
530 System.out.println("try to bind as " + name);
531 Naming.rebind(name, this);
532 System.out.println("Dialog Guide bound to " + name);
533 //WN040906 was Object Manager ...
534 } catch (java.rmi.ConnectException e) {
535 System.err.println("failed to contact as " + name
536 + " (creating RMI-Server on localhost: 1099)");
537 } catch (RemoteException e) {
538 // TODO Auto-generated catch block
540 } catch (MalformedURLException e) {
541 // TODO Auto-generated catch block
546 // public static void main(String[] args) {
548 // new DialogGuide(args[0]);
550 // } catch (RemoteException e) {
551 // e.printStackTrace();
557 // isac.util.interfaces.IToCalc#completeCalcHead(isac.util.formulae.CalcHead)
558 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
560 // public void completeCalcHead(CalcHead calcHead) {
561 // // TODO Auto-generated method stub
567 // isac.util.interfaces.IToCalc#completeCalcHead(isac.util.formulae.CalcHead,
569 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
571 // public void completeCalcHead(CalcHead calcHead, int completeItem) {
572 // // TODO Auto-generated method stub
579 // * @see isac.util.interfaces.IToCalc#completeCalcHead()
580 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
582 // public void completeCalcHead() throws RemoteException {
583 // // TODO Auto-generated method stub WN050223
588 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
591 // isac.util.interfaces.IToCalc#intermediateSteps(isac.util.interfaces.ICalcIterator)
593 // public int intermediateSteps(ICalcIterator i) throws RemoteException {
594 // // TODO Auto-generated method stub
600 // isac.util.interfaces.IToCalc#getElementsFromTo(isac.util.interfaces.ICalcIterator,
601 // * isac.util.interfaces.ICalcIterator, java.lang.Integer, boolean)
602 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
604 // public Vector getElementsFromTo(ICalcIterator iterator_from,
605 // ICalcIterator iterator_to, Integer level,
606 // boolean result_includes_tactics) throws RemoteException {
607 // // TODO Auto-generated method stub