java: isac.util.tactics .. end renamed, formatted, inst_var_, import cleaned
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 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_;
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();
164 * accept the changes in a calculation reported by the bridge ..............
165 * TODO handle these changes according to UserSettings and UserModel
167 * ... and pass them to the Worksheet
169 * @see isac.wsdialog.IToUser#calcChanged(isac.wsdialog.CalcChangedEvent)
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());
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));
182 } catch (Exception e) {
188 * @see isac.wsdialog.DGuide#iterator(CalcIterator, DGuide)
189 * @see isac.util.interfaces.IToCalc#iterator()
191 public ICalcIterator iterator(CalcIterator it, DGuide dg) {
192 //WN050512...was before:
193 //public ICalcIterator iterator() {
195 // return new DialogIterator((CalcIterator) calc_tree_.iterator(),
197 // } catch (Exception e) {
198 // e.printStackTrace();
202 return new DialogIterator(it, dg);
203 } catch (RemoteException e) {
210 * @see isac.wsdialog.IToCalc#addListener(isac.wsdialog.IToUser)
212 public boolean addDataChangeListener(IToUser listener)
213 throws RemoteException {
214 if (datachange_listeners_.contains(listener)) {
217 datachange_listeners_.add(listener);
222 public boolean registerUIControlListener(IToWorksheet listener)
223 throws RemoteException {
225 // if (ui_control_listener_ != null) {
226 // ui_control_listener_.doUIAction(new UserAction(UI_DO_DETACH));
228 ui_control_listener_ = listener;
233 // * @see isac.wsdialog.IToCalc#fetchProposedTactic
234 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
236 // public Tactic fetchProposedTactic() {
237 // return calc_tree_.fetchProposedTactic();
242 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
244 // public int setNextTactic(Tactic tactic) {
245 // return calc_tree_.setNextTactic(tactic);
249 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
251 // public int autoCalculate(int scope, int nSteps) {
252 // return calc_tree_.autoCalculate(scope, nSteps);
256 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
258 // public void tryMatch(CalcHead ch, CalcHeadCompoundID problemID)
259 // throws NotInSpecificationPhaseException {
261 // // MathEngine.getMathEngine().tryMatch(ch, problemID);
262 // math_engine_.tryMatch(ch, problemID);
266 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
268 // public void tryRefine(CalcHead ch, CalcHeadCompoundID problemID)
269 // throws NotInSpecificationPhaseException {
271 // // MathEngine.getMathEngine().tryRefine(ch , problemID);
272 // math_engine_.tryRefine(ch, problemID);
276 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
278 // public ICalcIterator getActiveFormula() {
279 // return calc_tree_.getActiveFormula();
283 // * @throws RemoteException
284 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
286 // public int replaceFormula(CalcFormula newFormula) throws RemoteException
288 // return calc_tree_.replaceFormula(newFormula);
292 // * @throws RemoteException
293 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
295 // public int appendFormula(CalcFormula newFormula) throws RemoteException {
296 // return calc_tree_.appendFormula(newFormula);
300 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
302 // public void moveActiveFormula(ICalcIterator newActiveFormula) {
303 // calc_tree_.moveActiveFormula(newActiveFormula);
307 * Notify the Dialog Guide about interaction from the User
310 * @return true, if the action has been processed, false if the processing
311 * the action has been denied
312 * @throws RemoteException
315 public boolean notifyUserAction(IUserAction action)
316 throws DialogProtocolException, RemoteException {
317 int request = action.getActionID();
318 logger_.fatal(" WS->DG: notifyUserAction request= " + request);
320 // two userActions dont care about the dialogphase
321 //WN050420 would be nicer, if DialogGuide would set the Buttons
323 case (UI_CALCULATE_1):
324 if (phase_ == DIALOGPHASE_SPECIFY)
325 request = UI_SPECIFY_TO_SOLVE;//WN#####################
327 request = UI_SOLVE_CALCULATE_1;
329 case (UI_CALCULATE_ALL):
330 if (phase_ == DIALOGPHASE_SPECIFY)
331 request = UI_SPECIFY_CALCULATE_ALL;
333 request = UI_SOLVE_CALCULATE_ALL;
335 // all other userActions belong to a certain dialogphase
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_);
345 case DIALOGPHASE_SOLVE:
346 if (request < UI_DUMMY_FIRST_SOLVE || request > UI_DUMMY_LAST_SOLVE)
347 throw new DialogProtocolException(request, phase_);
351 //*********** handle IUserAction's **********************
352 CalcHead calc_head = new CalcHead();//WN0502023?????????
354 calc_head = (CalcHead) calc_tree_.iterator().getFormula();
355 } catch (Exception e) {
359 case UI_SPECIFY_TRY_MATCH:
360 throw new DialogNotImplementedException(request, phase_);
362 // tryMatch(calc_head, ((UserActionOnCalcHeadCompoundID) action)
364 // } catch (NotInSpecificationPhaseException e) {
365 // throw new DialogMathException(request, phase_, e);
369 case UI_SPECIFY_TRY_REFINE:
370 throw new DialogNotImplementedException(request, phase_);
372 // tryRefine(calc_head, ((UserActionOnCalcHeadCompoundID) action)
374 // } catch (NotInSpecificationPhaseException e) {
375 // throw new DialogMathException(request, phase_, e);
379 case UI_SPECIFY_CHANGE_VIEW:
380 calc_head.setViewStyle(((UserActionOnInt) action).getInt());
383 case UI_SPECIFY_COMPLETE_CALCHEAD:
384 calc_tree_.completeCalcHead();
385 // old-style filling the fields of the calc-head by setting
387 // calc_head_.setCalcHeadStatus(CalcHead.MODEL_ITEM_HELP_ME);
388 // modifyCalcHead(calc_head_);
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_);
398 case UI_SPECIFY_CHECK_CALCHEAD:
399 calc_tree_.modifyCalcHead(calc_head);
402 // was: startCalculation() WN050428 ? really ?
403 case UI_SPECIFY_TO_SOLVE:
404 case UI_SPECIFY_CALCULATE_ALL:
405 //if (user_settings_.getValue(""))
407 calc_tree_.completeCalcHead();
409 calc_tree_.modifyCalcHead(calc_head);
411 if (calc_head.getCalcHeadStatus() != CalcHead.MODEL_ITEM_CORRECT)
415 calc_tree_.startSolving();
416 } catch (Exception e) {
417 // TODO Auto-generated catch block
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
425 phase_ = DIALOGPHASE_SOLVE;//FIXXXME.WN050428
426 // fall through to calculate
428 case UI_SOLVE_CALCULATE_1:
429 //WN050420 why not at (*) here ?
430 case UI_SOLVE_CALCULATE_ALL:
432 case UI_SPECIFY_CALCULATE_ALL:
433 case UI_SOLVE_CALCULATE_ALL:
434 calc_tree_.autoCalculate(IToCalc.SCOPE_CALCULATION, 0);
436 case UI_SPECIFY_TO_SOLVE://WN050420 ???
439 calc_tree_.autoCalculate(IToCalc.SCOPE_CALCULATION, 1);
440 //why not at (*) above ?
445 case UI_SOLVE_CALCULATE_SUBPROBLEM:
446 calc_tree_.autoCalculate(IToCalc.SCOPE_SUBPROBLEM, 0);
449 case UI_SOLVE_EDIT_ACTIVE_FORMULA:
450 ui_control_listener_.doUIAction(new UserAction(UI_DO_EDIT_FORMULA));
453 case UI_SOLVE_EDIT_ACTIVE_FORMULA_COMPLETE:
454 CalcFormula formula = (CalcFormula) ((UserActionOnCalcElement) action)
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);
461 calc_tree_.replaceFormula(formula);
465 case UI_SOLVE_APPEND_USER_FORMULA:
466 // appendFormula((Formula) ((UserActionOnCalcElement)
467 // action).getCalcElement());
469 .doUIAction(new UserAction(UI_DO_APPEND_FORMULA));
472 case UI_SOLVE_MOVE_ACTIVE_FORMULA:
473 calc_tree_.moveActiveFormula(((UserActionOnIterator) action)
477 case UI_SOLVE_GET_PROPOSED_TACTIC:
478 throw new DialogNotImplementedException(request, phase_);
480 case UI_SOLVE_GET_APPLICABLE_TACTICS:
481 throw new DialogNotImplementedException(request, phase_);
483 case UI_SOLVE_SET_NEXT_TACTIC:
485 .setNextTactic((Tactic) ((UserActionOnCalcElement) action)
487 //WN041126 extracted from isac.bridge.CalcTree#setNextTactic @@@
488 //autoCalculate(SCOPE_CALCULATION, 1);
491 case UI_SOLVE_HELP_ENTERING_FORMULA:
492 throw new DialogNotImplementedException(request, phase_);
494 case UI_SOLVE_SHOW_ASSUMPTIONS:
495 throw new DialogNotImplementedException(request, phase_);
497 case UI_SOLVE_SHOW_DETAILS:
498 throw new DialogNotImplementedException(request, phase_);
501 throw new DialogUnknownActionException(request, phase_);
506 private void rmiBind() {
507 if (System.getSecurityManager() == null) {
508 System.setSecurityManager(new RMISecurityManager());
512 LocateRegistry.createRegistry(1099);
513 } catch (java.rmi.RemoteException exc2) {
514 System.err.println("can not create registry: " + exc2.getMessage());
517 String name = "//localhost/isac-DialogGuide";
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
529 } catch (MalformedURLException e) {
530 // TODO Auto-generated catch block
535 // public static void main(String[] args) {
537 // new DialogGuide(args[0]);
539 // } catch (RemoteException e) {
540 // e.printStackTrace();
546 // isac.util.interfaces.IToCalc#completeCalcHead(isac.util.formulae.CalcHead)
547 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
549 // public void completeCalcHead(CalcHead calcHead) {
550 // // TODO Auto-generated method stub
556 // isac.util.interfaces.IToCalc#completeCalcHead(isac.util.formulae.CalcHead,
558 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
560 // public void completeCalcHead(CalcHead calcHead, int completeItem) {
561 // // TODO Auto-generated method stub
568 // * @see isac.util.interfaces.IToCalc#completeCalcHead()
569 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
571 // public void completeCalcHead() throws RemoteException {
572 // // TODO Auto-generated method stub WN050223
577 // * @deprecated Use {@link #notifyUserAction(IUserAction)}instead
580 // isac.util.interfaces.IToCalc#intermediateSteps(isac.util.interfaces.ICalcIterator)
582 // public int intermediateSteps(ICalcIterator i) throws RemoteException {
583 // // TODO Auto-generated method stub
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
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