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

Last change on this file since 808 was 808, checked in by pharms, 12 years ago
  • to make it easier, removed creation of sub selections
File size: 5.4 KB
Line 
1package de.ugoe.cs.quest.tasktrees.temporalrelation;
2
3import de.ugoe.cs.quest.eventcore.gui.ValueSelection;
4import de.ugoe.cs.quest.eventcore.guimodel.ITrackBar;
5import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality;
6import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager;
7import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask;
8import de.ugoe.cs.quest.tasktrees.treeifc.IIteration;
9import de.ugoe.cs.quest.tasktrees.treeifc.ISelection;
10import de.ugoe.cs.quest.tasktrees.treeifc.ISequence;
11import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder;
12import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
13import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory;
14
15/**
16 * TODO comment
17 *
18 * @version $Revision: $ $Date: 28.04.2012$
19 * @author 2012, last modified by $Author: patrick$
20 */
21public class TrackBarSelectionDetectionRule implements TemporalRelationshipRule {
22
23    /** */
24    private NodeEqualityRuleManager nodeEqualityRuleManager;
25
26    /**
27     * TODO: comment
28     *
29     */
30    TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager) {
31        super();
32        this.nodeEqualityRuleManager = nodeEqualityRuleManager;
33    }
34
35    /*
36     * (non-Javadoc)
37     *
38     * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory)
39     */
40    @Override
41    public RuleApplicationResult apply(ITaskTreeNode        parent,
42                                       ITaskTreeBuilder     builder,
43                                       ITaskTreeNodeFactory nodeFactory,
44                                       boolean              finalize)
45    {
46        if (!(parent instanceof ISequence)) {
47            return null;
48        }
49
50        RuleApplicationResult result = new RuleApplicationResult();
51
52        int valueSelectionStartIndex = -1;
53
54        int index = 0;
55        while (index < parent.getChildren().size()) {
56            ITaskTreeNode child = parent.getChildren().get(index);
57
58            if ((child instanceof IEventTask) &&
59                (((IEventTask) child).getEventTarget() instanceof ITrackBar) &&
60                (((IEventTask) child).getEventType() instanceof ValueSelection))
61            {
62                if (valueSelectionStartIndex < 0) {
63                    // let the show begin
64                    valueSelectionStartIndex = index;
65                }
66            }
67            else if (valueSelectionStartIndex >= 0) {
68                // current child is no more value selection. But the preceding tasks were.
69                // Therefore,
70                // create an iteration with the different selectable values as selection children
71                handleValueSelections(valueSelectionStartIndex, index - 1, parent, builder,
72                                      nodeFactory, result);
73
74                return result;
75            }
76
77            index++;
78        }
79
80        if (valueSelectionStartIndex >= 0) {
81            if (finalize) {
82                handleValueSelections(valueSelectionStartIndex, parent.getChildren().size() - 1,
83                                      parent, builder, nodeFactory, result);
84            }
85            else {
86                result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
87            }
88        }
89
90        return result;
91    }
92
93    /**
94     * TODO: comment
95     *
96     * @param valueSelectionStartIndex
97     * @param i
98     */
99    private void handleValueSelections(int                   startIndex,
100                                       int                   endIndex,
101                                       ITaskTreeNode         parent,
102                                       ITaskTreeBuilder      builder,
103                                       ITaskTreeNodeFactory  nodeFactory,
104                                       RuleApplicationResult result)
105    {
106        IIteration iteration = nodeFactory.createNewIteration();
107        result.addNewlyCreatedParentNode(iteration);
108
109        ISelection selection = nodeFactory.createNewSelection();
110        result.addNewlyCreatedParentNode(selection);
111        builder.setChild(iteration, selection);
112
113        for (int i = endIndex - startIndex; i >= 0; i--) {
114            addChildIfNecessary(selection, parent.getChildren().get(startIndex), builder,
115                                nodeFactory, result);
116            builder.removeChild((ISequence) parent, startIndex);
117        }
118
119        builder.addChild((ISequence) parent, startIndex, iteration);
120
121        result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
122    }
123
124    /**
125     *
126     */
127    private void addChildIfNecessary(ISelection            parentSelection,
128                                     ITaskTreeNode         node,
129                                     ITaskTreeBuilder      builder,
130                                     ITaskTreeNodeFactory  nodeFactory,
131                                     RuleApplicationResult result)
132    {
133        for (int i = 0; i < parentSelection.getChildren().size(); i++) {
134            ITaskTreeNode child = parentSelection.getChildren().get(i);
135
136            // check, if the new node is a variant for the current event task
137            NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node);
138            if (nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) {
139                return;
140            }
141        }
142
143        // if we did not return in the previous checks, then the node must be added
144        builder.addChild(parentSelection, node);
145    }
146
147}
Note: See TracBrowser for help on using the repository browser.