[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 | */ |
---|
[1107] | 17 | class DefaultEventTargetSequenceDetectionRule implements TemporalRelationshipRule { |
---|
[439] | 18 | |
---|
[1107] | 19 | /** |
---|
| 20 | * <p> |
---|
| 21 | * the task tree node factory to be used for creating substructures for the temporal |
---|
| 22 | * relationships identified during rule |
---|
| 23 | * </p> |
---|
| 24 | */ |
---|
| 25 | private ITaskTreeNodeFactory taskTreeNodeFactory; |
---|
| 26 | /** |
---|
| 27 | * <p> |
---|
| 28 | * the task tree builder to be used for creating substructures for the temporal relationships |
---|
| 29 | * identified during rule application |
---|
| 30 | * </p> |
---|
| 31 | */ |
---|
| 32 | private ITaskTreeBuilder taskTreeBuilder; |
---|
| 33 | |
---|
| 34 | /** |
---|
| 35 | * <p> |
---|
| 36 | * instantiates the rule with a task tree node factory and builder to be used during rule |
---|
| 37 | * application. |
---|
| 38 | * </p> |
---|
| 39 | * |
---|
| 40 | * @param taskTreeNodeFactory the task tree node factory to be used for creating substructures |
---|
| 41 | * for the temporal relationships identified during rule |
---|
| 42 | * application |
---|
| 43 | * @param taskTreeBuilder the task tree builder to be used for creating substructures for |
---|
| 44 | * the temporal relationships identified during rule application |
---|
| 45 | */ |
---|
| 46 | DefaultEventTargetSequenceDetectionRule(ITaskTreeNodeFactory taskTreeNodeFactory, |
---|
| 47 | ITaskTreeBuilder taskTreeBuilder) |
---|
| 48 | { |
---|
| 49 | this.taskTreeNodeFactory = taskTreeNodeFactory; |
---|
| 50 | this.taskTreeBuilder = taskTreeBuilder; |
---|
| 51 | } |
---|
| 52 | |
---|
[557] | 53 | /* |
---|
| 54 | * (non-Javadoc) |
---|
| 55 | * |
---|
| 56 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
[1107] | 57 | * boolean) |
---|
[557] | 58 | */ |
---|
| 59 | @Override |
---|
[1107] | 60 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
[557] | 61 | if (!(parent instanceof ISequence)) { |
---|
| 62 | return null; |
---|
[439] | 63 | } |
---|
| 64 | |
---|
[557] | 65 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 66 | |
---|
| 67 | IEventTarget currentEventTarget = null; |
---|
| 68 | int startingIndex = -1; |
---|
| 69 | |
---|
| 70 | int index = 0; |
---|
| 71 | while (index < parent.getChildren().size()) { |
---|
| 72 | ITaskTreeNode child = parent.getChildren().get(index); |
---|
| 73 | |
---|
| 74 | IEventTarget eventTarget = determineEventTarget(child); |
---|
| 75 | |
---|
[798] | 76 | if (((eventTarget == null) && (currentEventTarget != null)) || |
---|
| 77 | ((eventTarget != null) && (!eventTarget.equals(currentEventTarget)))) |
---|
| 78 | { |
---|
[557] | 79 | if (startingIndex < 0) { |
---|
| 80 | startingIndex = index; |
---|
| 81 | currentEventTarget = eventTarget; |
---|
| 82 | } |
---|
| 83 | else { |
---|
[804] | 84 | int endIndex = index - 1; |
---|
| 85 | |
---|
| 86 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 87 | // or if this child is not a sequence itself |
---|
| 88 | if ((startingIndex != endIndex) || |
---|
| 89 | (!(parent.getChildren().get(startingIndex) instanceof ISequence))) |
---|
| 90 | { |
---|
[1107] | 91 | handleEventTargetSequence |
---|
| 92 | (parent, currentEventTarget, startingIndex, endIndex, result); |
---|
[557] | 93 | |
---|
[804] | 94 | result.setRuleApplicationStatus |
---|
| 95 | (RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 96 | return result; |
---|
| 97 | } |
---|
[985] | 98 | else if (eventTarget != null) { |
---|
[804] | 99 | // here a new sequence on a new target begins |
---|
| 100 | startingIndex = index; |
---|
| 101 | currentEventTarget = eventTarget; |
---|
| 102 | } |
---|
[985] | 103 | else { |
---|
| 104 | startingIndex = -1; |
---|
| 105 | currentEventTarget = null; |
---|
| 106 | } |
---|
[557] | 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | index++; |
---|
[439] | 111 | } |
---|
[557] | 112 | |
---|
| 113 | if (startingIndex > -1) { |
---|
[804] | 114 | int endIndex = parent.getChildren().size() - 1; |
---|
| 115 | |
---|
| 116 | if (finalize) { |
---|
| 117 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 118 | // or if this child is not a sequence itself |
---|
| 119 | if ((startingIndex > 0) && |
---|
| 120 | ((startingIndex != endIndex) || |
---|
| 121 | (!(parent.getChildren().get(startingIndex) instanceof ISequence)))) |
---|
| 122 | { |
---|
[1107] | 123 | handleEventTargetSequence |
---|
| 124 | (parent, currentEventTarget, startingIndex, endIndex, result); |
---|
[804] | 125 | |
---|
| 126 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 127 | } |
---|
[557] | 128 | } |
---|
| 129 | else { |
---|
| 130 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | return result; |
---|
[439] | 135 | } |
---|
[557] | 136 | |
---|
| 137 | /** |
---|
[1107] | 138 | * |
---|
[557] | 139 | */ |
---|
| 140 | private IEventTarget determineEventTarget(ITaskTreeNode node) { |
---|
| 141 | if (node instanceof IEventTask) { |
---|
| 142 | return ((IEventTask) node).getEventTarget(); |
---|
| 143 | } |
---|
| 144 | else { |
---|
[804] | 145 | IEventTarget commonTarget = null; |
---|
| 146 | |
---|
| 147 | for (ITaskTreeNode child : node.getChildren()) { |
---|
| 148 | if (commonTarget == null) { |
---|
| 149 | commonTarget = determineEventTarget(child); |
---|
| 150 | } |
---|
| 151 | else { |
---|
| 152 | if (!commonTarget.equals(determineEventTarget(child))) { |
---|
| 153 | return null; |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | return commonTarget; |
---|
[557] | 159 | } |
---|
[439] | 160 | } |
---|
| 161 | |
---|
[557] | 162 | /** |
---|
[1107] | 163 | * |
---|
[557] | 164 | */ |
---|
| 165 | private void handleEventTargetSequence(ITaskTreeNode parent, |
---|
[985] | 166 | IEventTarget target, |
---|
[557] | 167 | int startIndex, |
---|
| 168 | int endIndex, |
---|
| 169 | RuleApplicationResult result) |
---|
[439] | 170 | { |
---|
[1107] | 171 | String description = "interactions on " + target.getStringIdentifier(); |
---|
| 172 | |
---|
| 173 | ISequence sequence = RuleUtils.createNewSubSequenceInRange |
---|
| 174 | (parent, startIndex, endIndex, description, taskTreeNodeFactory, taskTreeBuilder); |
---|
[439] | 175 | |
---|
[557] | 176 | result.addNewlyCreatedParentNode(sequence); |
---|
[439] | 177 | } |
---|
| 178 | |
---|
| 179 | } |
---|