[439] | 1 | // Module : $RCSfile: TrackBarSelectionDetectionRule.java,v $ |
---|
| 2 | // Version : $Revision: 0.0 $ $Author: patrick $ $Date: 28.04.2012 $ |
---|
| 3 | // Project : TaskTreeTemporalRelationship |
---|
| 4 | // Creation : 2012 by patrick |
---|
| 5 | // Copyright : Patrick Harms, 2012 |
---|
[557] | 6 | |
---|
[439] | 7 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
| 8 | |
---|
[557] | 9 | import de.ugoe.cs.quest.eventcore.gui.ValueSelection; |
---|
| 10 | import de.ugoe.cs.quest.eventcore.guimodel.ITrackBar; |
---|
[439] | 11 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality; |
---|
| 12 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
[557] | 13 | import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; |
---|
| 14 | import de.ugoe.cs.quest.tasktrees.treeifc.IIteration; |
---|
| 15 | import de.ugoe.cs.quest.tasktrees.treeifc.ISelection; |
---|
| 16 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
| 17 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 18 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 19 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
[439] | 20 | |
---|
| 21 | /** |
---|
| 22 | * TODO comment |
---|
| 23 | * |
---|
| 24 | * @version $Revision: $ $Date: 28.04.2012$ |
---|
| 25 | * @author 2012, last modified by $Author: patrick$ |
---|
| 26 | */ |
---|
[557] | 27 | public class TrackBarSelectionDetectionRule implements TemporalRelationshipRule { |
---|
[439] | 28 | |
---|
[557] | 29 | /** */ |
---|
| 30 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
[439] | 31 | |
---|
[557] | 32 | /** |
---|
| 33 | * TODO: comment |
---|
| 34 | * |
---|
| 35 | */ |
---|
| 36 | TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager) { |
---|
| 37 | super(); |
---|
| 38 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
| 39 | } |
---|
[439] | 40 | |
---|
[557] | 41 | /* |
---|
| 42 | * (non-Javadoc) |
---|
| 43 | * |
---|
| 44 | * @see TemporalRelationshipRule#apply(TaskTreeNode, TaskTreeBuilder, TaskTreeNodeFactory) |
---|
| 45 | */ |
---|
| 46 | @Override |
---|
| 47 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
| 48 | ITaskTreeBuilder builder, |
---|
| 49 | ITaskTreeNodeFactory nodeFactory, |
---|
| 50 | boolean finalize) |
---|
[439] | 51 | { |
---|
[557] | 52 | if (!(parent instanceof ISequence)) { |
---|
| 53 | return null; |
---|
[439] | 54 | } |
---|
[557] | 55 | |
---|
| 56 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 57 | |
---|
| 58 | int valueSelectionStartIndex = -1; |
---|
| 59 | |
---|
| 60 | int index = 0; |
---|
| 61 | while (index < parent.getChildren().size()) { |
---|
| 62 | ITaskTreeNode child = parent.getChildren().get(index); |
---|
| 63 | |
---|
| 64 | if ((child instanceof IEventTask) && |
---|
| 65 | (((IEventTask) child).getEventTarget() instanceof ITrackBar) && |
---|
| 66 | (((IEventTask) child).getEventType() instanceof ValueSelection)) |
---|
| 67 | { |
---|
| 68 | if (valueSelectionStartIndex < 0) { |
---|
| 69 | // let the show begin |
---|
| 70 | valueSelectionStartIndex = index; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | else if (valueSelectionStartIndex >= 0) { |
---|
| 74 | // current child is no more value selection. But the preceding tasks were. |
---|
| 75 | // Therefore, |
---|
| 76 | // create an iteration with the different selectable values as selection children |
---|
| 77 | handleValueSelections(valueSelectionStartIndex, index - 1, parent, builder, |
---|
| 78 | nodeFactory, result); |
---|
| 79 | |
---|
| 80 | return result; |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | index++; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | if (valueSelectionStartIndex >= 0) { |
---|
| 87 | if (finalize) { |
---|
| 88 | handleValueSelections(valueSelectionStartIndex, parent.getChildren().size() - 1, |
---|
| 89 | parent, builder, nodeFactory, result); |
---|
| 90 | } |
---|
| 91 | else { |
---|
| 92 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 93 | } |
---|
| 94 | } |
---|
| 95 | |
---|
[439] | 96 | return result; |
---|
| 97 | } |
---|
[557] | 98 | |
---|
| 99 | /** |
---|
| 100 | * TODO: comment |
---|
| 101 | * |
---|
| 102 | * @param valueSelectionStartIndex |
---|
| 103 | * @param i |
---|
| 104 | */ |
---|
| 105 | private void handleValueSelections(int startIndex, |
---|
| 106 | int endIndex, |
---|
| 107 | ITaskTreeNode parent, |
---|
| 108 | ITaskTreeBuilder builder, |
---|
| 109 | ITaskTreeNodeFactory nodeFactory, |
---|
| 110 | RuleApplicationResult result) |
---|
[439] | 111 | { |
---|
[557] | 112 | IIteration iteration = nodeFactory.createNewIteration(); |
---|
| 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 | /** |
---|
[439] | 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 | |
---|
[557] | 142 | if (child instanceof IEventTask) { |
---|
| 143 | // check, if the new node is a variant for the current event task |
---|
| 144 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); |
---|
| 145 | if (nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { |
---|
| 146 | // the node is a variant. If it not structurally equal, a new sub-selection for |
---|
| 147 | // the existing and the new node must be created. Otherwise, the new node does |
---|
| 148 | // not need to be added |
---|
| 149 | if (!nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { |
---|
| 150 | ISelection selection = nodeFactory.createNewSelection(); |
---|
| 151 | result.addNewlyCreatedParentNode(selection); |
---|
| 152 | builder.addChild(parentSelection, selection); |
---|
[439] | 153 | |
---|
[557] | 154 | builder.addChild(selection, child); |
---|
| 155 | builder.addChild(selection, node); |
---|
| 156 | builder.removeChild(parentSelection, child); |
---|
| 157 | } |
---|
[439] | 158 | |
---|
[557] | 159 | return; |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | else if (child instanceof ISelection) { |
---|
| 163 | // check, if the new node is a variant for the semantically equal children of the |
---|
| 164 | // current |
---|
| 165 | // selection |
---|
| 166 | boolean addNode = true; |
---|
| 167 | for (int j = 0; j < child.getChildren().size(); j++) { |
---|
| 168 | NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node); |
---|
| 169 | if (!nodeEquality.isAtLeast(NodeEquality.SEMANTICALLY_EQUAL)) { |
---|
| 170 | // the new node is no semantical equivalent of the nodes in the current |
---|
| 171 | // selection - break up |
---|
| 172 | addNode = false; |
---|
| 173 | break; |
---|
| 174 | } |
---|
| 175 | else if (nodeEquality.isAtLeast(NodeEquality.LEXICALLY_EQUAL)) { |
---|
| 176 | addNode = false; |
---|
| 177 | break; |
---|
| 178 | } |
---|
| 179 | } |
---|
[439] | 180 | |
---|
[557] | 181 | if (addNode) { |
---|
| 182 | // the node is a semantical equivalent to all the nodes in the existing |
---|
| 183 | // sub-selection |
---|
| 184 | // but it is not structurally identical to either of them. Therefore add it. |
---|
| 185 | builder.addChild((ISelection) child, node); |
---|
| 186 | return; |
---|
| 187 | } |
---|
| 188 | } |
---|
[439] | 189 | } |
---|
| 190 | |
---|
[557] | 191 | // if we did not return in the previous checks, then the node must be added |
---|
| 192 | builder.addChild(parentSelection, node); |
---|
[439] | 193 | } |
---|
| 194 | |
---|
| 195 | } |
---|