[1113] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
[439] | 16 | |
---|
[922] | 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; |
---|
[439] | 28 | |
---|
| 29 | /** |
---|
| 30 | * TODO comment |
---|
| 31 | * |
---|
| 32 | * @version $Revision: $ $Date: 28.04.2012$ |
---|
| 33 | * @author 2012, last modified by $Author: patrick$ |
---|
| 34 | */ |
---|
[1107] | 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; |
---|
[439] | 50 | |
---|
[1107] | 51 | /** |
---|
| 52 | * <p> |
---|
| 53 | * the node equality manager needed for comparing task tree nodes with each other |
---|
| 54 | * </p> |
---|
| 55 | */ |
---|
[557] | 56 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
[439] | 57 | |
---|
[557] | 58 | /** |
---|
[1107] | 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> |
---|
[557] | 63 | */ |
---|
[1107] | 64 | TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager, |
---|
| 65 | ITaskTreeNodeFactory taskTreeNodeFactory, |
---|
| 66 | ITaskTreeBuilder taskTreeBuilder) |
---|
| 67 | { |
---|
[557] | 68 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
[1107] | 69 | this.taskTreeNodeFactory = taskTreeNodeFactory; |
---|
| 70 | this.taskTreeBuilder = taskTreeBuilder; |
---|
[557] | 71 | } |
---|
[439] | 72 | |
---|
[557] | 73 | /* |
---|
| 74 | * (non-Javadoc) |
---|
| 75 | * |
---|
[1107] | 76 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
| 77 | * boolean) |
---|
[557] | 78 | */ |
---|
| 79 | @Override |
---|
[1107] | 80 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
[557] | 81 | if (!(parent instanceof ISequence)) { |
---|
| 82 | return null; |
---|
[439] | 83 | } |
---|
[557] | 84 | |
---|
| 85 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 86 | |
---|
| 87 | int valueSelectionStartIndex = -1; |
---|
[971] | 88 | ITrackBar currentTrackBar = null; |
---|
[557] | 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) && |
---|
[971] | 96 | (((IEventTask) child).getEventType() instanceof ValueSelection) && |
---|
| 97 | ((currentTrackBar == null) || |
---|
| 98 | (currentTrackBar.equals((((IEventTask) child).getEventTarget()))))) |
---|
[557] | 99 | { |
---|
| 100 | if (valueSelectionStartIndex < 0) { |
---|
| 101 | // let the show begin |
---|
| 102 | valueSelectionStartIndex = index; |
---|
[971] | 103 | currentTrackBar = (ITrackBar) ((IEventTask) child).getEventTarget(); |
---|
[557] | 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 |
---|
[1107] | 110 | handleValueSelections |
---|
| 111 | (parent, currentTrackBar, valueSelectionStartIndex, index - 1, result); |
---|
[557] | 112 | |
---|
| 113 | return result; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | index++; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | if (valueSelectionStartIndex >= 0) { |
---|
| 120 | if (finalize) { |
---|
[986] | 121 | handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex, |
---|
[1107] | 122 | parent.getChildren().size() - 1, result); |
---|
[557] | 123 | } |
---|
| 124 | else { |
---|
| 125 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | |
---|
[439] | 129 | return result; |
---|
| 130 | } |
---|
[557] | 131 | |
---|
| 132 | /** |
---|
[1099] | 133 | * |
---|
[557] | 134 | */ |
---|
[986] | 135 | private void handleValueSelections(ITaskTreeNode parent, |
---|
| 136 | ITrackBar trackbar, |
---|
| 137 | int startIndex, |
---|
[557] | 138 | int endIndex, |
---|
| 139 | RuleApplicationResult result) |
---|
[439] | 140 | { |
---|
[1107] | 141 | IIteration iteration = taskTreeNodeFactory.createNewIteration(); |
---|
| 142 | taskTreeBuilder.setDescription |
---|
| 143 | (iteration, "value selection on " + trackbar.getStringIdentifier()); |
---|
[557] | 144 | result.addNewlyCreatedParentNode(iteration); |
---|
| 145 | |
---|
[1107] | 146 | ISelection selection = taskTreeNodeFactory.createNewSelection(); |
---|
[557] | 147 | result.addNewlyCreatedParentNode(selection); |
---|
[1107] | 148 | taskTreeBuilder.setChild(iteration, selection); |
---|
[557] | 149 | |
---|
| 150 | for (int i = endIndex - startIndex; i >= 0; i--) { |
---|
[1107] | 151 | addChildIfNecessary(selection, parent.getChildren().get(startIndex), result); |
---|
| 152 | taskTreeBuilder.removeChild((ISequence) parent, startIndex); |
---|
[557] | 153 | } |
---|
| 154 | |
---|
[1107] | 155 | taskTreeBuilder.addChild((ISequence) parent, startIndex, iteration); |
---|
[557] | 156 | |
---|
| 157 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
[439] | 158 | } |
---|
| 159 | |
---|
[557] | 160 | /** |
---|
[808] | 161 | * |
---|
| 162 | */ |
---|
[557] | 163 | private void addChildIfNecessary(ISelection parentSelection, |
---|
| 164 | ITaskTreeNode node, |
---|
[439] | 165 | RuleApplicationResult result) |
---|
| 166 | { |
---|
[557] | 167 | for (int i = 0; i < parentSelection.getChildren().size(); i++) { |
---|
| 168 | ITaskTreeNode child = parentSelection.getChildren().get(i); |
---|
[439] | 169 | |
---|
[808] | 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; |
---|
[557] | 174 | } |
---|
[439] | 175 | } |
---|
| 176 | |
---|
[557] | 177 | // if we did not return in the previous checks, then the node must be added |
---|
[1107] | 178 | taskTreeBuilder.addChild(parentSelection, node); |
---|
[439] | 179 | } |
---|
| 180 | |
---|
| 181 | } |
---|