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

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