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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
16 | |
---|
17 | import static org.junit.Assert.fail; |
---|
18 | import static org.junit.Assert.assertTrue; |
---|
19 | import static org.junit.Assert.assertNotNull; |
---|
20 | |
---|
21 | import java.lang.reflect.Constructor; |
---|
22 | import java.lang.reflect.InvocationTargetException; |
---|
23 | import java.util.LinkedList; |
---|
24 | import java.util.List; |
---|
25 | import java.util.Stack; |
---|
26 | import java.util.logging.Level; |
---|
27 | |
---|
28 | import org.junit.Before; |
---|
29 | |
---|
30 | import de.ugoe.cs.autoquest.tasktrees.TaskTreeChecker; |
---|
31 | import de.ugoe.cs.autoquest.tasktrees.TaskTreeDecoder; |
---|
32 | import de.ugoe.cs.autoquest.tasktrees.TaskTreeEncoder; |
---|
33 | import de.ugoe.cs.autoquest.tasktrees.TaskTreeValidator; |
---|
34 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEquality; |
---|
35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
40 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskBuilder; |
---|
41 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskFactory; |
---|
42 | import de.ugoe.cs.util.console.Console; |
---|
43 | import de.ugoe.cs.util.console.TextConsole; |
---|
44 | |
---|
45 | /** |
---|
46 | * @version $Revision: $ $Date: 28.04.2012$ |
---|
47 | * @author 2012, last modified by $Author: patrick$ |
---|
48 | */ |
---|
49 | public class AbstractTemporalRelationshipTC { |
---|
50 | |
---|
51 | /** */ |
---|
52 | private ITaskBuilder taskBuilder = new TaskBuilder(); |
---|
53 | |
---|
54 | /** */ |
---|
55 | private ITaskFactory taskFactory = new TaskFactory(); |
---|
56 | |
---|
57 | /** */ |
---|
58 | private TaskTreeDecoder decoder = null; |
---|
59 | |
---|
60 | /** */ |
---|
61 | private TaskTreeEncoder encoder = new TaskTreeEncoder(); |
---|
62 | |
---|
63 | /** |
---|
64 | * |
---|
65 | */ |
---|
66 | @Before |
---|
67 | public void setUp() { |
---|
68 | Console.reset(); |
---|
69 | new TextConsole(Level.FINEST); |
---|
70 | |
---|
71 | decoder = new TaskTreeDecoder(taskFactory, taskBuilder); |
---|
72 | } |
---|
73 | |
---|
74 | /** |
---|
75 | * |
---|
76 | */ |
---|
77 | protected void applyRule(Class<? extends ITaskInstanceScopeRule> ruleClass, |
---|
78 | String inputSpec, |
---|
79 | String expectedOutputSpec) |
---|
80 | { |
---|
81 | ITaskInstanceScopeRule rule = null; |
---|
82 | |
---|
83 | CONSTRUCTOR_ITERATION: |
---|
84 | for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) { |
---|
85 | List<Object> parameters = new LinkedList<Object>(); |
---|
86 | |
---|
87 | for (Class<?> type : constructor.getParameterTypes()) { |
---|
88 | if (ITaskFactory.class.equals(type)) { |
---|
89 | parameters.add(taskFactory); |
---|
90 | } |
---|
91 | else if (ITaskBuilder.class.equals(type)) { |
---|
92 | parameters.add(taskBuilder); |
---|
93 | } |
---|
94 | else if (TaskEquality.class.equals(type)) { |
---|
95 | parameters.add(TaskEquality.LEXICALLY_EQUAL); |
---|
96 | } |
---|
97 | else { |
---|
98 | continue CONSTRUCTOR_ITERATION; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | try { |
---|
103 | rule = (ITaskInstanceScopeRule) constructor.newInstance(parameters.toArray()); |
---|
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 | |
---|
130 | ITaskInstance inputList = (ITaskInstance) decoder.decode(inputSpec); |
---|
131 | |
---|
132 | Stack<ITaskInstance> toBeAppliedOn = new Stack<ITaskInstance>(); |
---|
133 | toBeAppliedOn.push(inputList); |
---|
134 | |
---|
135 | do { |
---|
136 | result = rule.apply(toBeAppliedOn.peek()); |
---|
137 | |
---|
138 | if (result != null) { |
---|
139 | status = result.getRuleApplicationStatus(); |
---|
140 | assertNotNull(status); |
---|
141 | } |
---|
142 | else { |
---|
143 | status = RuleApplicationStatus.NOT_APPLIED; |
---|
144 | } |
---|
145 | |
---|
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)); |
---|
149 | } |
---|
150 | } |
---|
151 | |
---|
152 | if (status == RuleApplicationStatus.NOT_APPLIED) { |
---|
153 | toBeAppliedOn.pop(); |
---|
154 | } |
---|
155 | |
---|
156 | } |
---|
157 | while ((!toBeAppliedOn.isEmpty()) || (status == RuleApplicationStatus.FINISHED)); |
---|
158 | |
---|
159 | ITaskInstance expectedList = (ITaskInstance) decoder.decode(expectedOutputSpec); |
---|
160 | |
---|
161 | new TaskTreeChecker().assertTaskInstancesEqual(expectedList, inputList); |
---|
162 | } |
---|
163 | |
---|
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 | |
---|
243 | } |
---|