[927] | 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 | |
---|
[922] | 15 | package de.ugoe.cs.autoquest.tasktrees; |
---|
[790] | 16 | |
---|
| 17 | import static org.junit.Assert.assertTrue; |
---|
| 18 | import static org.junit.Assert.fail; |
---|
| 19 | |
---|
| 20 | import java.io.FileNotFoundException; |
---|
| 21 | import java.io.FileOutputStream; |
---|
| 22 | import java.io.PrintWriter; |
---|
| 23 | import java.util.ArrayList; |
---|
| 24 | import java.util.HashMap; |
---|
| 25 | import java.util.Iterator; |
---|
| 26 | import java.util.List; |
---|
| 27 | import java.util.Map; |
---|
| 28 | import java.util.regex.Matcher; |
---|
| 29 | import java.util.regex.Pattern; |
---|
| 30 | |
---|
[922] | 31 | import de.ugoe.cs.autoquest.eventcore.gui.TextInput; |
---|
| 32 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 33 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
| 34 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
| 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree; |
---|
| 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeInfo; |
---|
[790] | 39 | |
---|
| 40 | /** |
---|
| 41 | * TODO comment |
---|
| 42 | * |
---|
| 43 | * @version $Revision: $ $Date: 01.04.2012$ |
---|
| 44 | * @author 2012, last modified by $Author: patrick$ |
---|
| 45 | */ |
---|
| 46 | public class TaskTreeChecker { |
---|
| 47 | |
---|
| 48 | /** */ |
---|
| 49 | private static Pattern taskPattern = Pattern.compile("([^{}]+)\\{|\\}"); |
---|
| 50 | |
---|
| 51 | /** */ |
---|
| 52 | private static Pattern taskDetailsPattern = |
---|
[977] | 53 | Pattern.compile("\\s*(\\w*)\\s*([\\w\\(\\)\"]*)\\s*((\\w*)|(\".*\"))?"); |
---|
[790] | 54 | |
---|
| 55 | /** */ |
---|
| 56 | private boolean doTrace; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * TODO: comment |
---|
| 60 | * |
---|
| 61 | */ |
---|
| 62 | public TaskTreeChecker() { |
---|
| 63 | this(false); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * TODO: comment |
---|
| 68 | * |
---|
| 69 | */ |
---|
| 70 | public TaskTreeChecker(boolean doTrace) { |
---|
| 71 | this.doTrace = doTrace; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * |
---|
| 76 | */ |
---|
| 77 | public void assertTaskTree(String taskTreeSpec, ITaskTree taskTree) { |
---|
| 78 | Map<ITaskTreeNode, Integer> taskMapCopy = new HashMap<ITaskTreeNode, Integer>(); |
---|
| 79 | |
---|
| 80 | for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : taskTree.getTaskMap().entrySet()) { |
---|
| 81 | if (entry.getValue().getNoOfOccurencesInTree() > 0) { |
---|
| 82 | taskMapCopy.put(entry.getKey(), entry.getValue().getNoOfOccurencesInTree()); |
---|
| 83 | } |
---|
| 84 | else { |
---|
| 85 | taskMapCopy.put(entry.getKey(), 1); |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | if (doTrace) { |
---|
| 90 | dumpTaskMap(taskMapCopy); |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | TaskSpec task = null; |
---|
| 94 | |
---|
| 95 | Matcher taskMatcher = taskPattern.matcher(taskTreeSpec); |
---|
| 96 | |
---|
| 97 | while (taskMatcher.find()) { |
---|
| 98 | |
---|
| 99 | task = parseTask(taskMatcher); |
---|
| 100 | |
---|
| 101 | if (task != null) { |
---|
| 102 | assertTaskAndChildrenInMapAndRemove(task, taskMapCopy); |
---|
| 103 | } |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | assertTrue("more tasks in map, than expected", taskMapCopy.isEmpty()); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /** |
---|
| 110 | * TODO: comment |
---|
| 111 | * |
---|
| 112 | * @param taskTree |
---|
| 113 | */ |
---|
| 114 | public void dumpAsCheckString(ITaskTree taskTree) { |
---|
| 115 | dumpNodeAsCheckString(taskTree.getRoot(), new int[4], ""); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * TODO: comment |
---|
| 120 | * |
---|
| 121 | * @param root |
---|
| 122 | * @param string |
---|
| 123 | */ |
---|
| 124 | private void dumpNodeAsCheckString(ITaskTreeNode node, int[] typeCounters, String indent) { |
---|
| 125 | System.out.print(" \""); |
---|
| 126 | System.out.print(indent); |
---|
| 127 | |
---|
| 128 | if (node instanceof ISequence) { |
---|
| 129 | System.out.print("Sequence sequence"); |
---|
| 130 | System.out.print(typeCounters[0]++); |
---|
| 131 | System.out.println(" {\" +"); |
---|
| 132 | } |
---|
| 133 | else if (node instanceof IIteration) { |
---|
| 134 | System.out.print("Iteration iteration"); |
---|
| 135 | System.out.print(typeCounters[1]++); |
---|
| 136 | System.out.println(" {\" +"); |
---|
| 137 | } |
---|
| 138 | else if (node instanceof ISelection) { |
---|
| 139 | System.out.print("Selection selection"); |
---|
| 140 | System.out.print(typeCounters[2]++); |
---|
| 141 | System.out.println(" {\" +"); |
---|
| 142 | } |
---|
| 143 | else if (node instanceof IEventTask) { |
---|
| 144 | if (((IEventTask) node).getEventType() instanceof TextInput) { |
---|
| 145 | System.out.print("TextInputEvent textInput"); |
---|
| 146 | System.out.print(typeCounters[3]++); |
---|
| 147 | System.out.print(" \""); |
---|
| 148 | System.out.print(((TextInput) ((IEventTask) node).getEventType()).getEnteredText()); |
---|
| 149 | System.out.print("\""); |
---|
| 150 | } |
---|
| 151 | else { |
---|
| 152 | System.out.print("Event "); |
---|
| 153 | System.out.print(((IEventTask) node).getEventType().getName()); |
---|
| 154 | } |
---|
| 155 | System.out.print(" {}\" +"); |
---|
| 156 | } |
---|
| 157 | else { |
---|
| 158 | fail("unknown type of node in task tree " + node); |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | for (ITaskTreeNode child : node.getChildren()) { |
---|
| 162 | dumpNodeAsCheckString(child, typeCounters, indent + " "); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | if (!(node instanceof IEventTask)) { |
---|
| 166 | System.out.print(" \""); |
---|
| 167 | System.out.print(indent); |
---|
| 168 | System.out.print("}\" +"); |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | System.out.println(); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * TODO: comment |
---|
| 176 | * |
---|
| 177 | * @param taskTree |
---|
| 178 | */ |
---|
| 179 | public void dumpFullTaskTree(ITaskTree taskTree) throws FileNotFoundException { |
---|
| 180 | PrintWriter out = null; |
---|
| 181 | try { |
---|
| 182 | out = new PrintWriter(new FileOutputStream("taskTree.txt")); |
---|
| 183 | dumpFullNode(taskTree.getRoot(), out, "", 0); |
---|
| 184 | } |
---|
| 185 | finally { |
---|
| 186 | if (out != null) { |
---|
| 187 | out.close(); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * |
---|
| 195 | */ |
---|
| 196 | private void dumpFullNode(ITaskTreeNode node, PrintWriter out, String indent, int index) { |
---|
| 197 | if (node instanceof ISequence) { |
---|
| 198 | if (index > 0) { |
---|
| 199 | out.println(); |
---|
| 200 | } |
---|
| 201 | out.print(indent); |
---|
[991] | 202 | out.print("Sequence"); |
---|
| 203 | if (node.getDescription() != null) { |
---|
| 204 | out.print("("); |
---|
| 205 | out.print(node.getDescription()); |
---|
| 206 | out.print(")"); |
---|
| 207 | } |
---|
| 208 | out.println(" {"); |
---|
[790] | 209 | } |
---|
| 210 | else if (node instanceof IIteration) { |
---|
| 211 | if (index > 0) { |
---|
| 212 | out.println(); |
---|
| 213 | } |
---|
| 214 | out.print(indent); |
---|
[991] | 215 | out.print("Iteration"); |
---|
| 216 | if (node.getDescription() != null) { |
---|
| 217 | out.print("("); |
---|
| 218 | out.print(node.getDescription()); |
---|
| 219 | out.print(")"); |
---|
| 220 | } |
---|
| 221 | out.println(" {"); |
---|
[790] | 222 | } |
---|
| 223 | else if (node instanceof ISelection) { |
---|
| 224 | if (index > 0) { |
---|
| 225 | out.println(); |
---|
| 226 | } |
---|
| 227 | out.print(indent); |
---|
[991] | 228 | out.print("Selection"); |
---|
| 229 | if (node.getDescription() != null) { |
---|
| 230 | out.print("("); |
---|
| 231 | out.print(node.getDescription()); |
---|
| 232 | out.print(")"); |
---|
| 233 | } |
---|
| 234 | out.println(" {"); |
---|
[790] | 235 | } |
---|
| 236 | else if (node instanceof IEventTask) { |
---|
| 237 | out.print(indent); |
---|
| 238 | out.print(((IEventTask) node).getEventType().getName()); |
---|
| 239 | out.print(" "); |
---|
[977] | 240 | out.print(((IEventTask) node).getEventTarget().getStringIdentifier()); |
---|
[790] | 241 | // if (((IEventTask) node).getEventTarget() instanceof IGUIElement) { |
---|
| 242 | // out.print(" "); |
---|
| 243 | // out.print(((IGUIElement) ((IEventTask) node).getEventTarget()).getSpecification()); |
---|
| 244 | // } |
---|
| 245 | } |
---|
| 246 | else { |
---|
| 247 | fail("unknown type of node in task tree " + node); |
---|
| 248 | } |
---|
| 249 | |
---|
| 250 | int i = 0; |
---|
| 251 | for (ITaskTreeNode child : node.getChildren()) { |
---|
| 252 | dumpFullNode(child, out, indent + " ", i++); |
---|
| 253 | } |
---|
| 254 | |
---|
| 255 | if (!(node instanceof IEventTask)) { |
---|
| 256 | out.print(indent); |
---|
| 257 | out.print("}"); |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | out.println(); |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | * |
---|
| 265 | */ |
---|
| 266 | private TaskSpec parseTask(Matcher taskMatcher) { |
---|
| 267 | if ("}".equals(taskMatcher.group(1))) { |
---|
| 268 | throw new IllegalArgumentException("invalid task specification"); |
---|
| 269 | } |
---|
| 270 | |
---|
| 271 | String taskDetails = taskMatcher.group(1); |
---|
| 272 | |
---|
| 273 | Matcher matcher = taskDetailsPattern.matcher(taskDetails); |
---|
| 274 | |
---|
| 275 | if (!matcher.find()) { |
---|
| 276 | throw new IllegalArgumentException("could not parse task details"); |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | TaskSpec task = new TaskSpec(); |
---|
| 280 | task.type = matcher.group(1); |
---|
| 281 | |
---|
| 282 | task.name = matcher.group(2); |
---|
| 283 | if ((matcher.group(4) != null) && (!"".equals(matcher.group(4).trim()))) { |
---|
| 284 | task.name += " " + matcher.group(4).trim(); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | if ((matcher.group(5) != null) && (!"".equals(matcher.group(5).trim()))) { |
---|
| 288 | task.additionalInfo = matcher.group(5).trim(); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | if ("".equals(task.name)) { |
---|
| 292 | task.name = null; |
---|
| 293 | } |
---|
| 294 | |
---|
| 295 | List<TaskSpec> children = new ArrayList<TaskSpec>(); |
---|
| 296 | while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) { |
---|
| 297 | children.add(parseTask(taskMatcher)); |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | if (children.size() > 0) { |
---|
| 301 | task.children = children.toArray(new TaskSpec[children.size()]); |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | return task; |
---|
| 305 | } |
---|
| 306 | |
---|
| 307 | /** |
---|
| 308 | * @param task |
---|
| 309 | * @param taskMapCopy |
---|
| 310 | */ |
---|
| 311 | private void assertTaskAndChildrenInMapAndRemove(TaskSpec task, |
---|
| 312 | Map<ITaskTreeNode, Integer> taskMap) |
---|
| 313 | { |
---|
| 314 | for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) { |
---|
| 315 | if (taskSpecEqualsTask(task, entry.getKey())) { |
---|
| 316 | if (task.children != null) { |
---|
| 317 | for (TaskSpec child : task.children) { |
---|
| 318 | assertTaskAndChildrenInMapAndRemove(child, taskMap); |
---|
| 319 | } |
---|
| 320 | } |
---|
| 321 | |
---|
| 322 | int count = taskMap.get(entry.getKey()); |
---|
| 323 | if (count == 1) { |
---|
| 324 | taskMap.remove(entry.getKey()); |
---|
| 325 | } |
---|
| 326 | else { |
---|
| 327 | taskMap.put(entry.getKey(), count - 1); |
---|
| 328 | } |
---|
| 329 | return; |
---|
| 330 | } |
---|
| 331 | } |
---|
| 332 | |
---|
[1111] | 333 | fail("expected task " + task.type + " " + task.name + " not included in task map"); |
---|
[790] | 334 | } |
---|
| 335 | |
---|
| 336 | /** |
---|
| 337 | * |
---|
| 338 | */ |
---|
| 339 | private boolean taskSpecEqualsTask(TaskSpec taskSpec, ITaskTreeNode task) { |
---|
| 340 | if (doTrace) { |
---|
| 341 | System.err.println("comparing " + taskSpec.name + " with"); |
---|
| 342 | dumpTask(task, 0, ""); |
---|
| 343 | } |
---|
| 344 | |
---|
| 345 | if (("Event".equals(taskSpec.type) && (!(task instanceof IEventTask))) || |
---|
| 346 | ("TextInputEvent".equals(taskSpec.type) && |
---|
| 347 | ((!(task instanceof IEventTask)) || |
---|
| 348 | (!(((IEventTask) task).getEventType() instanceof TextInput)))) || |
---|
| 349 | ("Sequence".equals(taskSpec.type) && (!(task instanceof ISequence))) || |
---|
| 350 | ("Selection".equals(taskSpec.type) && (!(task instanceof ISelection))) || |
---|
| 351 | ("Iteration".equals(taskSpec.type) && (!(task instanceof IIteration)))) |
---|
| 352 | { |
---|
| 353 | if (doTrace) { |
---|
| 354 | System.err.println("task types do not match: " + taskSpec.type + " != " + |
---|
| 355 | task.getClass().getSimpleName() + "\n"); |
---|
| 356 | } |
---|
| 357 | return false; |
---|
| 358 | } |
---|
| 359 | else if (!"Event".equals(taskSpec.type) && |
---|
| 360 | !"TextInputEvent".equals(taskSpec.type) && |
---|
| 361 | !"Sequence".equals(taskSpec.type) && |
---|
| 362 | !"Selection".equals(taskSpec.type) && |
---|
| 363 | !"Iteration".equals(taskSpec.type)) |
---|
| 364 | { |
---|
| 365 | fail("unknown task type " + taskSpec.type + " --> please extend test case"); |
---|
| 366 | } |
---|
| 367 | |
---|
| 368 | if ("TextInputEvent".equals(taskSpec.type)) { |
---|
| 369 | TextInput eventType = (TextInput) ((IEventTask) task).getEventType(); |
---|
| 370 | if ((taskSpec.additionalInfo != null) && |
---|
| 371 | !"".equals(taskSpec.additionalInfo) && |
---|
| 372 | !(taskSpec.additionalInfo.equals(eventType.getEnteredText()))) |
---|
| 373 | { |
---|
| 374 | if (doTrace) { |
---|
| 375 | System.err.println("expected text \"" + taskSpec.additionalInfo + |
---|
| 376 | "\" is not equal to the text " + "provided by the task \"" + |
---|
| 377 | eventType.getEnteredText() + "\"\n"); |
---|
| 378 | } |
---|
| 379 | return false; |
---|
| 380 | } |
---|
| 381 | } |
---|
| 382 | else if ((task instanceof IEventTask) && (((IEventTask) task).getEventType() != null) && |
---|
| 383 | (!taskSpec.name.equals(((IEventTask) task).getEventType().getName()))) |
---|
| 384 | { |
---|
| 385 | // simple event names do not match. But what about the event name in |
---|
| 386 | // combination with the additional info |
---|
| 387 | String complexName = |
---|
| 388 | taskSpec.name + |
---|
| 389 | (!"".equals(taskSpec.additionalInfo) ? " " + taskSpec.additionalInfo : ""); |
---|
| 390 | |
---|
| 391 | if (!complexName.equals(((IEventTask) task).getEventType().getName())) { |
---|
| 392 | if (doTrace) { |
---|
| 393 | System.err.println("event names do not match: " + taskSpec.name + " != " + |
---|
| 394 | ((IEventTask) task).getEventType().getName() + "\n"); |
---|
| 395 | } |
---|
| 396 | return false; |
---|
| 397 | } |
---|
| 398 | } |
---|
| 399 | |
---|
| 400 | if (((taskSpec.children == null) && (task.getChildren().size() > 0)) || |
---|
| 401 | ((taskSpec.children != null) && (taskSpec.children.length != task.getChildren().size()))) |
---|
| 402 | { |
---|
| 403 | if (doTrace) { |
---|
| 404 | System.err.println |
---|
| 405 | ("numbers of children do not match: " + |
---|
| 406 | (taskSpec.children == null ? "0" : taskSpec.children.length) + " != " + |
---|
| 407 | (task.getChildren() == null ? "0" : task.getChildren().size()) + "\n"); |
---|
| 408 | } |
---|
| 409 | return false; |
---|
| 410 | } |
---|
| 411 | |
---|
| 412 | Iterator<ITaskTreeNode> children = task.getChildren().iterator(); |
---|
| 413 | if (taskSpec.children != null) { |
---|
| 414 | for (TaskSpec child : taskSpec.children) { |
---|
| 415 | if (!taskSpecEqualsTask(child, children.next())) { |
---|
| 416 | if (doTrace) { |
---|
| 417 | System.err.println("one of the children does not match\n"); |
---|
| 418 | } |
---|
| 419 | return false; |
---|
| 420 | } |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | if (!children.hasNext()) { |
---|
| 425 | if (doTrace) { |
---|
| 426 | System.err.println("nodes match\n"); |
---|
| 427 | } |
---|
| 428 | return true; |
---|
| 429 | } |
---|
| 430 | else { |
---|
| 431 | if (doTrace) { |
---|
| 432 | System.err.println("number of children does not match\n"); |
---|
| 433 | } |
---|
| 434 | return false; |
---|
| 435 | } |
---|
| 436 | } |
---|
| 437 | |
---|
| 438 | /** |
---|
| 439 | * |
---|
| 440 | */ |
---|
| 441 | private void dumpTaskMap(Map<ITaskTreeNode, Integer> taskMap) { |
---|
| 442 | System.err.println(); |
---|
| 443 | for (Map.Entry<ITaskTreeNode, Integer> entry : taskMap.entrySet()) { |
---|
| 444 | dumpTask(entry.getKey(), entry.getValue(), ""); |
---|
| 445 | System.err.println(); |
---|
| 446 | } |
---|
| 447 | } |
---|
| 448 | |
---|
| 449 | /** |
---|
| 450 | * |
---|
| 451 | */ |
---|
| 452 | private void dumpTask(ITaskTreeNode task, int count, String indent) { |
---|
| 453 | System.err.print(indent); |
---|
| 454 | System.err.print(task); |
---|
| 455 | System.err.print(" "); |
---|
| 456 | System.err.print(task.getDescription()); |
---|
| 457 | System.err.print(" "); |
---|
| 458 | |
---|
| 459 | if (count > 0) { |
---|
| 460 | System.err.print("("); |
---|
| 461 | System.err.print(count); |
---|
| 462 | System.err.print(" occurrences)"); |
---|
| 463 | } |
---|
| 464 | |
---|
| 465 | System.err.println(); |
---|
| 466 | |
---|
| 467 | if ((task.getChildren() != null) && (task.getChildren().size() > 0)) { |
---|
| 468 | for (ITaskTreeNode child : task.getChildren()) { |
---|
| 469 | dumpTask(child, 0, indent + " "); |
---|
| 470 | } |
---|
| 471 | } |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | /** |
---|
| 475 | * TODO comment |
---|
| 476 | * |
---|
| 477 | * @version $Revision: $ $Date: $ |
---|
| 478 | * @author 2011, last modified by $Author: $ |
---|
| 479 | */ |
---|
| 480 | private class TaskSpec { |
---|
| 481 | public String type; |
---|
| 482 | public String name; |
---|
| 483 | public String additionalInfo; |
---|
| 484 | public TaskSpec[] children; |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | } |
---|