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

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