[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 | |
---|
[557] | 67 | /* |
---|
| 68 | * (non-Javadoc) |
---|
| 69 | * |
---|
| 70 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
[1107] | 71 | * boolean) |
---|
[557] | 72 | */ |
---|
| 73 | @Override |
---|
[1107] | 74 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
[557] | 75 | if (!(parent instanceof ISequence)) { |
---|
| 76 | return null; |
---|
[439] | 77 | } |
---|
| 78 | |
---|
[557] | 79 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
| 80 | |
---|
| 81 | IEventTarget currentEventTarget = null; |
---|
| 82 | int startingIndex = -1; |
---|
| 83 | |
---|
| 84 | int index = 0; |
---|
| 85 | while (index < parent.getChildren().size()) { |
---|
| 86 | ITaskTreeNode child = parent.getChildren().get(index); |
---|
| 87 | |
---|
| 88 | IEventTarget eventTarget = determineEventTarget(child); |
---|
| 89 | |
---|
[798] | 90 | if (((eventTarget == null) && (currentEventTarget != null)) || |
---|
| 91 | ((eventTarget != null) && (!eventTarget.equals(currentEventTarget)))) |
---|
| 92 | { |
---|
[557] | 93 | if (startingIndex < 0) { |
---|
| 94 | startingIndex = index; |
---|
| 95 | currentEventTarget = eventTarget; |
---|
| 96 | } |
---|
| 97 | else { |
---|
[804] | 98 | int endIndex = index - 1; |
---|
| 99 | |
---|
| 100 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 101 | // or if this child is not a sequence itself |
---|
| 102 | if ((startingIndex != endIndex) || |
---|
| 103 | (!(parent.getChildren().get(startingIndex) instanceof ISequence))) |
---|
| 104 | { |
---|
[1107] | 105 | handleEventTargetSequence |
---|
| 106 | (parent, currentEventTarget, startingIndex, endIndex, result); |
---|
[557] | 107 | |
---|
[804] | 108 | result.setRuleApplicationStatus |
---|
| 109 | (RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 110 | return result; |
---|
| 111 | } |
---|
[985] | 112 | else if (eventTarget != null) { |
---|
[804] | 113 | // here a new sequence on a new target begins |
---|
| 114 | startingIndex = index; |
---|
| 115 | currentEventTarget = eventTarget; |
---|
| 116 | } |
---|
[985] | 117 | else { |
---|
| 118 | startingIndex = -1; |
---|
| 119 | currentEventTarget = null; |
---|
| 120 | } |
---|
[557] | 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | index++; |
---|
[439] | 125 | } |
---|
[557] | 126 | |
---|
| 127 | if (startingIndex > -1) { |
---|
[804] | 128 | int endIndex = parent.getChildren().size() - 1; |
---|
| 129 | |
---|
| 130 | if (finalize) { |
---|
| 131 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
| 132 | // or if this child is not a sequence itself |
---|
| 133 | if ((startingIndex > 0) && |
---|
| 134 | ((startingIndex != endIndex) || |
---|
| 135 | (!(parent.getChildren().get(startingIndex) instanceof ISequence)))) |
---|
| 136 | { |
---|
[1107] | 137 | handleEventTargetSequence |
---|
| 138 | (parent, currentEventTarget, startingIndex, endIndex, result); |
---|
[804] | 139 | |
---|
| 140 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
| 141 | } |
---|
[557] | 142 | } |
---|
| 143 | else { |
---|
| 144 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
| 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | return result; |
---|
[439] | 149 | } |
---|
[557] | 150 | |
---|
| 151 | /** |
---|
[1107] | 152 | * |
---|
[557] | 153 | */ |
---|
| 154 | private IEventTarget determineEventTarget(ITaskTreeNode node) { |
---|
| 155 | if (node instanceof IEventTask) { |
---|
| 156 | return ((IEventTask) node).getEventTarget(); |
---|
| 157 | } |
---|
| 158 | else { |
---|
[804] | 159 | IEventTarget commonTarget = null; |
---|
| 160 | |
---|
| 161 | for (ITaskTreeNode child : node.getChildren()) { |
---|
| 162 | if (commonTarget == null) { |
---|
| 163 | commonTarget = determineEventTarget(child); |
---|
| 164 | } |
---|
| 165 | else { |
---|
| 166 | if (!commonTarget.equals(determineEventTarget(child))) { |
---|
| 167 | return null; |
---|
| 168 | } |
---|
| 169 | } |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | return commonTarget; |
---|
[557] | 173 | } |
---|
[439] | 174 | } |
---|
| 175 | |
---|
[557] | 176 | /** |
---|
[1107] | 177 | * |
---|
[557] | 178 | */ |
---|
| 179 | private void handleEventTargetSequence(ITaskTreeNode parent, |
---|
[985] | 180 | IEventTarget target, |
---|
[557] | 181 | int startIndex, |
---|
| 182 | int endIndex, |
---|
| 183 | RuleApplicationResult result) |
---|
[439] | 184 | { |
---|
[1107] | 185 | String description = "interactions on " + target.getStringIdentifier(); |
---|
| 186 | |
---|
| 187 | ISequence sequence = RuleUtils.createNewSubSequenceInRange |
---|
| 188 | (parent, startIndex, endIndex, description, taskTreeNodeFactory, taskTreeBuilder); |
---|
[439] | 189 | |
---|
[557] | 190 | result.addNewlyCreatedParentNode(sequence); |
---|
[439] | 191 | } |
---|
| 192 | |
---|
| 193 | } |
---|