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