source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/TaskAndIterationComparisonRule.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: 6.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 de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
19
20/**
21 * <p>
22 * This class is capable of comparing any task which is not an iteration with an
23 * iteration. This is needed, because iterations may iterate exactly that task. In this
24 * case, the iteration would be equal to that task if it was executed exactly once. The rule
25 * returns lexically equal, it the child of the iteration is lexically equal to the task
26 * or if the child of the iteration is a selection and this selections contains a lexically equal
27 * task. The same applies for syntactical and semantical equality.
28 * </p>
29
30 * @author Patrick Harms
31 */
32public class TaskAndIterationComparisonRule implements TaskComparisonRule {
33   
34    /* (non-Javadoc)
35     * @see NodeComparisonRule#isApplicable(ITask, ITask)
36     */
37    @Override
38    public boolean isApplicable(ITask task1, ITask task2) {
39        return ((task1 instanceof IIteration) && (!(task2 instanceof IIteration))) ||
40               ((task2 instanceof IIteration) && (!(task1 instanceof IIteration)));
41    }
42
43    /* (non-Javadoc)
44     * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask)
45     */
46    @Override
47    public boolean areLexicallyEqual(ITask task1, ITask task2) {
48        TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL);
49        return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL));
50    }
51
52    /* (non-Javadoc)
53     * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask)
54     */
55    @Override
56    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
57        TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL);
58        return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL));
59    }
60
61    /* (non-Javadoc)
62     * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask)
63     */
64    @Override
65    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
66        TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL);
67        return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL));
68    }
69
70    /* (non-Javadoc)
71     * @see NodeComparisonRule#compare(ITask, ITask)
72     */
73    @Override
74    public TaskEquality compare(ITask task1, ITask task2) {
75        return getEquality(task1, task2, null);
76    }
77
78    /**
79     * <p>
80     * compares two tasks with each other checking for the provided required level of
81     * equality. One of the tasks must be an iteration, the other one not. If this is not the
82     * case, the method returns null. The returned equality level is at most lexical equality
83     * as the iteration can not be identical to something not being an iteration.
84     * </p>
85     *
86     * @param task1                 the first task to be compared
87     * @param task2                 the second task to be compared
88     * @param requiredEqualityLevel the equality level to be checked for
89     *
90     * @return the determined equality.
91     */
92    private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) {
93        IIteration iteration = null;
94        ITask task = null;
95       
96        if (task1 instanceof IIteration) {
97            if (task2 instanceof IIteration) {
98                // the rule is not responsible for two iterations
99                return null;
100            }
101           
102            iteration = (IIteration) task1;
103            task = task2;
104        }
105        else if (task2 instanceof IIteration) {
106            if (task1 instanceof IIteration) {
107                // the rule is not responsible for two iterations
108                return null;
109            }
110           
111            iteration = (IIteration) task2;
112            task = task1;
113        }
114        else {
115            return null;
116        }
117
118        ITask child = iteration.getMarkedTask();
119       
120        // now, that we found the iteration and the task, lets compare the child of the iteration
121        // with the task.
122        if (child == null) {
123            return null;
124        }
125
126        TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel);
127
128        // although the subtask may be identical to the task, we can not return identical, as
129        // the iteration is not identical to the task, but at most lexically equal
130        if (taskEquality == TaskEquality.IDENTICAL) {
131            return TaskEquality.LEXICALLY_EQUAL;
132        }
133        else {
134            return taskEquality;
135        }
136
137    }
138   
139    /**
140     * <p>
141     * used to to call the task equality rule manager for the comparison of the two provided
142     * children. If no required equality level is provided, than the most concrete equality is
143     * returned. Otherwise, the required equality is returned as long as the children are equal
144     * on that level.
145     * </p>
146     *
147     * @param child1                the first task to be compared
148     * @param child2                the second task to be compared
149     * @param requiredEqualityLevel the equality level to be checked for
150     *
151     * @return the determined equality
152     */
153    private TaskEquality callRuleManager(ITask        child1,
154                                         ITask        child2,
155                                         TaskEquality requiredEqualityLevel)
156    {
157        if (requiredEqualityLevel == null) {
158            return TaskEqualityRuleManager.getInstance().compare(child1, child2);
159        }
160        else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual
161                     (child1, child2, requiredEqualityLevel))
162        {
163            return requiredEqualityLevel;
164        }
165        else {
166            return TaskEquality.UNEQUAL;
167        }
168    }
169}
Note: See TracBrowser for help on using the repository browser.