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 TaskTreeEncoder encoder = new TaskTreeEncoder(); |
---|
59 | |
---|
60 | /** |
---|
61 | * |
---|
62 | */ |
---|
63 | @Before |
---|
64 | public void setUp() { |
---|
65 | Console.reset(); |
---|
66 | new TextConsole(Level.FINEST); |
---|
67 | } |
---|
68 | |
---|
69 | /** |
---|
70 | * |
---|
71 | */ |
---|
72 | protected void applyRule(Class<? extends ITaskInstanceScopeRule> ruleClass, |
---|
73 | String inputSpec, |
---|
74 | String expectedOutputSpec) |
---|
75 | { |
---|
76 | ITaskInstanceScopeRule rule = null; |
---|
77 | |
---|
78 | CONSTRUCTOR_ITERATION: |
---|
79 | for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) { |
---|
80 | List<Object> parameters = new LinkedList<Object>(); |
---|
81 | |
---|
82 | for (Class<?> type : constructor.getParameterTypes()) { |
---|
83 | if (ITaskFactory.class.equals(type)) { |
---|
84 | parameters.add(taskFactory); |
---|
85 | } |
---|
86 | else if (ITaskBuilder.class.equals(type)) { |
---|
87 | parameters.add(taskBuilder); |
---|
88 | } |
---|
89 | else if (TaskEquality.class.equals(type)) { |
---|
90 | parameters.add(TaskEquality.LEXICALLY_EQUAL); |
---|
91 | } |
---|
92 | else { |
---|
93 | continue CONSTRUCTOR_ITERATION; |
---|
94 | } |
---|
95 | } |
---|
96 | |
---|
97 | try { |
---|
98 | rule = (ITaskInstanceScopeRule) constructor.newInstance(parameters.toArray()); |
---|
99 | } |
---|
100 | catch (IllegalArgumentException e) { |
---|
101 | e.printStackTrace(); |
---|
102 | fail("could not invoke the constructor " + constructor); |
---|
103 | } |
---|
104 | catch (InstantiationException e) { |
---|
105 | e.printStackTrace(); |
---|
106 | fail("could not invoke the constructor " + constructor); |
---|
107 | } |
---|
108 | catch (IllegalAccessException e) { |
---|
109 | e.printStackTrace(); |
---|
110 | fail("could not invoke the constructor " + constructor); |
---|
111 | } |
---|
112 | catch (InvocationTargetException e) { |
---|
113 | e.printStackTrace(); |
---|
114 | fail("could not invoke the constructor " + constructor); |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | if (rule == null) { |
---|
119 | fail("no matching constructor found to instantiate rule " + ruleClass); |
---|
120 | } |
---|
121 | |
---|
122 | RuleApplicationResult result; |
---|
123 | RuleApplicationStatus status; |
---|
124 | |
---|
125 | TaskTreeDecoder decoder = new TaskTreeDecoder(taskFactory, taskBuilder); |
---|
126 | ITaskInstance inputList = (ITaskInstance) decoder.decode(inputSpec); |
---|
127 | |
---|
128 | Stack<ITaskInstance> toBeAppliedOn = new Stack<ITaskInstance>(); |
---|
129 | toBeAppliedOn.push(inputList); |
---|
130 | |
---|
131 | do { |
---|
132 | result = rule.apply(toBeAppliedOn.peek()); |
---|
133 | |
---|
134 | if (result != null) { |
---|
135 | status = result.getRuleApplicationStatus(); |
---|
136 | assertNotNull(status); |
---|
137 | } |
---|
138 | else { |
---|
139 | status = RuleApplicationStatus.NOT_APPLIED; |
---|
140 | } |
---|
141 | |
---|
142 | if ((result != null) && (result.getNewlyCreatedTaskInstances() != null)) { |
---|
143 | for (int i = result.getNewlyCreatedTaskInstances().size() - 1; i >= 0; i--) { |
---|
144 | toBeAppliedOn.push(result.getNewlyCreatedTaskInstances().get(i)); |
---|
145 | } |
---|
146 | } |
---|
147 | |
---|
148 | if (status == RuleApplicationStatus.NOT_APPLIED) { |
---|
149 | toBeAppliedOn.pop(); |
---|
150 | } |
---|
151 | |
---|
152 | } |
---|
153 | while ((!toBeAppliedOn.isEmpty()) || (status == RuleApplicationStatus.FINISHED)); |
---|
154 | |
---|
155 | ITaskInstance expectedList = (ITaskInstance) decoder.decode(expectedOutputSpec); |
---|
156 | |
---|
157 | new TaskTreeChecker().assertTaskInstancesEqual(expectedList, inputList); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * |
---|
162 | */ |
---|
163 | protected void applySessionScopeRule(Class<? extends ISessionScopeRule> ruleClass, |
---|
164 | String inputSpec, |
---|
165 | String expectedOutputSpec) |
---|
166 | { |
---|
167 | System.out.println("#####################################################################"); |
---|
168 | ISessionScopeRule rule = null; |
---|
169 | |
---|
170 | CONSTRUCTOR_ITERATION: |
---|
171 | for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) { |
---|
172 | List<Object> parameters = new LinkedList<Object>(); |
---|
173 | |
---|
174 | for (Class<?> type : constructor.getParameterTypes()) { |
---|
175 | if (ITaskFactory.class.equals(type)) { |
---|
176 | parameters.add(taskFactory); |
---|
177 | } |
---|
178 | else if (ITaskBuilder.class.equals(type)) { |
---|
179 | parameters.add(taskBuilder); |
---|
180 | } |
---|
181 | else if (TaskEquality.class.equals(type)) { |
---|
182 | parameters.add(TaskEquality.LEXICALLY_EQUAL); |
---|
183 | } |
---|
184 | else { |
---|
185 | continue CONSTRUCTOR_ITERATION; |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | try { |
---|
190 | rule = (ISessionScopeRule) constructor.newInstance(parameters.toArray()); |
---|
191 | } |
---|
192 | catch (IllegalArgumentException e) { |
---|
193 | e.printStackTrace(); |
---|
194 | fail("could not invoke the constructor " + constructor); |
---|
195 | } |
---|
196 | catch (InstantiationException e) { |
---|
197 | e.printStackTrace(); |
---|
198 | fail("could not invoke the constructor " + constructor); |
---|
199 | } |
---|
200 | catch (IllegalAccessException e) { |
---|
201 | e.printStackTrace(); |
---|
202 | fail("could not invoke the constructor " + constructor); |
---|
203 | } |
---|
204 | catch (InvocationTargetException e) { |
---|
205 | e.printStackTrace(); |
---|
206 | fail("could not invoke the constructor " + constructor); |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | if (rule == null) { |
---|
211 | fail("no matching constructor found to instantiate rule " + ruleClass); |
---|
212 | } |
---|
213 | |
---|
214 | TaskTreeDecoder decoder = new TaskTreeDecoder(taskFactory, taskBuilder); |
---|
215 | ITaskInstanceList inputList = decoder.decode(inputSpec); |
---|
216 | assertTrue(inputList instanceof IUserSession); |
---|
217 | |
---|
218 | ITaskInstanceList expectedList = decoder.decode(expectedOutputSpec); |
---|
219 | assertTrue(expectedList instanceof IUserSession); |
---|
220 | |
---|
221 | new TaskTreeChecker().assertEventTaskInstancesEqual(expectedList, inputList); |
---|
222 | |
---|
223 | // reparse the input list to ensure that no doublings of task instances occurs |
---|
224 | decoder = new TaskTreeDecoder(taskFactory, taskBuilder); |
---|
225 | inputList = decoder.decode(inputSpec); |
---|
226 | |
---|
227 | List<IUserSession> sessionList = new LinkedList<IUserSession>(); |
---|
228 | sessionList.add((IUserSession) inputList); |
---|
229 | |
---|
230 | System.out.println("Input:"); |
---|
231 | encoder.encode(inputList, System.out); |
---|
232 | |
---|
233 | RuleApplicationResult result = rule.apply(sessionList); |
---|
234 | |
---|
235 | assertNotNull(result); |
---|
236 | assertNotNull(result.getRuleApplicationStatus()); |
---|
237 | |
---|
238 | // reparse the expected list to ensure that no doublings of tasks occur |
---|
239 | expectedList = decoder.decode(expectedOutputSpec); |
---|
240 | |
---|
241 | System.out.println("\nExpected Result:"); |
---|
242 | encoder.encode(expectedList, System.out); |
---|
243 | System.out.println("\nResult:"); |
---|
244 | encoder.encode(inputList, System.out); |
---|
245 | |
---|
246 | new TaskTreeChecker().assertTaskInstanceListsEqual(expectedList, inputList); |
---|
247 | List<IUserSession> sessions = new LinkedList<IUserSession>(); |
---|
248 | sessions.add((IUserSession) inputList); |
---|
249 | sessions.add((IUserSession) expectedList); |
---|
250 | new TaskTreeValidator().validate(sessions, true); |
---|
251 | } |
---|
252 | |
---|
253 | } |
---|