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

Last change on this file since 1196 was 1190, checked in by pharms, 11 years ago
  • remove a find bugs warning
  • Property svn:executable set to *
File size: 10.5 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.ISelection;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
21
22/**
23 * <p>
24 * this task comparison rule is capable of comparing selections. If both selections do not have
25 * children, they are treated as lexically equal. If they have children, each child of both
26 * selections is compared to each child of the respective other selection. The resulting equality
27 * is the most concrete one of all these comparisons. I.e. if all children are at least lexically
28 * equal, then the selections are lexically equal. If all children are at least syntactically
29 * equal, then the selections are syntactically equal. If all children are at least semantically
30 * equal, then the selections are semantically equal. If only one of the selections has children,
31 * then the selections are unequal. The comparison is broken up, if only a specific equality is
32 * checked for and this equality is ensured.
33 * </p>
34 *
35 * @version $Revision: $ $Date: 19.02.2012$
36 * @author 2012, last modified by $Author: patrick$
37 */
38public class SelectionComparisonRule implements TaskComparisonRule {
39
40    /* (non-Javadoc)
41     * @see NodeComparisonRule#isApplicable(ITask, ITask)
42     */
43    @Override
44    public boolean isApplicable(ITask task1, ITask task2) {
45        return (task1 instanceof ISelection) && (task2 instanceof ISelection);
46    }
47
48    /* (non-Javadoc)
49     * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask)
50     */
51    @Override
52    public boolean areLexicallyEqual(ITask task1, ITask task2) {
53        TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL);
54        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
55    }
56
57    /* (non-Javadoc)
58     * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask)
59     */
60    @Override
61    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
62        TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL);
63        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
64    }
65
66    /* (non-Javadoc)
67     * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask)
68     */
69    @Override
70    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
71        TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL);
72        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
73    }
74
75    /* (non-Javadoc)
76     * @see NodeComparisonRule#compare(ITask, ITask)
77     */
78    @Override
79    public TaskEquality compare(ITask task1, ITask task2) {
80        return getEquality(task1, task2, null);
81    }
82
83    /**
84     * <p>
85     * compares two selections with each other checking for the provided required level of
86     * equality. If this level is ensured, the method immediately returns. The more concrete
87     * the required equality level, the more checks this method performs.
88     * </p>
89     *
90     * @param task1                 the first task to be compared
91     * @param task2                 the second task to be compared
92     * @param requiredEqualityLevel the equality level to be checked for
93     *
94     * @return the determined equality.
95     */
96    private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) {
97        List<ITask> children1 = ((ISelection) task1).getChildren();
98        List<ITask> children2 = ((ISelection) task2).getChildren();
99
100        // if both selections do not have children, they are lexically equal. If only one of them
101        // has children, they are unequal.
102        if ((children1.size() == 0) && (children2.size() == 0)) {
103            return TaskEquality.LEXICALLY_EQUAL;
104        }
105        else if ((children1.size() == 0) || (children2.size() == 0)) {
106            return TaskEquality.UNEQUAL;
107        }
108
109        TaskEquality selectionEquality;
110
111        if (requiredEqualityLevel == null) {
112            // calculate the common equality level for all children of both selections.
113            // do it in both directions to ensure commutative comparison
114            selectionEquality = getCommonEqualityLevel(children1, children2);
115            if (selectionEquality != TaskEquality.UNEQUAL) {
116                return selectionEquality.getCommonDenominator
117                    (getCommonEqualityLevel(children2, children1));
118            }
119            else {
120                return TaskEquality.UNEQUAL;
121            }
122        }
123        else {
124            // we are searching for a specific equality
125            if (checkEqualityLevel(children1, children2, requiredEqualityLevel) &&
126                checkEqualityLevel(children2, children1, requiredEqualityLevel))
127            {
128                return requiredEqualityLevel;
129            }
130            else {
131                return TaskEquality.UNEQUAL;
132            }
133        }
134    }
135
136    /**
137     * <p>
138     * determines the common equality level for all tasks in the first list compared to all
139     * tasks in the second list. If for one task in the first list, there is no equal task in the
140     * second list, the method return unequality.
141     * </p>
142     *
143     * @param children1 the first list to be compared
144     * @param children2 the second list to be compared
145     *
146     * @return the common task equality identified for all tasks in the first list with respect to
147     *         the second list
148     */
149    private TaskEquality getCommonEqualityLevel(List<ITask> children1, List<ITask> children2) {
150        TaskEquality listEquality = TaskEquality.LEXICALLY_EQUAL;
151       
152        TaskEquality childEquality;
153        TaskEquality currentEquality;
154        for (ITask child1 : children1) {
155            childEquality = null;
156            for (ITask child2 : children2) {
157                currentEquality = callRuleManager(child1, child2, null);
158                if ((currentEquality != null) && (currentEquality != TaskEquality.UNEQUAL)) {
159                    if (childEquality == null) {
160                        childEquality = currentEquality;
161                    }
162                    else {
163                        childEquality = childEquality.getCommonDenominator(currentEquality);
164                    }
165                   
166                    if (childEquality == TaskEquality.SEMANTICALLY_EQUAL) {
167                        // as we calculate only the common denominator, we can break up here for
168                        // the current child. We will not improve the denominator anymore
169                        break;
170                    }
171                }
172            }
173           
174            if (childEquality == null) {
175                // we did not find any child in the second list, that is equal to the searched
176                // child
177                return TaskEquality.UNEQUAL;
178            }
179            else {
180                listEquality = listEquality.getCommonDenominator(childEquality);
181            }
182        }
183
184        return listEquality;
185    }
186
187    /**
188     * <p>
189     * ensures for the two given lists, that for each task in the first list there is a task
190     * in the second list being on the given level equal to the task in the first list.
191     * </p>
192     *
193     * @param children1             the first list to be compared
194     * @param children2             the second list to be compared
195     * @param requiredEqualityLevel the equality level to be checked for
196     *
197     * @return true if each task in the first list has an equal task in the second list when
198     *         considering the given equality level, false else.
199     */
200    private boolean checkEqualityLevel(List<ITask>  children1,
201                                       List<ITask>  children2,
202                                       TaskEquality requiredEqualityLevel)
203    {
204        TaskEquality childEquality;
205        TaskEquality currentEquality;
206        for (ITask child1 : children1) {
207            childEquality = null;
208            for (ITask child2 : children2) {
209                currentEquality = callRuleManager(child1, child2, requiredEqualityLevel);
210                if ((currentEquality != null) && (currentEquality.isAtLeast(requiredEqualityLevel)))
211                {
212                    // we found at least one equal child with sufficient equality in the
213                    // second list. So be can break up for this child.
214                    childEquality = currentEquality;
215                    break;
216                }
217            }
218           
219            if (childEquality == null) {
220                // we did not find any child in the second list, that is equal to the searched
221                // child
222                return false;
223            }
224        }
225
226        // for all children, we found an equality
227        return true;
228    }
229
230    /**
231     * <p>
232     * used to to call the task equality rule manager for the comparison of the two provided
233     * children. If no required equality level is provided, than the most concrete equality is
234     * returned. Otherwise, the required equality is returned as long as the children are equal
235     * on that level.
236     * </p>
237     *
238     * @param child1                the first task to be compared
239     * @param child2                the second task to be compared
240     * @param requiredEqualityLevel the equality level to be checked for
241     *
242     * @return the determined equality
243     */
244    private TaskEquality callRuleManager(ITask        child1,
245                                         ITask        child2,
246                                         TaskEquality requiredEqualityLevel)
247    {
248        if (requiredEqualityLevel == null) {
249            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
250        }
251        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
252                     (child1, child2, requiredEqualityLevel))
253        {
254            return requiredEqualityLevel;
255        }
256        else {
257            return TaskEquality.UNEQUAL;
258        }
259    }
260
261}
Note: See TracBrowser for help on using the repository browser.