[1113] | 1 | // Copyright 2012 Georg-August-Universität Göttingen, Germany |
---|
| 2 | // |
---|
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 4 | // you may not use this file except in compliance with the License. |
---|
| 5 | // You may obtain a copy of the License at |
---|
| 6 | // |
---|
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 8 | // |
---|
| 9 | // Unless required by applicable law or agreed to in writing, software |
---|
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 12 | // See the License for the specific language governing permissions and |
---|
| 13 | // limitations under the License. |
---|
| 14 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
[439] | 16 | |
---|
[922] | 17 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
| 18 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
[439] | 23 | |
---|
| 24 | /** |
---|
[972] | 25 | * This rule subdivides long sequences into subsequences of tasks on the same event target. |
---|
| 26 | * Event targets are compared using the equals method. A more complex GUI model is ignored. |
---|
[439] | 27 | * |
---|
| 28 | * @version $Revision: $ $Date: 18.03.2012$ |
---|
| 29 | * @author 2012, last modified by $Author: patrick$ |
---|
| 30 | */ |
---|
[1107] | 31 | class DefaultEventTargetSequenceDetectionRule implements TemporalRelationshipRule { |
---|
[439] | 32 | |
---|
[1107] | 33 | /** |
---|
| 34 | * <p> |
---|
| 35 | * the task tree node factory to be used for creating substructures for the temporal |
---|
| 36 | * relationships identified during rule |
---|
| 37 | * </p> |
---|
| 38 | */ |
---|
| 39 | private ITaskTreeNodeFactory taskTreeNodeFactory; |
---|
| 40 | /** |
---|
| 41 | * <p> |
---|
| 42 | * the task tree builder to be used for creating substructures for the temporal relationships |
---|
| 43 | * identified during rule application |
---|
| 44 | * </p> |
---|
| 45 | */ |
---|
| 46 | private ITaskTreeBuilder taskTreeBuilder; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * <p> |
---|
| 50 | * instantiates the rule with a task tree node factory and builder to be used during rule |
---|
| 51 | * application. |
---|
| 52 | * </p> |
---|
| 53 | * |
---|
| 54 | * @param taskTreeNodeFactory the task tree node factory to be used for creating substructures |
---|
| 55 | * for the temporal relationships identified during rule |
---|
| 56 | * application |
---|
| 57 | * @param taskTreeBuilder the task tree builder to be used for creating substructures for |
---|
| 58 | * the temporal relationships identified during rule application |
---|
| 59 | */ |
---|
| 60 | DefaultEventTargetSequenceDetectionRule(ITaskTreeNodeFactory taskTreeNodeFactory, |
---|
| 61 | ITaskTreeBuilder taskTreeBuilder) |
---|
| 62 | { |
---|
| 63 | this.taskTreeNodeFactory = taskTreeNodeFactory; |
---|
| 64 | this.taskTreeBuilder = taskTreeBuilder; |
---|
| 65 | } |
---|
| 66 | |
---|
[1117] | 67 | /* (non-Javadoc) |
---|
| 68 | * @see java.lang.Object#toString() |
---|
| 69 | */ |
---|
| 70 | @Override |
---|
| 71 | public String toString() { |
---|
| 72 | return "DefaultEventTargetSequenceDetectionRule"; |
---|
| 73 | } |
---|
| 74 | |
---|
[557] | 75 | /* |
---|
| 76 | * (non-Javadoc) |
---|
| 77 | * |
---|
| 78 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
[1107] | 79 | * boolean) |
---|
[557] | 80 | */ |
---|
| 81 | @Override |
---|
[1107] | 82 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
[557] | 83 | if (!(parent instanceof ISequence)) { |
---|
| 84 | return null; |
---|
[439] | 85 | } |
---|
| 86 | |
---|
[557] | 87 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 88 | |
---|
| 89 | IEventTarget currentEventTarget = null; |
---|
| 90 | int startingIndex = -1; |
---|
| 91 | |
---|
| 92 | int index = 0; |
---|
| 93 | while (index < parent.getChildren().size()) { |
---|
| 94 | ITaskTreeNode child = parent.getChildren().get(index); |
---|
| 95 | |
---|
| 96 | IEventTarget eventTarget = determineEventTarget(child); |
---|
| 97 | |
---|
[798] | 98 | if (((eventTarget == null) && (currentEventTarget != null)) || |
---|
| 99 | ((eventTarget != null) && (!eventTarget.equals(currentEventTarget)))) |
---|
| 100 | { |
---|
[557] | 101 | if (startingIndex < 0) { |
---|
| 102 | startingIndex = index; |
---|
| 103 | currentEventTarget = eventTarget; |
---|
| 104 | } |
---|
| 105 | else { |
---|
[804] | 106 | int endIndex = index - 1; |
---|
| 107 | |
---|
| 108 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 109 | // or if this child is not a sequence itself |
---|
| 110 | if ((startingIndex != endIndex) || |
---|
| 111 | (!(parent.getChildren().get(startingIndex) instanceof ISequence))) |
---|
| 112 | { |
---|
[1107] | 113 | handleEventTargetSequence |
---|
| 114 | (parent, currentEventTarget, startingIndex, endIndex, result); |
---|
[557] | 115 | |
---|
[804] | 116 | result.setRuleApplicationStatus |
---|
| 117 | (RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 118 | return result; |
---|
| 119 | } |
---|
[985] | 120 | else if (eventTarget != null) { |
---|
[804] | 121 | // here a new sequence on a new target begins |
---|
| 122 | startingIndex = index; |
---|
| 123 | currentEventTarget = eventTarget; |
---|
| 124 | } |
---|
[985] | 125 | else { |
---|
| 126 | startingIndex = -1; |
---|
| 127 | currentEventTarget = null; |
---|
| 128 | } |
---|
[557] | 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | index++; |
---|
[439] | 133 | } |
---|
[557] | 134 | |
---|
| 135 | if (startingIndex > -1) { |
---|
[804] | 136 | int endIndex = parent.getChildren().size() - 1; |
---|
| 137 | |
---|
| 138 | if (finalize) { |
---|
| 139 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 140 | // or if this child is not a sequence itself |
---|
| 141 | if ((startingIndex > 0) && |
---|
| 142 | ((startingIndex != endIndex) || |
---|
| 143 | (!(parent.getChildren().get(startingIndex) instanceof ISequence)))) |
---|
| 144 | { |
---|
[1107] | 145 | handleEventTargetSequence |
---|
| 146 | (parent, currentEventTarget, startingIndex, endIndex, result); |
---|
[804] | 147 | |
---|
| 148 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 149 | } |
---|
[557] | 150 | } |
---|
| 151 | else { |
---|
| 152 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | return result; |
---|
[439] | 157 | } |
---|
[557] | 158 | |
---|
| 159 | /** |
---|
[1107] | 160 | * |
---|
[557] | 161 | */ |
---|
| 162 | private IEventTarget determineEventTarget(ITaskTreeNode node) { |
---|
| 163 | if (node instanceof IEventTask) { |
---|
| 164 | return ((IEventTask) node).getEventTarget(); |
---|
| 165 | } |
---|
| 166 | else { |
---|
[804] | 167 | IEventTarget commonTarget = null; |
---|
| 168 | |
---|
| 169 | for (ITaskTreeNode child : node.getChildren()) { |
---|
| 170 | if (commonTarget == null) { |
---|
| 171 | commonTarget = determineEventTarget(child); |
---|
| 172 | } |
---|
| 173 | else { |
---|
| 174 | if (!commonTarget.equals(determineEventTarget(child))) { |
---|
| 175 | return null; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | return commonTarget; |
---|
[557] | 181 | } |
---|
[439] | 182 | } |
---|
| 183 | |
---|
[557] | 184 | /** |
---|
[1107] | 185 | * |
---|
[557] | 186 | */ |
---|
| 187 | private void handleEventTargetSequence(ITaskTreeNode parent, |
---|
[985] | 188 | IEventTarget target, |
---|
[557] | 189 | int startIndex, |
---|
| 190 | int endIndex, |
---|
| 191 | RuleApplicationResult result) |
---|
[439] | 192 | { |
---|
[1107] | 193 | String description = "interactions on " + target.getStringIdentifier(); |
---|
| 194 | |
---|
| 195 | ISequence sequence = RuleUtils.createNewSubSequenceInRange |
---|
| 196 | (parent, startIndex, endIndex, description, taskTreeNodeFactory, taskTreeBuilder); |
---|
[439] | 197 | |
---|
[557] | 198 | result.addNewlyCreatedParentNode(sequence); |
---|
[439] | 199 | } |
---|
| 200 | |
---|
| 201 | } |
---|