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