[922] | 1 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
[439] | 2 | |
---|
[922] | 3 | import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection; |
---|
| 4 | import de.ugoe.cs.autoquest.eventcore.guimodel.ITrackBar; |
---|
| 5 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality; |
---|
| 6 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
| 7 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 8 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 9 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 10 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 11 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 12 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 13 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
[439] | 14 | |
---|
| 15 | /** |
---|
| 16 | * TODO comment |
---|
| 17 | * |
---|
| 18 | * @version $Revision: $ $Date: 28.04.2012$ |
---|
| 19 | * @author 2012, last modified by $Author: patrick$ |
---|
| 20 | */ |
---|
[557] | 21 | public class TrackBarSelectionDetectionRule implements TemporalRelationshipRule { |
---|
[439] | 22 | |
---|
[557] | 23 | /** */ |
---|
| 24 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
[439] | 25 | |
---|
[557] | 26 | /** |
---|
[1099] | 27 | * |
---|
[557] | 28 | */ |
---|
| 29 | TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager) { |
---|
| 30 | super(); |
---|
| 31 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
| 32 | } |
---|
[439] | 33 | |
---|
[557] | 34 | /* |
---|
| 35 | * (non-Javadoc) |
---|
| 36 | * |
---|
| 37 | * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory) |
---|
| 38 | */ |
---|
| 39 | @Override |
---|
| 40 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
| 41 | ITaskTreeBuilder builder, |
---|
| 42 | ITaskTreeNodeFactory nodeFactory, |
---|
| 43 | boolean finalize) |
---|
[439] | 44 | { |
---|
[557] | 45 | if (!(parent instanceof ISequence)) { |
---|
| 46 | return null; |
---|
[439] | 47 | } |
---|
[557] | 48 | |
---|
| 49 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 50 | |
---|
| 51 | int valueSelectionStartIndex = -1; |
---|
[971] | 52 | ITrackBar currentTrackBar = null; |
---|
[557] | 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) && |
---|
[971] | 60 | (((IEventTask) child).getEventType() instanceof ValueSelection) && |
---|
| 61 | ((currentTrackBar == null) || |
---|
| 62 | (currentTrackBar.equals((((IEventTask) child).getEventTarget()))))) |
---|
[557] | 63 | { |
---|
| 64 | if (valueSelectionStartIndex < 0) { |
---|
| 65 | // let the show begin |
---|
| 66 | valueSelectionStartIndex = index; |
---|
[971] | 67 | currentTrackBar = (ITrackBar) ((IEventTask) child).getEventTarget(); |
---|
[557] | 68 | } |
---|
| 69 | } |
---|
| 70 | else if (valueSelectionStartIndex >= 0) { |
---|
| 71 | // current child is no more value selection. But the preceding tasks were. |
---|
| 72 | // Therefore, |
---|
| 73 | // create an iteration with the different selectable values as selection children |
---|
[986] | 74 | handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex, index - 1, |
---|
| 75 | builder, nodeFactory, result); |
---|
[557] | 76 | |
---|
| 77 | return result; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | index++; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | if (valueSelectionStartIndex >= 0) { |
---|
| 84 | if (finalize) { |
---|
[986] | 85 | handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex, |
---|
| 86 | parent.getChildren().size() - 1, builder, nodeFactory, result); |
---|
[557] | 87 | } |
---|
| 88 | else { |
---|
| 89 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | |
---|
[439] | 93 | return result; |
---|
| 94 | } |
---|
[557] | 95 | |
---|
| 96 | /** |
---|
[1099] | 97 | * |
---|
[557] | 98 | */ |
---|
[986] | 99 | private void handleValueSelections(ITaskTreeNode parent, |
---|
| 100 | ITrackBar trackbar, |
---|
| 101 | int startIndex, |
---|
[557] | 102 | int endIndex, |
---|
| 103 | ITaskTreeBuilder builder, |
---|
| 104 | ITaskTreeNodeFactory nodeFactory, |
---|
| 105 | RuleApplicationResult result) |
---|
[439] | 106 | { |
---|
[557] | 107 | IIteration iteration = nodeFactory.createNewIteration(); |
---|
[986] | 108 | builder.setDescription(iteration, "value selection on " + trackbar.getStringIdentifier()); |
---|
[557] | 109 | result.addNewlyCreatedParentNode(iteration); |
---|
| 110 | |
---|
| 111 | ISelection selection = nodeFactory.createNewSelection(); |
---|
| 112 | result.addNewlyCreatedParentNode(selection); |
---|
| 113 | builder.setChild(iteration, selection); |
---|
| 114 | |
---|
| 115 | for (int i = endIndex - startIndex; i >= 0; i--) { |
---|
| 116 | addChildIfNecessary(selection, parent.getChildren().get(startIndex), builder, |
---|
| 117 | nodeFactory, result); |
---|
| 118 | builder.removeChild((ISequence) parent, startIndex); |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | builder.addChild((ISequence) parent, startIndex, iteration); |
---|
| 122 | |
---|
| 123 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
[439] | 124 | } |
---|
| 125 | |
---|
[557] | 126 | /** |
---|
[808] | 127 | * |
---|
| 128 | */ |
---|
[557] | 129 | private void addChildIfNecessary(ISelection parentSelection, |
---|
| 130 | ITaskTreeNode node, |
---|
| 131 | ITaskTreeBuilder builder, |
---|
| 132 | ITaskTreeNodeFactory nodeFactory, |
---|
[439] | 133 | RuleApplicationResult result) |
---|
| 134 | { |
---|
[557] | 135 | for (int i = 0; i < parentSelection.getChildren().size(); i++) { |
---|
| 136 | ITaskTreeNode child = parentSelection.getChildren().get(i); |
---|
[439] | 137 | |
---|
[808] | 138 | // check, if the new node is a variant for the current event task |
---|
| 139 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); |
---|
| 140 | if (nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) { |
---|
| 141 | return; |
---|
[557] | 142 | } |
---|
[439] | 143 | } |
---|
| 144 | |
---|
[557] | 145 | // if we did not return in the previous checks, then the node must be added |
---|
| 146 | builder.addChild(parentSelection, node); |
---|
[439] | 147 | } |
---|
| 148 | |
---|
| 149 | } |
---|