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

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