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

Last change on this file since 1190 was 1190, checked in by pharms, 11 years ago
  • remove a find bugs warning
  • Property svn:executable set to *
File size: 5.7 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.ITask;
21
22/**
23 * <p>
24 * This rule is capable of comparing sequences. If both sequences do not have children, they are
25 * treated as lexically equal. Sequences are lexically equal, if they have the same number and
26 * order of lexically equal children. The rule can not decide, if two sequences are syntactically
27 * or semantically equal.
28 * </p>
29 *
30 * @version $Revision: $ $Date: 19.02.2012$
31 * @author 2012, last modified by $Author: patrick$
32 */
33public class SequenceComparisonRule implements TaskComparisonRule {
34
35    /* (non-Javadoc)
36     * @see NodeComparisonRule#isApplicable(ITask, ITask)
37     */
38    @Override
39    public boolean isApplicable(ITask task1, ITask task2) {
40        return (task1 instanceof ISequence) && (task2 instanceof ISequence);
41    }
42
43    /* (non-Javadoc)
44     * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask)
45     */
46    @Override
47    public boolean areLexicallyEqual(ITask task1, ITask task2) {
48        TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL);
49        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
50    }
51
52    /* (non-Javadoc)
53     * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask)
54     */
55    @Override
56    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
57        TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL);
58        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
59    }
60
61    /* (non-Javadoc)
62     * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask)
63     */
64    @Override
65    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
66        TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL);
67        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
68    }
69
70    /* (non-Javadoc)
71     * @see NodeComparisonRule#compare(ITask, ITask)
72     */
73    @Override
74    public TaskEquality compare(ITask task1, ITask task2) {
75        return getEquality(task1, task2, null);
76    }
77
78    /**
79     * <p>
80     * compares two sequences with each other checking for the provided required level of
81     * equality. If this level is ensured, the method immediately returns. The more concrete
82     * the required equality level, the more checks this method performs.
83     * </p>
84     *
85     * @param task1                 the first task to be compared
86     * @param task2                 the second task to be compared
87     * @param requiredEqualityLevel the equality level to be checked for
88     *
89     * @return the determined equality.
90     */
91    private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) {
92        List<ITask> children1 = ((ISequence) task1).getChildren();
93        List<ITask> children2 = ((ISequence) task2).getChildren();
94
95        // if both sequences do not have children, they are equal although this doesn't make sense
96        if ((children1.size() == 0) && (children2.size() == 0)) {
97            return TaskEquality.LEXICALLY_EQUAL;
98        }
99
100        if (children1.size() != children2.size()) {
101            return TaskEquality.UNEQUAL;
102        }
103
104        TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
105        for (int i = 0; i < children1.size(); i++) {
106            ITask child1 = children1.get(i);
107            ITask child2 = children2.get(i);
108
109            TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel);
110
111            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
112                return TaskEquality.UNEQUAL;
113            }
114           
115            resultingEquality = resultingEquality.getCommonDenominator(taskEquality);
116        }
117
118        return resultingEquality;
119    }
120
121    /**
122     * <p>
123     * used to to call the task equality rule manager for the comparison of the two provided
124     * children. If no required equality level is provided, than the most concrete equality is
125     * returned. Otherwise, the required equality is returned as long as the children are equal
126     * on that level.
127     * </p>
128     *
129     * @param child1                the first task to be compared
130     * @param child2                the second task to be compared
131     * @param requiredEqualityLevel the equality level to be checked for
132     *
133     * @return the determined equality
134     */
135    private TaskEquality callRuleManager(ITask        child1,
136                                         ITask        child2,
137                                         TaskEquality requiredEqualityLevel)
138    {
139        if (requiredEqualityLevel == null) {
140            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
141        }
142        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
143                    (child1, child2, requiredEqualityLevel))
144        {
145            return requiredEqualityLevel;
146        }
147        else {
148            return TaskEquality.UNEQUAL;
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.