[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; |
---|
[445] | 23 | import java.util.ArrayList; |
---|
[1106] | 24 | import java.util.LinkedList; |
---|
[445] | 25 | import java.util.List; |
---|
[1106] | 26 | import java.util.Stack; |
---|
[725] | 27 | import java.util.logging.Level; |
---|
[445] | 28 | |
---|
| 29 | import org.junit.Before; |
---|
| 30 | |
---|
[922] | 31 | import de.ugoe.cs.autoquest.eventcore.IEventTarget; |
---|
| 32 | import de.ugoe.cs.autoquest.eventcore.IEventType; |
---|
[1106] | 33 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality; |
---|
[922] | 34 | import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
| 35 | import de.ugoe.cs.autoquest.tasktrees.testutils.Utilities; |
---|
| 36 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask; |
---|
| 37 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
| 38 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTree; |
---|
| 39 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
[1106] | 40 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
[922] | 41 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
| 42 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeBuilder; |
---|
| 43 | import de.ugoe.cs.autoquest.tasktrees.treeimpl.TaskTreeNodeFactory; |
---|
[1106] | 44 | import de.ugoe.cs.util.console.Console; |
---|
[725] | 45 | import de.ugoe.cs.util.console.TextConsole; |
---|
[445] | 46 | |
---|
| 47 | /** |
---|
| 48 | * @version $Revision: $ $Date: 28.04.2012$ |
---|
| 49 | * @author 2012, last modified by $Author: patrick$ |
---|
| 50 | */ |
---|
[557] | 51 | public class AbstractTemporalRelationshipTC { |
---|
[445] | 52 | |
---|
[557] | 53 | /** */ |
---|
| 54 | private List<IEventTask> events; |
---|
[445] | 55 | |
---|
[557] | 56 | /** */ |
---|
| 57 | private ITaskTreeBuilder taskTreeBuilder = new TaskTreeBuilder(); |
---|
[445] | 58 | |
---|
[557] | 59 | /** */ |
---|
| 60 | private ITaskTreeNodeFactory taskTreeNodeFactory = new TaskTreeNodeFactory(); |
---|
| 61 | |
---|
| 62 | /** */ |
---|
| 63 | private NodeEqualityRuleManager nodeEqualityRuleManager = |
---|
| 64 | Utilities.getNodeEqualityRuleManagerForTests(); |
---|
| 65 | |
---|
| 66 | /** |
---|
[1106] | 67 | * |
---|
| 68 | */ |
---|
[557] | 69 | @Before |
---|
| 70 | public void setUp() { |
---|
[725] | 71 | new TextConsole(Level.FINEST); |
---|
[557] | 72 | events = new ArrayList<IEventTask>(); |
---|
| 73 | } |
---|
[445] | 74 | |
---|
[557] | 75 | /** |
---|
| 76 | * |
---|
| 77 | */ |
---|
| 78 | protected void simulateEvent(IEventType eventType, IEventTarget eventTarget) { |
---|
| 79 | events.add(taskTreeNodeFactory.createNewEventTask(eventType, eventTarget)); |
---|
[1106] | 80 | Console.reset(); |
---|
[557] | 81 | } |
---|
[445] | 82 | |
---|
[557] | 83 | /** |
---|
| 84 | * |
---|
| 85 | */ |
---|
| 86 | protected ITaskTree getTaskTree() { |
---|
| 87 | ISequence sequence = taskTreeNodeFactory.createNewSequence(); |
---|
| 88 | |
---|
| 89 | for (IEventTask task : events) { |
---|
| 90 | taskTreeBuilder.addChild(sequence, task); |
---|
| 91 | } |
---|
| 92 | |
---|
[1106] | 93 | TemporalRelationshipRuleManager ruleManager = new TemporalRelationshipRuleManager |
---|
| 94 | (nodeEqualityRuleManager, taskTreeNodeFactory, taskTreeBuilder); |
---|
[557] | 95 | |
---|
| 96 | ruleManager.init(); |
---|
[1106] | 97 | ruleManager.applyRules(sequence); |
---|
[557] | 98 | |
---|
| 99 | return taskTreeNodeFactory.createTaskTree(sequence); |
---|
[445] | 100 | } |
---|
| 101 | |
---|
[1106] | 102 | /** |
---|
| 103 | * |
---|
| 104 | */ |
---|
| 105 | protected ITaskTree getTaskTree(Class<? extends TemporalRelationshipRule> ruleClass, |
---|
| 106 | NodeEquality nodeEquality) |
---|
| 107 | { |
---|
[1132] | 108 | return getTaskTree(ruleClass, nodeEquality, false); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * |
---|
| 113 | */ |
---|
| 114 | protected ITaskTree getTaskTree(Class<? extends TemporalRelationshipRule> ruleClass, |
---|
| 115 | NodeEquality nodeEquality, |
---|
| 116 | boolean explicitSessionSeq) |
---|
| 117 | { |
---|
[1106] | 118 | ISequence sequence = taskTreeNodeFactory.createNewSequence(); |
---|
| 119 | |
---|
| 120 | for (IEventTask task : events) { |
---|
| 121 | taskTreeBuilder.addChild(sequence, task); |
---|
| 122 | } |
---|
| 123 | |
---|
[1132] | 124 | if (explicitSessionSeq) { |
---|
| 125 | ISequence root = taskTreeNodeFactory.createNewSequence(); |
---|
| 126 | taskTreeBuilder.addChild(root, sequence); |
---|
| 127 | sequence = root; |
---|
| 128 | } |
---|
| 129 | |
---|
[1106] | 130 | TemporalRelationshipRule rule = null; |
---|
| 131 | |
---|
| 132 | CONSTRUCTOR_ITERATION: |
---|
| 133 | for (Constructor<?> constructor : ruleClass.getDeclaredConstructors()) { |
---|
| 134 | List<Object> parameters = new LinkedList<Object>(); |
---|
| 135 | |
---|
| 136 | for (Class<?> type : constructor.getParameterTypes()) { |
---|
| 137 | if (ITaskTreeNodeFactory.class.equals(type)) { |
---|
| 138 | parameters.add(taskTreeNodeFactory); |
---|
| 139 | } |
---|
| 140 | else if (ITaskTreeBuilder.class.equals(type)) { |
---|
| 141 | parameters.add(taskTreeBuilder); |
---|
| 142 | } |
---|
| 143 | else if (NodeEqualityRuleManager.class.equals(type)) { |
---|
| 144 | parameters.add(nodeEqualityRuleManager); |
---|
| 145 | } |
---|
| 146 | else if (NodeEquality.class.equals(type)) { |
---|
| 147 | if (nodeEquality != null) { |
---|
| 148 | parameters.add(nodeEquality); |
---|
| 149 | } |
---|
| 150 | else { |
---|
| 151 | parameters.add(NodeEquality.LEXICALLY_EQUAL); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | else { |
---|
| 155 | continue CONSTRUCTOR_ITERATION; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | try { |
---|
| 160 | rule = (TemporalRelationshipRule) constructor.newInstance(parameters.toArray()); |
---|
| 161 | } |
---|
| 162 | catch (IllegalArgumentException e) { |
---|
| 163 | e.printStackTrace(); |
---|
| 164 | fail("could not invoke the constructor " + constructor); |
---|
| 165 | } |
---|
| 166 | catch (InstantiationException e) { |
---|
| 167 | e.printStackTrace(); |
---|
| 168 | fail("could not invoke the constructor " + constructor); |
---|
| 169 | } |
---|
| 170 | catch (IllegalAccessException e) { |
---|
| 171 | e.printStackTrace(); |
---|
| 172 | fail("could not invoke the constructor " + constructor); |
---|
| 173 | } |
---|
| 174 | catch (InvocationTargetException e) { |
---|
| 175 | e.printStackTrace(); |
---|
| 176 | fail("could not invoke the constructor " + constructor); |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | if (rule == null) { |
---|
| 181 | fail("no matching constructor found to instantiate rule " + ruleClass); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | RuleApplicationResult result; |
---|
| 185 | RuleApplicationStatus status; |
---|
| 186 | |
---|
| 187 | Stack<ITaskTreeNode> toBeAppliedOn = new Stack<ITaskTreeNode>(); |
---|
| 188 | toBeAppliedOn.push(sequence); |
---|
| 189 | |
---|
| 190 | do { |
---|
| 191 | result = rule.apply(toBeAppliedOn.peek(), true); |
---|
| 192 | |
---|
| 193 | if (result != null) { |
---|
| 194 | status = result.getRuleApplicationStatus(); |
---|
| 195 | assertNotNull(status); |
---|
| 196 | } |
---|
| 197 | else { |
---|
[1127] | 198 | status = RuleApplicationStatus.NOT_APPLIED; |
---|
[1106] | 199 | } |
---|
| 200 | |
---|
[1127] | 201 | assertTrue(status != RuleApplicationStatus.FEASIBLE); |
---|
[1106] | 202 | |
---|
| 203 | if ((result != null) && (result.getNewlyCreatedParentNodes() != null)) { |
---|
| 204 | for (int i = result.getNewlyCreatedParentNodes().size() - 1; i >= 0; i--) { |
---|
| 205 | toBeAppliedOn.push(result.getNewlyCreatedParentNodes().get(i)); |
---|
| 206 | } |
---|
| 207 | } |
---|
| 208 | |
---|
[1127] | 209 | if (status == RuleApplicationStatus.NOT_APPLIED) { |
---|
[1106] | 210 | toBeAppliedOn.pop(); |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | } |
---|
[1127] | 214 | while ((!toBeAppliedOn.isEmpty()) || (status == RuleApplicationStatus.FINISHED)); |
---|
[1106] | 215 | |
---|
| 216 | return taskTreeNodeFactory.createTaskTree(sequence); |
---|
| 217 | } |
---|
| 218 | |
---|
[445] | 219 | } |
---|