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

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

Added automatically created javadoc, still needs to be commented properly though

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