[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 | */ |
---|
[2126] | 83 | private final Collection<ITaskInstance> instances = new HashSet<ITaskInstance>();
|
---|
[1400] | 84 | |
---|
| 85 | /** |
---|
| 86 | * <p> |
---|
[2126] | 87 | * the unmodifiable view on the instances of this task |
---|
| 88 | * </p> |
---|
| 89 | */ |
---|
| 90 | private final Collection<ITaskInstance> unmodifiableInstances; |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * <p> |
---|
[1400] | 94 | * the execution variants of this task |
---|
| 95 | * </p> |
---|
| 96 | */ |
---|
| 97 | private Collection<Collection<ITaskInstance>> executionVariants; |
---|
[439] | 98 |
|
---|
[557] | 99 | /**
|
---|
[1215] | 100 | * <p> |
---|
| 101 | * constructs a new task with a new id. The id is generated using the {@link #getNewId()} |
---|
[1357] | 102 | * method |
---|
| 103 | * </p> |
---|
| 104 | * |
---|
| 105 | * @param type the human readable type of the task |
---|
| 106 | * |
---|
| 107 | * @throws IllegalArgumentException in the case the provided type is null
|
---|
[557] | 108 | */
|
---|
[1357] | 109 | Task(String type) {
|
---|
| 110 | this.id = getNewId(); |
---|
| 111 | this.type = type; |
---|
| 112 | |
---|
| 113 | if (type == null) { |
---|
| 114 | throw new IllegalArgumentException("type must not be null"); |
---|
[2126] | 115 | } |
---|
| 116 | |
---|
| 117 | unmodifiableInstances = Collections.unmodifiableCollection(instances);
|
---|
[439] | 118 | }
|
---|
| 119 |
|
---|
[557] | 120 | /**
|
---|
[1215] | 121 | * <p> |
---|
| 122 | * creates a new id for a task using {@link #temporalId} by incrementing it an returning its |
---|
| 123 | * current value. Resets the counter if {@link Integer.MAX_VALUE} is reached. |
---|
| 124 | * </p>
|
---|
[557] | 125 | *
|
---|
[1215] | 126 | * @return a new unique id for a task as long as {@link #temporalId} does not overflow
|
---|
[557] | 127 | */
|
---|
| 128 | private static synchronized int getNewId() {
|
---|
| 129 | if (temporalId == Integer.MAX_VALUE) {
|
---|
| 130 | temporalId = 0;
|
---|
| 131 | }
|
---|
[439] | 132 |
|
---|
[557] | 133 | return temporalId++;
|
---|
| 134 | }
|
---|
[1215] | 135 | |
---|
| 136 | /* (non-Javadoc) |
---|
| 137 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#geId() |
---|
| 138 | */ |
---|
| 139 | @Override
|
---|
[1146] | 140 | public int getId() {
|
---|
| 141 | return id;
|
---|
[439] | 142 | }
|
---|
[557] | 143 |
|
---|
[1357] | 144 | /* (non-Javadoc) |
---|
| 145 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getType() |
---|
| 146 | */ |
---|
| 147 | @Override |
---|
| 148 | public String getType() { |
---|
| 149 | return type; |
---|
| 150 | } |
---|
| 151 | |
---|
[1215] | 152 | /* (non-Javadoc)
|
---|
| 153 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getDescription()
|
---|
[557] | 154 | */
|
---|
| 155 | @Override
|
---|
| 156 | public String getDescription() {
|
---|
| 157 | return description;
|
---|
[439] | 158 | }
|
---|
[557] | 159 |
|
---|
[1215] | 160 | /* (non-Javadoc) |
---|
[1294] | 161 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getInstances() |
---|
| 162 | */ |
---|
| 163 | @Override |
---|
| 164 | public Collection<ITaskInstance> getInstances() { |
---|
[2136] | 165 | // the feature of an unmodifiable collection was added later. To ensure backward |
---|
| 166 | // compatibility with stored data, check if the object exists, if not return the instances. |
---|
| 167 | if (unmodifiableInstances == null) { |
---|
| 168 | return instances; |
---|
| 169 | } |
---|
| 170 | |
---|
[2126] | 171 | return unmodifiableInstances; |
---|
[1294] | 172 | } |
---|
| 173 | |
---|
| 174 | /* (non-Javadoc) |
---|
[1400] | 175 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#getExecutionVariants() |
---|
| 176 | */ |
---|
| 177 | @Override |
---|
| 178 | public synchronized Collection<Collection<ITaskInstance>> getExecutionVariants() { |
---|
| 179 | if (executionVariants == null) { |
---|
| 180 | executionVariants = new LinkedList<Collection<ITaskInstance>>(); |
---|
| 181 | determineExecutionVariants(executionVariants); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | return executionVariants; |
---|
| 185 | } |
---|
| 186 | |
---|
| 187 | /* (non-Javadoc) |
---|
[1215] | 188 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#equals(ITask) |
---|
| 189 | */ |
---|
[557] | 190 | @Override
|
---|
[1146] | 191 | public final boolean equals(ITask task) { |
---|
| 192 | // tasks are only equal if they are identical or if they have the same id |
---|
| 193 | // (may happen, if they are cloned)
|
---|
| 194 | return (this == task) || (this.hashCode() == task.hashCode());
|
---|
[439] | 195 | }
|
---|
[557] | 196 |
|
---|
[1215] | 197 | /* (non-Javadoc) |
---|
| 198 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#hashCode() |
---|
| 199 | */ |
---|
[557] | 200 | @Override
|
---|
| 201 | public synchronized int hashCode() {
|
---|
[1146] | 202 | return id;
|
---|
[439] | 203 | }
|
---|
[557] | 204 |
|
---|
[1215] | 205 | /* (non-Javadoc)
|
---|
[557] | 206 | * @see java.lang.Object#toString()
|
---|
| 207 | */
|
---|
| 208 | @Override
|
---|
[1126] | 209 | public synchronized String toString() { |
---|
| 210 | StringBuffer result = new StringBuffer(); |
---|
[1357] | 211 | result.append(type); |
---|
| 212 | result.append(" #"); |
---|
[1126] | 213 | result.append(id); |
---|
| 214 | |
---|
| 215 | if (description != null) { |
---|
[1146] | 216 | result.append(" ("); |
---|
[1126] | 217 | result.append(description); |
---|
[1146] | 218 | result.append(')'); |
---|
[1126] | 219 | } |
---|
| 220 | |
---|
| 221 | return result.toString();
|
---|
[439] | 222 | }
|
---|
| 223 |
|
---|
[1215] | 224 | /* (non-Javadoc) |
---|
| 225 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#clone() |
---|
| 226 | */ |
---|
[557] | 227 | @Override
|
---|
[1146] | 228 | public synchronized ITask clone() {
|
---|
| 229 | Task clone = null;
|
---|
[557] | 230 | try {
|
---|
[1146] | 231 | clone = (Task) super.clone();
|
---|
[470] | 232 | }
|
---|
[557] | 233 | catch (CloneNotSupportedException e) {
|
---|
| 234 | // this should never happen. Therefore simply dump the exception
|
---|
| 235 | e.printStackTrace();
|
---|
| 236 | }
|
---|
| 237 |
|
---|
| 238 | return clone;
|
---|
[467] | 239 | }
|
---|
[439] | 240 |
|
---|
[1146] | 241 | /** |
---|
[1215] | 242 | * <p> |
---|
| 243 | * internally used to set the human readable description of the task |
---|
| 244 | * </p> |
---|
[1146] | 245 | * |
---|
[1215] | 246 | * @param description the new human readable description of the task |
---|
[1146] | 247 | */ |
---|
| 248 | void setDescription(String description) { |
---|
| 249 | this.description = description; |
---|
| 250 | } |
---|
| 251 | |
---|
[1294] | 252 | /** |
---|
| 253 | * <p> |
---|
[1357] | 254 | * internally used to remove an instance from this task |
---|
| 255 | * </p> |
---|
| 256 | * |
---|
| 257 | * @param instance the instance to be removed from this task |
---|
| 258 | */ |
---|
[1400] | 259 | synchronized void removeInstance(ITaskInstance instance) { |
---|
[1357] | 260 | this.instances.remove(instance); |
---|
[1400] | 261 | this.executionVariants = null; |
---|
[1357] | 262 | } |
---|
| 263 | |
---|
| 264 | /** |
---|
| 265 | * <p> |
---|
[1294] | 266 | * internally used to add an instance to this task |
---|
| 267 | * </p> |
---|
| 268 | * |
---|
| 269 | * @param instance the instance belonging to this task |
---|
| 270 | */ |
---|
[1400] | 271 | synchronized void addInstance(ITaskInstance instance) { |
---|
[1294] | 272 | this.instances.add(instance); |
---|
[1400] | 273 | this.executionVariants = null; |
---|
[1294] | 274 | } |
---|
| 275 | |
---|
[1157] | 276 | /* (non-Javadoc) |
---|
[1215] | 277 | * @see de.ugoe.cs.autoquest.tasktrees.treeifc.ITask#accept(ITaskVisitor) |
---|
[1157] | 278 | */ |
---|
| 279 | @Override |
---|
| 280 | public void accept(ITaskVisitor visitor) { |
---|
| 281 | visitor.visit(this); |
---|
| 282 | } |
---|
| 283 | |
---|
[1400] | 284 | /** |
---|
| 285 | * |
---|
| 286 | */ |
---|
| 287 | private void determineExecutionVariants(Collection<Collection<ITaskInstance>> executionVariants) |
---|
| 288 | { |
---|
| 289 | for (ITaskInstance instance : instances) { |
---|
| 290 | boolean added = false; |
---|
| 291 | for (Collection<ITaskInstance> variant : executionVariants) { |
---|
| 292 | if (!variant.isEmpty() && (isSameExecution(variant.iterator().next(), instance))) { |
---|
| 293 | variant.add(instance); |
---|
| 294 | added = true; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | |
---|
| 298 | if (!added) { |
---|
| 299 | Collection<ITaskInstance> variant = new HashSet<ITaskInstance>(); |
---|
| 300 | variant.add(instance); |
---|
| 301 | executionVariants.add(variant); |
---|
| 302 | } |
---|
| 303 | } |
---|
| 304 | } |
---|
| 305 | |
---|
| 306 | /** |
---|
| 307 | * |
---|
| 308 | */ |
---|
| 309 | private boolean isSameExecution(ITaskInstance instance1, ITaskInstance instance2) { |
---|
| 310 | if (instance1 instanceof IIterationInstance) { |
---|
| 311 | if (!(instance2 instanceof IIterationInstance)) { |
---|
| 312 | return false; |
---|
| 313 | } |
---|
| 314 | |
---|
| 315 | ITaskInstanceList iteration1 = (ITaskInstanceList) instance1; |
---|
| 316 | ITaskInstanceList iteration2 = (ITaskInstanceList) instance2; |
---|
| 317 | |
---|
| 318 | return isSameExecutionList(iteration1, iteration2); |
---|
| 319 | } |
---|
| 320 | else if (instance1 instanceof ISequenceInstance) { |
---|
| 321 | if (!(instance2 instanceof ISequenceInstance)) { |
---|
| 322 | return false; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | ITaskInstanceList selection1 = (ITaskInstanceList) instance1; |
---|
| 326 | ITaskInstanceList selection2 = (ITaskInstanceList) instance2; |
---|
| 327 | |
---|
| 328 | return isSameExecutionList(selection1, selection2); |
---|
| 329 | } |
---|
| 330 | else if (instance1 instanceof ISelectionInstance) { |
---|
| 331 | if (!(instance2 instanceof ISelectionInstance)) { |
---|
| 332 | return false; |
---|
| 333 | } |
---|
| 334 | else { |
---|
| 335 | return isSameExecution(((ISelectionInstance) instance1).getChild(), |
---|
| 336 | ((ISelectionInstance) instance2).getChild()); |
---|
| 337 | } |
---|
| 338 | } |
---|
| 339 | else if (instance1 instanceof IOptionalInstance) { |
---|
| 340 | if (!(instance2 instanceof IOptionalInstance)) { |
---|
| 341 | return false; |
---|
| 342 | } |
---|
| 343 | else { |
---|
| 344 | return isSameExecution(((IOptionalInstance) instance1).getChild(), |
---|
| 345 | ((IOptionalInstance) instance2).getChild()); |
---|
| 346 | } |
---|
| 347 | } |
---|
| 348 | else if (instance1 instanceof IEventTaskInstance) { |
---|
| 349 | if (!(instance2 instanceof IEventTaskInstance)) { |
---|
| 350 | return false; |
---|
| 351 | } |
---|
| 352 | else { |
---|
| 353 | return ((IEventTaskInstance) instance1).getTask().equals |
---|
| 354 | (((IEventTaskInstance) instance2).getTask()); |
---|
| 355 | } |
---|
| 356 | } |
---|
| 357 | else if (instance1 == null) { |
---|
| 358 | return instance2 == null; |
---|
| 359 | } |
---|
| 360 | else { |
---|
| 361 | throw new IllegalArgumentException("unknown type of task instance: " + instance1); |
---|
| 362 | } |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | /** |
---|
| 366 | * |
---|
| 367 | */ |
---|
| 368 | private boolean isSameExecutionList(ITaskInstanceList list1, ITaskInstanceList list2) { |
---|
| 369 | if (list1.size() == list2.size()) { |
---|
| 370 | for (int i = 0; i < list1.size(); i++) { |
---|
| 371 | if (!isSameExecution(list1.get(i), list2.get(i))) { |
---|
| 372 | return false; |
---|
| 373 | } |
---|
| 374 | } |
---|
| 375 | |
---|
| 376 | return true; |
---|
| 377 | } |
---|
| 378 | else { |
---|
| 379 | return false; |
---|
| 380 | } |
---|
| 381 | } |
---|
[439] | 382 | }
|
---|