source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/temporalrelation/TrackBarSelectionDetectionRule.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

File size: 8.1 KB
Line 
1//-------------------------------------------------------------------------------------------------
2// Module    : $RCSfile: TrackBarSelectionDetectionRule.java,v $
3// Version   : $Revision: 0.0 $  $Author: patrick $  $Date: 28.04.2012 $
4// Project   : TaskTreeTemporalRelationship
5// Creation  : 2012 by patrick
6// Copyright : Patrick Harms, 2012
7//-------------------------------------------------------------------------------------------------
8package de.ugoe.cs.quest.tasktrees.temporalrelation;
9
10import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality;
11import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager;
12import de.ugoe.cs.quest.tasktrees.treeifc.InteractionTask;
13import de.ugoe.cs.quest.tasktrees.treeifc.Iteration;
14import de.ugoe.cs.quest.tasktrees.treeifc.Selection;
15import de.ugoe.cs.quest.tasktrees.treeifc.Sequence;
16import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeBuilder;
17import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNode;
18import de.ugoe.cs.quest.tasktrees.treeifc.TaskTreeNodeFactory;
19import de.ugoe.cs.tasktree.guimodel.TrackBar;
20import de.ugoe.cs.tasktree.userinteraction.ValueSelection;
21
22//-------------------------------------------------------------------------------------------------
23/**
24 * TODO comment
25 *
26 * @version $Revision: $ $Date: 28.04.2012$
27 * @author 2012, last modified by $Author: patrick$
28 */
29//-------------------------------------------------------------------------------------------------
30public class TrackBarSelectionDetectionRule implements TemporalRelationshipRule
31{
32
33  /** */
34  private NodeEqualityRuleManager mNodeEqualityRuleManager;
35
36  //-----------------------------------------------------------------------------------------------
37  /**
38   * TODO: comment
39   *
40   */
41  //-----------------------------------------------------------------------------------------------
42  TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager)
43  {
44    super();
45    mNodeEqualityRuleManager = nodeEqualityRuleManager;
46  }
47
48  //-----------------------------------------------------------------------------------------------
49  /* (non-Javadoc)
50   * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory)
51   */
52  //-----------------------------------------------------------------------------------------------
53  @Override
54  public RuleApplicationResult apply(TaskTreeNode        parent,
55                                     TaskTreeBuilder     builder,
56                                     TaskTreeNodeFactory nodeFactory,
57                                     boolean             finalize)
58  {
59    if (!(parent instanceof Sequence))
60    {
61      return null;
62    }
63   
64    RuleApplicationResult result = new RuleApplicationResult();
65   
66    int valueSelectionStartIndex = -1;
67   
68    int index = 0;
69    while (index < parent.getChildren().size())
70    {
71      TaskTreeNode child = parent.getChildren().get(index);
72     
73      if ((child instanceof InteractionTask) &&
74          (((InteractionTask) child).getGUIElement() instanceof TrackBar) &&
75          (((InteractionTask) child).getInteraction() instanceof ValueSelection))
76      {
77        if (valueSelectionStartIndex < 0)
78        {
79          // let the show begin
80          valueSelectionStartIndex = index;
81        }
82      }
83      else if (valueSelectionStartIndex >= 0)
84      {
85        // current child is no more value selection. But the preceding tasks were. Therefore,
86        // create an iteration with the different selectable values as selection children
87        handleValueSelections
88          (valueSelectionStartIndex, index - 1, parent, builder, nodeFactory, result);
89       
90        return result;
91      }
92     
93      index++;
94    }
95   
96    if (valueSelectionStartIndex >= 0)
97    {
98      if (finalize)
99      {
100        handleValueSelections(valueSelectionStartIndex, parent.getChildren().size() - 1, parent,
101                              builder, nodeFactory, result);
102      }
103      else
104      {
105        result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
106      }
107    }
108   
109    return result;
110  }
111
112  //-----------------------------------------------------------------------------------------------
113  /**
114   * TODO: comment
115   *
116   * @param valueSelectionStartIndex
117   * @param i
118   */
119  //-----------------------------------------------------------------------------------------------
120  private void handleValueSelections(int                   startIndex,
121                                     int                   endIndex,
122                                     TaskTreeNode          parent,
123                                     TaskTreeBuilder       builder,
124                                     TaskTreeNodeFactory   nodeFactory,
125                                     RuleApplicationResult result)
126  {
127    Iteration iteration = nodeFactory.createNewIteration();
128    result.addNewlyCreatedParentNode(iteration);
129   
130    Selection selection = nodeFactory.createNewSelection();
131    result.addNewlyCreatedParentNode(selection);
132    builder.setChild(iteration, selection);
133   
134    for (int i = endIndex - startIndex; i >= 0 ; i--)
135    {
136      addChildIfNecessary
137        (selection, parent.getChildren().get(startIndex), builder, nodeFactory, result);
138      builder.removeChild((Sequence) parent, startIndex);
139    }
140   
141    builder.addChild((Sequence) parent, startIndex, iteration);
142   
143    result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
144  }
145
146  //-----------------------------------------------------------------------------------------------
147  /**
148   *
149   */
150  //-----------------------------------------------------------------------------------------------
151  private void addChildIfNecessary(Selection             parentSelection,
152                                   TaskTreeNode          node,
153                                   TaskTreeBuilder       builder,
154                                   TaskTreeNodeFactory   nodeFactory,
155                                   RuleApplicationResult result)
156  {
157    for (int i = 0; i < parentSelection.getChildren().size(); i++)
158    {
159      TaskTreeNode child = parentSelection.getChildren().get(i);
160
161      if (child instanceof InteractionTask)
162      {
163        // check, if the new node is a variant for the current interaction task
164        NodeEquality nodeEquality = mNodeEqualityRuleManager.applyRules(child, node);
165        if (nodeEquality.getSemanticalEquality())
166        {
167          // the node is a variant. If it not structurally equal, a new sub-selection for the
168          // existing and the new node must be created. Otherwise, the new node does not need
169          // to be added
170          if (!nodeEquality.getStructuralEquality())
171          {
172            Selection selection = nodeFactory.createNewSelection();
173            result.addNewlyCreatedParentNode(selection);
174            builder.addChild(parentSelection, selection);
175
176            builder.addChild(selection, child);
177            builder.addChild(selection, node);
178            builder.removeChild(parentSelection, child);
179          }
180
181          return;
182        }
183      }
184      else if (child instanceof Selection)
185      {
186        // check, if the new node is a variant for the semantically equal children of the current
187        // selection
188        boolean addNode = true;
189        for (int j = 0; j < child.getChildren().size(); j++)
190        {
191          NodeEquality nodeEquality = mNodeEqualityRuleManager.applyRules(child, node);
192          if (!nodeEquality.getSemanticalEquality())
193          {
194            // the new node is no semantical equivalent of the nodes in the current selection -
195            // break up
196            addNode = false;
197            break;
198          }
199          else if (nodeEquality.getStructuralEquality())
200          {
201            addNode = false;
202            break;
203          }
204        }
205
206        if (addNode)
207        {
208          // the node is a semantical equivalent to all the nodes in the existing sub-selection
209          // but it is not structurally identical to either of them. Therefore add it.
210          builder.addChild((Selection) child, node);
211          return;
212        }
213      }
214    }
215   
216    // if we did not return in the previous checks, then the node must be added
217    builder.addChild(parentSelection, node);
218  }
219
220}
Note: See TracBrowser for help on using the repository browser.