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

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

Used Eclipse code cleanup

File size: 11.4 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.taskequality;
16
17import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IIterationInstance;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
21
22/**
23 * <p>
24 * This class is capable of comparing any task which is not an iteration with an
25 * iteration. This is needed, because iterations may iterate exactly that task.
26 * In this case, the iteration would be equal to that task if it was executed
27 * exactly once. The rule returns lexically equal, it the child of the iteration
28 * is lexically equal to the task or if the child of the iteration is a
29 * selection and this selections contains a lexically equal task. The same
30 * applies for syntactical and semantical equality.
31 * </p>
32 *
33 * @author Patrick Harms
34 */
35public class TaskAndIterationComparisonRule implements TaskComparisonRule {
36
37        /*
38         * (non-Javadoc)
39         *
40         * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
41         */
42        @Override
43        public boolean areLexicallyEqual(ITask task1, ITask task2) {
44                final TaskEquality equality = getEquality(task1, task2,
45                                TaskEquality.LEXICALLY_EQUAL);
46                return (equality != null)
47                                && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
48        }
49
50        /*
51         * (non-Javadoc)
52         *
53         * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance)
54         */
55        @Override
56        public boolean areLexicallyEqual(ITaskInstance instance1,
57                        ITaskInstance instance2) {
58                final TaskEquality equality = getEquality(instance1, instance2,
59                                TaskEquality.LEXICALLY_EQUAL);
60                return (equality != null)
61                                && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
62        }
63
64        /*
65         * (non-Javadoc)
66         *
67         * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
68         */
69        @Override
70        public boolean areSemanticallyEqual(ITask task1, ITask task2) {
71                final TaskEquality equality = getEquality(task1, task2,
72                                TaskEquality.SEMANTICALLY_EQUAL);
73                return (equality != null)
74                                && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
75        }
76
77        /*
78         * (non-Javadoc)
79         *
80         * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance,
81         * ITaskInstance)
82         */
83        @Override
84        public boolean areSemanticallyEqual(ITaskInstance instance1,
85                        ITaskInstance instance2) {
86                final TaskEquality equality = getEquality(instance1, instance2,
87                                TaskEquality.SEMANTICALLY_EQUAL);
88                return (equality != null)
89                                && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
90        }
91
92        /*
93         * (non-Javadoc)
94         *
95         * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
96         */
97        @Override
98        public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
99                final TaskEquality equality = getEquality(task1, task2,
100                                TaskEquality.SYNTACTICALLY_EQUAL);
101                return (equality != null)
102                                && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
103        }
104
105        /*
106         * (non-Javadoc)
107         *
108         * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance,
109         * ITaskInstance)
110         */
111        @Override
112        public boolean areSyntacticallyEqual(ITaskInstance instance1,
113                        ITaskInstance instance2) {
114                final TaskEquality equality = getEquality(instance1, instance2,
115                                TaskEquality.SYNTACTICALLY_EQUAL);
116                return (equality != null)
117                                && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
118        }
119
120        /**
121         * <p>
122         * used to to call the task equality rule manager for the comparison of the
123         * two provided children. If no required equality level is provided, than
124         * the most concrete equality is returned. Otherwise, the required equality
125         * is returned as long as the children are equal on that level.
126         * </p>
127         *
128         * @param child1
129         *            the first task to be compared
130         * @param child2
131         *            the second task to be compared
132         * @param requiredEqualityLevel
133         *            the equality level to be checked for
134         *
135         * @return the determined equality
136         */
137        private TaskEquality callRuleManager(ITask child1, ITask child2,
138                        TaskEquality requiredEqualityLevel) {
139                if (requiredEqualityLevel == null) {
140                        return TaskEqualityRuleManager.getInstance()
141                                        .compare(child1, child2);
142                } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual(
143                                child1, child2, requiredEqualityLevel)) {
144                        return requiredEqualityLevel;
145                } else {
146                        return TaskEquality.UNEQUAL;
147                }
148        }
149
150        /**
151         * <p>
152         * used to to call the task equality rule manager for the comparison of the
153         * two provided children. If no required equality level is provided, than
154         * the most concrete equality is returned. Otherwise, the required equality
155         * is returned as long as the children are equal on that level.
156         * </p>
157         *
158         * @param taskInstance1
159         *            the first task instance to be compared
160         * @param taskInstance2
161         *            the second task instance to be compared
162         * @param requiredEqualityLevel
163         *            the equality level to be checked for
164         *
165         * @return the determined equality
166         */
167        private TaskEquality callRuleManager(ITaskInstance taskInstance1,
168                        ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) {
169                if (requiredEqualityLevel == null) {
170                        return TaskEqualityRuleManager.getInstance().compare(taskInstance1,
171                                        taskInstance2);
172                } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual(
173                                taskInstance1, taskInstance2, requiredEqualityLevel)) {
174                        return requiredEqualityLevel;
175                } else {
176                        return TaskEquality.UNEQUAL;
177                }
178        }
179
180        /*
181         * (non-Javadoc)
182         *
183         * @see TaskComparisonRule#compare(ITask, ITask)
184         */
185        @Override
186        public TaskEquality compare(ITask task1, ITask task2) {
187                return getEquality(task1, task2, null);
188        }
189
190        /*
191         * (non-Javadoc)
192         *
193         * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance)
194         */
195        @Override
196        public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) {
197                return getEquality(instance1, instance2, null);
198        }
199
200        /**
201         * <p>
202         * compares two tasks with each other checking for the provided required
203         * level of equality. One of the tasks must be an iteration, the other one
204         * not. If this is not the case, the method returns null. The returned
205         * equality level is at most lexical equality as the iteration can not be
206         * identical to something not being an iteration.
207         * </p>
208         *
209         * @param task1
210         *            the first task to be compared
211         * @param task2
212         *            the second task to be compared
213         * @param requiredEqualityLevel
214         *            the equality level to be checked for
215         *
216         * @return the determined equality.
217         */
218        private TaskEquality getEquality(ITask task1, ITask task2,
219                        TaskEquality requiredEqualityLevel) {
220                IIteration iteration = null;
221                ITask task = null;
222
223                if (task1 instanceof IIteration) {
224                        if (task2 instanceof IIteration) {
225                                // the rule is not responsible for two iterations
226                                return null;
227                        }
228
229                        iteration = (IIteration) task1;
230                        task = task2;
231                } else if (task2 instanceof IIteration) {
232                        if (task1 instanceof IIteration) {
233                                // the rule is not responsible for two iterations
234                                return null;
235                        }
236
237                        iteration = (IIteration) task2;
238                        task = task1;
239                } else {
240                        return null;
241                }
242
243                final ITask child = iteration.getMarkedTask();
244
245                // now, that we found the iteration and the task, lets compare the child
246                // of the iteration
247                // with the task.
248                if (child == null) {
249                        return null;
250                }
251
252                final TaskEquality taskEquality = callRuleManager(child, task,
253                                requiredEqualityLevel);
254
255                // although the subtask may be identical to the task, we can not return
256                // identical, as
257                // the iteration is not identical to the task, but at most lexically
258                // equal
259                if (taskEquality == TaskEquality.IDENTICAL) {
260                        return TaskEquality.LEXICALLY_EQUAL;
261                } else {
262                        return taskEquality;
263                }
264
265        }
266
267        /**
268         * <p>
269         * compares two task instances with each other checking for the provided
270         * required level of equality. One of the task instances must be a
271         * iteration, the other one not. If this is not the case, the method returns
272         * null. The returned equality level is at most lexical equality as the
273         * iteration can not be identical to something not being a iteration.
274         * </p>
275         *
276         * @param taskInstance1
277         *            the first task instance to be compared
278         * @param taskInstance2
279         *            the second task instance to be compared
280         * @param requiredEqualityLevel
281         *            the equality level to be checked for
282         *
283         * @return the determined equality.
284         */
285        private TaskEquality getEquality(ITaskInstance taskInstance1,
286                        ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) {
287                IIterationInstance iteration = null;
288                ITaskInstance task = null;
289
290                if (taskInstance1 instanceof IIterationInstance) {
291                        if (taskInstance2 instanceof IIterationInstance) {
292                                // the rule is not responsible for two iterations
293                                return null;
294                        }
295
296                        iteration = (IIterationInstance) taskInstance1;
297                        task = taskInstance2;
298                } else if (taskInstance2 instanceof IIterationInstance) {
299                        if (taskInstance1 instanceof IIterationInstance) {
300                                // the rule is not responsible for two iterations
301                                return null;
302                        }
303
304                        iteration = (IIterationInstance) taskInstance2;
305                        task = taskInstance1;
306                } else {
307                        return null;
308                }
309
310                // now, that we found the iteration and the task, lets compare the
311                // children of the iteration
312                // with the task.
313
314                if (iteration.size() < 1) {
315                        return null;
316                }
317
318                TaskEquality mostConcreteNodeEquality = null;
319
320                for (final ITaskInstance child : iteration) {
321                        final TaskEquality taskEquality = callRuleManager(child, task,
322                                        requiredEqualityLevel);
323
324                        if (taskEquality != TaskEquality.UNEQUAL) {
325                                if (mostConcreteNodeEquality == null) {
326                                        mostConcreteNodeEquality = taskEquality;
327                                } else if (mostConcreteNodeEquality.isAtLeast(taskEquality)) {
328                                        mostConcreteNodeEquality = taskEquality;
329
330                                }
331
332                                if ((requiredEqualityLevel != null)
333                                                && (mostConcreteNodeEquality
334                                                                .isAtLeast(requiredEqualityLevel))) {
335                                        // if we found one child of the selection that is as equal
336                                        // as required, then
337                                        // we can consider the selection to be sufficiently equal to
338                                        // the other task.
339                                        // So we break up checking further children.
340                                        break;
341                                }
342                        }
343                }
344
345                // although the subtask may be identical to the task, we can not return
346                // identical, as
347                // the selection is not identical to the task, but at most lexically
348                // equal
349                if (mostConcreteNodeEquality == TaskEquality.IDENTICAL) {
350                        return TaskEquality.LEXICALLY_EQUAL;
351                } else {
352                        return mostConcreteNodeEquality;
353                }
354
355        }
356
357        /*
358         * (non-Javadoc)
359         *
360         * @see TaskComparisonRule#isApplicable(ITask, ITask)
361         */
362        @Override
363        public boolean isApplicable(ITask task1, ITask task2) {
364                return ((task1 instanceof IIteration) && (!(task2 instanceof IIteration)))
365                                || ((task2 instanceof IIteration) && (!(task1 instanceof IIteration)));
366        }
367
368        /*
369         * (non-Javadoc)
370         *
371         * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance)
372         */
373        @Override
374        public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) {
375                return isApplicable(instance1.getTask(), instance2.getTask());
376        }
377}
Note: See TracBrowser for help on using the repository browser.