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

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