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