[922] | 1 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
[439] | 2 | |
---|
[922] | 3 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
| 4 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 5 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 6 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 7 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 8 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
[439] | 9 | |
---|
| 10 | /** |
---|
[972] | 11 | * This rule subdivides long sequences into subsequences of tasks on the same event target. |
---|
| 12 | * Event targets are compared using the equals method. A more complex GUI model is ignored. |
---|
[439] | 13 | * |
---|
| 14 | * @version $Revision: $ $Date: 18.03.2012$ |
---|
| 15 | * @author 2012, last modified by $Author: patrick$ |
---|
| 16 | */ |
---|
[557] | 17 | public class DefaultEventTargetSequenceDetectionRule implements TemporalRelationshipRule { |
---|
[439] | 18 | |
---|
[557] | 19 | /* |
---|
| 20 | * (non-Javadoc) |
---|
| 21 | * |
---|
| 22 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
| 23 | * TaskTreeBuilder, TaskTreeNodeFactory) |
---|
| 24 | */ |
---|
| 25 | @Override |
---|
| 26 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
| 27 | ITaskTreeBuilder builder, |
---|
| 28 | ITaskTreeNodeFactory nodeFactory, |
---|
| 29 | boolean finalize) |
---|
[439] | 30 | { |
---|
[557] | 31 | if (!(parent instanceof ISequence)) { |
---|
| 32 | return null; |
---|
[439] | 33 | } |
---|
| 34 | |
---|
[557] | 35 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 36 | |
---|
| 37 | IEventTarget currentEventTarget = null; |
---|
| 38 | int startingIndex = -1; |
---|
| 39 | |
---|
| 40 | int index = 0; |
---|
| 41 | while (index < parent.getChildren().size()) { |
---|
| 42 | ITaskTreeNode child = parent.getChildren().get(index); |
---|
| 43 | |
---|
| 44 | IEventTarget eventTarget = determineEventTarget(child); |
---|
| 45 | |
---|
[798] | 46 | if (((eventTarget == null) && (currentEventTarget != null)) || |
---|
| 47 | ((eventTarget != null) && (!eventTarget.equals(currentEventTarget)))) |
---|
| 48 | { |
---|
[557] | 49 | if (startingIndex < 0) { |
---|
| 50 | startingIndex = index; |
---|
| 51 | currentEventTarget = eventTarget; |
---|
| 52 | } |
---|
| 53 | else { |
---|
[804] | 54 | int endIndex = index - 1; |
---|
| 55 | |
---|
| 56 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 57 | // or if this child is not a sequence itself |
---|
| 58 | if ((startingIndex != endIndex) || |
---|
| 59 | (!(parent.getChildren().get(startingIndex) instanceof ISequence))) |
---|
| 60 | { |
---|
[985] | 61 | handleEventTargetSequence(parent, currentEventTarget, startingIndex, |
---|
| 62 | endIndex, builder, nodeFactory, result); |
---|
[557] | 63 | |
---|
[804] | 64 | result.setRuleApplicationStatus |
---|
| 65 | (RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 66 | return result; |
---|
| 67 | } |
---|
[985] | 68 | else if (eventTarget != null) { |
---|
[804] | 69 | // here a new sequence on a new target begins |
---|
| 70 | startingIndex = index; |
---|
| 71 | currentEventTarget = eventTarget; |
---|
| 72 | } |
---|
[985] | 73 | else { |
---|
| 74 | startingIndex = -1; |
---|
| 75 | currentEventTarget = null; |
---|
| 76 | } |
---|
[557] | 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | index++; |
---|
[439] | 81 | } |
---|
[557] | 82 | |
---|
| 83 | if (startingIndex > -1) { |
---|
[804] | 84 | int endIndex = parent.getChildren().size() - 1; |
---|
| 85 | |
---|
| 86 | if (finalize) { |
---|
| 87 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 88 | // or if this child is not a sequence itself |
---|
| 89 | if ((startingIndex > 0) && |
---|
| 90 | ((startingIndex != endIndex) || |
---|
| 91 | (!(parent.getChildren().get(startingIndex) instanceof ISequence)))) |
---|
| 92 | { |
---|
[985] | 93 | handleEventTargetSequence(parent, currentEventTarget, startingIndex, endIndex, |
---|
| 94 | builder, nodeFactory, result); |
---|
[804] | 95 | |
---|
| 96 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 97 | } |
---|
[557] | 98 | } |
---|
| 99 | else { |
---|
| 100 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | return result; |
---|
[439] | 105 | } |
---|
[557] | 106 | |
---|
| 107 | /** |
---|
| 108 | * TODO: comment |
---|
| 109 | * |
---|
| 110 | * @param child |
---|
| 111 | * @return |
---|
| 112 | */ |
---|
| 113 | private IEventTarget determineEventTarget(ITaskTreeNode node) { |
---|
| 114 | if (node instanceof IEventTask) { |
---|
| 115 | return ((IEventTask) node).getEventTarget(); |
---|
| 116 | } |
---|
| 117 | else { |
---|
[804] | 118 | IEventTarget commonTarget = null; |
---|
| 119 | |
---|
| 120 | for (ITaskTreeNode child : node.getChildren()) { |
---|
| 121 | if (commonTarget == null) { |
---|
| 122 | commonTarget = determineEventTarget(child); |
---|
| 123 | } |
---|
| 124 | else { |
---|
| 125 | if (!commonTarget.equals(determineEventTarget(child))) { |
---|
| 126 | return null; |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | return commonTarget; |
---|
[557] | 132 | } |
---|
[439] | 133 | } |
---|
| 134 | |
---|
[557] | 135 | /** |
---|
| 136 | * TODO: comment |
---|
| 137 | * |
---|
| 138 | */ |
---|
| 139 | private void handleEventTargetSequence(ITaskTreeNode parent, |
---|
[985] | 140 | IEventTarget target, |
---|
[557] | 141 | int startIndex, |
---|
| 142 | int endIndex, |
---|
| 143 | ITaskTreeBuilder builder, |
---|
| 144 | ITaskTreeNodeFactory nodeFactory, |
---|
| 145 | RuleApplicationResult result) |
---|
[439] | 146 | { |
---|
[557] | 147 | ISequence sequence = nodeFactory.createNewSequence(); |
---|
[985] | 148 | builder.setDescription(sequence, "interactions on " + target.getStringIdentifier()); |
---|
[439] | 149 | |
---|
[557] | 150 | for (int i = startIndex; i <= endIndex; i++) { |
---|
| 151 | builder.addChild(sequence, parent.getChildren().get(startIndex)); |
---|
| 152 | builder.removeChild((ISequence) parent, startIndex); |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | builder.addChild((ISequence) parent, startIndex, sequence); |
---|
| 156 | |
---|
| 157 | result.addNewlyCreatedParentNode(sequence); |
---|
[439] | 158 | } |
---|
| 159 | |
---|
| 160 | } |
---|