source: trunk/quest-core-tasktrees-test/src/test/java/de/ugoe/cs/quest/tasktrees/testutils/TaskTreeChecker.java @ 735

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