source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/manager/TaskTreeManager.java @ 2132

Last change on this file since 2132 was 2132, checked in by pharms, 8 years ago
  • added possibility to select the minimal amount of action instances a detected sequence must cover
  • Property svn:executable set to *
File size: 9.5 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.manager;
16
17import java.util.Collection;
18import java.util.HashMap;
19import java.util.LinkedList;
20import java.util.List;
21import java.util.Map;
22import java.util.logging.Level;
23
24import de.ugoe.cs.autoquest.eventcore.Event;
25import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskModel;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
31import de.ugoe.cs.util.console.Console;
32
33/**
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
44 */
45public class TaskTreeManager {
46   
47    /**
48     * <p>
49     * the internally used task builder
50     * </p>
51     */
52    private ITaskBuilder taskBuilder = ComponentManager.getDefaultTaskBuilder();
53
54    /**
55     * <p>
56     * the internally used task factory
57     * </p>
58     */
59    private ITaskFactory taskFactory = ComponentManager.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     * 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<>();
107
108    /**
109     * <p>
110     * initializes the task tree manager
111     * </p>
112     */
113    public TaskTreeManager() {
114        this.sessions = new LinkedList<IUserSession>();
115    }
116
117    /**
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
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
177     */
178    public synchronized ITaskModel createTaskModel(Collection<List<Event>> newSessions) {
179        return createTaskModel(newSessions, false, 0);
180    }
181
182    /**
183     * <p>
184     * handles a single event that occurred in a user session.
185     * </p>
186     *
187     * @param event the event to handle
188     */
189    public void handleNewEvent(Event event) {
190        assertSessionSequence();
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        }
209    }
210
211    /**
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>
217     */
218    public void finishSession() {
219        if ((currentSession != null) && (currentSession.size() > 0)) {
220            sessions.add(currentSession);
221            currentSession = null;
222        }
223    }
224
225    /**
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
231     */
232    public synchronized ITaskModel getTaskModel() {
233        finishSession();
234       
235        Console.traceln
236            (Level.INFO, "applying temporal relationship generation rules for detecting tasks");
237       
238        ComponentManager.getTemporalRelationshipRuleManager().applyTaskDetectionRule
239            (sessions, minimalTaskEquality, minimumSequenceCoverage);
240
241        return taskFactory.createTaskModel(sessions);
242    }
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    }
263
264    /**
265     * <p>
266     * internally asserts that there is a current session to add new events to
267     * </p>
268     */
269    private void assertSessionSequence() {
270        if (currentSession == null) {
271            currentSession = taskFactory.createUserSession();
272        }
273    }
274
275}
Note: See TracBrowser for help on using the repository browser.