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

Last change on this file since 1146 was 1146, checked in by pharms, 11 years ago
  • complete refactoring of task tree model with a separation of task models and task instances
  • appropriate adaptation of task tree generation process
  • appropriate adaptation of commands and task tree visualization
  • Property svn:executable set to *
File size: 6.8 KB
RevLine 
[1113]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
[1146]15package de.ugoe.cs.autoquest.tasktrees.taskequality;
[439]16
17import java.util.ArrayList;
18import java.util.List;
19
[1146]20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
[439]21
22/**
[557]23 * <p>
[1146]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
[557]26 * {@link IterationComparisonRule}, the {@link SequenceComparisonRule}, and
[1146]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
[557]29 * returned. Otherwise the next rule is asked.
30 * </p>
[439]31 *
32 * @version $Revision: $ $Date: 19.02.2012$
33 * @author 2012, last modified by $Author: patrick$
34 */
[1146]35public class TaskEqualityRuleManager {
[439]36
[557]37    /** */
[1146]38    private List<TaskComparisonRule> mRuleIndex = null;
[439]39
[557]40    /**
41     * <p>
[1146]42     * initializes the task equality rule manager by filling the internal list of comparison rules.
[557]43     * This method must be called before any other method is called on the rule manager.
44     * </p>
45     */
46    public void init() {
[1146]47        mRuleIndex = new ArrayList<TaskComparisonRule>();
48        mRuleIndex.add(new TaskIdentityRule());
[807]49        mRuleIndex.add(new GUIEventTaskComparisonRule());
50        mRuleIndex.add(new EventTaskComparisonRule());
[557]51        mRuleIndex.add(new IterationComparisonRule(this));
52        mRuleIndex.add(new SequenceComparisonRule(this));
53        mRuleIndex.add(new SelectionComparisonRule(this));
[1146]54        mRuleIndex.add(new TaskAndIterationComparisonRule(this));
55        mRuleIndex.add(new TaskAndSelectionComparisonRule(this));
[557]56    }
[439]57
[557]58    /**
59     * <p>
[1146]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,
[557]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     *
[1146]66     * @param task1 the first task to be compared
67     * @param task2 the second task to be compared
[557]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     */
[1146]74    public TaskEquality compare(ITask task1, ITask task2)
[557]75        throws IllegalStateException
[439]76    {
[557]77        if (mRuleIndex == null) {
78            throw new IllegalStateException("not initialized");
79        }
80       
[1146]81        // LOG.info("checking for equality of " + task1 + " and " + task2);
82        TaskEquality taskEquality = null;
[557]83
[1146]84        for (TaskComparisonRule rule : mRuleIndex) {
85            if (rule.isApplicable(task1, task2)) {
86                taskEquality = rule.compare(task1, task2);
87                if (taskEquality != null) {
[1125]88                    // LOG.warning("used rule " + rule + " for equality check");
[1146]89                    return taskEquality;
[1125]90                }
[557]91            }
92        }
93
[1146]94        // LOG.warning("no rule could be applied --> handling tasks as unequal");
[557]95
[1146]96        return TaskEquality.UNEQUAL;
[439]97    }
98
[1125]99    /**
100     * <p>
101     * TODO: comment
102     * </p>
103     *
104     * @param child1
105     * @param child2
106     * @param equalityLevel
107     * @return
108     */
[1146]109    public boolean areAtLeastEqual(ITask task1, ITask task2, TaskEquality equalityLevel) {
[1125]110        if (equalityLevel == null) {
111            throw new IllegalArgumentException("required equality level must not be null");
112        }
113       
114        switch (equalityLevel) {
115            case IDENTICAL:
[1146]116                return areIdentical(task1, task2);
[1125]117            case LEXICALLY_EQUAL:
[1146]118                return areLexicallyEqual(task1, task2);
[1125]119            case SYNTACTICALLY_EQUAL:
[1146]120                return areSyntacticallyEqual(task1, task2);
[1125]121            case SEMANTICALLY_EQUAL:
[1146]122                return areSemanticallyEqual(task1, task2);
[1125]123            case UNEQUAL:
[1146]124                return !areSemanticallyEqual(task1, task2);
[1125]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     */
[1146]139    public boolean areIdentical(ITask task1, ITask task2) {
[1125]140        if (mRuleIndex == null) {
141            throw new IllegalStateException("not initialized");
142        }
143       
[1146]144        for (TaskComparisonRule rule : mRuleIndex) {
145            if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) {
[1125]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     */
[1146]162    public boolean areLexicallyEqual(ITask task1, ITask task2) {
[1125]163        if (mRuleIndex == null) {
164            throw new IllegalStateException("not initialized");
165        }
166       
[1146]167        for (TaskComparisonRule rule : mRuleIndex) {
168            if (rule.isApplicable(task1, task2) && rule.areLexicallyEqual(task1, task2)) {
[1125]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     */
[1146]185    public boolean areSyntacticallyEqual(ITask task1, ITask task2) {
[1125]186        if (mRuleIndex == null) {
187            throw new IllegalStateException("not initialized");
188        }
189       
[1146]190        for (TaskComparisonRule rule : mRuleIndex) {
191            if (rule.isApplicable(task1, task2) && rule.areSyntacticallyEqual(task1, task2)) {
[1125]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     */
[1146]208    public boolean areSemanticallyEqual(ITask task1, ITask task2) {
[1125]209        if (mRuleIndex == null) {
210            throw new IllegalStateException("not initialized");
211        }
212       
[1146]213        for (TaskComparisonRule rule : mRuleIndex) {
214            if (rule.isApplicable(task1, task2) && rule.areSemanticallyEqual(task1, task2)) {
[1125]215                 return true;
216            }
217        }
218
219        return false;
220    }
221
[439]222}
Note: See TracBrowser for help on using the repository browser.