[1113] | 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.treeimpl;
|
---|
[439] | 16 |
|
---|
[1294] | 17 | import java.util.Collection; |
---|
| 18 | import java.util.Collections; |
---|
| 19 | import java.util.HashSet; |
---|
[1400] | 20 | import java.util.LinkedList; |
---|
[1294] | 21 | |
---|
[1400] | 22 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTaskInstance; |
---|
| 23 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance; |
---|
| 24 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IOptionalInstance; |
---|
| 25 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelectionInstance; |
---|
| 26 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance; |
---|
[1146] | 27 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[1294] | 28 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance; |
---|
[1400] | 29 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList; |
---|
[1157] | 30 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskVisitor; |
---|
[439] | 31 |
|
---|
| 32 | /**
|
---|
[1215] | 33 | * <p> |
---|
| 34 | * this is the default implementation of the interface {@link ITask}. It |
---|
| 35 | * does not do anything fancy except implementing the interface. |
---|
| 36 | * </p> |
---|
| 37 | * |
---|
| 38 | * @author Patrick Harms |
---|
[439] | 39 | */
|
---|
[1400] | 40 | abstract class Task implements ITask {
|
---|
[1146] | 41 | |
---|
[1215] | 42 | /** |
---|
| 43 | * <p> |
---|
| 44 | * default serial version UID |
---|
| 45 | * </p> |
---|
| 46 | */ |
---|
[1146] | 47 | private static final long serialVersionUID = 1L; |
---|
| 48 | |
---|
[1215] | 49 | /** |
---|
| 50 | * <p> |
---|
| 51 | * used as a counter to generate new ids for each newly created task. May overflow. |
---|
| 52 | * </p> |
---|
| 53 | */
|
---|
[557] | 54 | private static int temporalId = 0;
|
---|
[439] | 55 |
|
---|
[1215] | 56 | /** |
---|
| 57 | * <p> |
---|
| 58 | * the id of the task (unique throughout the system as long as {@link #temporalId} does not |
---|
| 59 | * overflow. |
---|
| 60 | * </p> |
---|
| 61 | */
|
---|
[1357] | 62 | private int id; |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * <p> |
---|
| 66 | * a human readable type of the task (used for visualization purposes) |
---|
| 67 | * </p> |
---|
| 68 | */ |
---|
| 69 | private String type;
|
---|
[439] | 70 |
|
---|
[1215] | 71 | /** |
---|
| 72 | * <p> |
---|
| 73 | * a human readable description of the task |
---|
| 74 | * </p> |
---|
| 75 | */
|
---|
[1294] | 76 | private String description; |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * <p> |
---|
| 80 | * the instances of this task |
---|
| 81 | * </p> |
---|
| 82 | */ |
---|
| 83 | private Collection<ITaskInstance> instances = new HashSet<ITaskInstance>();
|
---|
[1400] | 84 | |
---|
| 85 | /** |
---|
| 86 | * <p> |
---|
| 87 | * the execution variants of this task |
---|
| 88 | * </p> |
---|
| 89 | */ |
---|
| 90 | private Collection<Collection<ITaskInstance>> executionVariants; |
---|
[439] | 91 |
|
---|
[557] | 92 | /**
|
---|
[1215] | 93 | * <p> |
---|
| 94 | * constructs a new task with a new id. The id is generated using the {@link #getNewId()} |
---|
[1357] | 95 | * method |
---|
| 96 | * </p> |
---|
| 97 | * |
---|
| 98 | * @param type the human readable type of the task |
---|
| 99 | * |
---|
| 100 | * @throws IllegalArgumentException in the case the provided type is null
|
---|
[557] | 101 | */
|
---|
[1357] | 102 | Task(String type) {
|
---|
| 103 | this.id = getNewId(); |
---|
| 104 | this.type = type; |
---|
| 105 | |
---|
| 106 | if (type == null) { |
---|
| 107 | throw new IllegalArgumentException("type must not be null"); |
---|
| 108 | }
|
---|
[439] | 109 | }
|
---|
| 110 |
|
---|
[557] | 111 | /**
|
---|
[1215] | 112 | * <p> |
---|
| 113 | * creates a new id for a task using {@link #temporalId} by incrementing it an returning its |
---|
| 114 | * current value. Resets the counter if {@link Integer.MAX_VALUE} is reached. |
---|
| 115 | * </p>
|
---|
[557] | 116 | *
|
---|
[1215] | 117 | * @return a new unique id for a task as long as {@link #temporalId} does not overflow
|
---|
[557] | 118 | */
|
---|
| 119 | private static synchronized int getNewId() {
|
---|
| 120 | if (temporalId == Integer.MAX_VALUE) {
|
---|
| 121 | temporalId = 0;
|
---|
| 122 | }
|
---|
[439] | 123 |
|
---|
[557] | 124 | return temporalId++;
|
---|
| 125 | }
|
---|
[1215] | 126 | |
---|
| 127 | /* (non-Javadoc) |
---|
| 128 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#geId() |
---|
| 129 | */ |
---|
| 130 | @Override
|
---|
[1146] | 131 | public int getId() {
|
---|
| 132 | return id;
|
---|
[439] | 133 | }
|
---|
[557] | 134 |
|
---|
[1357] | 135 | /* (non-Javadoc) |
---|
| 136 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getType() |
---|
| 137 | */ |
---|
| 138 | @Override |
---|
| 139 | public String getType() { |
---|
| 140 | return type; |
---|
| 141 | } |
---|
| 142 | |
---|
[1215] | 143 | /* (non-Javadoc)
|
---|
| 144 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getDescription()
|
---|
[557] | 145 | */
|
---|
| 146 | @Override
|
---|
| 147 | public String getDescription() {
|
---|
| 148 | return description;
|
---|
[439] | 149 | }
|
---|
[557] | 150 |
|
---|
[1215] | 151 | /* (non-Javadoc) |
---|
[1294] | 152 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getInstances() |
---|
| 153 | */ |
---|
| 154 | @Override |
---|
| 155 | public Collection<ITaskInstance> getInstances() { |
---|
| 156 | return Collections.unmodifiableCollection(instances); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /* (non-Javadoc) |
---|
[1400] | 160 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getExecutionVariants() |
---|
| 161 | */ |
---|
| 162 | @Override |
---|
| 163 | public synchronized Collection<Collection<ITaskInstance>> getExecutionVariants() { |
---|
| 164 | if (executionVariants == null) { |
---|
| 165 | executionVariants = new LinkedList<Collection<ITaskInstance>>(); |
---|
| 166 | determineExecutionVariants(executionVariants); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | return executionVariants; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* (non-Javadoc) |
---|
[1215] | 173 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#equals(ITask) |
---|
| 174 | */ |
---|
[557] | 175 | @Override
|
---|
[1146] | 176 | public final boolean equals(ITask task) { |
---|
| 177 | // tasks are only equal if they are identical or if they have the same id |
---|
| 178 | // (may happen, if they are cloned)
|
---|
| 179 | return (this == task) || (this.hashCode() == task.hashCode());
|
---|
[439] | 180 | }
|
---|
[557] | 181 |
|
---|
[1215] | 182 | /* (non-Javadoc) |
---|
| 183 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#hashCode() |
---|
| 184 | */ |
---|
[557] | 185 | @Override
|
---|
| 186 | public synchronized int hashCode() {
|
---|
[1146] | 187 | return id;
|
---|
[439] | 188 | }
|
---|
[557] | 189 |
|
---|
[1215] | 190 | /* (non-Javadoc)
|
---|
[557] | 191 | * @see java.lang.Object#toString()
|
---|
| 192 | */
|
---|
| 193 | @Override
|
---|
[1126] | 194 | public synchronized String toString() { |
---|
| 195 | StringBuffer result = new StringBuffer(); |
---|
[1357] | 196 | result.append(type); |
---|
| 197 | result.append(" #"); |
---|
[1126] | 198 | result.append(id); |
---|
| 199 | |
---|
| 200 | if (description != null) { |
---|
[1146] | 201 | result.append(" ("); |
---|
[1126] | 202 | result.append(description); |
---|
[1146] | 203 | result.append(')'); |
---|
[1126] | 204 | } |
---|
| 205 | |
---|
| 206 | return result.toString();
|
---|
[439] | 207 | }
|
---|
| 208 |
|
---|
[1215] | 209 | /* (non-Javadoc) |
---|
| 210 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#clone() |
---|
| 211 | */ |
---|
[557] | 212 | @Override
|
---|
[1146] | 213 | public synchronized ITask clone() {
|
---|
| 214 | Task clone = null;
|
---|
[557] | 215 | try {
|
---|
[1146] | 216 | clone = (Task) super.clone();
|
---|
[470] | 217 | }
|
---|
[557] | 218 | catch (CloneNotSupportedException e) {
|
---|
| 219 | // this should never happen. Therefore simply dump the exception
|
---|
| 220 | e.printStackTrace();
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | return clone;
|
---|
[467] | 224 | }
|
---|
[439] | 225 |
|
---|
[1146] | 226 | /** |
---|
[1215] | 227 | * <p> |
---|
| 228 | * internally used to set the human readable description of the task |
---|
| 229 | * </p> |
---|
[1146] | 230 | * |
---|
[1215] | 231 | * @param description the new human readable description of the task |
---|
[1146] | 232 | */ |
---|
| 233 | void setDescription(String description) { |
---|
| 234 | this.description = description; |
---|
| 235 | } |
---|
| 236 | |
---|
[1294] | 237 | /** |
---|
| 238 | * <p> |
---|
[1357] | 239 | * internally used to remove an instance from this task |
---|
| 240 | * </p> |
---|
| 241 | * |
---|
| 242 | * @param instance the instance to be removed from this task |
---|
| 243 | */ |
---|
[1400] | 244 | synchronized void removeInstance(ITaskInstance instance) { |
---|
[1357] | 245 | this.instances.remove(instance); |
---|
[1400] | 246 | this.executionVariants = null; |
---|
[1357] | 247 | } |
---|
| 248 | |
---|
| 249 | /** |
---|
| 250 | * <p> |
---|
[1294] | 251 | * internally used to add an instance to this task |
---|
| 252 | * </p> |
---|
| 253 | * |
---|
| 254 | * @param instance the instance belonging to this task |
---|
| 255 | */ |
---|
[1400] | 256 | synchronized void addInstance(ITaskInstance instance) { |
---|
[1294] | 257 | this.instances.add(instance); |
---|
[1400] | 258 | this.executionVariants = null; |
---|
[1294] | 259 | } |
---|
| 260 | |
---|
[1157] | 261 | /* (non-Javadoc) |
---|
[1215] | 262 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#accept(ITaskVisitor) |
---|
[1157] | 263 | */ |
---|
| 264 | @Override |
---|
| 265 | public void accept(ITaskVisitor visitor) { |
---|
| 266 | visitor.visit(this); |
---|
| 267 | } |
---|
| 268 | |
---|
[1400] | 269 | /** |
---|
| 270 | * |
---|
| 271 | */ |
---|
| 272 | private void determineExecutionVariants(Collection<Collection<ITaskInstance>> executionVariants) |
---|
| 273 | { |
---|
| 274 | for (ITaskInstance instance : instances) { |
---|
| 275 | boolean added = false; |
---|
| 276 | for (Collection<ITaskInstance> variant : executionVariants) { |
---|
| 277 | if (!variant.isEmpty() && (isSameExecution(variant.iterator().next(), instance))) { |
---|
| 278 | variant.add(instance); |
---|
| 279 | added = true; |
---|
| 280 | } |
---|
| 281 | } |
---|
| 282 | |
---|
| 283 | if (!added) { |
---|
| 284 | Collection<ITaskInstance> variant = new HashSet<ITaskInstance>(); |
---|
| 285 | variant.add(instance); |
---|
| 286 | executionVariants.add(variant); |
---|
| 287 | } |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | /** |
---|
| 292 | * |
---|
| 293 | */ |
---|
| 294 | private boolean isSameExecution(ITaskInstance instance1, ITaskInstance instance2) { |
---|
| 295 | if (instance1 instanceof IIterationInstance) { |
---|
| 296 | if (!(instance2 instanceof IIterationInstance)) { |
---|
| 297 | return false; |
---|
| 298 | } |
---|
| 299 | |
---|
| 300 | ITaskInstanceList iteration1 = (ITaskInstanceList) instance1; |
---|
| 301 | ITaskInstanceList iteration2 = (ITaskInstanceList) instance2; |
---|
| 302 | |
---|
| 303 | return isSameExecutionList(iteration1, iteration2); |
---|
| 304 | } |
---|
| 305 | else if (instance1 instanceof ISequenceInstance) { |
---|
| 306 | if (!(instance2 instanceof ISequenceInstance)) { |
---|
| 307 | return false; |
---|
| 308 | } |
---|
| 309 | |
---|
| 310 | ITaskInstanceList selection1 = (ITaskInstanceList) instance1; |
---|
| 311 | ITaskInstanceList selection2 = (ITaskInstanceList) instance2; |
---|
| 312 | |
---|
| 313 | return isSameExecutionList(selection1, selection2); |
---|
| 314 | } |
---|
| 315 | else if (instance1 instanceof ISelectionInstance) { |
---|
| 316 | if (!(instance2 instanceof ISelectionInstance)) { |
---|
| 317 | return false; |
---|
| 318 | } |
---|
| 319 | else { |
---|
| 320 | return isSameExecution(((ISelectionInstance) instance1).getChild(), |
---|
| 321 | ((ISelectionInstance) instance2).getChild()); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | else if (instance1 instanceof IOptionalInstance) { |
---|
| 325 | if (!(instance2 instanceof IOptionalInstance)) { |
---|
| 326 | return false; |
---|
| 327 | } |
---|
| 328 | else { |
---|
| 329 | return isSameExecution(((IOptionalInstance) instance1).getChild(), |
---|
| 330 | ((IOptionalInstance) instance2).getChild()); |
---|
| 331 | } |
---|
| 332 | } |
---|
| 333 | else if (instance1 instanceof IEventTaskInstance) { |
---|
| 334 | if (!(instance2 instanceof IEventTaskInstance)) { |
---|
| 335 | return false; |
---|
| 336 | } |
---|
| 337 | else { |
---|
| 338 | return ((IEventTaskInstance) instance1).getTask().equals |
---|
| 339 | (((IEventTaskInstance) instance2).getTask()); |
---|
| 340 | } |
---|
| 341 | } |
---|
| 342 | else if (instance1 == null) { |
---|
| 343 | return instance2 == null; |
---|
| 344 | } |
---|
| 345 | else { |
---|
| 346 | throw new IllegalArgumentException("unknown type of task instance: " + instance1); |
---|
| 347 | } |
---|
| 348 | } |
---|
| 349 | |
---|
| 350 | /** |
---|
| 351 | * |
---|
| 352 | */ |
---|
| 353 | private boolean isSameExecutionList(ITaskInstanceList list1, ITaskInstanceList list2) { |
---|
| 354 | if (list1.size() == list2.size()) { |
---|
| 355 | for (int i = 0; i < list1.size(); i++) { |
---|
| 356 | if (!isSameExecution(list1.get(i), list2.get(i))) { |
---|
| 357 | return false; |
---|
| 358 | } |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | return true; |
---|
| 362 | } |
---|
| 363 | else { |
---|
| 364 | return false; |
---|
| 365 | } |
---|
| 366 | } |
---|
[439] | 367 | }
|
---|