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