source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/treeimpl/Task.java @ 1733

Last change on this file since 1733 was 1733, checked in by rkrimmel, 10 years ago

Used Eclipse code cleanup

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