source: branches/autoquest-core-tasktrees-alignment/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SequenceComparisonRule.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.9 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 java.util.List;
18
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
23
24/**
25 * <p>
26 * This rule is capable of comparing sequences. If both sequences do not have
27 * children, they are treated as lexically equal. Sequences are lexically equal,
28 * if they have the same number and order of lexically equal children. The rule
29 * can not decide, if two sequences are syntactically or semantically equal.
30 * </p>
31 *
32 * @version $Revision: $ $Date: 19.02.2012$
33 * @author 2012, last modified by $Author: patrick$
34 */
35public class SequenceComparisonRule 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 sequences with each other checking for the provided required
203         * level of equality. If this level is ensured, the method immediately
204         * returns. The more concrete the required equality level, the more checks
205         * this method performs.
206         * </p>
207         *
208         * @param task1
209         *            the first task to be compared
210         * @param task2
211         *            the second task to be compared
212         * @param requiredEqualityLevel
213         *            the equality level to be checked for
214         *
215         * @return the determined equality.
216         */
217        private TaskEquality getEquality(ITask task1, ITask task2,
218                        TaskEquality requiredEqualityLevel) {
219                final List<ITask> children1 = ((ISequence) task1).getChildren();
220                final List<ITask> children2 = ((ISequence) task2).getChildren();
221
222                // if both sequences do not have children, they are equal although this
223                // doesn't make sense
224                if ((children1.size() == 0) && (children2.size() == 0)) {
225                        return TaskEquality.LEXICALLY_EQUAL;
226                }
227
228                if (children1.size() != children2.size()) {
229                        return TaskEquality.UNEQUAL;
230                }
231
232                TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
233                for (int i = 0; i < children1.size(); i++) {
234                        final ITask child1 = children1.get(i);
235                        final ITask child2 = children2.get(i);
236
237                        final TaskEquality taskEquality = callRuleManager(child1, child2,
238                                        requiredEqualityLevel);
239
240                        if ((taskEquality == null)
241                                        || (taskEquality == TaskEquality.UNEQUAL)) {
242                                return TaskEquality.UNEQUAL;
243                        }
244
245                        resultingEquality = resultingEquality
246                                        .getCommonDenominator(taskEquality);
247                }
248
249                return resultingEquality;
250        }
251
252        /**
253         * <p>
254         * compares two sequence instances with each other checking for the provided
255         * required level of equality. If this level is ensured, the method
256         * immediately returns. The more concrete the required equality level, the
257         * more checks this method performs.
258         * </p>
259         *
260         * @param taskInstance1
261         *            the first task instance to be compared
262         * @param taskInstance2
263         *            the second task instance to be compared
264         * @param requiredEqualityLevel
265         *            the equality level to be checked for
266         *
267         * @return the determined equality.
268         */
269        private TaskEquality getEquality(ITaskInstance taskInstance1,
270                        ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) {
271                final ISequenceInstance sequence1 = (ISequenceInstance) taskInstance1;
272                final ISequenceInstance sequence2 = (ISequenceInstance) taskInstance2;
273
274                // if both sequences do not have children, they are equal although this
275                // doesn't make sense
276                if ((sequence1.size() == 0) && (sequence2.size() == 0)) {
277                        return TaskEquality.LEXICALLY_EQUAL;
278                }
279
280                if (sequence1.size() != sequence2.size()) {
281                        return TaskEquality.UNEQUAL;
282                }
283
284                TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
285                for (int i = 0; i < sequence1.size(); i++) {
286                        final ITaskInstance child1 = sequence1.get(i);
287                        final ITaskInstance child2 = sequence2.get(i);
288
289                        final TaskEquality taskEquality = callRuleManager(child1, child2,
290                                        requiredEqualityLevel);
291
292                        if ((taskEquality == null)
293                                        || (taskEquality == TaskEquality.UNEQUAL)) {
294                                return TaskEquality.UNEQUAL;
295                        }
296
297                        resultingEquality = resultingEquality
298                                        .getCommonDenominator(taskEquality);
299                }
300
301                return resultingEquality;
302        }
303
304        /*
305         * (non-Javadoc)
306         *
307         * @see TaskComparisonRule#isApplicable(ITask, ITask)
308         */
309        @Override
310        public boolean isApplicable(ITask task1, ITask task2) {
311                return (task1 instanceof ISequence) && (task2 instanceof ISequence);
312        }
313
314        /*
315         * (non-Javadoc)
316         *
317         * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance)
318         */
319        @Override
320        public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) {
321                return isApplicable(instance1.getTask(), instance2.getTask());
322        }
323}
Note: See TracBrowser for help on using the repository browser.