[1146] | 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; |
---|
| 16 | |
---|
| 17 | import static org.junit.Assert.*; |
---|
| 18 | |
---|
| 19 | import java.io.FileNotFoundException; |
---|
| 20 | import java.io.FileOutputStream; |
---|
| 21 | import java.io.PrintStream; |
---|
| 22 | import java.util.HashMap; |
---|
| 23 | import java.util.List; |
---|
| 24 | import java.util.Map; |
---|
| 25 | |
---|
[1294] | 26 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
[1146] | 27 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
[1294] | 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
[1146] | 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
[1294] | 32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
[1146] | 33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
[1294] | 34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1146] | 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITemporalRelationship; |
---|
| 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * TODO comment |
---|
| 44 | * |
---|
| 45 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
| 46 | * @author 2012, last modified by $Author: patrick$ |
---|
| 47 | */ |
---|
| 48 | public class TaskTreeEncoder { |
---|
| 49 | |
---|
| 50 | /** |
---|
| 51 | * |
---|
| 52 | */ |
---|
| 53 | public void encode(List<IUserSession> userSessions) |
---|
| 54 | throws FileNotFoundException |
---|
| 55 | { |
---|
| 56 | PrintStream out = null; |
---|
| 57 | try { |
---|
| 58 | out = new PrintStream(new FileOutputStream("userSessions.txt")); |
---|
| 59 | for (IUserSession session : userSessions) { |
---|
| 60 | encode(session, out); |
---|
| 61 | } |
---|
| 62 | } |
---|
| 63 | finally { |
---|
| 64 | if (out != null) { |
---|
| 65 | out.close(); |
---|
| 66 | } |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * |
---|
| 72 | */ |
---|
| 73 | public void encode(ITaskInstanceList taskInstanceList) |
---|
| 74 | throws FileNotFoundException |
---|
| 75 | { |
---|
| 76 | PrintStream out = null; |
---|
| 77 | try { |
---|
| 78 | out = new PrintStream(new FileOutputStream("taskInstanceList.txt")); |
---|
| 79 | encode(taskInstanceList, out); |
---|
| 80 | } |
---|
| 81 | finally { |
---|
| 82 | if (out != null) { |
---|
| 83 | out.close(); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * |
---|
| 90 | */ |
---|
| 91 | public void encode(ITaskInstanceList taskInstanceList, PrintStream out) { |
---|
| 92 | if (taskInstanceList instanceof IUserSession) { |
---|
| 93 | out.println("UserSession {"); |
---|
| 94 | } |
---|
| 95 | else { |
---|
| 96 | out.println("TaskInstances {"); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | for (ITaskInstance taskInstance : taskInstanceList) { |
---|
| 100 | encode(taskInstance, out, " ", 0); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | out.println('}'); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * |
---|
| 108 | */ |
---|
| 109 | public void encode(ITaskInstance taskInstance, PrintStream out) { |
---|
| 110 | encode(taskInstance, out, "", 0); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * |
---|
| 115 | */ |
---|
| 116 | private void encode(ITaskInstance taskInstance, |
---|
| 117 | PrintStream out, |
---|
| 118 | String indent, |
---|
| 119 | int index) |
---|
| 120 | { |
---|
| 121 | ITask task = taskInstance.getTask(); |
---|
| 122 | |
---|
| 123 | if (task instanceof ITemporalRelationship) { |
---|
| 124 | if (index > 0) { |
---|
| 125 | out.println(); |
---|
| 126 | } |
---|
| 127 | out.print(indent); |
---|
| 128 | |
---|
| 129 | if (task instanceof ISequence) { |
---|
| 130 | out.print("Sequence "); |
---|
| 131 | } |
---|
| 132 | else if (task instanceof ISelection) { |
---|
| 133 | out.print("Selection "); |
---|
| 134 | } |
---|
| 135 | else if (task instanceof IIteration) { |
---|
| 136 | out.print("Iteration "); |
---|
| 137 | } |
---|
| 138 | else if (task instanceof IOptional) { |
---|
| 139 | out.print("Optional "); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | out.print(task.getId()); |
---|
| 143 | out.println(" {"); |
---|
| 144 | } |
---|
| 145 | else if (task instanceof IEventTask) { |
---|
| 146 | out.print(indent); |
---|
[1294] | 147 | out.print(task); |
---|
[1146] | 148 | } |
---|
| 149 | else { |
---|
| 150 | fail("unknown type of task referred by task instance " + taskInstance); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | int i = 0; |
---|
[1294] | 154 | |
---|
| 155 | if (taskInstance instanceof ITaskInstanceList) { |
---|
| 156 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
| 157 | encode(child, out, indent + " ", i++); |
---|
| 158 | } |
---|
[1146] | 159 | } |
---|
[1294] | 160 | else if (taskInstance instanceof ISelectionInstance) { |
---|
| 161 | encode(((ISelectionInstance) taskInstance).getChild(), out, indent + " ", i++); |
---|
| 162 | } |
---|
| 163 | else if (taskInstance instanceof IOptionalInstance) { |
---|
| 164 | encode(((IOptionalInstance) taskInstance).getChild(), out, indent + " ", i++); |
---|
| 165 | } |
---|
[1146] | 166 | |
---|
| 167 | if (task instanceof ITemporalRelationship) { |
---|
| 168 | out.print(indent); |
---|
| 169 | out.print("}"); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | out.println(); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * |
---|
| 177 | */ |
---|
| 178 | public void dumpAsCheckString(IUserSession userSession) { |
---|
| 179 | int[] typeCounters = new int[4]; |
---|
| 180 | Map<ITask, String> taskIds = new HashMap<ITask, String>(); |
---|
| 181 | |
---|
| 182 | for (ITaskInstance taskInstance : userSession) { |
---|
| 183 | dumpTaskInstanceAsCheckString(taskInstance, typeCounters, taskIds, ""); |
---|
| 184 | } |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /** |
---|
| 188 | * |
---|
| 189 | */ |
---|
| 190 | private void dumpTaskInstanceAsCheckString(ITaskInstance taskInstance, |
---|
| 191 | int[] typeCounters, |
---|
| 192 | Map<ITask, String> taskIds, |
---|
| 193 | String indent) |
---|
| 194 | { |
---|
| 195 | ITask task = taskInstance.getTask(); |
---|
| 196 | |
---|
| 197 | System.out.print(" \""); |
---|
| 198 | System.out.print(indent); |
---|
| 199 | |
---|
| 200 | String id = taskIds.get(task); |
---|
| 201 | |
---|
| 202 | if (task instanceof ISequence) { |
---|
| 203 | if (id == null) { |
---|
| 204 | id = "sequence" + typeCounters[0]++; |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | System.out.print("Sequence "); |
---|
| 208 | System.out.print(id); |
---|
| 209 | System.out.println(" {\" +"); |
---|
| 210 | } |
---|
| 211 | else if (task instanceof IIteration) { |
---|
| 212 | if (id == null) { |
---|
| 213 | id = "iteration" + typeCounters[1]++; |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | System.out.print("Iteration "); |
---|
| 217 | System.out.print(id); |
---|
| 218 | System.out.println(" {\" +"); |
---|
| 219 | } |
---|
| 220 | else if (task instanceof ISelection) { |
---|
| 221 | if (id == null) { |
---|
| 222 | id = "selection" + typeCounters[2]++; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | System.out.print("Selection "); |
---|
| 226 | System.out.print(id); |
---|
| 227 | System.out.println(" {\" +"); |
---|
| 228 | } |
---|
| 229 | else if (task instanceof IEventTask) { |
---|
[1294] | 230 | Event event = ((IEventTaskInstance) taskInstance).getEvent(); |
---|
| 231 | if (event.getType() instanceof TextInput) { |
---|
[1146] | 232 | if (id == null) { |
---|
| 233 | id = "textInput" + typeCounters[3]++; |
---|
| 234 | } |
---|
| 235 | |
---|
| 236 | System.out.print("TextInputEvent "); |
---|
| 237 | System.out.print(id); |
---|
| 238 | System.out.print(" \""); |
---|
[1294] | 239 | System.out.print(((TextInput) event.getType()).getEnteredText()); |
---|
[1146] | 240 | System.out.print("\""); |
---|
| 241 | } |
---|
| 242 | else { |
---|
| 243 | if (id == null) { |
---|
| 244 | id = "event" + typeCounters[3]++; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | System.out.print("Event "); |
---|
| 248 | System.out.print(id); |
---|
| 249 | System.out.print(' '); |
---|
[1294] | 250 | System.out.print(event.getType().getName()); |
---|
[1146] | 251 | } |
---|
| 252 | System.out.print(" {}\" +"); |
---|
| 253 | } |
---|
| 254 | else { |
---|
| 255 | fail("unknown type of task referred by task instance " + taskInstance); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | taskIds.put(task, id); |
---|
| 259 | |
---|
[1294] | 260 | if (taskInstance instanceof ITaskInstanceList) { |
---|
| 261 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
| 262 | dumpTaskInstanceAsCheckString(child, typeCounters, taskIds, indent + " "); |
---|
| 263 | } |
---|
[1146] | 264 | } |
---|
[1294] | 265 | else if (taskInstance instanceof ISelectionInstance) { |
---|
| 266 | dumpTaskInstanceAsCheckString |
---|
| 267 | (((ISelectionInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
| 268 | } |
---|
| 269 | else if (taskInstance instanceof IOptionalInstance) { |
---|
| 270 | dumpTaskInstanceAsCheckString |
---|
| 271 | (((IOptionalInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
| 272 | } |
---|
[1146] | 273 | |
---|
| 274 | if (!(task instanceof IEventTask)) { |
---|
| 275 | System.out.print(" \""); |
---|
| 276 | System.out.print(indent); |
---|
| 277 | System.out.print("}\" +"); |
---|
| 278 | } |
---|
| 279 | |
---|
| 280 | System.out.println(); |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | } |
---|