source: trunk/autoquest-core-tasktrees-test/src/test/java/de/ugoe/cs/autoquest/tasktrees/temporalrelation/AbstractTemporalRelationshipTC.java @ 1735

Last change on this file since 1735 was 1294, checked in by pharms, 11 years ago
  • rework of task model to move event instance stuff to task instances
  • introduction of sequence, selection, iteration and optional instances
File size: 8.7 KB
RevLine 
[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]15package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
[445]16
[1106]17import static org.junit.Assert.fail;
18import static org.junit.Assert.assertTrue;
19import static org.junit.Assert.assertNotNull;
20
21import java.lang.reflect.Constructor;
22import java.lang.reflect.InvocationTargetException;
23import java.util.LinkedList;
[445]24import java.util.List;
[1106]25import java.util.Stack;
[725]26import java.util.logging.Level;
[445]27
28import org.junit.Before;
29
[1146]30import de.ugoe.cs.autoquest.tasktrees.TaskTreeChecker;
31import de.ugoe.cs.autoquest.tasktrees.TaskTreeDecoder;
32import de.ugoe.cs.autoquest.tasktrees.TaskTreeEncoder;
33import de.ugoe.cs.autoquest.tasktrees.TaskTreeValidator;
34import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality;
[1294]35import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
[1146]36import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
37import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
38import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
39import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
40import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskBuilder;
41import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskFactory;
[1106]42import de.ugoe.cs.util.console.Console;
[725]43import de.ugoe.cs.util.console.TextConsole;
[445]44
45/**
46 * @version $Revision: $ $Date: 28.04.2012$
47 * @author 2012, last modified by $Author: patrick$
48 */
[557]49public class AbstractTemporalRelationshipTC {
[445]50
[557]51    /** */
[1146]52    private ITaskBuilder taskBuilder = new TaskBuilder();
[445]53
[557]54    /** */
[1146]55    private ITaskFactory taskFactory = new TaskFactory();
56   
[557]57    /** */
[1146]58    private TaskTreeDecoder decoder = null;
[557]59
60    /** */
[1146]61    private TaskTreeEncoder encoder = new TaskTreeEncoder();
62   
[557]63    /**
[1106]64     *
65     */
[557]66    @Before
67    public void setUp() {
[1146]68        Console.reset();
[725]69        new TextConsole(Level.FINEST);
[1146]70       
71        decoder = new TaskTreeDecoder(taskFactory, taskBuilder);
[557]72    }
[445]73
[557]74    /**
75     *
76     */
[1294]77    protected void applyRule(Class<? extends ITaskInstanceScopeRule> ruleClass,
78                             String                                  inputSpec,
79                             String                                  expectedOutputSpec)
[1106]80    {
[1294]81        ITaskInstanceScopeRule rule = null;
[1132]82       
[1106]83        CONSTRUCTOR_ITERATION:
84        for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) {
85            List<Object> parameters = new LinkedList<Object>();
86           
87            for (Class<?> type : constructor.getParameterTypes()) {
[1146]88                if (ITaskFactory.class.equals(type)) {
89                    parameters.add(taskFactory);
[1106]90                }
[1146]91                else if (ITaskBuilder.class.equals(type)) {
92                    parameters.add(taskBuilder);
[1106]93                }
[1146]94                else if (TaskEquality.class.equals(type)) {
95                    parameters.add(TaskEquality.LEXICALLY_EQUAL);
[1106]96                }
97                else {
98                    continue CONSTRUCTOR_ITERATION;
99                }
100            }
101           
102            try {
[1294]103                rule = (ITaskInstanceScopeRule) constructor.newInstance(parameters.toArray());
[1106]104            }
105            catch (IllegalArgumentException e) {
106                e.printStackTrace();
107                fail("could not invoke the constructor " + constructor);
108            }
109            catch (InstantiationException e) {
110                e.printStackTrace();
111                fail("could not invoke the constructor " + constructor);
112            }
113            catch (IllegalAccessException e) {
114                e.printStackTrace();
115                fail("could not invoke the constructor " + constructor);
116            }
117            catch (InvocationTargetException e) {
118                e.printStackTrace();
119                fail("could not invoke the constructor " + constructor);
120            }
121        }
122       
123        if (rule == null) {
124            fail("no matching constructor found to instantiate rule " + ruleClass);
125        }
126       
127        RuleApplicationResult result;
128        RuleApplicationStatus status;
129       
[1294]130        ITaskInstance inputList = (ITaskInstance) decoder.decode(inputSpec);
[1106]131       
[1294]132        Stack<ITaskInstance> toBeAppliedOn = new Stack<ITaskInstance>();
[1146]133        toBeAppliedOn.push(inputList);
134       
[1106]135        do {
[1146]136            result = rule.apply(toBeAppliedOn.peek());
[1106]137           
138            if (result != null) {
139                status = result.getRuleApplicationStatus();
140                assertNotNull(status);
141            }
142            else {
[1127]143                status = RuleApplicationStatus.NOT_APPLIED;
[1106]144            }
145           
[1146]146            if ((result != null) && (result.getNewlyCreatedTaskInstances() != null)) {
147                for (int i = result.getNewlyCreatedTaskInstances().size() - 1; i >= 0; i--) {
148                    toBeAppliedOn.push(result.getNewlyCreatedTaskInstances().get(i));
[1106]149                }
150            }
151           
[1127]152            if (status == RuleApplicationStatus.NOT_APPLIED) {
[1106]153                toBeAppliedOn.pop();
154            }
155           
156        }
[1127]157        while ((!toBeAppliedOn.isEmpty()) || (status == RuleApplicationStatus.FINISHED));
[1106]158
[1294]159        ITaskInstance expectedList = (ITaskInstance) decoder.decode(expectedOutputSpec);
[1146]160       
[1294]161        new TaskTreeChecker().assertTaskInstancesEqual(expectedList, inputList);
[1106]162    }
163
[1146]164    /**
165     *
166     */
167    protected void applySessionScopeRule(Class<? extends ISessionScopeRule> ruleClass,
168                                         String                             inputSpec,
169                                         String                             expectedOutputSpec)
170    {
171        ISessionScopeRule rule = null;
172       
173        CONSTRUCTOR_ITERATION:
174        for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) {
175            List<Object> parameters = new LinkedList<Object>();
176           
177            for (Class<?> type : constructor.getParameterTypes()) {
178                if (ITaskFactory.class.equals(type)) {
179                    parameters.add(taskFactory);
180                }
181                else if (ITaskBuilder.class.equals(type)) {
182                    parameters.add(taskBuilder);
183                }
184                else if (TaskEquality.class.equals(type)) {
185                    parameters.add(TaskEquality.LEXICALLY_EQUAL);
186                }
187                else {
188                    continue CONSTRUCTOR_ITERATION;
189                }
190            }
191           
192            try {
193                rule = (ISessionScopeRule) constructor.newInstance(parameters.toArray());
194            }
195            catch (IllegalArgumentException e) {
196                e.printStackTrace();
197                fail("could not invoke the constructor " + constructor);
198            }
199            catch (InstantiationException e) {
200                e.printStackTrace();
201                fail("could not invoke the constructor " + constructor);
202            }
203            catch (IllegalAccessException e) {
204                e.printStackTrace();
205                fail("could not invoke the constructor " + constructor);
206            }
207            catch (InvocationTargetException e) {
208                e.printStackTrace();
209                fail("could not invoke the constructor " + constructor);
210            }
211        }
212       
213        if (rule == null) {
214            fail("no matching constructor found to instantiate rule " + ruleClass);
215        }
216       
217        ITaskInstanceList inputList = decoder.decode(inputSpec);
218       
219        assertTrue(inputList instanceof IUserSession);
220       
221        List<IUserSession> sessionList = new LinkedList<IUserSession>();
222        sessionList.add((IUserSession) inputList);
223       
224        System.out.println("Input:");
225        encoder.encode(inputList, System.out);
226       
227        RuleApplicationResult result = rule.apply(sessionList);
228       
229        assertNotNull(result);
230        assertNotNull(result.getRuleApplicationStatus());
231           
232        ITaskInstanceList expectedList = decoder.decode(expectedOutputSpec);
233       
234        System.out.println("\nExpected Result:");
235        encoder.encode(expectedList, System.out);
236        System.out.println("\nResult:");
237        encoder.encode(inputList, System.out);
238
239        new TaskTreeChecker().assertTaskInstanceListsEqual(expectedList, inputList);
240        new TaskTreeValidator().validate(inputList);
241    }
242
[445]243}
Note: See TracBrowser for help on using the repository browser.