source: trunk/autoquest-core-tasktrees-test/src/test/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/TaskTreeImplTest.java @ 927

Last change on this file since 927 was 927, checked in by sherbold, 12 years ago
  • added copyright under the Apache License, Version 2.0
File size: 16.1 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.treeimpl;
16
17import static org.junit.Assert.assertEquals;
18import static org.junit.Assert.assertNotNull;
19import static org.junit.Assert.assertNull;
20
21import java.util.HashMap;
22import java.util.Map;
23
24import org.junit.Test;
25
26import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree;
31import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
32import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
33import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
34import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeInfo;
35import de.ugoe.cs.autoquest.tasktrees.treeimpl.NodeInfo;
36import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeBuilder;
37import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeNodeFactory;
38import de.ugoe.cs.autoquest.test.DummyGUIElement;
39import de.ugoe.cs.autoquest.test.DummyInteraction;
40
41/**
42 * TODO comment
43 *
44 * @version $Revision: $ $Date: 02.04.2012$
45 * @author 2012, last modified by $Author: patrick$
46 */
47public class TaskTreeImplTest {
48   
49    /** */
50    private static final int MAX_TREE_DEPTH = 15;
51
52    /** */
53    private ITaskTreeBuilder taskTreeBuilder = new TaskTreeBuilder();
54
55    /** */
56    private ITaskTreeNodeFactory taskTreeNodeFactory = new TaskTreeNodeFactory();
57
58    /**
59     * @throws Exception
60     *
61     */
62    @Test
63    public void testRandomTrees() throws Exception {
64        int noOfTrees = 10;
65        int noOfMaxChildren = 8;
66        int maxDepth = MAX_TREE_DEPTH;
67
68        for (int i = 0; i < noOfTrees; i++) {
69            System.err.println("iteration " + (i + 1) + ":");
70            System.err.println("  creating tree");
71            Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos =
72                new HashMap<ITaskTreeNode, ITaskTreeNodeInfo>();
73            ITaskTreeNode rootNode = createTree(noOfMaxChildren, maxDepth, treeInfos);
74            System.err.println("  creating task tree");
75            ITaskTree taskTree = taskTreeNodeFactory.createTaskTree(rootNode);
76
77            System.err.println("  validating task tree");
78            assertEquals(rootNode, taskTree.getRoot());
79            assertMapsEqual(treeInfos, taskTree.getTaskMap());
80        }
81    }
82
83    /**
84     * TODO: comment
85     *
86     * @param treeInfos
87     * @param taskMap
88     */
89    private void assertMapsEqual(Map<ITaskTreeNode, ITaskTreeNodeInfo> map1,
90                                 Map<ITaskTreeNode, ITaskTreeNodeInfo> map2)
91    {
92        try {
93            if (map1 == null) {
94                assertNull(map2);
95                return;
96            }
97
98            assertEquals(map1.size(), map2.size());
99
100            for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : map1.entrySet()) {
101                ITaskTreeNodeInfo value2 = map2.get(entry.getKey());
102                assertNotNull(value2);
103                assertEquals(entry.getValue().getTask(), value2.getTask());
104                assertEquals(entry.getValue().getNoOfOccurencesInTree(),
105                             value2.getNoOfOccurencesInTree());
106            }
107        }
108        catch (AssertionError e) {
109            dumpMap(map1);
110            dumpMap(map2);
111            throw e;
112        }
113    }
114
115    /**
116     * TODO: comment
117     *
118     * @param map2
119     */
120    private void dumpMap(Map<ITaskTreeNode, ITaskTreeNodeInfo> map) {
121        System.err.println();
122
123        if (map == null) {
124            System.err.println("map is null");
125        }
126        else {
127            System.err.println("map:");
128            for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : map.entrySet()) {
129                System.err.print("  ");
130                System.err.print(entry.getKey());
131                for (int i = entry.getKey().toString().length(); i < 49; i++) {
132                    System.err.print(" ");
133                }
134                System.err.print(" : ");
135                System.err.println(entry.getValue());
136            }
137        }
138
139        System.err.println();
140    }
141
142    /**
143     * TODO: comment
144     *
145     * @param noOfMaxChildren
146     * @param maxDepth
147     * @param treeInfos
148     * @return
149     */
150    private ITaskTreeNode createTree(int                                   maxNoOfChildren,
151                                     int                                   maxDepth,
152                                     Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
153        throws Exception
154    {
155        /*
156         * for (int i = 0; i < (MAX_TREE_DEPTH + 1 - maxDepth); i++) { System.err.print("  "); }
157         */
158
159        ITaskTreeNode tree;
160
161        // integrating the maximum depth here assures, that either something between 0 and 8 will
162        // be the type, or if the max depth decreases near 0 only event tasks will be created
163        // to finish the tree creation
164        int type = (int) (Math.random() * (Math.min(8, maxDepth)));
165
166        switch (type)
167        {
168            case 0: {
169                // System.err.print("creating new event task ");
170                tree = createNewEventTask(treeInfos);
171                break;
172            }
173            case 1: {
174                // System.err.print("reusing event task ");
175                tree = reuseEventTask(treeInfos);
176                break;
177            }
178            case 2: {
179                // System.err.println("creating new sequence {");
180                tree = createNewSequence(maxNoOfChildren, maxDepth, treeInfos);
181                break;
182            }
183            case 3: {
184                // System.err.println("reusing sequence {");
185                tree = reuseSequence(maxNoOfChildren, maxDepth, treeInfos);
186                break;
187            }
188            case 4: {
189                // System.err.println("creating new selection {");
190                tree = createNewSelection(maxNoOfChildren, maxDepth, treeInfos);
191                break;
192            }
193            case 5: {
194                // System.err.println("reusing selection {");
195                tree = reuseSelection(maxNoOfChildren, maxDepth, treeInfos);
196                break;
197            }
198            case 6: {
199                // System.err.println("creating new iteration {");
200                tree = createNewIteration(maxNoOfChildren, maxDepth, treeInfos);
201                break;
202            }
203            case 7: {
204                // System.err.println("reusing iteration {");
205                tree = reuseIteration(maxNoOfChildren, maxDepth, treeInfos);
206                break;
207            }
208            default: {
209                // System.err.print("creating new event task per default ");
210                tree = createNewEventTask(treeInfos);
211            }
212        }
213
214        /*
215         * if (!(tree instanceof InteractionTask)) { for (int i = 0; i < (MAX_TREE_DEPTH + 1 -
216         * maxDepth); i++) { System.err.print("  "); }
217         *
218         * System.err.print("} "); }
219         *
220         * System.err.println(tree);
221         */
222
223        return tree;
224    }
225
226    /**
227     * TODO: comment
228     *
229     * @param treeInfos
230     * @return
231     */
232    private IEventTask createNewEventTask(Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
233        throws Exception
234    {
235        Thread.sleep(2);
236        long id = System.currentTimeMillis();
237        IEventTask task =
238            taskTreeNodeFactory.createNewEventTask(new DummyInteraction("interaction" + id, 1),
239                                                   new DummyGUIElement("elem" + id));
240
241        treeInfos.put(task, new NodeInfo(task));
242
243        return task;
244    }
245
246    /**
247     * TODO: comment
248     *
249     * @param treeInfos
250     * @return
251     */
252    private IEventTask reuseEventTask(Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
253        throws Exception
254    {
255        int noOfEventTasks = 0;
256
257        for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
258            if (entry.getKey() instanceof IEventTask) {
259                noOfEventTasks++;
260            }
261        }
262
263        if (noOfEventTasks > 0) {
264            noOfEventTasks = (int) (Math.random() * noOfEventTasks);
265
266            for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
267                if (entry.getKey() instanceof IEventTask) {
268                    if (--noOfEventTasks <= 0) {
269                        return (IEventTask) entry.getKey();
270                    }
271                }
272            }
273        }
274        else {
275            return createNewEventTask(treeInfos);
276        }
277
278        throw new RuntimeException("this is an implementation error");
279    }
280
281    /**
282     * TODO: comment
283     *
284     * @param treeInfos
285     * @return
286     */
287    private ISequence createNewSequence(int                                   maxNoOfChildren,
288                                        int                                   maxDepth,
289                                        Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
290        throws Exception
291    {
292        ISequence sequence = taskTreeNodeFactory.createNewSequence();
293
294        int noOfChildren = (int) (Math.random() * maxNoOfChildren);
295
296        for (int i = 0; i < noOfChildren; i++) {
297            ITaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos);
298
299            // through first removing an existing parent it is assured, that a parent is recorded
300            // only once. This is needed, because parent may be reused in a tree as well, but we
301            // always
302            // iterate the whole tree
303            ((NodeInfo) treeInfos.get(child)).removeParent(sequence);
304            ((NodeInfo) treeInfos.get(child)).addParent(sequence);
305            taskTreeBuilder.addChild(sequence, child);
306        }
307
308        treeInfos.put(sequence, new NodeInfo(sequence));
309        return sequence;
310    }
311
312    /**
313     * TODO: comment
314     *
315     * @param treeInfos
316     * @return
317     */
318    private ISequence reuseSequence(int                                   maxNoOfChildren,
319                                    int                                   maxDepth,
320                                    Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
321        throws Exception
322    {
323        int noOfSequences = 0;
324
325        for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
326            if (entry.getKey() instanceof ISequence) {
327                noOfSequences++;
328            }
329        }
330
331        if (noOfSequences > 0) {
332            noOfSequences = (int) (Math.random() * noOfSequences);
333
334            for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
335                if (entry.getKey() instanceof ISequence) {
336                    if (--noOfSequences <= 0) {
337                        return (ISequence) entry.getKey();
338                    }
339                }
340            }
341        }
342        else {
343            return createNewSequence(maxNoOfChildren, maxDepth, treeInfos);
344        }
345
346        throw new RuntimeException("this is an implementation error");
347    }
348
349    /**
350     * TODO: comment
351     *
352     * @param treeInfos
353     * @return
354     */
355    private ISelection createNewSelection(int                                   maxNoOfChildren,
356                                          int                                   maxDepth,
357                                          Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
358        throws Exception
359    {
360        ISelection selection = taskTreeNodeFactory.createNewSelection();
361
362        int noOfChildren = (int) (Math.random() * maxNoOfChildren);
363
364        for (int i = 0; i < noOfChildren; i++) {
365            ITaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos);
366
367            // through first removing an existing parent it is assured, that a parent is recorded
368            // only once. This is needed, because parent may be reused in a tree as well, but we
369            // always
370            // iterate the whole tree
371            ((NodeInfo) treeInfos.get(child)).removeParent(selection);
372            ((NodeInfo) treeInfos.get(child)).addParent(selection);
373            taskTreeBuilder.addChild(selection, child);
374        }
375
376        treeInfos.put(selection, new NodeInfo(selection));
377        return selection;
378    }
379
380    /**
381     * TODO: comment
382     *
383     * @param treeInfos
384     * @return
385     */
386    private ISelection reuseSelection(int                                   maxNoOfChildren,
387                                      int                                   maxDepth,
388                                      Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
389        throws Exception
390    {
391        int noOfSelections = 0;
392
393        for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
394            if (entry.getKey() instanceof ISelection) {
395                noOfSelections++;
396            }
397        }
398
399        if (noOfSelections > 0) {
400            noOfSelections = (int) (Math.random() * noOfSelections);
401
402            for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
403                if (entry.getKey() instanceof ISelection) {
404                    if (--noOfSelections <= 0) {
405                        return (ISelection) entry.getKey();
406                    }
407                }
408            }
409        }
410        else {
411            return createNewSelection(maxNoOfChildren, maxDepth, treeInfos);
412        }
413
414        throw new RuntimeException("this is an implementation error");
415    }
416
417    /**
418     * TODO: comment
419     *
420     * @param treeInfos
421     * @return
422     */
423    private IIteration createNewIteration(int                                   maxNoOfChildren,
424                                          int                                   maxDepth,
425                                          Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
426        throws Exception
427    {
428        IIteration iteration = taskTreeNodeFactory.createNewIteration();
429
430        ITaskTreeNode child = createTree(maxNoOfChildren, maxDepth - 1, treeInfos);
431
432        // through first removing an existing parent it is assured, that a parent is recorded
433        // only once. This is needed, because parent may be reused in a tree as well, but we always
434        // iterate the whole tree
435        ((NodeInfo) treeInfos.get(child)).removeParent(iteration);
436        ((NodeInfo) treeInfos.get(child)).addParent(iteration);
437        taskTreeBuilder.setChild(iteration, child);
438
439        treeInfos.put(iteration, new NodeInfo(iteration));
440        return iteration;
441    }
442
443    /**
444     * TODO: comment
445     *
446     * @param treeInfos
447     * @return
448     */
449    private IIteration reuseIteration(int                                   maxNoOfChildren,
450                                      int                                   maxDepth,
451                                      Map<ITaskTreeNode, ITaskTreeNodeInfo> treeInfos)
452        throws Exception
453    {
454        int noOfIterations = 0;
455
456        for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
457            if (entry.getKey() instanceof IIteration) {
458                noOfIterations++;
459            }
460        }
461
462        if (noOfIterations > 0) {
463            noOfIterations = (int) (Math.random() * noOfIterations);
464
465            for (Map.Entry<ITaskTreeNode, ITaskTreeNodeInfo> entry : treeInfos.entrySet()) {
466                if (entry.getKey() instanceof IIteration) {
467                    if (--noOfIterations <= 0) {
468                        return (IIteration) entry.getKey();
469                    }
470                }
471            }
472        }
473        else {
474            return createNewIteration(maxNoOfChildren, maxDepth, treeInfos);
475        }
476
477        throw new RuntimeException("this is an implementation error");
478    }
479
480}
Note: See TracBrowser for help on using the repository browser.