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

Last change on this file since 655 was 655, checked in by pharms, 12 years ago
  • removed old copyright file header
File size: 7.5 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            if (child instanceof IEventTask) {
137                // check, if the new node is a variant for the current event task
138                NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node);
139                if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) {
140                    // the node is a variant. If it not structurally equal, a new sub-selection for
141                    // the existing and the new node must be created. Otherwise, the new node does
142                    // not need to be added
143                    if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) {
144                        ISelection selection = nodeFactory.createNewSelection();
145                        result.addNewlyCreatedParentNode(selection);
146                        builder.addChild(parentSelection, selection);
147
148                        builder.addChild(selection, child);
149                        builder.addChild(selection, node);
150                        builder.removeChild(parentSelection, child);
151                    }
152
153                    return;
154                }
155            }
156            else if (child instanceof ISelection) {
157                // check, if the new node is a variant for the semantically equal children of the
158                // current
159                // selection
160                boolean addNode = true;
161                for (int j = 0; j < child.getChildren().size(); j++) {
162                    NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node);
163                    if (!nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) {
164                        // the new node is no semantical equivalent of the nodes in the current
165                        // selection - break up
166                        addNode = false;
167                        break;
168                    }
169                    else if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) {
170                        addNode = false;
171                        break;
172                    }
173                }
174
175                if (addNode) {
176                    // the node is a semantical equivalent to all the nodes in the existing
177                    // sub-selection
178                    // but it is not structurally identical to either of them. Therefore add it.
179                    builder.addChild((ISelection) child, node);
180                    return;
181                }
182            }
183        }
184
185        // if we did not return in the previous checks, then the node must be added
186        builder.addChild(parentSelection, node);
187    }
188
189}
Note: See TracBrowser for help on using the repository browser.