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

Last change on this file since 1154 was 1154, checked in by pharms, 11 years ago
  • improved java doc
  • Property svn:executable set to *
File size: 9.1 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 java.util.ArrayList;
18import java.util.List;
19
20import 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. These rules are asked for comparing the two provided tasks. If a rule
26 * returns a task equality other than null, this equality is returned. Otherwise the next rule
27 * is asked.
28 * </p>
29 *
30 * @version $Revision: $ $Date: 19.02.2012$
31 * @author 2012, last modified by $Author: patrick$
32 */
33public class TaskEqualityRuleManager {
34
35    /**
36     * <p>
37     * the rules that can be used for comparing tasks
38     * </p>
39     */
40    private List<TaskComparisonRule> mRuleIndex = null;
41
42    /**
43     * <p>
44     * initializes the task equality rule manager by filling the internal list of comparison rules.
45     * This method must be called before any other method is called on the rule manager.
46     * </p>
47     */
48    public void init() {
49        mRuleIndex = new ArrayList<TaskComparisonRule>();
50        mRuleIndex.add(new TaskIdentityRule());
51        mRuleIndex.add(new GUIEventTaskComparisonRule());
52        mRuleIndex.add(new EventTaskComparisonRule());
53        mRuleIndex.add(new IterationComparisonRule(this));
54        mRuleIndex.add(new SequenceComparisonRule(this));
55        mRuleIndex.add(new SelectionComparisonRule(this));
56        mRuleIndex.add(new TaskAndIterationComparisonRule(this));
57        mRuleIndex.add(new TaskAndSelectionComparisonRule(this));
58    }
59
60    /**
61     * <p>
62     * this method performs a comparison of the two provided tasks. It iterates its internal
63     * comparison rules. If the first rule returns a task equality other than null,
64     * this equality is returned. Otherwise the next rule is tried. If no rule returns an equality
65     * <code>NodeEquality.UNEQUAL</code> is returned.
66     * </p>
67     *
68     * @param task1 the first task to be compared
69     * @param task2 the second task to be compared
70     *
71     * @return as described
72     *
73     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
74     *                               manager before a call to this method.
75     */
76    public TaskEquality compare(ITask task1, ITask task2)
77        throws IllegalStateException
78    {
79        if (mRuleIndex == null) {
80            throw new IllegalStateException("not initialized");
81        }
82       
83        // LOG.info("checking for equality of " + task1 + " and " + task2);
84        TaskEquality taskEquality = null;
85
86        for (TaskComparisonRule rule : mRuleIndex) {
87            if (rule.isApplicable(task1, task2)) {
88                taskEquality = rule.compare(task1, task2);
89                if (taskEquality != null) {
90                    // LOG.warning("used rule " + rule + " for equality check");
91                    return taskEquality;
92                }
93            }
94        }
95
96        // LOG.warning("no rule could be applied --> handling tasks as unequal");
97
98        return TaskEquality.UNEQUAL;
99    }
100
101    /**
102     * <p>
103     * this method two tasks with respect to the fiven equality level and returns true, if this
104     * level is given.
105     * </p>
106     *
107     * @param task1         the first task to be compared
108     * @param task2         the second task to be compared
109     * @param equalityLevel the level of equality to be checked for
110     *
111     * @return as described
112     *
113     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
114     *                               manager before a call to this method.
115     */
116    public boolean areAtLeastEqual(ITask task1, ITask task2, TaskEquality equalityLevel) {
117        if (equalityLevel == null) {
118            throw new IllegalArgumentException("required equality level must not be null");
119        }
120       
121        switch (equalityLevel) {
122            case IDENTICAL:
123                return areIdentical(task1, task2);
124            case LEXICALLY_EQUAL:
125                return areLexicallyEqual(task1, task2);
126            case SYNTACTICALLY_EQUAL:
127                return areSyntacticallyEqual(task1, task2);
128            case SEMANTICALLY_EQUAL:
129                return areSemanticallyEqual(task1, task2);
130            case UNEQUAL:
131                return !areSemanticallyEqual(task1, task2);
132            default:
133                throw new IllegalArgumentException("unknown required equality: " + equalityLevel);
134        }
135    }
136
137    /**
138     * <p>
139     * this method checks if the two given tasks are identical. For this, it iterates its internal
140     * comparison rules. If the first rule returns true, than this method returns true as well.
141     * If no rule returns true, this method returns false.
142     * </p>
143     *
144     * @param task1 the first task to be compared
145     * @param task2 the second task to be compared
146     *
147     * @return as described
148     *
149     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
150     *                               manager before a call to this method.
151     */
152    public boolean areIdentical(ITask task1, ITask task2) {
153        if (mRuleIndex == null) {
154            throw new IllegalStateException("not initialized");
155        }
156       
157        for (TaskComparisonRule rule : mRuleIndex) {
158            if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) {
159                 return true;
160            }
161        }
162
163        return false;
164    }
165
166    /**
167     * <p>
168     * this method checks if the two given tasks are lexically equal. For this, it iterates its
169     * internal comparison rules. If the first rule returns true, than this method returns true
170     * as well. If no rule returns true, this method returns false.
171     * </p>
172     *
173     * @param task1 the first task to be compared
174     * @param task2 the second task to be compared
175     *
176     * @return as described
177     *
178     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
179     *                               manager before a call to this method.
180     */
181    public boolean areLexicallyEqual(ITask task1, ITask task2) {
182        if (mRuleIndex == null) {
183            throw new IllegalStateException("not initialized");
184        }
185       
186        for (TaskComparisonRule rule : mRuleIndex) {
187            if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) {
188                 return true;
189            }
190        }
191
192        return false;
193    }
194
195    /**
196     * <p>
197     * this method checks if the two given tasks are syntactically equal. For this, it iterates its
198     * internal comparison rules. If the first rule returns true, than this method returns true
199     * as well. If no rule returns true, this method returns false.
200     * </p>
201     *
202     * @param task1 the first task to be compared
203     * @param task2 the second task to be compared
204     *
205     * @return as described
206     *
207     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
208     *                               manager before a call to this method.
209     */
210    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
211        if (mRuleIndex == null) {
212            throw new IllegalStateException("not initialized");
213        }
214       
215        for (TaskComparisonRule rule : mRuleIndex) {
216            if (rule.isApplicable(task1, task2) && rule.areSyntacticallyEqual(task1, task2)) {
217                 return true;
218            }
219        }
220
221        return false;
222    }
223
224    /**
225     * <p>
226     * this method checks if the two given tasks are semantically equal. For this, it iterates its
227     * internal comparison rules. If the first rule returns true, than this method returns true
228     * as well. If no rule returns true, this method returns false.
229     * </p>
230     *
231     * @param task1 the first task to be compared
232     * @param task2 the second task to be compared
233     *
234     * @return as described
235     *
236     * @throws IllegalStateException in the case, the {@link #init()} method was not called on the
237     *                               manager before a call to this method.
238     */
239    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
240        if (mRuleIndex == null) {
241            throw new IllegalStateException("not initialized");
242        }
243       
244        for (TaskComparisonRule rule : mRuleIndex) {
245            if (rule.isApplicable(task1, task2) && rule.areSemanticallyEqual(task1, task2)) {
246                 return true;
247            }
248        }
249
250        return false;
251    }
252
253}
Note: See TracBrowser for help on using the repository browser.