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

Last change on this file since 1117 was 1117, checked in by pharms, 11 years ago
  • added toString methods
File size: 7.1 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
16
17import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
18import de.ugoe.cs.autoquest.eventcore.guimodel.ITrackBar;
19import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality;
20import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
28
29/**
30 * TODO comment
31 *
32 * @version $Revision: $ $Date: 28.04.2012$
33 * @author 2012, last modified by $Author: patrick$
34 */
35class TrackBarSelectionDetectionRule implements TemporalRelationshipRule {
36    /**
37     * <p>
38     * the task tree node factory to be used for creating substructures for the temporal
39     * relationships identified during rule
40     * </p>
41     */
42    private ITaskTreeNodeFactory taskTreeNodeFactory;
43    /**
44     * <p>
45     * the task tree builder to be used for creating substructures for the temporal relationships
46     * identified during rule application
47     * </p>
48     */
49    private ITaskTreeBuilder taskTreeBuilder;
50
51    /**
52     * <p>
53     * the node equality manager needed for comparing task tree nodes with each other
54     * </p>
55     */
56    private NodeEqualityRuleManager nodeEqualityRuleManager;
57
58    /**
59     * <p>
60     * instantiates the rule and initializes it with a node equality rule manager and the minimal
61     * node equality identified sublist must have to consider them as iterated.
62     * </p>
63     */
64    TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager,
65                                   ITaskTreeNodeFactory    taskTreeNodeFactory,
66                                   ITaskTreeBuilder        taskTreeBuilder)
67    {
68        this.nodeEqualityRuleManager = nodeEqualityRuleManager;
69        this.taskTreeNodeFactory = taskTreeNodeFactory;
70        this.taskTreeBuilder = taskTreeBuilder;
71    }
72
73    /* (non-Javadoc)
74     * @see java.lang.Object#toString()
75     */
76    @Override
77    public String toString() {
78        return "TrackBarSelectionDetectionRule";
79    }
80
81    /*
82     * (non-Javadoc)
83     *
84     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
85     * boolean)
86     */
87    @Override
88    public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) {
89        if (!(parent instanceof ISequence)) {
90            return null;
91        }
92
93        RuleApplicationResult result = new RuleApplicationResult();
94
95        int valueSelectionStartIndex = -1;
96        ITrackBar currentTrackBar = null;
97
98        int index = 0;
99        while (index < parent.getChildren().size()) {
100            ITaskTreeNode child = parent.getChildren().get(index);
101
102            if ((child instanceof IEventTask) &&
103                (((IEventTask) child).getEventTarget() instanceof ITrackBar) &&
104                (((IEventTask) child).getEventType() instanceof ValueSelection) &&
105                ((currentTrackBar == null) ||
106                 (currentTrackBar.equals((((IEventTask) child).getEventTarget())))))
107            {
108                if (valueSelectionStartIndex < 0) {
109                    // let the show begin
110                    valueSelectionStartIndex = index;
111                    currentTrackBar = (ITrackBar) ((IEventTask) child).getEventTarget();
112                }
113            }
114            else if (valueSelectionStartIndex >= 0) {
115                // current child is no more value selection. But the preceding tasks were.
116                // Therefore,
117                // create an iteration with the different selectable values as selection children
118                handleValueSelections
119                    (parent, currentTrackBar, valueSelectionStartIndex, index - 1, result);
120
121                return result;
122            }
123
124            index++;
125        }
126
127        if (valueSelectionStartIndex >= 0) {
128            if (finalize) {
129                handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex,
130                                      parent.getChildren().size() - 1, result);
131            }
132            else {
133                result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
134            }
135        }
136
137        return result;
138    }
139
140    /**
141     *
142     */
143    private void handleValueSelections(ITaskTreeNode         parent,
144                                       ITrackBar             trackbar,
145                                       int                   startIndex,
146                                       int                   endIndex,
147                                       RuleApplicationResult result)
148    {
149        IIteration iteration = taskTreeNodeFactory.createNewIteration();
150        taskTreeBuilder.setDescription
151            (iteration, "value selection on " + trackbar.getStringIdentifier());
152        result.addNewlyCreatedParentNode(iteration);
153
154        ISelection selection = taskTreeNodeFactory.createNewSelection();
155        result.addNewlyCreatedParentNode(selection);
156        taskTreeBuilder.setChild(iteration, selection);
157
158        for (int i = endIndex - startIndex; i >= 0; i--) {
159            addChildIfNecessary(selection, parent.getChildren().get(startIndex), result);
160            taskTreeBuilder.removeChild((ISequence) parent, startIndex);
161        }
162
163        taskTreeBuilder.addChild((ISequence) parent, startIndex, iteration);
164
165        result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
166    }
167
168    /**
169     *
170     */
171    private void addChildIfNecessary(ISelection            parentSelection,
172                                     ITaskTreeNode         node,
173                                     RuleApplicationResult result)
174    {
175        for (int i = 0; i < parentSelection.getChildren().size(); i++) {
176            ITaskTreeNode child = parentSelection.getChildren().get(i);
177
178            // check, if the new node is a variant for the current event task
179            NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node);
180            if (nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) {
181                return;
182            }
183        }
184
185        // if we did not return in the previous checks, then the node must be added
186        taskTreeBuilder.addChild(parentSelection, node);
187    }
188
189}
Note: See TracBrowser for help on using the repository browser.