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