[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; |
---|
| 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) && |
---|
| 60 | (((IEventTask) child).getEventType() instanceof ValueSelection)) |
---|
| 61 | { |
---|
| 62 | if (valueSelectionStartIndex < 0) { |
---|
| 63 | // let the show begin |
---|
| 64 | valueSelectionStartIndex = index; |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | else if (valueSelectionStartIndex >= 0) { |
---|
| 68 | // current child is no more value selection. But the preceding tasks were. |
---|
| 69 | // Therefore, |
---|
| 70 | // create an iteration with the different selectable values as selection children |
---|
| 71 | handleValueSelections(valueSelectionStartIndex, index - 1, parent, builder, |
---|
| 72 | nodeFactory, result); |
---|
| 73 | |
---|
| 74 | return result; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | index++; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | if (valueSelectionStartIndex >= 0) { |
---|
| 81 | if (finalize) { |
---|
| 82 | handleValueSelections(valueSelectionStartIndex, parent.getChildren().size() - 1, |
---|
| 83 | parent, builder, nodeFactory, result); |
---|
| 84 | } |
---|
| 85 | else { |
---|
| 86 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | |
---|
[439] | 90 | return result; |
---|
| 91 | } |
---|
[557] | 92 | |
---|
| 93 | /** |
---|
| 94 | * TODO: comment |
---|
| 95 | * |
---|
| 96 | * @param valueSelectionStartIndex |
---|
| 97 | * @param i |
---|
| 98 | */ |
---|
| 99 | private void handleValueSelections(int startIndex, |
---|
| 100 | int endIndex, |
---|
| 101 | ITaskTreeNode parent, |
---|
| 102 | ITaskTreeBuilder builder, |
---|
| 103 | ITaskTreeNodeFactory nodeFactory, |
---|
| 104 | RuleApplicationResult result) |
---|
[439] | 105 | { |
---|
[557] | 106 | IIteration iteration = nodeFactory.createNewIteration(); |
---|
| 107 | result.addNewlyCreatedParentNode(iteration); |
---|
| 108 | |
---|
| 109 | ISelection selection = nodeFactory.createNewSelection(); |
---|
| 110 | result.addNewlyCreatedParentNode(selection); |
---|
| 111 | builder.setChild(iteration, selection); |
---|
| 112 | |
---|
| 113 | for (int i = endIndex - startIndex; i >= 0; i--) { |
---|
| 114 | addChildIfNecessary(selection, parent.getChildren().get(startIndex), builder, |
---|
| 115 | nodeFactory, result); |
---|
| 116 | builder.removeChild((ISequence) parent, startIndex); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | builder.addChild((ISequence) parent, startIndex, iteration); |
---|
| 120 | |
---|
| 121 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
[439] | 122 | } |
---|
| 123 | |
---|
[557] | 124 | /** |
---|
[808] | 125 | * |
---|
| 126 | */ |
---|
[557] | 127 | private void addChildIfNecessary(ISelection parentSelection, |
---|
| 128 | ITaskTreeNode node, |
---|
| 129 | ITaskTreeBuilder builder, |
---|
| 130 | ITaskTreeNodeFactory nodeFactory, |
---|
[439] | 131 | RuleApplicationResult result) |
---|
| 132 | { |
---|
[557] | 133 | for (int i = 0; i < parentSelection.getChildren().size(); i++) { |
---|
| 134 | ITaskTreeNode child = parentSelection.getChildren().get(i); |
---|
[439] | 135 | |
---|
[808] | 136 | // check, if the new node is a variant for the current event task |
---|
| 137 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); |
---|
| 138 | if (nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) { |
---|
| 139 | return; |
---|
[557] | 140 | } |
---|
[439] | 141 | } |
---|
| 142 | |
---|
[557] | 143 | // if we did not return in the previous checks, then the node must be added |
---|
| 144 | builder.addChild(parentSelection, node); |
---|
[439] | 145 | } |
---|
| 146 | |
---|
| 147 | } |
---|