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 de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
18 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITrackBar; |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality; |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
27 | import 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 | */ |
---|
35 | class 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 | /* |
---|
74 | * (non-Javadoc) |
---|
75 | * |
---|
76 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
77 | * boolean) |
---|
78 | */ |
---|
79 | @Override |
---|
80 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
81 | if (!(parent instanceof ISequence)) { |
---|
82 | return null; |
---|
83 | } |
---|
84 | |
---|
85 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
86 | |
---|
87 | int valueSelectionStartIndex = -1; |
---|
88 | ITrackBar currentTrackBar = null; |
---|
89 | |
---|
90 | int index = 0; |
---|
91 | while (index < parent.getChildren().size()) { |
---|
92 | ITaskTreeNode child = parent.getChildren().get(index); |
---|
93 | |
---|
94 | if ((child instanceof IEventTask) && |
---|
95 | (((IEventTask) child).getEventTarget() instanceof ITrackBar) && |
---|
96 | (((IEventTask) child).getEventType() instanceof ValueSelection) && |
---|
97 | ((currentTrackBar == null) || |
---|
98 | (currentTrackBar.equals((((IEventTask) child).getEventTarget()))))) |
---|
99 | { |
---|
100 | if (valueSelectionStartIndex < 0) { |
---|
101 | // let the show begin |
---|
102 | valueSelectionStartIndex = index; |
---|
103 | currentTrackBar = (ITrackBar) ((IEventTask) child).getEventTarget(); |
---|
104 | } |
---|
105 | } |
---|
106 | else if (valueSelectionStartIndex >= 0) { |
---|
107 | // current child is no more value selection. But the preceding tasks were. |
---|
108 | // Therefore, |
---|
109 | // create an iteration with the different selectable values as selection children |
---|
110 | handleValueSelections |
---|
111 | (parent, currentTrackBar, valueSelectionStartIndex, index - 1, result); |
---|
112 | |
---|
113 | return result; |
---|
114 | } |
---|
115 | |
---|
116 | index++; |
---|
117 | } |
---|
118 | |
---|
119 | if (valueSelectionStartIndex >= 0) { |
---|
120 | if (finalize) { |
---|
121 | handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex, |
---|
122 | parent.getChildren().size() - 1, result); |
---|
123 | } |
---|
124 | else { |
---|
125 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | return result; |
---|
130 | } |
---|
131 | |
---|
132 | /** |
---|
133 | * |
---|
134 | */ |
---|
135 | private void handleValueSelections(ITaskTreeNode parent, |
---|
136 | ITrackBar trackbar, |
---|
137 | int startIndex, |
---|
138 | int endIndex, |
---|
139 | RuleApplicationResult result) |
---|
140 | { |
---|
141 | IIteration iteration = taskTreeNodeFactory.createNewIteration(); |
---|
142 | taskTreeBuilder.setDescription |
---|
143 | (iteration, "value selection on " + trackbar.getStringIdentifier()); |
---|
144 | result.addNewlyCreatedParentNode(iteration); |
---|
145 | |
---|
146 | ISelection selection = taskTreeNodeFactory.createNewSelection(); |
---|
147 | result.addNewlyCreatedParentNode(selection); |
---|
148 | taskTreeBuilder.setChild(iteration, selection); |
---|
149 | |
---|
150 | for (int i = endIndex - startIndex; i >= 0; i--) { |
---|
151 | addChildIfNecessary(selection, parent.getChildren().get(startIndex), result); |
---|
152 | taskTreeBuilder.removeChild((ISequence) parent, startIndex); |
---|
153 | } |
---|
154 | |
---|
155 | taskTreeBuilder.addChild((ISequence) parent, startIndex, iteration); |
---|
156 | |
---|
157 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * |
---|
162 | */ |
---|
163 | private void addChildIfNecessary(ISelection parentSelection, |
---|
164 | ITaskTreeNode node, |
---|
165 | RuleApplicationResult result) |
---|
166 | { |
---|
167 | for (int i = 0; i < parentSelection.getChildren().size(); i++) { |
---|
168 | ITaskTreeNode child = parentSelection.getChildren().get(i); |
---|
169 | |
---|
170 | // check, if the new node is a variant for the current event task |
---|
171 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); |
---|
172 | if (nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) { |
---|
173 | return; |
---|
174 | } |
---|
175 | } |
---|
176 | |
---|
177 | // if we did not return in the previous checks, then the node must be added |
---|
178 | taskTreeBuilder.addChild(parentSelection, node); |
---|
179 | } |
---|
180 | |
---|
181 | } |
---|