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