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

Last change on this file 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.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// TODO: Auto-generated Javadoc
25/**
26 * <p>
27 * This rule is capable of comparing sequences. If both sequences do not have
28 * children, they are treated as lexically equal. Sequences are lexically equal,
29 * if they have the same number and order of lexically equal children. The rule
30 * can not decide, if two sequences are syntactically or semantically equal.
31 * </p>
32 *
33 * @author 2012, last modified by $Author: patrick$
34 * @version $Revision: $ $Date: 19.02.2012$
35 */
36public class SequenceComparisonRule implements TaskComparisonRule {
37
38        /*
39         * (non-Javadoc)
40         *
41         * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
42         */
43        @Override
44        public boolean areLexicallyEqual(ITask task1, ITask task2) {
45                final TaskEquality equality = getEquality(task1, task2,
46                                TaskEquality.LEXICALLY_EQUAL);
47                return (equality != null)
48                                && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
49        }
50
51        /*
52         * (non-Javadoc)
53         *
54         * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance)
55         */
56        @Override
57        public boolean areLexicallyEqual(ITaskInstance instance1,
58                        ITaskInstance instance2) {
59                final TaskEquality equality = getEquality(instance1, instance2,
60                                TaskEquality.LEXICALLY_EQUAL);
61                return (equality != null)
62                                && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
63        }
64
65        /*
66         * (non-Javadoc)
67         *
68         * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
69         */
70        @Override
71        public boolean areSemanticallyEqual(ITask task1, ITask task2) {
72                final TaskEquality equality = getEquality(task1, task2,
73                                TaskEquality.SEMANTICALLY_EQUAL);
74                return (equality != null)
75                                && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
76        }
77
78        /*
79         * (non-Javadoc)
80         *
81         * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance,
82         * ITaskInstance)
83         */
84        @Override
85        public boolean areSemanticallyEqual(ITaskInstance instance1,
86                        ITaskInstance instance2) {
87                final TaskEquality equality = getEquality(instance1, instance2,
88                                TaskEquality.SEMANTICALLY_EQUAL);
89                return (equality != null)
90                                && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
91        }
92
93        /*
94         * (non-Javadoc)
95         *
96         * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
97         */
98        @Override
99        public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
100                final TaskEquality equality = getEquality(task1, task2,
101                                TaskEquality.SYNTACTICALLY_EQUAL);
102                return (equality != null)
103                                && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
104        }
105
106        /*
107         * (non-Javadoc)
108         *
109         * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance,
110         * ITaskInstance)
111         */
112        @Override
113        public boolean areSyntacticallyEqual(ITaskInstance instance1,
114                        ITaskInstance instance2) {
115                final TaskEquality equality = getEquality(instance1, instance2,
116                                TaskEquality.SYNTACTICALLY_EQUAL);
117                return (equality != null)
118                                && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
119        }
120
121        /**
122         * <p>
123         * used to to call the task equality rule manager for the comparison of the
124         * two provided children. If no required equality level is provided, than
125         * the most concrete equality is returned. Otherwise, the required equality
126         * is returned as long as the children are equal on that level.
127         * </p>
128         *
129         * @param child1
130         *            the first task to be compared
131         * @param child2
132         *            the second task to be compared
133         * @param requiredEqualityLevel
134         *            the equality level to be checked for
135         *
136         * @return the determined equality
137         */
138        private TaskEquality callRuleManager(ITask child1, ITask child2,
139                        TaskEquality requiredEqualityLevel) {
140                if (requiredEqualityLevel == null) {
141                        return TaskEqualityRuleManager.getInstance()
142                                        .compare(child1, child2);
143                } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual(
144                                child1, child2, requiredEqualityLevel)) {
145                        return requiredEqualityLevel;
146                } else {
147                        return TaskEquality.UNEQUAL;
148                }
149        }
150
151        /**
152         * <p>
153         * used to to call the task equality rule manager for the comparison of the
154         * two provided children. If no required equality level is provided, than
155         * the most concrete equality is returned. Otherwise, the required equality
156         * is returned as long as the children are equal on that level.
157         * </p>
158         *
159         * @param taskInstance1
160         *            the first task instance to be compared
161         * @param taskInstance2
162         *            the second task instance to be compared
163         * @param requiredEqualityLevel
164         *            the equality level to be checked for
165         *
166         * @return the determined equality
167         */
168        private TaskEquality callRuleManager(ITaskInstance taskInstance1,
169                        ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) {
170                if (requiredEqualityLevel == null) {
171                        return TaskEqualityRuleManager.getInstance().compare(taskInstance1,
172                                        taskInstance2);
173                } else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual(
174                                taskInstance1, taskInstance2, requiredEqualityLevel)) {
175                        return requiredEqualityLevel;
176                } else {
177                        return TaskEquality.UNEQUAL;
178                }
179        }
180
181        /*
182         * (non-Javadoc)
183         *
184         * @see TaskComparisonRule#compare(ITask, ITask)
185         */
186        @Override
187        public TaskEquality compare(ITask task1, ITask task2) {
188                return getEquality(task1, task2, null);
189        }
190
191        /*
192         * (non-Javadoc)
193         *
194         * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance)
195         */
196        @Override
197        public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) {
198                return getEquality(instance1, instance2, null);
199        }
200
201        /**
202         * <p>
203         * compares two sequences with each other checking for the provided required
204         * level of equality. If this level is ensured, the method immediately
205         * returns. The more concrete the required equality level, the more checks
206         * this method performs.
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                final List<ITask> children1 = ((ISequence) task1).getChildren();
221                final List<ITask> children2 = ((ISequence) task2).getChildren();
222
223                // if both sequences do not have children, they are equal although this
224                // doesn't make sense
225                if ((children1.size() == 0) && (children2.size() == 0)) {
226                        return TaskEquality.LEXICALLY_EQUAL;
227                }
228
229                if (children1.size() != children2.size()) {
230                        return TaskEquality.UNEQUAL;
231                }
232
233                TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
234                for (int i = 0; i < children1.size(); i++) {
235                        final ITask child1 = children1.get(i);
236                        final ITask child2 = children2.get(i);
237
238                        final TaskEquality taskEquality = callRuleManager(child1, child2,
239                                        requiredEqualityLevel);
240
241                        if ((taskEquality == null)
242                                        || (taskEquality == TaskEquality.UNEQUAL)) {
243                                return TaskEquality.UNEQUAL;
244                        }
245
246                        resultingEquality = resultingEquality
247                                        .getCommonDenominator(taskEquality);
248                }
249
250                return resultingEquality;
251        }
252
253        /**
254         * <p>
255         * compares two sequence instances with each other checking for the provided
256         * required level of equality. If this level is ensured, the method
257         * immediately returns. The more concrete the required equality level, the
258         * more checks this method performs.
259         * </p>
260         *
261         * @param taskInstance1
262         *            the first task instance to be compared
263         * @param taskInstance2
264         *            the second task instance to be compared
265         * @param requiredEqualityLevel
266         *            the equality level to be checked for
267         *
268         * @return the determined equality.
269         */
270        private TaskEquality getEquality(ITaskInstance taskInstance1,
271                        ITaskInstance taskInstance2, TaskEquality requiredEqualityLevel) {
272                final ISequenceInstance sequence1 = (ISequenceInstance) taskInstance1;
273                final ISequenceInstance sequence2 = (ISequenceInstance) taskInstance2;
274
275                // if both sequences do not have children, they are equal although this
276                // doesn't make sense
277                if ((sequence1.size() == 0) && (sequence2.size() == 0)) {
278                        return TaskEquality.LEXICALLY_EQUAL;
279                }
280
281                if (sequence1.size() != sequence2.size()) {
282                        return TaskEquality.UNEQUAL;
283                }
284
285                TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
286                for (int i = 0; i < sequence1.size(); i++) {
287                        final ITaskInstance child1 = sequence1.get(i);
288                        final ITaskInstance child2 = sequence2.get(i);
289
290                        final TaskEquality taskEquality = callRuleManager(child1, child2,
291                                        requiredEqualityLevel);
292
293                        if ((taskEquality == null)
294                                        || (taskEquality == TaskEquality.UNEQUAL)) {
295                                return TaskEquality.UNEQUAL;
296                        }
297
298                        resultingEquality = resultingEquality
299                                        .getCommonDenominator(taskEquality);
300                }
301
302                return resultingEquality;
303        }
304
305        /*
306         * (non-Javadoc)
307         *
308         * @see TaskComparisonRule#isApplicable(ITask, ITask)
309         */
310        @Override
311        public boolean isApplicable(ITask task1, ITask task2) {
312                return (task1 instanceof ISequence) && (task2 instanceof ISequence);
313        }
314
315        /*
316         * (non-Javadoc)
317         *
318         * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance)
319         */
320        @Override
321        public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) {
322                return isApplicable(instance1.getTask(), instance2.getTask());
323        }
324}
Note: See TracBrowser for help on using the repository browser.