[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; |
---|
[1412] | 31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
[1146] | 32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
[1294] | 33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
[1146] | 34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
[1294] | 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
[1146] | 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
[1412] | 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship; |
---|
[1146] | 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITemporalRelationship; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
| 43 | |
---|
| 44 | /** |
---|
[2258] | 45 | * Convenience class to encode user sessions, task instance lists, task instances or tasks into |
---|
| 46 | * a human readable textual representation. |
---|
[1146] | 47 | * |
---|
| 48 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
| 49 | * @author 2012, last modified by $Author: patrick$ |
---|
| 50 | */ |
---|
| 51 | public class TaskTreeEncoder { |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * |
---|
| 55 | */ |
---|
| 56 | public void encode(List<IUserSession> userSessions) |
---|
| 57 | throws FileNotFoundException |
---|
| 58 | { |
---|
| 59 | PrintStream out = null; |
---|
| 60 | try { |
---|
| 61 | out = new PrintStream(new FileOutputStream("userSessions.txt")); |
---|
| 62 | for (IUserSession session : userSessions) { |
---|
| 63 | encode(session, out); |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | finally { |
---|
| 67 | if (out != null) { |
---|
| 68 | out.close(); |
---|
| 69 | } |
---|
| 70 | } |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * |
---|
| 75 | */ |
---|
| 76 | public void encode(ITaskInstanceList taskInstanceList) |
---|
| 77 | throws FileNotFoundException |
---|
| 78 | { |
---|
| 79 | PrintStream out = null; |
---|
| 80 | try { |
---|
| 81 | out = new PrintStream(new FileOutputStream("taskInstanceList.txt")); |
---|
| 82 | encode(taskInstanceList, out); |
---|
| 83 | } |
---|
| 84 | finally { |
---|
| 85 | if (out != null) { |
---|
| 86 | out.close(); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | } |
---|
[1412] | 90 | |
---|
| 91 | /** |
---|
| 92 | * |
---|
| 93 | */ |
---|
| 94 | public void encode(ITask task) |
---|
| 95 | throws FileNotFoundException |
---|
| 96 | { |
---|
| 97 | PrintStream out = null; |
---|
| 98 | try { |
---|
| 99 | out = new PrintStream(new FileOutputStream("task.txt")); |
---|
| 100 | encode(task, out); |
---|
| 101 | } |
---|
| 102 | finally { |
---|
| 103 | if (out != null) { |
---|
| 104 | out.close(); |
---|
| 105 | } |
---|
| 106 | } |
---|
| 107 | } |
---|
[1146] | 108 | |
---|
| 109 | /** |
---|
| 110 | * |
---|
| 111 | */ |
---|
| 112 | public void encode(ITaskInstanceList taskInstanceList, PrintStream out) { |
---|
| 113 | if (taskInstanceList instanceof IUserSession) { |
---|
| 114 | out.println("UserSession {"); |
---|
| 115 | } |
---|
| 116 | else { |
---|
| 117 | out.println("TaskInstances {"); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | for (ITaskInstance taskInstance : taskInstanceList) { |
---|
| 121 | encode(taskInstance, out, " ", 0); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | out.println('}'); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * |
---|
| 129 | */ |
---|
| 130 | public void encode(ITaskInstance taskInstance, PrintStream out) { |
---|
| 131 | encode(taskInstance, out, "", 0); |
---|
| 132 | } |
---|
[1412] | 133 | |
---|
| 134 | /** |
---|
| 135 | * |
---|
| 136 | */ |
---|
| 137 | public void encode(ITask task, PrintStream out) { |
---|
| 138 | encode(task, out, "", 0); |
---|
| 139 | } |
---|
[1146] | 140 | |
---|
| 141 | /** |
---|
| 142 | * |
---|
| 143 | */ |
---|
| 144 | private void encode(ITaskInstance taskInstance, |
---|
| 145 | PrintStream out, |
---|
| 146 | String indent, |
---|
| 147 | int index) |
---|
| 148 | { |
---|
| 149 | ITask task = taskInstance.getTask(); |
---|
| 150 | |
---|
| 151 | if (task instanceof ITemporalRelationship) { |
---|
| 152 | if (index > 0) { |
---|
| 153 | out.println(); |
---|
| 154 | } |
---|
| 155 | out.print(indent); |
---|
| 156 | |
---|
| 157 | if (task instanceof ISequence) { |
---|
| 158 | out.print("Sequence "); |
---|
| 159 | } |
---|
| 160 | else if (task instanceof ISelection) { |
---|
| 161 | out.print("Selection "); |
---|
| 162 | } |
---|
| 163 | else if (task instanceof IIteration) { |
---|
| 164 | out.print("Iteration "); |
---|
| 165 | } |
---|
| 166 | else if (task instanceof IOptional) { |
---|
| 167 | out.print("Optional "); |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | out.print(task.getId()); |
---|
| 171 | out.println(" {"); |
---|
| 172 | } |
---|
| 173 | else if (task instanceof IEventTask) { |
---|
| 174 | out.print(indent); |
---|
[1294] | 175 | out.print(task); |
---|
[1146] | 176 | } |
---|
| 177 | else { |
---|
| 178 | fail("unknown type of task referred by task instance " + taskInstance); |
---|
| 179 | } |
---|
| 180 | |
---|
| 181 | int i = 0; |
---|
[1294] | 182 | |
---|
| 183 | if (taskInstance instanceof ITaskInstanceList) { |
---|
| 184 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
| 185 | encode(child, out, indent + " ", i++); |
---|
| 186 | } |
---|
[1146] | 187 | } |
---|
[1294] | 188 | else if (taskInstance instanceof ISelectionInstance) { |
---|
| 189 | encode(((ISelectionInstance) taskInstance).getChild(), out, indent + " ", i++); |
---|
| 190 | } |
---|
| 191 | else if (taskInstance instanceof IOptionalInstance) { |
---|
[1638] | 192 | if (((IOptionalInstance) taskInstance).getChild() != null) { |
---|
| 193 | encode(((IOptionalInstance) taskInstance).getChild(), out, indent + " ", i++); |
---|
| 194 | } |
---|
[1294] | 195 | } |
---|
[1146] | 196 | |
---|
| 197 | if (task instanceof ITemporalRelationship) { |
---|
| 198 | out.print(indent); |
---|
| 199 | out.print("}"); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | out.println(); |
---|
| 203 | } |
---|
| 204 | |
---|
| 205 | /** |
---|
| 206 | * |
---|
| 207 | */ |
---|
[1412] | 208 | private void encode(ITask task, |
---|
| 209 | PrintStream out, |
---|
| 210 | String indent, |
---|
| 211 | int index) |
---|
| 212 | { |
---|
| 213 | if (task instanceof ITemporalRelationship) { |
---|
| 214 | if (index > 0) { |
---|
| 215 | out.println(); |
---|
| 216 | } |
---|
| 217 | out.print(indent); |
---|
| 218 | |
---|
| 219 | if (task instanceof ISequence) { |
---|
| 220 | out.print("Sequence "); |
---|
| 221 | } |
---|
| 222 | else if (task instanceof ISelection) { |
---|
| 223 | out.print("Selection "); |
---|
| 224 | } |
---|
| 225 | else if (task instanceof IIteration) { |
---|
| 226 | out.print("Iteration "); |
---|
| 227 | } |
---|
| 228 | else if (task instanceof IOptional) { |
---|
| 229 | out.print("Optional "); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | out.print(task.getId()); |
---|
| 233 | out.println(" {"); |
---|
| 234 | } |
---|
| 235 | else if (task instanceof IEventTask) { |
---|
| 236 | out.print(indent); |
---|
| 237 | out.print(task); |
---|
| 238 | } |
---|
| 239 | else { |
---|
| 240 | fail("unknown type of task " + task); |
---|
| 241 | } |
---|
| 242 | |
---|
| 243 | int i = 0; |
---|
| 244 | |
---|
| 245 | if (task instanceof IStructuringTemporalRelationship) { |
---|
| 246 | for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
| 247 | encode(child, out, indent + " ", i++); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
| 251 | encode(((IMarkingTemporalRelationship) task).getMarkedTask(), out, indent + " ", i++); |
---|
| 252 | } |
---|
| 253 | |
---|
| 254 | if (task instanceof ITemporalRelationship) { |
---|
| 255 | out.print(indent); |
---|
| 256 | out.print("}"); |
---|
| 257 | } |
---|
| 258 | |
---|
| 259 | out.println(); |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | /** |
---|
| 263 | * |
---|
| 264 | */ |
---|
[1146] | 265 | public void dumpAsCheckString(IUserSession userSession) { |
---|
| 266 | int[] typeCounters = new int[4]; |
---|
| 267 | Map<ITask, String> taskIds = new HashMap<ITask, String>(); |
---|
| 268 | |
---|
| 269 | for (ITaskInstance taskInstance : userSession) { |
---|
| 270 | dumpTaskInstanceAsCheckString(taskInstance, typeCounters, taskIds, ""); |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /** |
---|
| 275 | * |
---|
| 276 | */ |
---|
| 277 | private void dumpTaskInstanceAsCheckString(ITaskInstance taskInstance, |
---|
| 278 | int[] typeCounters, |
---|
| 279 | Map<ITask, String> taskIds, |
---|
| 280 | String indent) |
---|
| 281 | { |
---|
| 282 | ITask task = taskInstance.getTask(); |
---|
| 283 | |
---|
| 284 | System.out.print(" \""); |
---|
| 285 | System.out.print(indent); |
---|
| 286 | |
---|
| 287 | String id = taskIds.get(task); |
---|
| 288 | |
---|
| 289 | if (task instanceof ISequence) { |
---|
| 290 | if (id == null) { |
---|
| 291 | id = "sequence" + typeCounters[0]++; |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | System.out.print("Sequence "); |
---|
| 295 | System.out.print(id); |
---|
| 296 | System.out.println(" {\" +"); |
---|
| 297 | } |
---|
| 298 | else if (task instanceof IIteration) { |
---|
| 299 | if (id == null) { |
---|
| 300 | id = "iteration" + typeCounters[1]++; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | System.out.print("Iteration "); |
---|
| 304 | System.out.print(id); |
---|
| 305 | System.out.println(" {\" +"); |
---|
| 306 | } |
---|
| 307 | else if (task instanceof ISelection) { |
---|
| 308 | if (id == null) { |
---|
| 309 | id = "selection" + typeCounters[2]++; |
---|
| 310 | } |
---|
| 311 | |
---|
| 312 | System.out.print("Selection "); |
---|
| 313 | System.out.print(id); |
---|
| 314 | System.out.println(" {\" +"); |
---|
| 315 | } |
---|
| 316 | else if (task instanceof IEventTask) { |
---|
[1294] | 317 | Event event = ((IEventTaskInstance) taskInstance).getEvent(); |
---|
| 318 | if (event.getType() instanceof TextInput) { |
---|
[1146] | 319 | if (id == null) { |
---|
| 320 | id = "textInput" + typeCounters[3]++; |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | System.out.print("TextInputEvent "); |
---|
| 324 | System.out.print(id); |
---|
| 325 | System.out.print(" \""); |
---|
[1294] | 326 | System.out.print(((TextInput) event.getType()).getEnteredText()); |
---|
[1146] | 327 | System.out.print("\""); |
---|
| 328 | } |
---|
| 329 | else { |
---|
| 330 | if (id == null) { |
---|
| 331 | id = "event" + typeCounters[3]++; |
---|
| 332 | } |
---|
| 333 | |
---|
| 334 | System.out.print("Event "); |
---|
| 335 | System.out.print(id); |
---|
| 336 | System.out.print(' '); |
---|
[1294] | 337 | System.out.print(event.getType().getName()); |
---|
[1146] | 338 | } |
---|
| 339 | System.out.print(" {}\" +"); |
---|
| 340 | } |
---|
| 341 | else { |
---|
| 342 | fail("unknown type of task referred by task instance " + taskInstance); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | taskIds.put(task, id); |
---|
| 346 | |
---|
[1294] | 347 | if (taskInstance instanceof ITaskInstanceList) { |
---|
| 348 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
| 349 | dumpTaskInstanceAsCheckString(child, typeCounters, taskIds, indent + " "); |
---|
| 350 | } |
---|
[1146] | 351 | } |
---|
[1294] | 352 | else if (taskInstance instanceof ISelectionInstance) { |
---|
| 353 | dumpTaskInstanceAsCheckString |
---|
| 354 | (((ISelectionInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
| 355 | } |
---|
| 356 | else if (taskInstance instanceof IOptionalInstance) { |
---|
| 357 | dumpTaskInstanceAsCheckString |
---|
| 358 | (((IOptionalInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
| 359 | } |
---|
[1146] | 360 | |
---|
| 361 | if (!(task instanceof IEventTask)) { |
---|
| 362 | System.out.print(" \""); |
---|
| 363 | System.out.print(indent); |
---|
| 364 | System.out.print("}\" +"); |
---|
| 365 | } |
---|
| 366 | |
---|
| 367 | System.out.println(); |
---|
| 368 | } |
---|
| 369 | |
---|
| 370 | } |
---|