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 java.util.List; |
---|
18 | |
---|
19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection; |
---|
20 | import 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 | */ |
---|
33 | public 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 | * |
---|
97 | */ |
---|
98 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
99 | ISelection selection = null; |
---|
100 | ITask task = null; |
---|
101 | |
---|
102 | if (task1 instanceof ISelection) { |
---|
103 | if (task2 instanceof ISelection) { |
---|
104 | // the rule is not responsible for two selections |
---|
105 | return null; |
---|
106 | } |
---|
107 | |
---|
108 | selection = (ISelection) task1; |
---|
109 | task = task2; |
---|
110 | } |
---|
111 | else if (task2 instanceof ISelection) { |
---|
112 | if (task1 instanceof ISelection) { |
---|
113 | // the rule is not responsible for two selections |
---|
114 | return null; |
---|
115 | } |
---|
116 | |
---|
117 | selection = (ISelection) task2; |
---|
118 | task = task1; |
---|
119 | } |
---|
120 | else { |
---|
121 | return null; |
---|
122 | } |
---|
123 | |
---|
124 | // now, that we found the selection and the task, lets compare the children of the selection |
---|
125 | // with the task. |
---|
126 | List<ITask> children = selection.getChildren(); |
---|
127 | |
---|
128 | if (children.size() < 1) { |
---|
129 | return null; |
---|
130 | } |
---|
131 | |
---|
132 | TaskEquality mostConcreteNodeEquality = null; |
---|
133 | |
---|
134 | for (ITask child : children) { |
---|
135 | TaskEquality taskEquality = callRuleManager(child, task, requiredEqualityLevel); |
---|
136 | |
---|
137 | if (taskEquality != TaskEquality.UNEQUAL) { |
---|
138 | if (mostConcreteNodeEquality == null) { |
---|
139 | mostConcreteNodeEquality = taskEquality; |
---|
140 | } |
---|
141 | else if (mostConcreteNodeEquality.isAtLeast(taskEquality)) { |
---|
142 | mostConcreteNodeEquality = taskEquality; |
---|
143 | |
---|
144 | } |
---|
145 | |
---|
146 | if ((requiredEqualityLevel != null) && |
---|
147 | (mostConcreteNodeEquality.isAtLeast(requiredEqualityLevel))) |
---|
148 | { |
---|
149 | // if we found one child of the selection that is as equal as required, then |
---|
150 | // we can consider the selection to be sufficiently equal to the other task. |
---|
151 | // So we break up checking further children. |
---|
152 | break; |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | // although the subtask may be identical to the task, we can not return identical, as |
---|
158 | // the selection is not identical to the task, but at most lexically equal |
---|
159 | if (mostConcreteNodeEquality == TaskEquality.IDENTICAL) { |
---|
160 | return TaskEquality.LEXICALLY_EQUAL; |
---|
161 | } |
---|
162 | else { |
---|
163 | return mostConcreteNodeEquality; |
---|
164 | } |
---|
165 | |
---|
166 | } |
---|
167 | |
---|
168 | /** |
---|
169 | * <p> |
---|
170 | * TODO: comment |
---|
171 | * </p> |
---|
172 | * |
---|
173 | * @param child1 |
---|
174 | * @param child2 |
---|
175 | * @param requiredEqualityLevel |
---|
176 | * @return |
---|
177 | */ |
---|
178 | private TaskEquality callRuleManager(ITask child1, |
---|
179 | ITask child2, |
---|
180 | TaskEquality requiredEqualityLevel) |
---|
181 | { |
---|
182 | if (requiredEqualityLevel == null) { |
---|
183 | return mRuleManager.compare(child1, child2); |
---|
184 | } |
---|
185 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
186 | return requiredEqualityLevel; |
---|
187 | } |
---|
188 | else { |
---|
189 | return TaskEquality.UNEQUAL; |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|