source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/temporalrelation/TrackBarSelectionDetectionRule.java @ 557

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