[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 |
|
---|
[727] | 17 | import java.util.Collection;
|
---|
[1109] | 18 | import java.util.LinkedList;
|
---|
[727] | 19 | import java.util.List;
|
---|
[725] | 20 | import java.util.logging.Level;
|
---|
[439] | 21 |
|
---|
[922] | 22 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
[1294] | 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
[1146] | 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
|
---|
| 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
|
---|
| 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
|
---|
| 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
[725] | 28 | import de.ugoe.cs.util.console.Console;
|
---|
[439] | 29 |
|
---|
| 30 | /**
|
---|
[1154] | 31 | * <p> |
---|
| 32 | * The task tree manager is responsible for transforming one or more user sessions into a task |
---|
| 33 | * model. It can be called by providing a collection of user sessions where the result |
---|
| 34 | * will be a task model. Furthermore, it can be used by providing single events in their respective |
---|
| 35 | * order including notifications about where a user session ends. The result is a task model, |
---|
| 36 | * as well. |
---|
| 37 | * </p> |
---|
| 38 | * |
---|
| 39 | * @version 1.0 |
---|
| 40 | * @author pharms |
---|
[439] | 41 | */
|
---|
[557] | 42 | public class TaskTreeManager {
|
---|
| 43 |
|
---|
[1154] | 44 | /** |
---|
| 45 | * <p> |
---|
| 46 | * the internally used task builder |
---|
| 47 | * </p> |
---|
| 48 | */
|
---|
[1146] | 49 | private ITaskBuilder taskBuilder = ComponentManager.getDefaultTaskBuilder();
|
---|
[439] | 50 |
|
---|
[1154] | 51 | /** |
---|
| 52 | * <p> |
---|
| 53 | * the internally used task factory |
---|
| 54 | * </p> |
---|
| 55 | */ |
---|
[1146] | 56 | private ITaskFactory taskFactory = ComponentManager.getDefaultTaskFactory();
|
---|
[557] | 57 |
|
---|
[1154] | 58 | /** |
---|
| 59 | * <p> |
---|
| 60 | * if single events are provided, the user sessions collected so far |
---|
| 61 | * </p> |
---|
| 62 | */ |
---|
[1146] | 63 | private List<IUserSession> sessions = null;
|
---|
[557] | 64 |
|
---|
[1154] | 65 | /** |
---|
| 66 | * <p> |
---|
| 67 | * if single events are provided, the currently collected user session |
---|
| 68 | * </p> |
---|
| 69 | */ |
---|
[1146] | 70 | private IUserSession currentSession = null;
|
---|
[557] | 71 |
|
---|
| 72 | /**
|
---|
[1154] | 73 | * <p> |
---|
| 74 | * initializes the task tree manager |
---|
| 75 | * </p> |
---|
[557] | 76 | */
|
---|
| 77 | public TaskTreeManager() {
|
---|
[1146] | 78 | sessions = new LinkedList<IUserSession>();
|
---|
[439] | 79 | }
|
---|
[557] | 80 |
|
---|
| 81 | /**
|
---|
[1154] | 82 | * <p> |
---|
| 83 | * creates a task model based on the provided user sessions. Yet, the user sessions are |
---|
| 84 | * list of events. Such will be transformed in into task instances of event tasks assigned |
---|
| 85 | * to {@link IUserSession}s. The {@link IUserSession}s will then be restructured using |
---|
| 86 | * the temporal relationship rule manager to detect tasks and respective instances. The |
---|
| 87 | * results of this transformation is stored in a task model which is the return value of |
---|
| 88 | * this method. |
---|
| 89 | * </p> |
---|
| 90 | * |
---|
| 91 | * @param newSessions the user sessions of which the task model shall be created |
---|
| 92 | * |
---|
| 93 | * @return the task model created from the user sessions |
---|
| 94 | * |
---|
| 95 | * @throws IllegalStateException if the task manager is already used by providing it with |
---|
| 96 | * single events |
---|
[557] | 97 | */
|
---|
[1146] | 98 | public synchronized ITaskModel createTaskModel(Collection<List<Event>> newSessions) {
|
---|
[1109] | 99 | if ((currentSession != null) || (sessions.size() > 0)) {
|
---|
| 100 | throw new IllegalStateException("do not mix calls to this method with calls to the " +
|
---|
| 101 | "other methods for handling tasks. Use only one " +
|
---|
| 102 | "variant instead.");
|
---|
| 103 | }
|
---|
[727] | 104 |
|
---|
[1109] | 105 | for (List<Event> newSession : newSessions) {
|
---|
| 106 | if (newSession.size() > 0) {
|
---|
| 107 | for (Event event : newSession) {
|
---|
| 108 | handleNewEvent(event);
|
---|
| 109 | }
|
---|
| 110 | finishSession();
|
---|
[727] | 111 | }
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[1146] | 114 | return getTaskModel();
|
---|
[727] | 115 | }
|
---|
| 116 |
|
---|
| 117 | /**
|
---|
[1154] | 118 | * <p> |
---|
| 119 | * handles a single event that occurred in a user session. |
---|
| 120 | * </p> |
---|
| 121 | * |
---|
| 122 | * @param event the event to handle
|
---|
[727] | 123 | */
|
---|
[557] | 124 | public void handleNewEvent(Event event) {
|
---|
[1146] | 125 | assertSessionSequence(); |
---|
[1294] | 126 | String description = event.getType().getName() + " \u21D2 " + event.getTarget(); |
---|
| 127 | IEventTask eventTask = taskFactory.createNewEventTask(description);
|
---|
| 128 | taskBuilder.addExecutedTask |
---|
| 129 | (currentSession, taskFactory.createNewTaskInstance(eventTask, event));
|
---|
[439] | 130 | }
|
---|
| 131 |
|
---|
[557] | 132 | /**
|
---|
[1154] | 133 | * <p> |
---|
| 134 | * used to denote, that all previously added events using {@link #handleNewEvent(Event)} |
---|
| 135 | * belong to the same session and that this session is now complete. All further events |
---|
| 136 | * will be added to a new session which may be ended using this method, as well. |
---|
| 137 | * </p> |
---|
[557] | 138 | */
|
---|
[1109] | 139 | public void finishSession() {
|
---|
[1397] | 140 | if ((currentSession != null) && (currentSession.size() > 0)) {
|
---|
[1109] | 141 | sessions.add(currentSession);
|
---|
| 142 | currentSession = null;
|
---|
| 143 | }
|
---|
[439] | 144 | }
|
---|
[557] | 145 |
|
---|
| 146 | /**
|
---|
[1154] | 147 | * <p> |
---|
| 148 | * returns the task model, that belongs to the events in the user sessions collected so far. |
---|
| 149 | * </p> |
---|
| 150 | * |
---|
| 151 | * @return the task model |
---|
[557] | 152 | */
|
---|
[1146] | 153 | public synchronized ITaskModel getTaskModel() {
|
---|
[1109] | 154 | finishSession();
|
---|
| 155 |
|
---|
| 156 | Console.traceln(Level.INFO, "applying temporal relationship generation rules");
|
---|
| 157 |
|
---|
[1146] | 158 | ComponentManager.getTemporalRelationshipRuleManager().applyRules(sessions);
|
---|
[1109] | 159 |
|
---|
[1146] | 160 | return taskFactory.createTaskModel(sessions);
|
---|
[439] | 161 | }
|
---|
[557] | 162 |
|
---|
| 163 | /**
|
---|
[1154] | 164 | * <p> |
---|
| 165 | * internally asserts that there is a current session to add new events to |
---|
| 166 | * </p> |
---|
[557] | 167 | */
|
---|
[1109] | 168 | private void assertSessionSequence() {
|
---|
| 169 | if (currentSession == null) {
|
---|
[1146] | 170 | currentSession = taskFactory.createUserSession();
|
---|
[557] | 171 | }
|
---|
[439] | 172 | }
|
---|
| 173 |
|
---|
| 174 | }
|
---|