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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
16 | |
---|
17 | import java.util.List; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
20 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITrackBar; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
29 | |
---|
30 | /** |
---|
31 | * TODO comment |
---|
32 | * |
---|
33 | * @version $Revision: $ $Date: 28.04.2012$ |
---|
34 | * @author 2012, last modified by $Author: patrick$ |
---|
35 | */ |
---|
36 | class TrackBarSelectionDetectionRule implements TemporalRelationshipRule { |
---|
37 | /** |
---|
38 | * <p> |
---|
39 | * the task tree node factory to be used for creating substructures for the temporal |
---|
40 | * relationships identified during rule |
---|
41 | * </p> |
---|
42 | */ |
---|
43 | private ITaskTreeNodeFactory taskTreeNodeFactory; |
---|
44 | /** |
---|
45 | * <p> |
---|
46 | * the task tree builder to be used for creating substructures for the temporal relationships |
---|
47 | * identified during rule application |
---|
48 | * </p> |
---|
49 | */ |
---|
50 | private ITaskTreeBuilder taskTreeBuilder; |
---|
51 | |
---|
52 | /** |
---|
53 | * <p> |
---|
54 | * the node equality manager needed for comparing task tree nodes with each other |
---|
55 | * </p> |
---|
56 | */ |
---|
57 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
58 | |
---|
59 | /** |
---|
60 | * <p> |
---|
61 | * instantiates the rule and initializes it with a node equality rule manager and the minimal |
---|
62 | * node equality identified sublist must have to consider them as iterated. |
---|
63 | * </p> |
---|
64 | */ |
---|
65 | TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager, |
---|
66 | ITaskTreeNodeFactory taskTreeNodeFactory, |
---|
67 | ITaskTreeBuilder taskTreeBuilder) |
---|
68 | { |
---|
69 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
70 | this.taskTreeNodeFactory = taskTreeNodeFactory; |
---|
71 | this.taskTreeBuilder = taskTreeBuilder; |
---|
72 | } |
---|
73 | |
---|
74 | /* (non-Javadoc) |
---|
75 | * @see java.lang.Object#toString() |
---|
76 | */ |
---|
77 | @Override |
---|
78 | public String toString() { |
---|
79 | return "TrackBarSelectionDetectionRule"; |
---|
80 | } |
---|
81 | |
---|
82 | /* |
---|
83 | * (non-Javadoc) |
---|
84 | * |
---|
85 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
86 | * boolean) |
---|
87 | */ |
---|
88 | @Override |
---|
89 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
90 | if (!(parent instanceof ISequence)) { |
---|
91 | return null; |
---|
92 | } |
---|
93 | |
---|
94 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
95 | |
---|
96 | int valueSelectionStartIndex = -1; |
---|
97 | ITrackBar currentTrackBar = null; |
---|
98 | |
---|
99 | int index = 0; |
---|
100 | List<ITaskTreeNode> children = parent.getChildren(); |
---|
101 | |
---|
102 | while (index < children.size()) { |
---|
103 | ITaskTreeNode child = children.get(index); |
---|
104 | |
---|
105 | if ((child instanceof IEventTask) && |
---|
106 | (((IEventTask) child).getEventTarget() instanceof ITrackBar) && |
---|
107 | (((IEventTask) child).getEventType() instanceof ValueSelection) && |
---|
108 | ((currentTrackBar == null) || |
---|
109 | (currentTrackBar.equals((((IEventTask) child).getEventTarget()))))) |
---|
110 | { |
---|
111 | if (valueSelectionStartIndex < 0) { |
---|
112 | // let the show begin |
---|
113 | valueSelectionStartIndex = index; |
---|
114 | currentTrackBar = (ITrackBar) ((IEventTask) child).getEventTarget(); |
---|
115 | } |
---|
116 | } |
---|
117 | else if (valueSelectionStartIndex >= 0) { |
---|
118 | // current child is no more value selection. But the preceding tasks were. |
---|
119 | // Therefore, |
---|
120 | // create an iteration with the different selectable values as selection children |
---|
121 | handleValueSelections |
---|
122 | (parent, currentTrackBar, valueSelectionStartIndex, index - 1, result); |
---|
123 | |
---|
124 | return result; |
---|
125 | } |
---|
126 | |
---|
127 | index++; |
---|
128 | } |
---|
129 | |
---|
130 | if (valueSelectionStartIndex >= 0) { |
---|
131 | if (finalize) { |
---|
132 | handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex, |
---|
133 | children.size() - 1, result); |
---|
134 | } |
---|
135 | else { |
---|
136 | result.setRuleApplicationStatus(RuleApplicationStatus.FEASIBLE); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | return result; |
---|
141 | } |
---|
142 | |
---|
143 | /** |
---|
144 | * |
---|
145 | */ |
---|
146 | private void handleValueSelections(ITaskTreeNode parent, |
---|
147 | ITrackBar trackbar, |
---|
148 | int startIndex, |
---|
149 | int endIndex, |
---|
150 | RuleApplicationResult result) |
---|
151 | { |
---|
152 | IIteration iteration = taskTreeNodeFactory.createNewIteration(); |
---|
153 | taskTreeBuilder.setDescription |
---|
154 | (iteration, "value selection on " + trackbar.getStringIdentifier()); |
---|
155 | result.addNewlyCreatedParentNode(iteration); |
---|
156 | |
---|
157 | ISelection selection = taskTreeNodeFactory.createNewSelection(); |
---|
158 | result.addNewlyCreatedParentNode(selection); |
---|
159 | taskTreeBuilder.setChild(iteration, selection); |
---|
160 | |
---|
161 | List<ITaskTreeNode> children = parent.getChildren(); |
---|
162 | |
---|
163 | for (int i = endIndex - startIndex; i >= 0; i--) { |
---|
164 | addChildIfNecessary(selection, children.get(startIndex), result); |
---|
165 | taskTreeBuilder.removeChild((ISequence) parent, startIndex); |
---|
166 | } |
---|
167 | |
---|
168 | taskTreeBuilder.addChild((ISequence) parent, startIndex, iteration); |
---|
169 | |
---|
170 | result.setRuleApplicationStatus(RuleApplicationStatus.FINISHED); |
---|
171 | } |
---|
172 | |
---|
173 | /** |
---|
174 | * |
---|
175 | */ |
---|
176 | private void addChildIfNecessary(ISelection parentSelection, |
---|
177 | ITaskTreeNode node, |
---|
178 | RuleApplicationResult result) |
---|
179 | { |
---|
180 | List<ITaskTreeNode> children = parentSelection.getChildren(); |
---|
181 | |
---|
182 | for (int i = 0; i < children.size(); i++) { |
---|
183 | ITaskTreeNode child = children.get(i); |
---|
184 | |
---|
185 | // check, if the new node is a variant for the current event task |
---|
186 | if (nodeEqualityRuleManager.areSyntacticallyEqual(child, node)) { |
---|
187 | return; |
---|
188 | } |
---|
189 | } |
---|
190 | |
---|
191 | // if we did not return in the previous checks, then the node must be added |
---|
192 | taskTreeBuilder.addChild(parentSelection, node); |
---|
193 | } |
---|
194 | |
---|
195 | } |
---|