[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; |
---|
| 35 | import de.ugoe.cs.autoquest.tasktrees.taskequality.TaskEqualityRuleManager; |
---|
[922] | 36 | import de.ugoe.cs.autoquest.tasktrees.testutils.Utilities; |
---|
[1146] | 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
| 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder; |
---|
| 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory; |
---|
| 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession; |
---|
| 41 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskBuilder; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskFactory; |
---|
[1106] | 43 | import de.ugoe.cs.util.console.Console; |
---|
[725] | 44 | import de.ugoe.cs.util.console.TextConsole; |
---|
[445] | 45 | |
---|
| 46 | /** |
---|
| 47 | * @version $Revision: $ $Date: 28.04.2012$ |
---|
| 48 | * @author 2012, last modified by $Author: patrick$ |
---|
| 49 | */ |
---|
[557] | 50 | public class AbstractTemporalRelationshipTC { |
---|
[445] | 51 | |
---|
[557] | 52 | /** */ |
---|
[1146] | 53 | private ITaskBuilder taskBuilder = new TaskBuilder(); |
---|
[445] | 54 | |
---|
[557] | 55 | /** */ |
---|
[1146] | 56 | private ITaskFactory taskFactory = new TaskFactory(); |
---|
| 57 | |
---|
[557] | 58 | /** */ |
---|
[1146] | 59 | private TaskTreeDecoder decoder = null; |
---|
[557] | 60 | |
---|
| 61 | /** */ |
---|
[1146] | 62 | private TaskTreeEncoder encoder = new TaskTreeEncoder(); |
---|
| 63 | |
---|
| 64 | /** */ |
---|
| 65 | private TaskEqualityRuleManager taskEqualityRuleManager = |
---|
| 66 | Utilities.getTaskEqualityRuleManagerForTests(); |
---|
[557] | 67 | |
---|
| 68 | /** |
---|
[1106] | 69 | * |
---|
| 70 | */ |
---|
[557] | 71 | @Before |
---|
| 72 | public void setUp() { |
---|
[1146] | 73 | Console.reset(); |
---|
[725] | 74 | new TextConsole(Level.FINEST); |
---|
[1146] | 75 | |
---|
| 76 | decoder = new TaskTreeDecoder(taskFactory, taskBuilder); |
---|
[557] | 77 | } |
---|
[445] | 78 | |
---|
[557] | 79 | /** |
---|
| 80 | * |
---|
| 81 | */ |
---|
[1146] | 82 | protected void applyRule(Class<? extends ITaskInstanceListScopeRule> ruleClass, |
---|
| 83 | String inputSpec, |
---|
| 84 | String expectedOutputSpec) |
---|
[1106] | 85 | { |
---|
[1146] | 86 | ITaskInstanceListScopeRule rule = null; |
---|
[1132] | 87 | |
---|
[1106] | 88 | CONSTRUCTOR_ITERATION: |
---|
| 89 | for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) { |
---|
| 90 | List<Object> parameters = new LinkedList<Object>(); |
---|
| 91 | |
---|
| 92 | for (Class<?> type : constructor.getParameterTypes()) { |
---|
[1146] | 93 | if (ITaskFactory.class.equals(type)) { |
---|
| 94 | parameters.add(taskFactory); |
---|
[1106] | 95 | } |
---|
[1146] | 96 | else if (ITaskBuilder.class.equals(type)) { |
---|
| 97 | parameters.add(taskBuilder); |
---|
[1106] | 98 | } |
---|
[1146] | 99 | else if (TaskEqualityRuleManager.class.equals(type)) { |
---|
| 100 | parameters.add(taskEqualityRuleManager); |
---|
[1106] | 101 | } |
---|
[1146] | 102 | else if (TaskEquality.class.equals(type)) { |
---|
| 103 | parameters.add(TaskEquality.LEXICALLY_EQUAL); |
---|
[1106] | 104 | } |
---|
| 105 | else { |
---|
| 106 | continue CONSTRUCTOR_ITERATION; |
---|
| 107 | } |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | try { |
---|
[1146] | 111 | rule = (ITaskInstanceListScopeRule) constructor.newInstance(parameters.toArray()); |
---|
[1106] | 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 | |
---|
[1146] | 138 | ITaskInstanceList inputList = decoder.decode(inputSpec); |
---|
[1106] | 139 | |
---|
[1146] | 140 | Stack<ITaskInstanceList> toBeAppliedOn = new Stack<ITaskInstanceList>(); |
---|
| 141 | toBeAppliedOn.push(inputList); |
---|
| 142 | |
---|
[1106] | 143 | do { |
---|
[1146] | 144 | result = rule.apply(toBeAppliedOn.peek()); |
---|
[1106] | 145 | |
---|
| 146 | if (result != null) { |
---|
| 147 | status = result.getRuleApplicationStatus(); |
---|
| 148 | assertNotNull(status); |
---|
| 149 | } |
---|
| 150 | else { |
---|
[1127] | 151 | status = RuleApplicationStatus.NOT_APPLIED; |
---|
[1106] | 152 | } |
---|
| 153 | |
---|
[1127] | 154 | assertTrue(status != RuleApplicationStatus.FEASIBLE); |
---|
[1106] | 155 | |
---|
[1146] | 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)); |
---|
[1106] | 159 | } |
---|
| 160 | } |
---|
| 161 | |
---|
[1127] | 162 | if (status == RuleApplicationStatus.NOT_APPLIED) { |
---|
[1106] | 163 | toBeAppliedOn.pop(); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | } |
---|
[1127] | 167 | while ((!toBeAppliedOn.isEmpty()) || (status == RuleApplicationStatus.FINISHED)); |
---|
[1106] | 168 | |
---|
[1146] | 169 | ITaskInstanceList expectedList = decoder.decode(expectedOutputSpec); |
---|
| 170 | |
---|
| 171 | new TaskTreeChecker().assertTaskInstanceListsEqual(expectedList, inputList); |
---|
[1106] | 172 | } |
---|
| 173 | |
---|
[1146] | 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 | |
---|
[445] | 257 | } |
---|