[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 | |
---|
| 17 | import java.util.ArrayList; |
---|
| 18 | import java.util.List; |
---|
[725] | 19 | import java.util.logging.Level; |
---|
[439] | 20 | |
---|
[987] | 21 | import de.ugoe.cs.autoquest.eventcore.guimodel.IDialog; |
---|
| 22 | import de.ugoe.cs.autoquest.eventcore.guimodel.IFrame; |
---|
| 23 | import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement; |
---|
[922] | 24 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality; |
---|
| 25 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
| 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
| 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
[725] | 29 | import de.ugoe.cs.util.console.Console; |
---|
[439] | 30 | |
---|
| 31 | /** |
---|
[809] | 32 | * <p> |
---|
| 33 | * This class is responsible for applying temporal relationship rules on a task tree. Through this, |
---|
| 34 | * a flat task tree is restructured to have more depth but to include more temporal relationships |
---|
| 35 | * between task tree nodes which are not only a major sequence. I.e. through the application of |
---|
| 36 | * rule iterations and selections of task tree nodes are detected. Which kind of temporal relations |
---|
| 37 | * between task tree nodes are detected depends on the {@link TemporalRelationshipRule}s known to |
---|
| 38 | * this class. |
---|
| 39 | * </p> |
---|
| 40 | * <p>The class holds references to the appropriate {@link TemporalRelationshipRule}s and calls |
---|
| 41 | * their {@link TemporalRelationshipRule#apply(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)} |
---|
| 42 | * method for each node in the task tree it is needed for. The general behavior of this class is |
---|
| 43 | * the following: |
---|
| 44 | * <ol> |
---|
| 45 | * <li> |
---|
| 46 | * An instance of this class is created using the constructor and calling the |
---|
| 47 | * {@link #init()} method afterwards |
---|
| 48 | * </li> |
---|
| 49 | * <li> |
---|
| 50 | * then the {@link #applyRules(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)} |
---|
| 51 | * method is called for a so far unstructured task tree node |
---|
| 52 | * </li> |
---|
| 53 | * <li> |
---|
| 54 | * the class iterates its internal list of rules and calls their |
---|
| 55 | * {@link TemporalRelationshipRule#apply(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)} |
---|
| 56 | * method. |
---|
| 57 | * </li> |
---|
| 58 | * <li> |
---|
| 59 | * the class evaluates the rule application result |
---|
| 60 | * <ul> |
---|
| 61 | * <li> |
---|
| 62 | * if a rule returns a rule application result that is null, the next rule is tried |
---|
| 63 | * </li> |
---|
| 64 | * <li> |
---|
| 65 | * if a rule returns that it would be feasible if more data was available and the rule |
---|
| 66 | * application shall not be finalized (see finalize parameter of the applyRules method) |
---|
| 67 | * the rule application is broken up |
---|
| 68 | * </li> |
---|
| 69 | * <li> |
---|
| 70 | * if a rule returns, that it was applied, the same rule is applied again until it returns |
---|
| 71 | * null or feasible. For each newly created parent node provided in the rule application |
---|
| 72 | * result, the {@link #applyRules(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)} |
---|
| 73 | * method is called. |
---|
| 74 | * </li> |
---|
| 75 | * </ul> |
---|
| 76 | * </li> |
---|
| 77 | * </ol> |
---|
| 78 | * Through this, all rules are tried to be applied at least once to the provided parent node and |
---|
| 79 | * all parent nodes created during the rule application. |
---|
| 80 | * </p> |
---|
[439] | 81 | * |
---|
[809] | 82 | * @author Patrick Harms |
---|
[439] | 83 | */ |
---|
[557] | 84 | public class TemporalRelationshipRuleManager { |
---|
| 85 | |
---|
[809] | 86 | /** |
---|
| 87 | * <p> |
---|
| 88 | * the node equality manager needed by the rules to compare task tree nodes with each other |
---|
| 89 | * </p> |
---|
| 90 | */ |
---|
[557] | 91 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
[439] | 92 | |
---|
[809] | 93 | /** |
---|
| 94 | * <p> |
---|
[1107] | 95 | * the task tree node factory to be used during rule application |
---|
[809] | 96 | * </p> |
---|
| 97 | */ |
---|
[1107] | 98 | private ITaskTreeNodeFactory taskTreeNodeFactory; |
---|
[439] | 99 | |
---|
[557] | 100 | /** |
---|
[809] | 101 | * <p> |
---|
[1107] | 102 | * the task tree builder to be used during rule application |
---|
[809] | 103 | * </p> |
---|
[557] | 104 | */ |
---|
[1107] | 105 | private ITaskTreeBuilder taskTreeBuilder; |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * <p> |
---|
| 109 | * the temporal relationship rules known to the manager that are executed on whole sub trees. |
---|
| 110 | * The rules are applied in the order they occur in this list. |
---|
| 111 | * </p> |
---|
| 112 | */ |
---|
| 113 | private TemporalRelationshipRule[] treeScopeRules; |
---|
| 114 | |
---|
| 115 | /** |
---|
| 116 | * <p> |
---|
| 117 | * the temporal relationship rules known to the manager that are executed on whole sub trees. |
---|
| 118 | * The rules are applied in the order they occur in this list. |
---|
| 119 | * </p> |
---|
| 120 | */ |
---|
| 121 | private TemporalRelationshipRule[] nodeScopeRules; |
---|
| 122 | |
---|
| 123 | /** |
---|
| 124 | * <p> |
---|
| 125 | * initialize the manager |
---|
| 126 | * </p> |
---|
| 127 | * |
---|
| 128 | * @param nodeEqualityRuleManager the node equality rule manager to be used by the known rules |
---|
| 129 | * for task tree node comparison during rule application |
---|
| 130 | * @param taskTreeNodeFactory the node factory to be used for instantiating new task tree |
---|
| 131 | * nodes during rule application |
---|
| 132 | * @param taskTreeBuilder the task tree builder to be used for linking task tree nodes |
---|
| 133 | * with each other during rule application |
---|
| 134 | */ |
---|
| 135 | public TemporalRelationshipRuleManager(NodeEqualityRuleManager nodeEqualityRuleManager, |
---|
| 136 | ITaskTreeNodeFactory taskTreeNodeFactory, |
---|
| 137 | ITaskTreeBuilder taskTreeBuilder) |
---|
| 138 | { |
---|
[557] | 139 | super(); |
---|
| 140 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
[1107] | 141 | this.taskTreeNodeFactory = taskTreeNodeFactory; |
---|
| 142 | this.taskTreeBuilder = taskTreeBuilder; |
---|
[557] | 143 | } |
---|
[439] | 144 | |
---|
[557] | 145 | /** |
---|
[809] | 146 | * <p> |
---|
| 147 | * initialized the temporal relationship rule manager by instantiating the known rules and |
---|
| 148 | * providing them with a reference to the node equality manager or other information they need. |
---|
| 149 | * </p> |
---|
[557] | 150 | */ |
---|
| 151 | public void init() { |
---|
[987] | 152 | List<Class<? extends IGUIElement>> frameFilter = |
---|
| 153 | new ArrayList<Class<? extends IGUIElement>>(); |
---|
| 154 | frameFilter.add(IFrame.class); |
---|
| 155 | frameFilter.add(IDialog.class); |
---|
[1107] | 156 | //frameFilter.add(ICanvas.class); |
---|
[987] | 157 | |
---|
[1107] | 158 | treeScopeRules = new TemporalRelationshipRule[] { |
---|
[1127] | 159 | new SequenceForTaskDetectionRule |
---|
| 160 | (nodeEqualityRuleManager, NodeEquality.SEMANTICALLY_EQUAL, |
---|
| 161 | taskTreeNodeFactory, taskTreeBuilder), |
---|
| 162 | /*new DefaultTaskSequenceDetectionRule |
---|
| 163 | (nodeEqualityRuleManager, NodeEquality.SYNTACTICALLY_EQUAL, |
---|
| 164 | taskTreeNodeFactory, taskTreeBuilder), |
---|
[1107] | 165 | new DefaultTaskSequenceDetectionRule |
---|
[1127] | 166 | (nodeEqualityRuleManager, NodeEquality.LEXICALLY_EQUAL, |
---|
| 167 | taskTreeNodeFactory, taskTreeBuilder),*/ |
---|
| 168 | /*new TreeScopeWrapperRule |
---|
| 169 | (new DefaultIterationDetectionRule |
---|
| 170 | (nodeEqualityRuleManager, NodeEquality.LEXICALLY_EQUAL, |
---|
| 171 | taskTreeNodeFactory, taskTreeBuilder)), |
---|
| 172 | new TreeScopeWrapperRule |
---|
| 173 | (new DefaultIterationDetectionRule |
---|
| 174 | (nodeEqualityRuleManager, NodeEquality.SYNTACTICALLY_EQUAL, |
---|
| 175 | taskTreeNodeFactory, taskTreeBuilder)), |
---|
| 176 | new TreeScopeWrapperRule |
---|
| 177 | (new DefaultIterationDetectionRule |
---|
| 178 | (nodeEqualityRuleManager, NodeEquality.SEMANTICALLY_EQUAL, |
---|
| 179 | taskTreeNodeFactory, taskTreeBuilder))*/ |
---|
[1107] | 180 | }; |
---|
[805] | 181 | |
---|
[1107] | 182 | //treeScopeRules.add(new DefaultGuiElementSequenceDetectionRule(frameFilter)); |
---|
| 183 | |
---|
| 184 | nodeScopeRules = new TemporalRelationshipRule[] { |
---|
[1127] | 185 | //new SequenceOnGuiElementDetectionRule(taskTreeNodeFactory, taskTreeBuilder), |
---|
| 186 | //new EventSequenceOnSameTargetDetectionRule(taskTreeNodeFactory, taskTreeBuilder), |
---|
[1107] | 187 | new TrackBarSelectionDetectionRule |
---|
| 188 | (nodeEqualityRuleManager, taskTreeNodeFactory, taskTreeBuilder), |
---|
| 189 | //new DefaultGuiEventSequenceDetectionRule(taskTreeNodeFactory, taskTreeBuilder), |
---|
| 190 | }; |
---|
| 191 | |
---|
[557] | 192 | } |
---|
[439] | 193 | |
---|
[557] | 194 | /** |
---|
[809] | 195 | * <p> |
---|
| 196 | * applies the known rules to the provided parent node. For the creation of further nodes, |
---|
[1107] | 197 | * the provided builder and node factory are utilized. The method expectes, that no more data |
---|
| 198 | * is available and, therefore, finalizes the rule application. |
---|
| 199 | * </p> |
---|
| 200 | * |
---|
| 201 | * @param nodeFactory the node factory to be used for instantiating new task tree nodes. |
---|
| 202 | */ |
---|
| 203 | public void applyRules(ITaskTreeNode parent) { |
---|
| 204 | applyRules(parent, true); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | /** |
---|
| 208 | * <p> |
---|
| 209 | * applies the known rules to the provided parent node. For the creation of further nodes, |
---|
[809] | 210 | * the provided builder and node factory are utilized. If the finalize parameter is true, the |
---|
| 211 | * rule application is finalized as far as possible without waiting for further data. If it is |
---|
| 212 | * false, the rule application is broken up at the first rule returning, that its application |
---|
| 213 | * would be feasible. |
---|
| 214 | * </p> |
---|
[557] | 215 | * |
---|
[809] | 216 | * @param parent the parent node to apply the rules on |
---|
| 217 | * @param finalize used to indicate, if the rule application shall break up if a rule would |
---|
| 218 | * be feasible if further data was available, or not. |
---|
[557] | 219 | */ |
---|
[1107] | 220 | public void applyRules(ITaskTreeNode parent, boolean finalize) { |
---|
| 221 | applyRules(treeScopeRules, parent, finalize, ""); |
---|
[439] | 222 | } |
---|
[557] | 223 | |
---|
| 224 | /** |
---|
[809] | 225 | * <p> |
---|
| 226 | * applies the known rules to the provided parent node. For the creation of further nodes, |
---|
| 227 | * the provided builder and node factory are utilized. If the finalize parameter is true, the |
---|
| 228 | * rule application is finalized as far as possible without waiting for further data. If it is |
---|
| 229 | * false, the rule application is broken up at the first rule returning, that its application |
---|
| 230 | * would be feasible. The method calls itself for each parent node created through the rule |
---|
| 231 | * application. In this case, the finalize parameter is always true. |
---|
| 232 | * </p> |
---|
[557] | 233 | * |
---|
[809] | 234 | * @param parent the parent node to apply the rules on |
---|
| 235 | * @param finalize used to indicate, if the rule application shall break up if a rule would |
---|
| 236 | * be feasible if further data was available, or not. |
---|
[987] | 237 | * @param logIndent simply used for logging purposes to indent the log messages depending |
---|
[809] | 238 | * on the recursion depth of calling this method. |
---|
[557] | 239 | */ |
---|
[1107] | 240 | private int applyRules(TemporalRelationshipRule[] rules, |
---|
| 241 | ITaskTreeNode parent, |
---|
| 242 | boolean finalize, |
---|
| 243 | String logIndent) |
---|
[439] | 244 | { |
---|
[725] | 245 | Console.traceln(Level.FINER, logIndent + "applying rules for " + parent); |
---|
[557] | 246 | |
---|
| 247 | int noOfRuleApplications = 0; |
---|
| 248 | |
---|
[809] | 249 | for (TemporalRelationshipRule rule : rules) { |
---|
[557] | 250 | RuleApplicationResult result; |
---|
| 251 | do { |
---|
[987] | 252 | Console.traceln(Level.FINER, logIndent + "trying rule " + rule + " on " + parent); |
---|
[1107] | 253 | result = rule.apply(parent, finalize); |
---|
[557] | 254 | |
---|
| 255 | if ((result != null) && |
---|
[1127] | 256 | (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED)) |
---|
[557] | 257 | { |
---|
[725] | 258 | Console.traceln |
---|
| 259 | (Level.FINE, logIndent + "applied rule " + rule + " on " + parent); |
---|
[557] | 260 | noOfRuleApplications++; |
---|
[987] | 261 | |
---|
[1127] | 262 | //dumpTask(parent, ""); |
---|
[557] | 263 | |
---|
| 264 | for (ITaskTreeNode newParent : result.getNewlyCreatedParentNodes()) { |
---|
| 265 | noOfRuleApplications += |
---|
[1107] | 266 | applyRules(nodeScopeRules, newParent, true, logIndent + " "); |
---|
[557] | 267 | } |
---|
| 268 | } |
---|
| 269 | } |
---|
| 270 | while ((result != null) && |
---|
[1127] | 271 | (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED)); |
---|
[557] | 272 | |
---|
| 273 | if ((!finalize) && |
---|
| 274 | (result != null) && |
---|
[1127] | 275 | (result.getRuleApplicationStatus() == RuleApplicationStatus.FEASIBLE)) |
---|
[557] | 276 | { |
---|
| 277 | // in this case, don't go on applying rules, which should not be applied yet |
---|
| 278 | break; |
---|
| 279 | } |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | if (noOfRuleApplications <= 0) { |
---|
[1127] | 283 | Console.traceln(Level.FINE, logIndent + "no rules applied --> no temporal " + |
---|
[725] | 284 | "relationship generated"); |
---|
[557] | 285 | } |
---|
| 286 | |
---|
| 287 | return noOfRuleApplications; |
---|
[439] | 288 | } |
---|
[557] | 289 | |
---|
| 290 | /** |
---|
[809] | 291 | * |
---|
| 292 | */ |
---|
[987] | 293 | private void dumpTask(ITaskTreeNode task, String indent) { |
---|
| 294 | StringBuffer message = new StringBuffer(); |
---|
| 295 | message.append(indent); |
---|
| 296 | message.append(task); |
---|
| 297 | if (task.getDescription() != null) { |
---|
| 298 | message.append('('); |
---|
| 299 | message.append(task.getDescription()); |
---|
| 300 | message.append(')'); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | Console.traceln(Level.FINER, message.toString()); |
---|
| 304 | |
---|
| 305 | if ((task.getChildren() != null) && (task.getChildren().size() > 0)) { |
---|
| 306 | for (ITaskTreeNode child : task.getChildren()) { |
---|
| 307 | dumpTask(child, indent + " "); |
---|
| 308 | } |
---|
| 309 | } |
---|
| 310 | } |
---|
[439] | 311 | |
---|
| 312 | } |
---|