source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SequenceComparisonRule.java @ 1887

Last change on this file since 1887 was 1887, checked in by pharms, 9 years ago
  • extended and corrected task comparison
  • Property svn:executable set to *
File size: 11.1 KB
RevLine 
[1113]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
[1146]15package de.ugoe.cs.autoquest.tasktrees.taskequality;
[439]16
[1125]17import java.util.List;
18
[922]19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
[1294]20import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequenceInstance;
[1146]21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
[1294]22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
[439]23
24/**
[557]25 * <p>
26 * This rule is capable of comparing sequences. If both sequences do not have children, they are
27 * treated as lexically equal. Sequences are lexically equal, if they have the same number and
28 * order of lexically equal children. The rule can not decide, if two sequences are syntactically
29 * or semantically equal.
30 * </p>
[439]31 *
32 * @version $Revision: $ $Date: 19.02.2012$
33 * @author 2012, last modified by $Author: patrick$
34 */
[1146]35public class SequenceComparisonRule implements TaskComparisonRule {
[439]36
[1125]37    /* (non-Javadoc)
[1294]38     * @see TaskComparisonRule#isApplicable(ITask, ITask)
[557]39     */
40    @Override
[1146]41    public boolean isApplicable(ITask task1, ITask task2) {
42        return (task1 instanceof ISequence) && (task2 instanceof ISequence);
[1125]43    }
44
45    /* (non-Javadoc)
[1294]46     * @see TaskComparisonRule#areLexicallyEqual(ITask, ITask)
[1125]47     */
48    @Override
[1146]49    public boolean areLexicallyEqual(ITask task1, ITask task2) {
50        TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL);
51        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
[1125]52    }
53
54    /* (non-Javadoc)
[1294]55     * @see TaskComparisonRule#areSyntacticallyEqual(ITask, ITask)
[1125]56     */
57    @Override
[1146]58    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
59        TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL);
60        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
[1125]61    }
62
63    /* (non-Javadoc)
[1294]64     * @see TaskComparisonRule#areSemanticallyEqual(ITask, ITask)
[1125]65     */
66    @Override
[1146]67    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
68        TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL);
69        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
[1125]70    }
71
72    /* (non-Javadoc)
[1294]73     * @see TaskComparisonRule#compare(ITask, ITask)
[1125]74     */
75    @Override
[1146]76    public TaskEquality compare(ITask task1, ITask task2) {
77        return getEquality(task1, task2, null);
[1125]78    }
[439]79
[1294]80    /* (non-Javadoc)
81     * @see TaskComparisonRule#isApplicable(ITaskInstance, ITaskInstance)
82     */
83    @Override
84    public boolean isApplicable(ITaskInstance instance1, ITaskInstance instance2) {
85        return isApplicable(instance1.getTask(), instance2.getTask());
86    }
87
88    /* (non-Javadoc)
89     * @see TaskComparisonRule#areLexicallyEqual(ITaskInstance, ITaskInstance)
90     */
91    @Override
92    public boolean areLexicallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
93        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.LEXICALLY_EQUAL);
94        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
95    }
96
97    /* (non-Javadoc)
98     * @see TaskComparisonRule#areSyntacticallyEqual(ITaskInstance, ITaskInstance)
99     */
100    @Override
101    public boolean areSyntacticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
102        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SYNTACTICALLY_EQUAL);
103        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
104    }
105
106    /* (non-Javadoc)
107     * @see TaskComparisonRule#areSemanticallyEqual(ITaskInstance, ITaskInstance)
108     */
109    @Override
110    public boolean areSemanticallyEqual(ITaskInstance instance1, ITaskInstance instance2) {
111        TaskEquality equality = getEquality(instance1, instance2, TaskEquality.SEMANTICALLY_EQUAL);
112        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
113    }
114
115    /* (non-Javadoc)
116     * @see TaskComparisonRule#compare(ITaskInstance, ITaskInstance)
117     */
118    @Override
119    public TaskEquality compare(ITaskInstance instance1, ITaskInstance instance2) {
120        return getEquality(instance1, instance2, null);
121    }
122
[1125]123    /**
[1154]124     * <p>
125     * compares two sequences with each other checking for the provided required level of
126     * equality. If this level is ensured, the method immediately returns. The more concrete
127     * the required equality level, the more checks this method performs.
128     * </p>
[1125]129     *
[1154]130     * @param task1                 the first task to be compared
131     * @param task2                 the second task to be compared
132     * @param requiredEqualityLevel the equality level to be checked for
133     *
134     * @return the determined equality.
[1125]135     */
[1146]136    private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) {
137        List<ITask> children1 = ((ISequence) task1).getChildren();
138        List<ITask> children2 = ((ISequence) task2).getChildren();
[807]139
[557]140        // if both sequences do not have children, they are equal although this doesn't make sense
[1125]141        if ((children1.size() == 0) && (children2.size() == 0)) {
[1146]142            return TaskEquality.LEXICALLY_EQUAL;
[557]143        }
144
[1125]145        if (children1.size() != children2.size()) {
[1146]146            return TaskEquality.UNEQUAL;
[557]147        }
148
[1887]149        TaskEquality resultingEquality = requiredEqualityLevel != null ?
150            requiredEqualityLevel : TaskEquality.LEXICALLY_EQUAL;
151       
[1125]152        for (int i = 0; i < children1.size(); i++) {
[1146]153            ITask child1 = children1.get(i);
154            ITask child2 = children2.get(i);
[557]155
[1887]156            // it is sufficient to check for the current at most achievable equality, i.e.,
157            // the so far resulting equality
158            TaskEquality taskEquality = callRuleManager(child1, child2, resultingEquality);
[557]159
[1146]160            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
161                return TaskEquality.UNEQUAL;
[557]162            }
[807]163           
[1146]164            resultingEquality = resultingEquality.getCommonDenominator(taskEquality);
[557]165        }
166
[807]167        return resultingEquality;
[439]168    }
169
[1125]170    /**
171     * <p>
[1154]172     * used to to call the task equality rule manager for the comparison of the two provided
173     * children. If no required equality level is provided, than the most concrete equality is
174     * returned. Otherwise, the required equality is returned as long as the children are equal
175     * on that level.
176     * </p>
177     *
178     * @param child1                the first task to be compared
179     * @param child2                the second task to be compared
180     * @param requiredEqualityLevel the equality level to be checked for
181     *
182     * @return the determined equality
[1125]183     */
[1146]184    private TaskEquality callRuleManager(ITask        child1,
185                                         ITask        child2,
186                                         TaskEquality requiredEqualityLevel)
[1125]187    {
188        if (requiredEqualityLevel == null) {
[1190]189            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
[1125]190        }
[1190]191        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
192                    (child1, child2, requiredEqualityLevel))
193        {
[1125]194            return requiredEqualityLevel;
195        }
196        else {
[1146]197            return TaskEquality.UNEQUAL;
[1125]198        }
199    }
[1294]200
201    /**
202     * <p>
203     * compares two sequence instances with each other checking for the provided required level of
204     * equality. If this level is ensured, the method immediately returns. The more concrete
205     * the required equality level, the more checks this method performs.
206     * </p>
207     *
208     * @param taskInstance1         the first task instance to be compared
209     * @param taskInstance2         the second task instance to be compared
210     * @param requiredEqualityLevel the equality level to be checked for
211     *
212     * @return the determined equality.
213     */
214    private TaskEquality getEquality(ITaskInstance taskInstance1,
215                                     ITaskInstance taskInstance2,
216                                     TaskEquality  requiredEqualityLevel)
217    {
218        ISequenceInstance sequence1 = (ISequenceInstance) taskInstance1;
219        ISequenceInstance sequence2 = (ISequenceInstance) taskInstance2;
220
221        // if both sequences do not have children, they are equal although this doesn't make sense
222        if ((sequence1.size() == 0) && (sequence2.size() == 0)) {
223            return TaskEquality.LEXICALLY_EQUAL;
224        }
225
226        if (sequence1.size() != sequence2.size()) {
227            return TaskEquality.UNEQUAL;
228        }
229
[1887]230        TaskEquality resultingEquality = requiredEqualityLevel != null ?
231            requiredEqualityLevel : TaskEquality.LEXICALLY_EQUAL;
232       
[1294]233        for (int i = 0; i < sequence1.size(); i++) {
234            ITaskInstance child1 = sequence1.get(i);
235            ITaskInstance child2 = sequence2.get(i);
236
[1887]237            // it is sufficient to check for the current at most achievable equality, i.e.,
238            // the so far resulting equality
239            TaskEquality taskEquality = callRuleManager(child1, child2, resultingEquality);
[1294]240
241            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
242                return TaskEquality.UNEQUAL;
243            }
244           
245            resultingEquality = resultingEquality.getCommonDenominator(taskEquality);
246        }
247
248        return resultingEquality;
249    }
250   
251    /**
252     * <p>
253     * used to to call the task equality rule manager for the comparison of the two provided
254     * children. If no required equality level is provided, than the most concrete equality is
255     * returned. Otherwise, the required equality is returned as long as the children are equal
256     * on that level.
257     * </p>
258     *
259     * @param taskInstance1         the first task instance to be compared
260     * @param taskInstance2         the second task instance to be compared
261     * @param requiredEqualityLevel the equality level to be checked for
262     *
263     * @return the determined equality
264     */
265    private TaskEquality callRuleManager(ITaskInstance taskInstance1,
266                                         ITaskInstance taskInstance2,
267                                         TaskEquality  requiredEqualityLevel)
268    {
269        if (requiredEqualityLevel == null) {
270            return TaskEqualityRuleManager.getInstance().compare(taskInstance1, taskInstance2);
271        }
272        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
273                     (taskInstance1, taskInstance2, requiredEqualityLevel))
274        {
275            return requiredEqualityLevel;
276        }
277        else {
278            return TaskEquality.UNEQUAL;
279        }
280    }
[439]281}
Note: See TracBrowser for help on using the repository browser.