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.ISequence;
|
---|
25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree;
|
---|
26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
|
---|
27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
|
---|
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 ITaskTreeBuilder taskTreeBuilder = ComponentManager.getDefaultTaskTreeBuilder();
|
---|
40 |
|
---|
41 | /** */
|
---|
42 | private ITaskTreeNodeFactory taskTreeNodeFactory =
|
---|
43 | ComponentManager.getDefaultTaskTreeNodeFactory();
|
---|
44 |
|
---|
45 | /** */
|
---|
46 | private List<List<IEventTask>> sessions = null;
|
---|
47 |
|
---|
48 | /** */
|
---|
49 | private List<IEventTask> currentSession = null;
|
---|
50 |
|
---|
51 | /**
|
---|
52 | *
|
---|
53 | */
|
---|
54 | public TaskTreeManager() {
|
---|
55 | sessions = new LinkedList<List<IEventTask>>();
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | *
|
---|
60 | */
|
---|
61 | public synchronized ITaskTree createTaskTree(Collection<List<Event>> newSessions) {
|
---|
62 | if ((currentSession != null) || (sessions.size() > 0)) {
|
---|
63 | throw new IllegalStateException("do not mix calls to this method with calls to the " +
|
---|
64 | "other methods for handling tasks. Use only one " +
|
---|
65 | "variant instead.");
|
---|
66 | }
|
---|
67 |
|
---|
68 | for (List<Event> newSession : newSessions) {
|
---|
69 | if (newSession.size() > 0) {
|
---|
70 | for (Event event : newSession) {
|
---|
71 | handleNewEvent(event);
|
---|
72 | }
|
---|
73 | finishSession();
|
---|
74 | }
|
---|
75 | }
|
---|
76 |
|
---|
77 | return getTaskTree();
|
---|
78 | }
|
---|
79 |
|
---|
80 | /**
|
---|
81 | *
|
---|
82 | */
|
---|
83 | public void handleNewEvent(Event event) {
|
---|
84 | assertSessionSequence();
|
---|
85 | currentSession.add
|
---|
86 | (taskTreeNodeFactory.createNewEventTask(event.getType(), event.getTarget()));
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | *
|
---|
91 | */
|
---|
92 | public void finishSession() {
|
---|
93 | if ((currentSession != null) && (currentSession.size() > 0)) {
|
---|
94 | sessions.add(currentSession);
|
---|
95 | currentSession = null;
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | *
|
---|
101 | */
|
---|
102 | public synchronized ITaskTree getTaskTree() {
|
---|
103 | finishSession();
|
---|
104 |
|
---|
105 | Console.traceln(Level.INFO, "applying temporal relationship generation rules");
|
---|
106 |
|
---|
107 | ISequence rootSequence = taskTreeNodeFactory.createNewSequence();
|
---|
108 | taskTreeBuilder.setDescription(rootSequence, "root");
|
---|
109 |
|
---|
110 | for (List<IEventTask> session : sessions) {
|
---|
111 | ISequence sequence = taskTreeNodeFactory.createNewSequence();
|
---|
112 | taskTreeBuilder.setDescription(sequence, "session");
|
---|
113 |
|
---|
114 | for (IEventTask eventTask : session) {
|
---|
115 | taskTreeBuilder.addChild(sequence, eventTask);
|
---|
116 | }
|
---|
117 |
|
---|
118 | taskTreeBuilder.addChild(rootSequence, sequence);
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | ComponentManager.getTemporalRelationshipRuleManager().applyRules(rootSequence);
|
---|
123 |
|
---|
124 | return taskTreeNodeFactory.createTaskTree(rootSequence);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | *
|
---|
129 | */
|
---|
130 | private void assertSessionSequence() {
|
---|
131 | if (currentSession == null) {
|
---|
132 | currentSession = new LinkedList<IEventTask>();
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | }
|
---|