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.ArrayList; |
---|
18 | import java.util.List; |
---|
19 | |
---|
20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
21 | |
---|
22 | /** |
---|
23 | * <p> |
---|
24 | * The task equality rule manager is capable of comparing tasks based on its internal list |
---|
25 | * of comparison rules. The current list of rules contains the {@link TaskIdentityRule}, the |
---|
26 | * {@link IterationComparisonRule}, the {@link SequenceComparisonRule}, and |
---|
27 | * {@link SelectionComparisonRule}. These rules are asked for comparing the two provided tasks |
---|
28 | * in the mentioned order. If a rule returns a task equality other than null, this equality is |
---|
29 | * returned. Otherwise the next rule is asked. |
---|
30 | * </p> |
---|
31 | * |
---|
32 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
33 | * @author 2012, last modified by $Author: patrick$ |
---|
34 | */ |
---|
35 | public class TaskEqualityRuleManager { |
---|
36 | |
---|
37 | /** */ |
---|
38 | private List<TaskComparisonRule> mRuleIndex = null; |
---|
39 | |
---|
40 | /** |
---|
41 | * <p> |
---|
42 | * initializes the task equality rule manager by filling the internal list of comparison rules. |
---|
43 | * This method must be called before any other method is called on the rule manager. |
---|
44 | * </p> |
---|
45 | */ |
---|
46 | public void init() { |
---|
47 | mRuleIndex = new ArrayList<TaskComparisonRule>(); |
---|
48 | mRuleIndex.add(new TaskIdentityRule()); |
---|
49 | mRuleIndex.add(new GUIEventTaskComparisonRule()); |
---|
50 | mRuleIndex.add(new EventTaskComparisonRule()); |
---|
51 | mRuleIndex.add(new IterationComparisonRule(this)); |
---|
52 | mRuleIndex.add(new SequenceComparisonRule(this)); |
---|
53 | mRuleIndex.add(new SelectionComparisonRule(this)); |
---|
54 | mRuleIndex.add(new TaskAndIterationComparisonRule(this)); |
---|
55 | mRuleIndex.add(new TaskAndSelectionComparisonRule(this)); |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * <p> |
---|
60 | * this method performs a comparison of the two provided tasks. It iterates its internal |
---|
61 | * comparison rules. If the first rule returns a task equality other than null, |
---|
62 | * this equality is returned. Otherwise the next rule is tried. If no rule returns an equality |
---|
63 | * <code>NodeEquality.UNEQUAL</code> is returned. |
---|
64 | * </p> |
---|
65 | * |
---|
66 | * @param task1 the first task to be compared |
---|
67 | * @param task2 the second task to be compared |
---|
68 | * |
---|
69 | * @return as described |
---|
70 | * |
---|
71 | * @throws IllegalStateException in the case, the {@link #init()} method was not called on the |
---|
72 | * manager before a call to this method. |
---|
73 | */ |
---|
74 | public TaskEquality compare(ITask task1, ITask task2) |
---|
75 | throws IllegalStateException |
---|
76 | { |
---|
77 | if (mRuleIndex == null) { |
---|
78 | throw new IllegalStateException("not initialized"); |
---|
79 | } |
---|
80 | |
---|
81 | // LOG.info("checking for equality of " + task1 + " and " + task2); |
---|
82 | TaskEquality taskEquality = null; |
---|
83 | |
---|
84 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
85 | if (rule.isApplicable(task1, task2)) { |
---|
86 | taskEquality = rule.compare(task1, task2); |
---|
87 | if (taskEquality != null) { |
---|
88 | // LOG.warning("used rule " + rule + " for equality check"); |
---|
89 | return taskEquality; |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | // LOG.warning("no rule could be applied --> handling tasks as unequal"); |
---|
95 | |
---|
96 | return TaskEquality.UNEQUAL; |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * <p> |
---|
101 | * TODO: comment |
---|
102 | * </p> |
---|
103 | * |
---|
104 | * @param child1 |
---|
105 | * @param child2 |
---|
106 | * @param equalityLevel |
---|
107 | * @return |
---|
108 | */ |
---|
109 | public boolean areAtLeastEqual(ITask task1, ITask task2, TaskEquality equalityLevel) { |
---|
110 | if (equalityLevel == null) { |
---|
111 | throw new IllegalArgumentException("required equality level must not be null"); |
---|
112 | } |
---|
113 | |
---|
114 | switch (equalityLevel) { |
---|
115 | case IDENTICAL: |
---|
116 | return areIdentical(task1, task2); |
---|
117 | case LEXICALLY_EQUAL: |
---|
118 | return areLexicallyEqual(task1, task2); |
---|
119 | case SYNTACTICALLY_EQUAL: |
---|
120 | return areSyntacticallyEqual(task1, task2); |
---|
121 | case SEMANTICALLY_EQUAL: |
---|
122 | return areSemanticallyEqual(task1, task2); |
---|
123 | case UNEQUAL: |
---|
124 | return !areSemanticallyEqual(task1, task2); |
---|
125 | default: |
---|
126 | throw new IllegalArgumentException("unknown required equality: " + equalityLevel); |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * <p> |
---|
132 | * TODO: comment |
---|
133 | * </p> |
---|
134 | * |
---|
135 | * @param child1 |
---|
136 | * @param child2 |
---|
137 | * @return |
---|
138 | */ |
---|
139 | public boolean areIdentical(ITask task1, ITask task2) { |
---|
140 | if (mRuleIndex == null) { |
---|
141 | throw new IllegalStateException("not initialized"); |
---|
142 | } |
---|
143 | |
---|
144 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
145 | if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) { |
---|
146 | return true; |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | return false; |
---|
151 | } |
---|
152 | |
---|
153 | /** |
---|
154 | * <p> |
---|
155 | * TODO: comment |
---|
156 | * </p> |
---|
157 | * |
---|
158 | * @param child1 |
---|
159 | * @param child2 |
---|
160 | * @return |
---|
161 | */ |
---|
162 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
163 | if (mRuleIndex == null) { |
---|
164 | throw new IllegalStateException("not initialized"); |
---|
165 | } |
---|
166 | |
---|
167 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
168 | if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) { |
---|
169 | return true; |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | return false; |
---|
174 | } |
---|
175 | |
---|
176 | /** |
---|
177 | * <p> |
---|
178 | * TODO: comment |
---|
179 | * </p> |
---|
180 | * |
---|
181 | * @param child1 |
---|
182 | * @param child2 |
---|
183 | * @return |
---|
184 | */ |
---|
185 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
186 | if (mRuleIndex == null) { |
---|
187 | throw new IllegalStateException("not initialized"); |
---|
188 | } |
---|
189 | |
---|
190 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
191 | if (rule.isApplicable(task1, task2) && rule.areSyntacticallyEqual(task1, task2)) { |
---|
192 | return true; |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | return false; |
---|
197 | } |
---|
198 | |
---|
199 | /** |
---|
200 | * <p> |
---|
201 | * TODO: comment |
---|
202 | * </p> |
---|
203 | * |
---|
204 | * @param child1 |
---|
205 | * @param child2 |
---|
206 | * @return |
---|
207 | */ |
---|
208 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
209 | if (mRuleIndex == null) { |
---|
210 | throw new IllegalStateException("not initialized"); |
---|
211 | } |
---|
212 | |
---|
213 | for (TaskComparisonRule rule : mRuleIndex) { |
---|
214 | if (rule.isApplicable(task1, task2) && rule.areSemanticallyEqual(task1, task2)) { |
---|
215 | return true; |
---|
216 | } |
---|
217 | } |
---|
218 | |
---|
219 | return false; |
---|
220 | } |
---|
221 | |
---|
222 | } |
---|