[439] | 1 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
| 2 | |
---|
[557] | 3 | import de.ugoe.cs.quest.eventcore.gui.ValueSelection; |
---|
| 4 | import de.ugoe.cs.quest.eventcore.guimodel.ITrackBar; |
---|
[439] | 5 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality; |
---|
| 6 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
[557] | 7 | import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; |
---|
| 8 | import de.ugoe.cs.quest.tasktrees.treeifc.IIteration; |
---|
| 9 | import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; |
---|
| 10 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
| 11 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 12 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 13 | import de.ugoe.cs.quest.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 | /** |
---|
[439] | 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 | |
---|
[557] | 136 | if (child instanceof IEventTask) { |
---|
| 137 | // check, if the new node is a variant for the current event task |
---|
| 138 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); |
---|
| 139 | if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { |
---|
| 140 | // the node is a variant. If it not structurally equal, a new sub-selection for |
---|
| 141 | // the existing and the new node must be created. Otherwise, the new node does |
---|
| 142 | // not need to be added |
---|
| 143 | if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { |
---|
| 144 | ISelection selection = nodeFactory.createNewSelection(); |
---|
| 145 | result.addNewlyCreatedParentNode(selection); |
---|
| 146 | builder.addChild(parentSelection, selection); |
---|
[439] | 147 | |
---|
[557] | 148 | builder.addChild(selection, child); |
---|
| 149 | builder.addChild(selection, node); |
---|
| 150 | builder.removeChild(parentSelection, child); |
---|
| 151 | } |
---|
[439] | 152 | |
---|
[557] | 153 | return; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | else if (child instanceof ISelection) { |
---|
| 157 | // check, if the new node is a variant for the semantically equal children of the |
---|
| 158 | // current |
---|
| 159 | // selection |
---|
| 160 | boolean addNode = true; |
---|
| 161 | for (int j = 0; j < child.getChildren().size(); j++) { |
---|
| 162 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); |
---|
| 163 | if (!nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { |
---|
| 164 | // the new node is no semantical equivalent of the nodes in the current |
---|
| 165 | // selection - break up |
---|
| 166 | addNode = false; |
---|
| 167 | break; |
---|
| 168 | } |
---|
| 169 | else if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { |
---|
| 170 | addNode = false; |
---|
| 171 | break; |
---|
| 172 | } |
---|
| 173 | } |
---|
[439] | 174 | |
---|
[557] | 175 | if (addNode) { |
---|
| 176 | // the node is a semantical equivalent to all the nodes in the existing |
---|
| 177 | // sub-selection |
---|
| 178 | // but it is not structurally identical to either of them. Therefore add it. |
---|
| 179 | builder.addChild((ISelection) child, node); |
---|
| 180 | return; |
---|
| 181 | } |
---|
| 182 | } |
---|
[439] | 183 | } |
---|
| 184 | |
---|
[557] | 185 | // if we did not return in the previous checks, then the node must be added |
---|
| 186 | builder.addChild(parentSelection, node); |
---|
[439] | 187 | } |
---|
| 188 | |
---|
| 189 | } |
---|