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 | |
---|
15 | package de.ugoe.cs.autoquest.tasktrees.taskequality; |
---|
16 | |
---|
17 | import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration; |
---|
18 | import 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 | */ |
---|
32 | public 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 | * |
---|
96 | */ |
---|
97 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
98 | IIteration iteration = null; |
---|
99 | ITask task = null; |
---|
100 | |
---|
101 | if (task1 instanceof IIteration) { |
---|
102 | if (task2 instanceof IIteration) { |
---|
103 | // the rule is not responsible for two iterations |
---|
104 | return null; |
---|
105 | } |
---|
106 | |
---|
107 | iteration = (IIteration) task1; |
---|
108 | task = task2; |
---|
109 | } |
---|
110 | else if (task2 instanceof IIteration) { |
---|
111 | if (task1 instanceof IIteration) { |
---|
112 | // the rule is not responsible for two iterations |
---|
113 | return null; |
---|
114 | } |
---|
115 | |
---|
116 | iteration = (IIteration) task2; |
---|
117 | task = task1; |
---|
118 | } |
---|
119 | else { |
---|
120 | return null; |
---|
121 | } |
---|
122 | |
---|
123 | ITask child = iteration.getMarkedTask(); |
---|
124 | |
---|
125 | // now, that we found the iteration and the task, lets compare the child of the iteration |
---|
126 | // with the task. |
---|
127 | if (child == null) { |
---|
128 | return null; |
---|
129 | } |
---|
130 | |
---|
131 | TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel); |
---|
132 | |
---|
133 | // although the subtask may be identical to the task, we can not return identical, as |
---|
134 | // the iteration is not identical to the task, but at most lexically equal |
---|
135 | if (taskEquality == TaskEquality.IDENTICAL) { |
---|
136 | return TaskEquality.LEXICALLY_EQUAL; |
---|
137 | } |
---|
138 | else { |
---|
139 | return taskEquality; |
---|
140 | } |
---|
141 | |
---|
142 | } |
---|
143 | |
---|
144 | /** |
---|
145 | * <p> |
---|
146 | * TODO: comment |
---|
147 | * </p> |
---|
148 | * |
---|
149 | * @param child1 |
---|
150 | * @param child2 |
---|
151 | * @param requiredEqualityLevel |
---|
152 | * @return |
---|
153 | */ |
---|
154 | private TaskEquality callRuleManager(ITask child1, |
---|
155 | ITask child2, |
---|
156 | TaskEquality requiredEqualityLevel) |
---|
157 | { |
---|
158 | if (requiredEqualityLevel == null) { |
---|
159 | return mRuleManager.compare(child1, child2); |
---|
160 | } |
---|
161 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
162 | return requiredEqualityLevel; |
---|
163 | } |
---|
164 | else { |
---|
165 | return TaskEquality.UNEQUAL; |
---|
166 | } |
---|
167 | } |
---|
168 | } |
---|