source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/manager/TaskTreeManager.java @ 439

Last change on this file since 439 was 439, checked in by pharms, 12 years ago

initial import after refactoring of module structure with Steffen

  • Property svn:executable set to *
File size: 9.9 KB
Line 
1//-------------------------------------------------------------------------------------------------
2// Module    : $RCSfile: TaskTreeManager.java,v $
3// Version   : $Revision: 0.0 $  $Author: Patrick $  $Date: 06.11.2011 10:14:21 $
4// Project   : TaskTreePerformanceTest
5// Creation  : 2011 by Patrick
6// Copyright : Patrick Harms, 2011
7//-------------------------------------------------------------------------------------------------
8
9package de.ugoe.cs.quest.tasktrees.manager;
10
11import java.util.Observable;
12import java.util.Observer;
13import java.util.logging.Logger;
14
15import de.ugoe.cs.quest.tasktrees.treeifc.InteractionTask;
16import de.ugoe.cs.quest.tasktrees.treeifc.Sequence;
17import de.ugoe.cs.quest.tasktrees.treeifc.TaskTree;
18import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeBuilder;
19import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode;
20import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNodeFactory;
21import de.ugoe.cs.tasktree.guimodel.GUIElement;
22import de.ugoe.cs.tasktree.userinteraction.InteractionEvent;
23import de.ugoe.cs.tasktree.userinteraction.KeyInteraction;
24import de.ugoe.cs.tasktree.userinteraction.KeyboardFocusChange;
25import de.ugoe.cs.tasktree.userinteraction.UserInteractionProvider;
26
27//-------------------------------------------------------------------------------------------------
28/**
29 * TODO comment
30 *
31 * @version $Revision: $ $Date: $
32 * @author  2011, last modified by $Author: $
33 */
34//-------------------------------------------------------------------------------------------------
35public class TaskTreeManager implements Observer
36{
37  /** */
38  private static final int MAX_INTERACTIONS_TILL_RULE_APPLICATION = 100;
39
40  /** */
41  private static Logger LOG = Logger.getLogger(TaskTreeManager.class.getName());
42 
43  /** */
44  private TaskTreeBuilder mTaskTreeBuilder = ComponentManager.getDefaultTaskTreeBuilder();
45 
46  /** */
47  private TaskTreeNodeFactory mTaskTreeNodeFactory =
48    ComponentManager.getDefaultTaskTreeNodeFactory();
49 
50  /** */
51  private int mInteractionsTillRuleApplication = MAX_INTERACTIONS_TILL_RULE_APPLICATION;
52 
53  /** */
54  private Sequence mRootSequence;
55 
56  /** */
57  private GUIElement mCurrentKeyboardFocusGUIElement;
58
59  //-----------------------------------------------------------------------------------------------
60  /**
61   * TODO: comment
62   *
63   */
64  //-----------------------------------------------------------------------------------------------
65  public TaskTreeManager()
66  {
67    mRootSequence = mTaskTreeNodeFactory.createNewSequence();
68  }
69
70  //-----------------------------------------------------------------------------------------------
71  /* (non-Javadoc)
72   * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
73   */
74  //-----------------------------------------------------------------------------------------------
75  public void update(Observable observable, Object event)
76  {
77    if ((observable instanceof UserInteractionProvider) &&
78        (event instanceof InteractionEvent))
79    {
80      handleNewInteractionEvent((InteractionEvent) event);
81    }
82    else
83    {
84      LOG.warning("could not handle notification of " + observable + " regarding " + event);
85    }
86  }
87 
88  //-----------------------------------------------------------------------------------------------
89  /**
90   *
91   */
92  //-----------------------------------------------------------------------------------------------
93  public void handleNewInteractionEvent(InteractionEvent event)
94  {
95    if (event.getInteraction() instanceof KeyInteraction)
96    {
97      if (mCurrentKeyboardFocusGUIElement == null)
98      {
99        mCurrentKeyboardFocusGUIElement = event.getGUIElement();
100      }
101     
102      addInteractionTask(mTaskTreeNodeFactory.createNewInteractionTask
103                           (mCurrentKeyboardFocusGUIElement, event.getInteraction()));
104    }
105    else
106    {
107      addInteractionTask(mTaskTreeNodeFactory.createNewInteractionTask
108                           (event.getGUIElement(), event.getInteraction()));
109    }
110  }
111
112  //-----------------------------------------------------------------------------------------------
113  /**
114   *
115   */
116  //-----------------------------------------------------------------------------------------------
117  public synchronized TaskTree getTaskTree()
118  {
119    LOG.info("applying rules temporal relationship generation rules");
120   
121    Sequence rootSequence = mRootSequence.clone();
122    ComponentManager.getTemporalRelationshipRuleManager().applyRules
123      (rootSequence, mTaskTreeBuilder, mTaskTreeNodeFactory, true);
124
125    return mTaskTreeNodeFactory.createTaskTree(rootSequence);
126  }
127 
128  //-----------------------------------------------------------------------------------------------
129  /**
130   * @param task
131   */
132  //-----------------------------------------------------------------------------------------------
133  private synchronized void addInteractionTask(InteractionTask task)
134  {
135    handleInteractionTask(task);
136  }
137
138  //-----------------------------------------------------------------------------------------------
139  /**
140   * adds the task to the current or the new sequence. The decision depends on the type
141   * of task. If the task finishes the current sequence, the sequence is marked as finished
142   * If the task does not start a new sequence, it is added to the current sequence, before it
143   * is marked s finished. Otherwise it is added to a new sequence.
144   */
145  //-----------------------------------------------------------------------------------------------
146  private void handleInteractionTask(InteractionTask interactionTask)
147  {
148    if (interactionTask.getInteraction() instanceof KeyboardFocusChange)
149    {
150      mCurrentKeyboardFocusGUIElement = interactionTask.getGUIElement();
151    }
152    else
153    {
154      LOG.info("handling interaction task \"" + interactionTask + "\"");
155      addTaskToSequence(interactionTask, mCurrentKeyboardFocusGUIElement);
156    }
157  }
158
159  //-----------------------------------------------------------------------------------------------
160  /**
161   * TODO: comment
162   *
163   * @param interactionTask
164   */
165  //-----------------------------------------------------------------------------------------------
166  /*private boolean handleAndCorrectFocusChanges(InteractionTask interactionTask)
167  {
168    GUIElement newGUIElement = interactionTask.getGUIElement();
169   
170    // find the identical parent element
171    GUIElement identicalParentGUIElement = null;
172   
173    while ((identicalParentGUIElement == null) && (newGUIElement != null))
174    {
175      GUIElement currentGUIElement = mCurrentGUIElement;
176     
177      while ((identicalParentGUIElement == null) && (currentGUIElement != null))
178      {
179        if (newGUIElement.equals(currentGUIElement))
180        {
181          identicalParentGUIElement = newGUIElement;
182        }
183        else
184        {
185          currentGUIElement = currentGUIElement.getParent();
186        }
187      }
188     
189      newGUIElement = newGUIElement.getParent();
190    }
191   
192    // now create focus lost interactions for each GUI element, that is not common with the
193    // hierarchy of the new GUI element
194    GUIElement currentGUIElement = mCurrentGUIElement;
195   
196    List<InteractionTask> tasksToBeAdded = new ArrayList<InteractionTask>();
197   
198    while ((currentGUIElement != null) &&
199           ((identicalParentGUIElement == null) ||
200            (!currentGUIElement.equals(identicalParentGUIElement))))
201    {
202      tasksToBeAdded.add
203        (mTaskTreeNodeFactory.createNewInteractionTask(currentGUIElement, new FocusLost()));
204      currentGUIElement = currentGUIElement.getParent();
205    }
206   
207    // now create focus received interactions for each GUI element, that is not common with
208    // with the old one. Ensure, that if the current interaction is a focus reception, that
209    // it is used instead of a generated one
210    newGUIElement = interactionTask.getGUIElement();
211   
212    int index = tasksToBeAdded.size();
213    while ((newGUIElement != null) &&
214           ((identicalParentGUIElement == null) ||
215            (!newGUIElement.equals(identicalParentGUIElement))))
216    {
217      tasksToBeAdded.add(index, mTaskTreeNodeFactory.createNewInteractionTask
218                           (newGUIElement, new FocusReceived()));
219      newGUIElement = newGUIElement.getParent();
220    }
221   
222    // this part ensures, that the original focus reception, if any, is preserved as is.
223    boolean taskAlreadyHandled = false;
224    if (interactionTask.getInteraction() instanceof FocusReceived)
225    {
226      if (tasksToBeAdded.size() > 0)
227      {
228        tasksToBeAdded.set(tasksToBeAdded.size() - 1, interactionTask);
229      }
230      //else
231      //{
232        // in this case, we already have focus on the element to which the focus shall be changed.
233        // therefore, we discard the new focus change on the same element.
234      //}
235     
236      taskAlreadyHandled = true;
237    }
238   
239    // now that all tasks are determined, add them to the sequence
240    for (InteractionTask task : tasksToBeAdded)
241    {
242      addTaskToSequence(task);
243    }
244   
245    mCurrentGUIElement = interactionTask.getGUIElement();
246   
247    return taskAlreadyHandled;
248  }*/
249
250  //-----------------------------------------------------------------------------------------------
251  /**
252   *
253   */
254  //-----------------------------------------------------------------------------------------------
255  private void addTaskToSequence(TaskTreeNode task, GUIElement currentKeyboardFocusGUIElement)
256  {
257    mTaskTreeBuilder.addChild(mRootSequence, task);
258   
259    if (--mInteractionsTillRuleApplication == 0)
260    {
261      mInteractionsTillRuleApplication = MAX_INTERACTIONS_TILL_RULE_APPLICATION;
262     
263      LOG.info("applying rules temporal relationship generation rules");
264      ComponentManager.getTemporalRelationshipRuleManager().applyRules
265        (mRootSequence, mTaskTreeBuilder, mTaskTreeNodeFactory, false);
266    }
267  }
268
269}
Note: See TracBrowser for help on using the repository browser.