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.ITask; |
---|
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 | /**
|
---|
31 | * TODO comment
|
---|
32 | *
|
---|
33 | * @version $Revision: $ $Date: $
|
---|
34 | * @author 2011, last modified by $Author: $
|
---|
35 | */
|
---|
36 | public class TaskTreeManager {
|
---|
37 |
|
---|
38 | /** */
|
---|
39 | private ITaskBuilder taskBuilder = ComponentManager.getDefaultTaskBuilder();
|
---|
40 |
|
---|
41 | /** */
|
---|
42 | private ITaskFactory taskFactory = ComponentManager.getDefaultTaskFactory();
|
---|
43 |
|
---|
44 | /** */
|
---|
45 | private List<IUserSession> sessions = null;
|
---|
46 |
|
---|
47 | /** */
|
---|
48 | private IUserSession currentSession = null;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | *
|
---|
52 | */
|
---|
53 | public TaskTreeManager() {
|
---|
54 | sessions = new LinkedList<IUserSession>();
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | *
|
---|
59 | */
|
---|
60 | public synchronized ITaskModel createTaskModel(Collection<List<Event>> newSessions) {
|
---|
61 | if ((currentSession != null) || (sessions.size() > 0)) {
|
---|
62 | throw new IllegalStateException("do not mix calls to this method with calls to the " +
|
---|
63 | "other methods for handling tasks. Use only one " +
|
---|
64 | "variant instead.");
|
---|
65 | }
|
---|
66 |
|
---|
67 | for (List<Event> newSession : newSessions) {
|
---|
68 | if (newSession.size() > 0) {
|
---|
69 | for (Event event : newSession) {
|
---|
70 | handleNewEvent(event);
|
---|
71 | }
|
---|
72 | finishSession();
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | return getTaskModel();
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | *
|
---|
81 | */
|
---|
82 | public void handleNewEvent(Event event) {
|
---|
83 | assertSessionSequence(); |
---|
84 | ITask eventTask = taskFactory.createNewEventTask(event.getType(), event.getTarget());
|
---|
85 | taskBuilder.addExecutedTask(currentSession, taskFactory.createNewTaskInstance(eventTask));
|
---|
86 | }
|
---|
87 |
|
---|
88 | /**
|
---|
89 | *
|
---|
90 | */
|
---|
91 | public void finishSession() {
|
---|
92 | if ((currentSession != null) && (currentSession.getExecutedTasks().size() > 0)) {
|
---|
93 | sessions.add(currentSession);
|
---|
94 | currentSession = null;
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | *
|
---|
100 | */
|
---|
101 | public synchronized ITaskModel getTaskModel() {
|
---|
102 | finishSession();
|
---|
103 |
|
---|
104 | Console.traceln(Level.INFO, "applying temporal relationship generation rules");
|
---|
105 |
|
---|
106 | ComponentManager.getTemporalRelationshipRuleManager().applyRules(sessions);
|
---|
107 |
|
---|
108 | return taskFactory.createTaskModel(sessions);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | *
|
---|
113 | */
|
---|
114 | private void assertSessionSequence() {
|
---|
115 | if (currentSession == null) {
|
---|
116 | currentSession = taskFactory.createUserSession();
|
---|
117 | }
|
---|
118 | }
|
---|
119 |
|
---|
120 | }
|
---|