[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.List; |
---|
[725] | 18 | import java.util.logging.Level; |
---|
[439] | 19 | |
---|
[1146] | 20 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; |
---|
| 21 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[725] | 24 | import de.ugoe.cs.util.console.Console; |
---|
[439] | 25 | |
---|
| 26 | /** |
---|
[1146] | 27 | * TODO update comment |
---|
| 28 | * |
---|
[809] | 29 | * <p> |
---|
| 30 | * This class is responsible for applying temporal relationship rules on a task tree. Through this, |
---|
| 31 | * a flat task tree is restructured to have more depth but to include more temporal relationships |
---|
[1146] | 32 | * between tasks which are not only a major sequence. I.e. through the application of the |
---|
| 33 | * rules iterations and selections of tasks are detected. Which kind of temporal relations |
---|
[1294] | 34 | * between tasks are detected depends on the {@link ITaskInstanceScopeRule}s known to |
---|
[809] | 35 | * this class. |
---|
| 36 | * </p> |
---|
[1294] | 37 | * <p>The class holds references to the appropriate {@link ITaskInstanceScopeRule}s and calls |
---|
[2218] | 38 | * their {@link ITaskInstanceScopeRule#apply(de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance)} |
---|
[1146] | 39 | * method for each task in the task tree it is needed for. The general behavior of this class is |
---|
[809] | 40 | * the following: |
---|
[2218] | 41 | * </p> |
---|
[809] | 42 | * <ol> |
---|
| 43 | * <li> |
---|
| 44 | * An instance of this class is created using the constructor and calling the |
---|
| 45 | * {@link #init()} method afterwards |
---|
| 46 | * </li> |
---|
| 47 | * <li> |
---|
[2218] | 48 | * then the {@link #applyTaskDetectionRule(List, TaskEquality, int)} |
---|
[1146] | 49 | * method is called for a so far unstructured task |
---|
[809] | 50 | * </li> |
---|
| 51 | * <li> |
---|
| 52 | * the class iterates its internal list of rules and calls their |
---|
[1294] | 53 | * {@link ITaskInstanceScopeRule#apply(ITask, ITaskBuilder, ITaskFactory, boolean)} |
---|
[809] | 54 | * method. |
---|
| 55 | * </li> |
---|
| 56 | * <li> |
---|
| 57 | * the class evaluates the rule application result |
---|
| 58 | * <ul> |
---|
| 59 | * <li> |
---|
| 60 | * if a rule returns a rule application result that is null, the next rule is tried |
---|
| 61 | * </li> |
---|
| 62 | * <li> |
---|
| 63 | * if a rule returns that it would be feasible if more data was available and the rule |
---|
| 64 | * application shall not be finalized (see finalize parameter of the applyRules method) |
---|
| 65 | * the rule application is broken up |
---|
| 66 | * </li> |
---|
| 67 | * <li> |
---|
| 68 | * if a rule returns, that it was applied, the same rule is applied again until it returns |
---|
[1146] | 69 | * null or feasible. For each newly created parent task provided in the rule application |
---|
| 70 | * result, the {@link #applyRules(ITask, ITaskBuilder, ITaskFactory, boolean)} |
---|
[809] | 71 | * method is called. |
---|
| 72 | * </li> |
---|
| 73 | * </ul> |
---|
| 74 | * </li> |
---|
| 75 | * </ol> |
---|
[2218] | 76 | * <p> |
---|
[1146] | 77 | * Through this, all rules are tried to be applied at least once to the provided parent task and |
---|
| 78 | * all parent tasks created during the rule application. |
---|
[809] | 79 | * </p> |
---|
[439] | 80 | * |
---|
[809] | 81 | * @author Patrick Harms |
---|
[439] | 82 | */ |
---|
[557] | 83 | public class TemporalRelationshipRuleManager { |
---|
| 84 | |
---|
[809] | 85 | /** |
---|
| 86 | * <p> |
---|
[1146] | 87 | * the task factory to be used during rule application |
---|
[809] | 88 | * </p> |
---|
| 89 | */ |
---|
[1146] | 90 | private ITaskFactory taskFactory; |
---|
[439] | 91 | |
---|
[557] | 92 | /** |
---|
[809] | 93 | * <p> |
---|
[1146] | 94 | * the task builder to be used during rule application |
---|
[809] | 95 | * </p> |
---|
[557] | 96 | */ |
---|
[1146] | 97 | private ITaskBuilder taskBuilder; |
---|
[1107] | 98 | |
---|
| 99 | /** |
---|
| 100 | * <p> |
---|
| 101 | * initialize the manager |
---|
| 102 | * </p> |
---|
| 103 | * |
---|
[1146] | 104 | * @param taskFactory the task factory to be used for instantiating new task tree |
---|
| 105 | * tasks during rule application |
---|
| 106 | * @param taskBuilder the task builder to be used for linking tasks |
---|
[1107] | 107 | * with each other during rule application |
---|
| 108 | */ |
---|
[1189] | 109 | public TemporalRelationshipRuleManager(ITaskFactory taskFactory, ITaskBuilder taskBuilder) { |
---|
[557] | 110 | super(); |
---|
[1146] | 111 | this.taskFactory = taskFactory; |
---|
| 112 | this.taskBuilder = taskBuilder; |
---|
[557] | 113 | } |
---|
[439] | 114 | |
---|
[557] | 115 | /** |
---|
[809] | 116 | * <p> |
---|
[1889] | 117 | * applies only the known rules for task detection, i.e., currently only the sequence for task |
---|
| 118 | * detection rule |
---|
| 119 | * </p> |
---|
| 120 | * |
---|
| 121 | * @param sessions the sessions to be processed |
---|
| 122 | */ |
---|
[2132] | 123 | public void applyTaskDetectionRule(List<IUserSession> sessions, |
---|
| 124 | TaskEquality minimalTaskEquality, |
---|
| 125 | int minimumSequenceCoverage) |
---|
| 126 | { |
---|
[1889] | 127 | ISessionScopeRule[] rules = new ISessionScopeRule[] { |
---|
[2132] | 128 | new SequenceForTaskDetectionRule(minimalTaskEquality, taskFactory, taskBuilder, |
---|
| 129 | minimumSequenceCoverage) |
---|
[1889] | 130 | }; |
---|
| 131 | |
---|
| 132 | applyRules(rules, sessions, ""); |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * <p> |
---|
| 137 | * applies only the known rules for task merging, i.e., currently only the condense similar task |
---|
| 138 | * rule |
---|
| 139 | * </p> |
---|
| 140 | * |
---|
| 141 | * @param sessions the sessions to be processed |
---|
| 142 | */ |
---|
| 143 | public void applyTaskMergingRule(List<IUserSession> sessions) { |
---|
| 144 | ISessionScopeRule[] rules = new ISessionScopeRule[] { |
---|
| 145 | new CondenseSimilarTasksRule |
---|
| 146 | (TaskEquality.SEMANTICALLY_EQUAL, taskFactory, taskBuilder) |
---|
| 147 | }; |
---|
| 148 | |
---|
| 149 | applyRules(rules, sessions, ""); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | /** |
---|
| 153 | * <p> |
---|
[1146] | 154 | * applies the known rules to the provided parent task. For the creation of further tasks, |
---|
| 155 | * the provided builder and task factory are utilized. If the finalize parameter is true, the |
---|
[809] | 156 | * rule application is finalized as far as possible without waiting for further data. If it is |
---|
| 157 | * false, the rule application is broken up at the first rule returning, that its application |
---|
[1146] | 158 | * would be feasible. The method calls itself for each parent task created through the rule |
---|
| 159 | * application. In this case, the finalize parameter is always true. |
---|
[809] | 160 | * </p> |
---|
[557] | 161 | * |
---|
[1146] | 162 | * @param parent the parent task to apply the rules on |
---|
[809] | 163 | * @param finalize used to indicate, if the rule application shall break up if a rule would |
---|
| 164 | * be feasible if further data was available, or not. |
---|
[1146] | 165 | * @param logIndent simply used for logging purposes to indent the log messages depending |
---|
| 166 | * on the recursion depth of calling this method. |
---|
[557] | 167 | */ |
---|
[1146] | 168 | private int applyRules(ISessionScopeRule[] rules, |
---|
| 169 | List<IUserSession> sessions, |
---|
| 170 | String logIndent) |
---|
| 171 | { |
---|
| 172 | Console.traceln |
---|
| 173 | (Level.FINER, logIndent + "applying rules for " + sessions.size() + " sessions"); |
---|
| 174 | |
---|
| 175 | int noOfRuleApplications = 0; |
---|
| 176 | |
---|
| 177 | for (ISessionScopeRule rule : rules) { |
---|
| 178 | RuleApplicationResult result; |
---|
| 179 | do { |
---|
| 180 | Console.traceln(Level.FINER, logIndent + "trying rule " + rule); |
---|
| 181 | result = rule.apply(sessions); |
---|
| 182 | |
---|
| 183 | if ((result != null) && |
---|
| 184 | (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED)) |
---|
| 185 | { |
---|
| 186 | Console.traceln(Level.FINE, logIndent + "applied rule " + rule); |
---|
| 187 | noOfRuleApplications++; |
---|
| 188 | |
---|
| 189 | //dumpTask(parent, ""); |
---|
| 190 | } |
---|
| 191 | } |
---|
| 192 | while ((result != null) && |
---|
| 193 | (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED)); |
---|
| 194 | |
---|
| 195 | } |
---|
| 196 | |
---|
| 197 | if (noOfRuleApplications <= 0) { |
---|
| 198 | Console.traceln(Level.FINE, logIndent + "no rules applied --> no temporal " + |
---|
| 199 | "relationship generated"); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | return noOfRuleApplications; |
---|
[439] | 203 | } |
---|
[557] | 204 | |
---|
| 205 | /** |
---|
[809] | 206 | * |
---|
| 207 | */ |
---|
[1146] | 208 | /*private void dumpTask(ITask task, String indent) { |
---|
[987] | 209 | StringBuffer message = new StringBuffer(); |
---|
| 210 | message.append(indent); |
---|
| 211 | message.append(task); |
---|
| 212 | if (task.getDescription() != null) { |
---|
| 213 | message.append('('); |
---|
| 214 | message.append(task.getDescription()); |
---|
| 215 | message.append(')'); |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | Console.traceln(Level.FINER, message.toString()); |
---|
| 219 | |
---|
| 220 | if ((task.getChildren() != null) && (task.getChildren().size() > 0)) { |
---|
[1146] | 221 | for (ITask child : task.getChildren()) { |
---|
[987] | 222 | dumpTask(child, indent + " "); |
---|
| 223 | } |
---|
| 224 | } |
---|
[1131] | 225 | }*/ |
---|
[439] | 226 | |
---|
| 227 | } |
---|