[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] | 15 | package de.ugoe.cs.autoquest.tasktrees.temporalrelation; |
---|
[445] | 16 | |
---|
[1106] | 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; |
---|
[445] | 24 | import java.util.List; |
---|
[1106] | 25 | import java.util.Stack; |
---|
[725] | 26 | import java.util.logging.Level; |
---|
[445] | 27 | |
---|
| 28 | import org.junit.Before; |
---|
| 29 | |
---|
[1146] | 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; |
---|
[1294] | 35 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
[1146] | 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; |
---|
[1106] | 42 | import de.ugoe.cs.util.console.Console; |
---|
[725] | 43 | import 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] | 49 | public class AbstractTemporalRelationshipTC { |
---|
[445] | 50 | |
---|
[557] | 51 | /** */ |
---|
[1146] | 52 | private ITaskBuilder taskBuilder = new TaskBuilder(); |
---|
[445] | 53 | |
---|
[557] | 54 | /** */ |
---|
[1146] | 55 | private ITaskFactory taskFactory = new TaskFactory(); |
---|
[557] | 56 | |
---|
| 57 | /** */ |
---|
[1146] | 58 | private TaskTreeEncoder encoder = new TaskTreeEncoder(); |
---|
| 59 | |
---|
[557] | 60 | /** |
---|
[1106] | 61 | * |
---|
| 62 | */ |
---|
[557] | 63 | @Before |
---|
| 64 | public void setUp() { |
---|
[1146] | 65 | Console.reset(); |
---|
[725] | 66 | new TextConsole(Level.FINEST); |
---|
[557] | 67 | } |
---|
[445] | 68 | |
---|
[557] | 69 | /** |
---|
| 70 | * |
---|
| 71 | */ |
---|
[1294] | 72 | protected void applyRule(Class<? extends ITaskInstanceScopeRule> ruleClass, |
---|
| 73 | String inputSpec, |
---|
| 74 | String expectedOutputSpec) |
---|
[1106] | 75 | { |
---|
[1294] | 76 | ITaskInstanceScopeRule rule = null; |
---|
[1132] | 77 | |
---|
[1106] | 78 | CONSTRUCTOR_ITERATION: |
---|
| 79 | for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) { |
---|
| 80 | List<Object> parameters = new LinkedList<Object>(); |
---|
| 81 | |
---|
| 82 | for (Class<?> type : constructor.getParameterTypes()) { |
---|
[1146] | 83 | if (ITaskFactory.class.equals(type)) { |
---|
| 84 | parameters.add(taskFactory); |
---|
[1106] | 85 | } |
---|
[1146] | 86 | else if (ITaskBuilder.class.equals(type)) { |
---|
| 87 | parameters.add(taskBuilder); |
---|
[1106] | 88 | } |
---|
[1146] | 89 | else if (TaskEquality.class.equals(type)) { |
---|
| 90 | parameters.add(TaskEquality.LEXICALLY_EQUAL); |
---|
[1106] | 91 | } |
---|
| 92 | else { |
---|
| 93 | continue CONSTRUCTOR_ITERATION; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | try { |
---|
[1294] | 98 | rule = (ITaskInstanceScopeRule) constructor.newInstance(parameters.toArray()); |
---|
[1106] | 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 | |
---|
[1768] | 125 | TaskTreeDecoder decoder = new TaskTreeDecoder(taskFactory, taskBuilder); |
---|
[1294] | 126 | ITaskInstance inputList = (ITaskInstance) decoder.decode(inputSpec); |
---|
[1106] | 127 | |
---|
[1294] | 128 | Stack<ITaskInstance> toBeAppliedOn = new Stack<ITaskInstance>(); |
---|
[1146] | 129 | toBeAppliedOn.push(inputList); |
---|
| 130 | |
---|
[1106] | 131 | do { |
---|
[1146] | 132 | result = rule.apply(toBeAppliedOn.peek()); |
---|
[1106] | 133 | |
---|
| 134 | if (result != null) { |
---|
| 135 | status = result.getRuleApplicationStatus(); |
---|
| 136 | assertNotNull(status); |
---|
| 137 | } |
---|
| 138 | else { |
---|
[1127] | 139 | status = RuleApplicationStatus.NOT_APPLIED; |
---|
[1106] | 140 | } |
---|
| 141 | |
---|
[1146] | 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)); |
---|
[1106] | 145 | } |
---|
| 146 | } |
---|
| 147 | |
---|
[1127] | 148 | if (status == RuleApplicationStatus.NOT_APPLIED) { |
---|
[1106] | 149 | toBeAppliedOn.pop(); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | } |
---|
[1127] | 153 | while ((!toBeAppliedOn.isEmpty()) || (status == RuleApplicationStatus.FINISHED)); |
---|
[1106] | 154 | |
---|
[1294] | 155 | ITaskInstance expectedList = (ITaskInstance) decoder.decode(expectedOutputSpec); |
---|
[1146] | 156 | |
---|
[1294] | 157 | new TaskTreeChecker().assertTaskInstancesEqual(expectedList, inputList); |
---|
[1106] | 158 | } |
---|
| 159 | |
---|
[1146] | 160 | /** |
---|
| 161 | * |
---|
| 162 | */ |
---|
| 163 | protected void applySessionScopeRule(Class<? extends ISessionScopeRule> ruleClass, |
---|
| 164 | String inputSpec, |
---|
| 165 | String expectedOutputSpec) |
---|
| 166 | { |
---|
[1768] | 167 | System.out.println("#####################################################################"); |
---|
[1146] | 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 | |
---|
[1768] | 214 | TaskTreeDecoder decoder = new TaskTreeDecoder(taskFactory, taskBuilder); |
---|
[1146] | 215 | ITaskInstanceList inputList = decoder.decode(inputSpec); |
---|
| 216 | assertTrue(inputList instanceof IUserSession); |
---|
| 217 | |
---|
[1768] | 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 | |
---|
[1146] | 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 | |
---|
[1768] | 238 | // reparse the expected list to ensure that no doublings of tasks occur |
---|
| 239 | expectedList = decoder.decode(expectedOutputSpec); |
---|
[1146] | 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); |
---|
[1768] | 247 | List<IUserSession> sessions = new LinkedList<IUserSession>(); |
---|
| 248 | sessions.add((IUserSession) inputList); |
---|
| 249 | sessions.add((IUserSession) expectedList); |
---|
| 250 | new TaskTreeValidator().validate(sessions, true); |
---|
[1146] | 251 | } |
---|
| 252 | |
---|
[445] | 253 | } |
---|