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
RevLine 
[1113]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
[1146]15package de.ugoe.cs.autoquest.tasktrees.taskequality;
[816]16
[1125]17import java.util.List;
18
[922]19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
[1146]20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
[816]21
22/**
23 * <p>
[1146]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
[816]28 * applies for syntactical and semantical equality.
29 * </p>
30
31 * @author Patrick Harms
32 */
[1146]33public class TaskAndSelectionComparisonRule implements TaskComparisonRule {
[816]34   
[1125]35    /* (non-Javadoc)
[1146]36     * @see NodeComparisonRule#isApplicable(ITask, ITask)
[816]37     */
38    @Override
[1146]39    public boolean isApplicable(ITask task1, ITask task2) {
40        return ((task1 instanceof ISelection) && (!(task2 instanceof ISelection))) ||
41               ((task2 instanceof ISelection) && (!(task1 instanceof ISelection)));
[1125]42    }
43
44    /* (non-Javadoc)
[1146]45     * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask)
[1125]46     */
47    @Override
[1146]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));
[1125]51    }
52
53    /* (non-Javadoc)
[1146]54     * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask)
[1125]55     */
56    @Override
[1146]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));
[1125]60    }
61
62    /* (non-Javadoc)
[1146]63     * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask)
[1125]64     */
65    @Override
[1146]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));
[1125]69    }
70
71    /* (non-Javadoc)
[1146]72     * @see NodeComparisonRule#compare(ITask, ITask)
[1125]73     */
74    @Override
[1146]75    public TaskEquality compare(ITask task1, ITask task2) {
76        return getEquality(task1, task2, null);
[1125]77    }
78   
79    /**
[1154]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>
[1125]86     *
[1154]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.
[1125]92     */
[1146]93    private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) {
[816]94        ISelection selection = null;
[1146]95        ITask task = null;
[816]96       
[1146]97        if (task1 instanceof ISelection) {
98            if (task2 instanceof ISelection) {
[1125]99                // the rule is not responsible for two selections
[816]100                return null;
101            }
102           
[1146]103            selection = (ISelection) task1;
104            task = task2;
[816]105        }
[1146]106        else if (task2 instanceof ISelection) {
107            if (task1 instanceof ISelection) {
[1125]108                // the rule is not responsible for two selections
[816]109                return null;
110            }
111           
[1146]112            selection = (ISelection) task2;
113            task = task1;
[816]114        }
115        else {
116            return null;
117        }
118
[1146]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();
[1125]122       
123        if (children.size() < 1) {
[816]124            return null;
125        }
126
[1146]127        TaskEquality mostConcreteNodeEquality = null;
[816]128       
[1146]129        for (ITask child : children) {
130            TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel);
[816]131           
[1146]132            if (taskEquality != TaskEquality.UNEQUAL) {
[816]133                if (mostConcreteNodeEquality == null) {
[1146]134                    mostConcreteNodeEquality = taskEquality;
[816]135                }
[1146]136                else if (mostConcreteNodeEquality.isAtLeast(taskEquality)) {
137                    mostConcreteNodeEquality = taskEquality;
[1125]138                   
[816]139                }
[1125]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
[1146]145                    // we can consider the selection to be sufficiently equal to the other task.
[1125]146                    // So we break up checking further children.
147                    break;
148                }
[816]149            }
150        }
151       
[1146]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;
[816]156        }
157        else {
158            return mostConcreteNodeEquality;
159        }
160
161    }
[1125]162   
163    /**
164     * <p>
[1154]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
[1125]176     */
[1146]177    private TaskEquality callRuleManager(ITask        child1,
178                                         ITask        child2,
179                                         TaskEquality requiredEqualityLevel)
[1125]180    {
181        if (requiredEqualityLevel == null) {
[1190]182            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
[1125]183        }
[1190]184        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
185                     (child1, child2, requiredEqualityLevel))
186        {
[1125]187            return requiredEqualityLevel;
188        }
189        else {
[1146]190            return TaskEquality.UNEQUAL;
[1125]191        }
192    }
[816]193}
Note: See TracBrowser for help on using the repository browser.