[1852] | 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.temporalrelation.utils; |
---|
| 16 | |
---|
| 17 | import java.io.FileNotFoundException; |
---|
| 18 | import java.io.FileOutputStream; |
---|
| 19 | import java.io.PrintStream; |
---|
[2237] | 20 | import java.io.UnsupportedEncodingException; |
---|
[1852] | 21 | import java.util.HashMap; |
---|
| 22 | import java.util.List; |
---|
| 23 | import java.util.Map; |
---|
| 24 | |
---|
| 25 | import de.ugoe.cs.autoquest.eventcore.Event; |
---|
| 26 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
| 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
| 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IMarkingTemporalRelationship; |
---|
| 31 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional; |
---|
| 32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
| 33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
| 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IStructuringTemporalRelationship; |
---|
| 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
| 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
| 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITemporalRelationship; |
---|
| 41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeifc.TaskPath; |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * TODO comment |
---|
| 46 | * |
---|
| 47 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
| 48 | * @author 2012, last modified by $Author: patrick$ |
---|
| 49 | */ |
---|
| 50 | public class TaskTreeEncoder { |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * |
---|
| 54 | */ |
---|
| 55 | public void encode(List<IUserSession> userSessions) |
---|
| 56 | throws FileNotFoundException |
---|
| 57 | { |
---|
| 58 | PrintStream out = null; |
---|
| 59 | try { |
---|
[2237] | 60 | out = new PrintStream(new FileOutputStream("userSessions.txt"), true, "UTF-8"); |
---|
[1852] | 61 | for (IUserSession session : userSessions) { |
---|
| 62 | encode(session, out); |
---|
| 63 | } |
---|
| 64 | } |
---|
[2237] | 65 | catch (UnsupportedEncodingException e) { |
---|
| 66 | // must not happen as the encoding should be correct |
---|
| 67 | throw new RuntimeException("programming error", e); |
---|
| 68 | } |
---|
[1852] | 69 | finally { |
---|
| 70 | if (out != null) { |
---|
| 71 | out.close(); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * |
---|
| 78 | */ |
---|
| 79 | public void encode(ITaskInstanceList taskInstanceList) |
---|
| 80 | throws FileNotFoundException |
---|
| 81 | { |
---|
| 82 | PrintStream out = null; |
---|
| 83 | try { |
---|
[2237] | 84 | out = new PrintStream(new FileOutputStream("taskInstanceList.txt"), true, "UTF-8"); |
---|
[1852] | 85 | encode(taskInstanceList, out); |
---|
| 86 | } |
---|
[2237] | 87 | catch (UnsupportedEncodingException e) { |
---|
| 88 | // must not happen as the encoding should be correct |
---|
| 89 | throw new RuntimeException("programming error", e); |
---|
| 90 | } |
---|
[1852] | 91 | finally { |
---|
| 92 | if (out != null) { |
---|
| 93 | out.close(); |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * |
---|
| 100 | */ |
---|
| 101 | public void encode(ITask task) |
---|
| 102 | throws FileNotFoundException |
---|
| 103 | { |
---|
| 104 | PrintStream out = null; |
---|
| 105 | try { |
---|
[2237] | 106 | out = new PrintStream(new FileOutputStream("task.txt"), true, "UTF-8"); |
---|
[1852] | 107 | encode(task, out); |
---|
| 108 | } |
---|
[2237] | 109 | catch (UnsupportedEncodingException e) { |
---|
| 110 | // must not happen as the encoding should be correct |
---|
| 111 | throw new RuntimeException("programming error", e); |
---|
| 112 | } |
---|
[1852] | 113 | finally { |
---|
| 114 | if (out != null) { |
---|
| 115 | out.close(); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * |
---|
| 122 | */ |
---|
| 123 | public void encode(ITaskInstanceList taskInstanceList, PrintStream out) { |
---|
| 124 | encode(taskInstanceList, out, null); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /** |
---|
| 128 | * |
---|
| 129 | */ |
---|
| 130 | public void encode(ITaskInstanceList taskInstanceList, |
---|
| 131 | PrintStream out, |
---|
| 132 | List<TaskPath> excludes) |
---|
| 133 | { |
---|
| 134 | TaskPath path = new TaskPath(); |
---|
| 135 | if (taskInstanceList instanceof IUserSession) { |
---|
| 136 | out.println("UserSession {"); |
---|
| 137 | } |
---|
| 138 | else { |
---|
| 139 | out.println("TaskInstances {"); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | if (taskInstanceList instanceof ITaskInstance) { |
---|
| 143 | path.add(((ITaskInstance) taskInstanceList).getTask(), 0); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | int i = 0; |
---|
| 147 | for (ITaskInstance taskInstance : taskInstanceList) { |
---|
| 148 | encode(taskInstance, out, " ", i++, path, excludes); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | out.println('}'); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * |
---|
| 156 | */ |
---|
| 157 | public void encode(ITaskInstance taskInstance, PrintStream out) { |
---|
| 158 | encode(taskInstance, out, null); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * |
---|
| 163 | */ |
---|
| 164 | public void encode(ITaskInstance taskInstance, PrintStream out, List<TaskPath> excludes) { |
---|
| 165 | TaskPath path = new TaskPath(); |
---|
| 166 | encode(taskInstance, out, "", 0, path, excludes); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * |
---|
| 171 | */ |
---|
| 172 | public void encode(ITask task, PrintStream out) { |
---|
| 173 | encode(task, out, "", 0); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /** |
---|
| 177 | * |
---|
| 178 | */ |
---|
| 179 | private void encode(ITaskInstance taskInstance, |
---|
| 180 | PrintStream out, |
---|
| 181 | String indent, |
---|
| 182 | int index, |
---|
| 183 | TaskPath parentPath, |
---|
| 184 | List<TaskPath> excludes) |
---|
| 185 | { |
---|
| 186 | ITask task = taskInstance.getTask(); |
---|
| 187 | TaskPath path = parentPath; |
---|
| 188 | path.add(task, index); |
---|
| 189 | |
---|
| 190 | if (task instanceof ITemporalRelationship) { |
---|
| 191 | if (index > 0) { |
---|
| 192 | out.println(); |
---|
| 193 | } |
---|
| 194 | out.print(indent); |
---|
| 195 | |
---|
| 196 | if (task instanceof ISequence) { |
---|
| 197 | out.print("Sequence "); |
---|
| 198 | } |
---|
| 199 | else if (task instanceof ISelection) { |
---|
| 200 | out.print("Selection "); |
---|
| 201 | } |
---|
| 202 | else if (task instanceof IIteration) { |
---|
| 203 | out.print("Iteration "); |
---|
| 204 | } |
---|
| 205 | else if (task instanceof IOptional) { |
---|
| 206 | out.print("Optional "); |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | out.print(task.getId()); |
---|
| 210 | |
---|
| 211 | if (task.getDescription() != null) { |
---|
| 212 | out.print('('); |
---|
| 213 | out.print(task.getDescription()); |
---|
| 214 | out.print(") "); |
---|
| 215 | } |
---|
| 216 | |
---|
| 217 | out.print('{'); |
---|
| 218 | |
---|
| 219 | if ((excludes == null) || (!excludes.contains(path))) { |
---|
| 220 | out.println(); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | else if (task instanceof IEventTask) { |
---|
| 224 | out.print(indent); |
---|
| 225 | out.print(task); |
---|
| 226 | } |
---|
| 227 | else { |
---|
| 228 | throw new RuntimeException |
---|
| 229 | ("unknown type of task referred by task instance " + taskInstance); |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | if ((excludes == null) || (!excludes.contains(path))) { |
---|
| 233 | int i = 0; |
---|
| 234 | |
---|
| 235 | if (taskInstance instanceof ITaskInstanceList) { |
---|
| 236 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
| 237 | encode(child, out, indent + " ", i++, path, excludes); |
---|
| 238 | } |
---|
| 239 | } |
---|
| 240 | else if (taskInstance instanceof ISelectionInstance) { |
---|
| 241 | encode(((ISelectionInstance) taskInstance).getChild(), out, indent + " ", i++, |
---|
| 242 | path, excludes); |
---|
| 243 | } |
---|
| 244 | else if (taskInstance instanceof IOptionalInstance) { |
---|
| 245 | if (((IOptionalInstance) taskInstance).getChild() != null) { |
---|
| 246 | encode(((IOptionalInstance) taskInstance).getChild(), out, indent + " ", i++, |
---|
| 247 | path, excludes); |
---|
| 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | if (task instanceof ITemporalRelationship) { |
---|
| 252 | out.print(indent); |
---|
| 253 | out.print("}"); |
---|
| 254 | } |
---|
| 255 | } |
---|
| 256 | else { |
---|
| 257 | if (task instanceof ITemporalRelationship) { |
---|
| 258 | out.print(" ... }"); |
---|
| 259 | } |
---|
| 260 | } |
---|
| 261 | |
---|
| 262 | out.println(); |
---|
| 263 | |
---|
| 264 | parentPath.removeLast(); |
---|
| 265 | } |
---|
| 266 | |
---|
| 267 | /** |
---|
| 268 | * |
---|
| 269 | */ |
---|
| 270 | private void encode(ITask task, |
---|
| 271 | PrintStream out, |
---|
| 272 | String indent, |
---|
| 273 | int index) |
---|
| 274 | { |
---|
| 275 | if (task instanceof ITemporalRelationship) { |
---|
| 276 | if (index > 0) { |
---|
| 277 | out.println(); |
---|
| 278 | } |
---|
| 279 | out.print(indent); |
---|
| 280 | |
---|
| 281 | if (task instanceof ISequence) { |
---|
| 282 | out.print("Sequence "); |
---|
| 283 | } |
---|
| 284 | else if (task instanceof ISelection) { |
---|
| 285 | out.print("Selection "); |
---|
| 286 | } |
---|
| 287 | else if (task instanceof IIteration) { |
---|
| 288 | out.print("Iteration "); |
---|
| 289 | } |
---|
| 290 | else if (task instanceof IOptional) { |
---|
| 291 | out.print("Optional "); |
---|
| 292 | } |
---|
| 293 | |
---|
| 294 | out.print(task.getId()); |
---|
| 295 | out.println(" {"); |
---|
| 296 | } |
---|
| 297 | else if (task instanceof IEventTask) { |
---|
| 298 | out.print(indent); |
---|
| 299 | out.print(task); |
---|
| 300 | } |
---|
| 301 | else { |
---|
| 302 | throw new RuntimeException("unknown type of task " + task); |
---|
| 303 | } |
---|
| 304 | |
---|
| 305 | int i = 0; |
---|
| 306 | |
---|
| 307 | if (task instanceof IStructuringTemporalRelationship) { |
---|
| 308 | for (ITask child : ((IStructuringTemporalRelationship) task).getChildren()) { |
---|
| 309 | encode(child, out, indent + " ", i++); |
---|
| 310 | } |
---|
| 311 | } |
---|
| 312 | else if (task instanceof IMarkingTemporalRelationship) { |
---|
| 313 | encode(((IMarkingTemporalRelationship) task).getMarkedTask(), out, indent + " ", i++); |
---|
| 314 | } |
---|
| 315 | |
---|
| 316 | if (task instanceof ITemporalRelationship) { |
---|
| 317 | out.print(indent); |
---|
| 318 | out.print("}"); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | out.println(); |
---|
| 322 | } |
---|
| 323 | |
---|
| 324 | /** |
---|
| 325 | * |
---|
| 326 | */ |
---|
| 327 | public void dumpAsCheckString(IUserSession userSession) { |
---|
| 328 | int[] typeCounters = new int[4]; |
---|
| 329 | Map<ITask, String> taskIds = new HashMap<ITask, String>(); |
---|
| 330 | |
---|
| 331 | for (ITaskInstance taskInstance : userSession) { |
---|
| 332 | dumpTaskInstanceAsCheckString(taskInstance, typeCounters, taskIds, ""); |
---|
| 333 | } |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | /** |
---|
| 337 | * |
---|
| 338 | */ |
---|
| 339 | private void dumpTaskInstanceAsCheckString(ITaskInstance taskInstance, |
---|
| 340 | int[] typeCounters, |
---|
| 341 | Map<ITask, String> taskIds, |
---|
| 342 | String indent) |
---|
| 343 | { |
---|
| 344 | ITask task = taskInstance.getTask(); |
---|
| 345 | |
---|
| 346 | System.out.print(" \""); |
---|
| 347 | System.out.print(indent); |
---|
| 348 | |
---|
| 349 | String id = taskIds.get(task); |
---|
| 350 | |
---|
| 351 | if (task instanceof ISequence) { |
---|
| 352 | if (id == null) { |
---|
| 353 | id = "sequence" + typeCounters[0]++; |
---|
| 354 | } |
---|
| 355 | |
---|
| 356 | System.out.print("Sequence "); |
---|
| 357 | System.out.print(id); |
---|
| 358 | System.out.println(" {\" +"); |
---|
| 359 | } |
---|
| 360 | else if (task instanceof IIteration) { |
---|
| 361 | if (id == null) { |
---|
| 362 | id = "iteration" + typeCounters[1]++; |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | System.out.print("Iteration "); |
---|
| 366 | System.out.print(id); |
---|
| 367 | System.out.println(" {\" +"); |
---|
| 368 | } |
---|
| 369 | else if (task instanceof ISelection) { |
---|
| 370 | if (id == null) { |
---|
| 371 | id = "selection" + typeCounters[2]++; |
---|
| 372 | } |
---|
| 373 | |
---|
| 374 | System.out.print("Selection "); |
---|
| 375 | System.out.print(id); |
---|
| 376 | System.out.println(" {\" +"); |
---|
| 377 | } |
---|
| 378 | else if (task instanceof IEventTask) { |
---|
| 379 | Event event = ((IEventTaskInstance) taskInstance).getEvent(); |
---|
| 380 | if (event.getType() instanceof TextInput) { |
---|
| 381 | if (id == null) { |
---|
| 382 | id = "textInput" + typeCounters[3]++; |
---|
| 383 | } |
---|
| 384 | |
---|
| 385 | System.out.print("TextInputEvent "); |
---|
| 386 | System.out.print(id); |
---|
| 387 | System.out.print(" \""); |
---|
| 388 | System.out.print(((TextInput) event.getType()).getEnteredText()); |
---|
| 389 | System.out.print("\""); |
---|
| 390 | } |
---|
| 391 | else { |
---|
| 392 | if (id == null) { |
---|
| 393 | id = "event" + typeCounters[3]++; |
---|
| 394 | } |
---|
| 395 | |
---|
| 396 | System.out.print("Event "); |
---|
| 397 | System.out.print(id); |
---|
| 398 | System.out.print(' '); |
---|
| 399 | System.out.print(event.getType().getName()); |
---|
| 400 | } |
---|
| 401 | System.out.print(" {}\" +"); |
---|
| 402 | } |
---|
| 403 | else { |
---|
| 404 | throw new RuntimeException("unknown type of task referred by task instance " + taskInstance); |
---|
| 405 | } |
---|
| 406 | |
---|
| 407 | taskIds.put(task, id); |
---|
| 408 | |
---|
| 409 | if (taskInstance instanceof ITaskInstanceList) { |
---|
| 410 | for (ITaskInstance child : (ITaskInstanceList) taskInstance) { |
---|
| 411 | dumpTaskInstanceAsCheckString(child, typeCounters, taskIds, indent + " "); |
---|
| 412 | } |
---|
| 413 | } |
---|
| 414 | else if (taskInstance instanceof ISelectionInstance) { |
---|
| 415 | dumpTaskInstanceAsCheckString |
---|
| 416 | (((ISelectionInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
| 417 | } |
---|
| 418 | else if (taskInstance instanceof IOptionalInstance) { |
---|
| 419 | dumpTaskInstanceAsCheckString |
---|
| 420 | (((IOptionalInstance) taskInstance).getChild(), typeCounters, taskIds, indent + " "); |
---|
| 421 | } |
---|
| 422 | |
---|
| 423 | if (!(task instanceof IEventTask)) { |
---|
| 424 | System.out.print(" \""); |
---|
| 425 | System.out.print(indent); |
---|
| 426 | System.out.print("}\" +"); |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | System.out.println(); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | } |
---|