source: trunk/autoquest-test-utils/src/main/java/de/ugoe/cs/autoquest/tasktrees/TaskTreeInstantiator.java @ 1123

Last change on this file since 1123 was 1123, checked in by pharms, 11 years ago
  • improved testing support for task trees
File size: 5.8 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;
16
17import java.util.HashMap;
18import java.util.Map;
19import java.util.regex.Matcher;
20import java.util.regex.Pattern;
21
22import de.ugoe.cs.autoquest.eventcore.IEventTarget;
23import de.ugoe.cs.autoquest.eventcore.StringEventType;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptional;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
30import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
31import de.ugoe.cs.autoquest.test.DummyGUIElement;
32
33/**
34 * TODO comment
35 *
36 * @version $Revision: $ $Date: 01.04.2012$
37 * @author 2012, last modified by $Author: patrick$
38 */
39public class TaskTreeInstantiator {
40   
41    /** */
42    private static Pattern taskPattern = Pattern.compile("([^{}]+)\\{|\\}");
43   
44    /** */
45    private static Pattern taskDetailsPattern =
46        Pattern.compile("\\s*(\\w*)\\s*([\\w\\(\\)\"]*)\\s*((\\w*)|(\".*\"))?");
47   
48    /** */
49    private ITaskTreeNodeFactory taskTreeNodeFactory;
50   
51    /** */
52    private ITaskTreeBuilder taskTreeBuilder;
53   
54    /** */
55    Map<String, IEventTarget> targets = new HashMap<String, IEventTarget>();
56   
57    /**
58     *
59     */
60    public TaskTreeInstantiator(ITaskTreeNodeFactory taskTreeNodeFactory,
61                                ITaskTreeBuilder taskTreeBuilder)
62    {
63        super();
64        this.taskTreeNodeFactory = taskTreeNodeFactory;
65        this.taskTreeBuilder = taskTreeBuilder;
66    }
67
68    /**
69     *
70     */
71    public ITaskTreeNode instantiateTaskTree(String taskTreeSpec) {
72        ITaskTreeNode task = null;
73
74        Matcher taskMatcher = taskPattern.matcher(taskTreeSpec);
75
76        if (taskMatcher.find()) {
77            task = parseTask(taskMatcher);
78        }
79       
80        if (taskMatcher.find()) {
81            throw new IllegalArgumentException("too many tasks specified");
82        }
83       
84        return task;
85    }
86
87    /**
88     *
89     */
90    private ITaskTreeNode parseTask(Matcher taskMatcher) {
91        if ("}".equals(taskMatcher.group(1))) {
92            throw new IllegalArgumentException("invalid task specification");
93        }
94       
95        String taskDetails = taskMatcher.group(1);
96       
97        Matcher matcher = taskDetailsPattern.matcher(taskDetails);
98       
99        if (!matcher.find()) {
100            throw new IllegalArgumentException("could not parse task details");
101        }
102
103        ITaskTreeNode task;
104       
105        String type = matcher.group(1);
106       
107        String targetId = matcher.group(2);
108        if ((matcher.group(4) != null) && (!"".equals(matcher.group(4).trim()))) {
109            targetId += matcher.group(4).trim();
110        }
111       
112        IEventTarget target = targets.get(targetId);
113       
114        if (target == null) {
115            target = new DummyGUIElement(targetId);
116            targets.put(targetId, target);
117        }
118       
119        if ("Sequence".equals(type)) {
120            task = taskTreeNodeFactory.createNewSequence();
121        }
122        else if ("Selection".equals(type)) {
123            task = taskTreeNodeFactory.createNewSelection();
124        }
125        else if ("Iteration".equals(type)) {
126            task = taskTreeNodeFactory.createNewIteration();
127        }
128        else if ("Optional".equals(type)) {
129            task = taskTreeNodeFactory.createNewOptional();
130        }
131        else {
132            task = taskTreeNodeFactory.createNewEventTask(new StringEventType(type), target);
133        }
134       
135        if ((matcher.group(5) != null) && (!"".equals(matcher.group(5).trim()))) {
136            taskTreeBuilder.setDescription(task, matcher.group(5).trim());
137        }
138
139        while (taskMatcher.find() && !"}".equals(taskMatcher.group(0))) {
140            if (task instanceof ISequence) {
141                taskTreeBuilder.addChild((ISequence) task, parseTask(taskMatcher));
142            }
143            else if (task instanceof ISelection) {
144                taskTreeBuilder.addChild((ISelection) task, parseTask(taskMatcher));
145            }
146            else if (task instanceof IIteration) {
147                if ((task.getChildren() == null) || (task.getChildren().size() == 0)) {
148                    taskTreeBuilder.setChild((IIteration) task, parseTask(taskMatcher));
149                }
150                else {
151                    throw new IllegalArgumentException
152                        ("can not add more than one child to an iteration");
153                }
154            }
155            else if (task instanceof IOptional) {
156                if ((task.getChildren() == null) || (task.getChildren().size() == 0)) {
157                    taskTreeBuilder.setChild((IOptional) task, parseTask(taskMatcher));
158                }
159                else {
160                    throw new IllegalArgumentException
161                        ("can not add more than one child to an optional");
162                }
163            }
164            else {
165                throw new IllegalArgumentException("can not add children to something that is no " +
166                                                   "sequence, selection, or iteration");
167            }
168        }
169
170        return task;
171    }
172
173}
Note: See TracBrowser for help on using the repository browser.