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

Last change on this file since 1190 was 1190, checked in by pharms, 11 years ago
  • remove a find bugs warning
File size: 7.3 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 class is capable of comparing any task which is not a selection with a
25 * selection. This is needed, because selections may contain exactly that task. Therefore, if
26 * this task is selected out of a selection the selection is equal to the task itself.
27 * The rule returns lexically equal, if the selection contains a lexically equal task. The same
28 * applies for syntactical and semantical equality.
29 * </p>
30
31 * @author Patrick Harms
32 */
33public class TaskAndSelectionComparisonRule 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 ISelection) && (!(task2 instanceof ISelection))) ||
41               ((task2 instanceof ISelection) && (!(task1 instanceof ISelection)));
42    }
43
44    /* (non-Javadoc)
45     * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask)
46     */
47    @Override
48    public boolean areLexicallyEqual(ITask task1, ITask task2) {
49        TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL);
50        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
51    }
52
53    /* (non-Javadoc)
54     * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask)
55     */
56    @Override
57    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
58        TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL);
59        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
60    }
61
62    /* (non-Javadoc)
63     * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask)
64     */
65    @Override
66    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
67        TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL);
68        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
69    }
70
71    /* (non-Javadoc)
72     * @see NodeComparisonRule#compare(ITask, ITask)
73     */
74    @Override
75    public TaskEquality compare(ITask task1, ITask task2) {
76        return getEquality(task1, task2, null);
77    }
78   
79    /**
80     * <p>
81     * compares two tasks with each other checking for the provided required level of
82     * equality. One of the tasks must be a selection, the other one not. If this is not the
83     * case, the method returns null. The returned equality level is at most lexical equality
84     * as the selection can not be identical to something not being a selection.
85     * </p>
86     *
87     * @param task1                 the first task to be compared
88     * @param task2                 the second task to be compared
89     * @param requiredEqualityLevel the equality level to be checked for
90     *
91     * @return the determined equality.
92     */
93    private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) {
94        ISelection selection = null;
95        ITask task = null;
96       
97        if (task1 instanceof ISelection) {
98            if (task2 instanceof ISelection) {
99                // the rule is not responsible for two selections
100                return null;
101            }
102           
103            selection = (ISelection) task1;
104            task = task2;
105        }
106        else if (task2 instanceof ISelection) {
107            if (task1 instanceof ISelection) {
108                // the rule is not responsible for two selections
109                return null;
110            }
111           
112            selection = (ISelection) task2;
113            task = task1;
114        }
115        else {
116            return null;
117        }
118
119        // now, that we found the selection and the task, lets compare the children of the selection
120        // with the task.
121        List<ITask> children = selection.getChildren();
122       
123        if (children.size() < 1) {
124            return null;
125        }
126
127        TaskEquality mostConcreteNodeEquality = null;
128       
129        for (ITask child : children) {
130            TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel);
131           
132            if (taskEquality != TaskEquality.UNEQUAL) {
133                if (mostConcreteNodeEquality == null) {
134                    mostConcreteNodeEquality = taskEquality;
135                }
136                else if (mostConcreteNodeEquality.isAtLeast(taskEquality)) {
137                    mostConcreteNodeEquality = taskEquality;
138                   
139                }
140               
141                if ((requiredEqualityLevel != null) &&
142                    (mostConcreteNodeEquality.isAtLeast(requiredEqualityLevel)))
143                {
144                    // if we found one child of the selection that is as equal as required, then
145                    // we can consider the selection to be sufficiently equal to the other task.
146                    // So we break up checking further children.
147                    break;
148                }
149            }
150        }
151       
152        // although the subtask may be identical to the task, we can not return identical, as
153        // the selection is not identical to the task, but at most lexically equal
154        if (mostConcreteNodeEquality == TaskEquality.IDENTICAL) {
155            return TaskEquality.LEXICALLY_EQUAL;
156        }
157        else {
158            return mostConcreteNodeEquality;
159        }
160
161    }
162   
163    /**
164     * <p>
165     * used to to call the task equality rule manager for the comparison of the two provided
166     * children. If no required equality level is provided, than the most concrete equality is
167     * returned. Otherwise, the required equality is returned as long as the children are equal
168     * on that level.
169     * </p>
170     *
171     * @param child1                the first task to be compared
172     * @param child2                the second task to be compared
173     * @param requiredEqualityLevel the equality level to be checked for
174     *
175     * @return the determined equality
176     */
177    private TaskEquality callRuleManager(ITask        child1,
178                                         ITask        child2,
179                                         TaskEquality requiredEqualityLevel)
180    {
181        if (requiredEqualityLevel == null) {
182            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
183        }
184        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
185                     (child1, child2, requiredEqualityLevel))
186        {
187            return requiredEqualityLevel;
188        }
189        else {
190            return TaskEquality.UNEQUAL;
191        }
192    }
193}
Note: See TracBrowser for help on using the repository browser.