[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.manager;
|
---|
[439] | 16 |
|
---|
[2132] | 17 | import java.util.Collection; |
---|
| 18 | import java.util.HashMap; |
---|
[1109] | 19 | import java.util.LinkedList;
|
---|
[2132] | 20 | import java.util.List; |
---|
| 21 | import java.util.Map; |
---|
[725] | 22 | import java.util.logging.Level;
|
---|
[439] | 23 |
|
---|
[2132] | 24 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 25 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; |
---|
[1294] | 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
[1146] | 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
|
---|
| 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
|
---|
| 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[725] | 31 | import de.ugoe.cs.util.console.Console;
|
---|
[439] | 32 |
|
---|
| 33 | /**
|
---|
[1154] | 34 | * <p> |
---|
| 35 | * The task tree manager is responsible for transforming one or more user sessions into a task |
---|
| 36 | * model. It can be called by providing a collection of user sessions where the result |
---|
| 37 | * will be a task model. Furthermore, it can be used by providing single events in their respective |
---|
| 38 | * order including notifications about where a user session ends. The result is a task model, |
---|
| 39 | * as well. |
---|
| 40 | * </p> |
---|
| 41 | * |
---|
| 42 | * @version 1.0 |
---|
| 43 | * @author pharms |
---|
[439] | 44 | */
|
---|
[557] | 45 | public class TaskTreeManager {
|
---|
| 46 |
|
---|
[1154] | 47 | /** |
---|
| 48 | * <p> |
---|
| 49 | * the internally used task builder |
---|
| 50 | * </p> |
---|
| 51 | */
|
---|
[1146] | 52 | private ITaskBuilder taskBuilder = ComponentManager.getDefaultTaskBuilder();
|
---|
[439] | 53 |
|
---|
[1154] | 54 | /** |
---|
| 55 | * <p> |
---|
| 56 | * the internally used task factory |
---|
| 57 | * </p> |
---|
| 58 | */ |
---|
[1146] | 59 | private ITaskFactory taskFactory = ComponentManager.getDefaultTaskFactory();
|
---|
[557] | 60 |
|
---|
[1154] | 61 | /** |
---|
| 62 | * <p> |
---|
| 63 | * if single events are provided, the user sessions collected so far |
---|
| 64 | * </p> |
---|
| 65 | */ |
---|
[1146] | 66 | private List<IUserSession> sessions = null;
|
---|
[557] | 67 |
|
---|
[1154] | 68 | /** |
---|
| 69 | * <p> |
---|
| 70 | * if single events are provided, the currently collected user session |
---|
| 71 | * </p> |
---|
| 72 | */ |
---|
[1146] | 73 | private IUserSession currentSession = null;
|
---|
[2132] | 74 | |
---|
| 75 | /** |
---|
| 76 | * <p> |
---|
| 77 | * The default task equality considered when comparing task instances initially |
---|
| 78 | * </p> |
---|
| 79 | */ |
---|
| 80 | private TaskEquality minimalTaskEquality = TaskEquality.SEMANTICALLY_EQUAL; |
---|
| 81 | |
---|
| 82 | /** |
---|
| 83 | * <p> |
---|
| 84 | * If this flag is set, then the task tree manager will not create new event tasks for any |
---|
| 85 | * events. Instead, it will check using the hashcode and equal methods of the provided events |
---|
| 86 | * if a certain event has already occurred, and if so, the corresponding event task will be |
---|
| 87 | * reused. |
---|
| 88 | * </p> |
---|
| 89 | */ |
---|
| 90 | private boolean useEventEqualityForTaskComparison; |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * <p> |
---|
| 94 | * the minimum number of event task instances a sequence must cover to still detect it as a |
---|
| 95 | * representative task |
---|
| 96 | * </p> |
---|
| 97 | */ |
---|
| 98 | private int minimumSequenceCoverage;; |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * <p> |
---|
| 102 | * If the flag {@link #useEventEqualityForTaskComparison} is set, then here the task tree |
---|
| 103 | * manager stores the already created event tasks. |
---|
| 104 | * </p> |
---|
| 105 | */ |
---|
| 106 | private Map<Event, IEventTask> eventTasks = new HashMap<>(); |
---|
[557] | 107 |
|
---|
| 108 | /**
|
---|
[1154] | 109 | * <p> |
---|
| 110 | * initializes the task tree manager |
---|
| 111 | * </p> |
---|
[557] | 112 | */
|
---|
[2132] | 113 | public TaskTreeManager() { |
---|
| 114 | this.sessions = new LinkedList<IUserSession>(); |
---|
[439] | 115 | }
|
---|
[2132] | 116 | |
---|
| 117 | /** |
---|
[1154] | 118 | * <p> |
---|
| 119 | * creates a task model based on the provided user sessions. Yet, the user sessions are |
---|
| 120 | * list of events. Such will be transformed in into task instances of event tasks assigned |
---|
| 121 | * to {@link IUserSession}s. The {@link IUserSession}s will then be restructured using |
---|
| 122 | * the temporal relationship rule manager to detect tasks and respective instances. The |
---|
| 123 | * results of this transformation is stored in a task model which is the return value of |
---|
| 124 | * this method. |
---|
| 125 | * </p> |
---|
| 126 | * |
---|
| 127 | * @param newSessions the user sessions of which the task model shall be created |
---|
| 128 | * |
---|
| 129 | * @return the task model created from the user sessions |
---|
| 130 | * |
---|
| 131 | * @throws IllegalStateException if the task manager is already used by providing it with |
---|
| 132 | * single events |
---|
[2132] | 133 | */ |
---|
| 134 | public synchronized ITaskModel createTaskModel(Collection<List<Event>> newSessions, |
---|
| 135 | boolean useEventEqualityForTaskComparison, |
---|
| 136 | int minimumSequenceCoverage) |
---|
| 137 | { |
---|
| 138 | if ((currentSession != null) || (sessions.size() > 0)) { |
---|
| 139 | throw new IllegalStateException("do not mix calls to this method with calls to the " + |
---|
| 140 | "other methods for handling tasks. Use only one " + |
---|
| 141 | "variant instead."); |
---|
| 142 | } |
---|
| 143 | |
---|
| 144 | this.useEventEqualityForTaskComparison = useEventEqualityForTaskComparison; |
---|
| 145 | |
---|
| 146 | if (useEventEqualityForTaskComparison) { |
---|
| 147 | // if we do this, we also need to consider identity of event tasks afterwards |
---|
| 148 | minimalTaskEquality = TaskEquality.IDENTICAL; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | this.minimumSequenceCoverage = minimumSequenceCoverage; |
---|
| 152 | |
---|
| 153 | for (List<Event> newSession : newSessions) { |
---|
| 154 | if (newSession.size() > 0) { |
---|
| 155 | for (Event event : newSession) { |
---|
| 156 | handleNewEvent(event); |
---|
| 157 | } |
---|
| 158 | finishSession(); |
---|
| 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | return getTaskModel(); |
---|
| 163 | } |
---|
| 164 |
|
---|
| 165 | /**
|
---|
| 166 | * <p> |
---|
| 167 | * call {@link #createTaskModel(Collection, boolean, int)} with false for use event equality |
---|
| 168 | * for comparison and 0 for the minimum number of covered events per sequence |
---|
| 169 | * </p> |
---|
| 170 | * |
---|
| 171 | * @param newSessions the user sessions of which the task model shall be created |
---|
| 172 | * |
---|
| 173 | * @return the task model created from the user sessions |
---|
| 174 | * |
---|
| 175 | * @throws IllegalStateException if the task manager is already used by providing it with |
---|
| 176 | * single events |
---|
[557] | 177 | */
|
---|
[1146] | 178 | public synchronized ITaskModel createTaskModel(Collection<List<Event>> newSessions) {
|
---|
[2132] | 179 | return createTaskModel(newSessions, false, 0);
|
---|
[727] | 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
[1154] | 183 | * <p> |
---|
| 184 | * handles a single event that occurred in a user session. |
---|
| 185 | * </p> |
---|
| 186 | * |
---|
| 187 | * @param event the event to handle
|
---|
[727] | 188 | */
|
---|
[557] | 189 | public void handleNewEvent(Event event) {
|
---|
[1146] | 190 | assertSessionSequence(); |
---|
[2132] | 191 | |
---|
| 192 | if (!useEventEqualityForTaskComparison) { |
---|
| 193 | String description = event.getType().getName() + " \u21D2 " + event.getTarget(); |
---|
| 194 | IEventTask eventTask = taskFactory.createNewEventTask(description);
|
---|
| 195 | taskBuilder.addExecutedTask |
---|
| 196 | (currentSession, taskFactory.createNewTaskInstance(eventTask, event)); |
---|
| 197 | } |
---|
| 198 | else { |
---|
| 199 | IEventTask eventTask = eventTasks.get(event); |
---|
| 200 | if (eventTask == null) { |
---|
| 201 | String description = event.getType().getName() + " \u21D2 " + event.getTarget(); |
---|
| 202 | eventTask = taskFactory.createNewEventTask(description); |
---|
| 203 | eventTasks.put(event, eventTask); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | taskBuilder.addExecutedTask |
---|
| 207 | (currentSession, taskFactory.createNewTaskInstance(eventTask, event)); |
---|
| 208 | }
|
---|
[439] | 209 | }
|
---|
| 210 |
|
---|
[557] | 211 | /**
|
---|
[1154] | 212 | * <p> |
---|
| 213 | * used to denote, that all previously added events using {@link #handleNewEvent(Event)} |
---|
| 214 | * belong to the same session and that this session is now complete. All further events |
---|
| 215 | * will be added to a new session which may be ended using this method, as well. |
---|
| 216 | * </p> |
---|
[557] | 217 | */
|
---|
[1109] | 218 | public void finishSession() {
|
---|
[1397] | 219 | if ((currentSession != null) && (currentSession.size() > 0)) {
|
---|
[1109] | 220 | sessions.add(currentSession);
|
---|
| 221 | currentSession = null;
|
---|
| 222 | }
|
---|
[439] | 223 | }
|
---|
[557] | 224 |
|
---|
| 225 | /**
|
---|
[1154] | 226 | * <p> |
---|
| 227 | * returns the task model, that belongs to the events in the user sessions collected so far. |
---|
| 228 | * </p> |
---|
| 229 | * |
---|
| 230 | * @return the task model |
---|
[557] | 231 | */
|
---|
[1146] | 232 | public synchronized ITaskModel getTaskModel() {
|
---|
[1109] | 233 | finishSession();
|
---|
| 234 |
|
---|
[1889] | 235 | Console.traceln |
---|
| 236 | (Level.INFO, "applying temporal relationship generation rules for detecting tasks");
|
---|
[1109] | 237 |
|
---|
[2132] | 238 | ComponentManager.getTemporalRelationshipRuleManager().applyTaskDetectionRule |
---|
| 239 | (sessions, minimalTaskEquality, minimumSequenceCoverage);
|
---|
[1109] | 240 |
|
---|
[1146] | 241 | return taskFactory.createTaskModel(sessions);
|
---|
[439] | 242 | }
|
---|
[1889] | 243 | |
---|
| 244 | /** |
---|
| 245 | * <p> |
---|
| 246 | * merges similar tasks in the given task model and returns a condensed task model |
---|
| 247 | * </p> |
---|
| 248 | * |
---|
| 249 | * @param inputModel the model with the tasks to be merged |
---|
| 250 | * |
---|
| 251 | * @return as described |
---|
| 252 | */ |
---|
| 253 | public synchronized ITaskModel mergeSimilarTasks(ITaskModel inputModel) { |
---|
| 254 | Console.traceln |
---|
| 255 | (Level.INFO, "applying temporal relationship generation rules for merging tasks"); |
---|
| 256 | |
---|
| 257 | sessions = inputModel.getUserSessions(); |
---|
| 258 | |
---|
| 259 | ComponentManager.getTemporalRelationshipRuleManager().applyTaskMergingRule(sessions); |
---|
| 260 | |
---|
| 261 | return taskFactory.createTaskModel(sessions); |
---|
| 262 | } |
---|
[557] | 263 |
|
---|
| 264 | /**
|
---|
[1154] | 265 | * <p> |
---|
| 266 | * internally asserts that there is a current session to add new events to |
---|
| 267 | * </p> |
---|
[557] | 268 | */
|
---|
[1109] | 269 | private void assertSessionSequence() {
|
---|
| 270 | if (currentSession == null) {
|
---|
[1146] | 271 | currentSession = taskFactory.createUserSession();
|
---|
[557] | 272 | }
|
---|
[439] | 273 | }
|
---|
| 274 |
|
---|
| 275 | }
|
---|