source: trunk/autoquest-core-tasktrees/src/main/java/de/ugoe/cs/autoquest/tasktrees/taskequality/SequenceComparisonRule.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: 5.2 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.List;
18
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask;
21
22/**
23 * <p>
24 * This rule is capable of comparing sequences. If both sequences do not have children, they are
25 * treated as lexically equal. Sequences are lexically equal, if they have the same number and
26 * order of lexically equal children. The rule can not decide, if two sequences are syntactically
27 * or semantically equal.
28 * </p>
29 *
30 * @version $Revision: $ $Date: 19.02.2012$
31 * @author 2012, last modified by $Author: patrick$
32 */
33public class SequenceComparisonRule 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    SequenceComparisonRule(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 ISequence) && (task2 instanceof ISequence);
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        List<ITask> children1 = ((ISequence) task1).getChildren();
99        List<ITask> children2 = ((ISequence) task2).getChildren();
100
101        // if both sequences do not have children, they are equal although this doesn't make sense
102        if ((children1.size() == 0) && (children2.size() == 0)) {
103            return TaskEquality.LEXICALLY_EQUAL;
104        }
105
106        if (children1.size() != children2.size()) {
107            return TaskEquality.UNEQUAL;
108        }
109
110        TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL;
111        for (int i = 0; i < children1.size(); i++) {
112            ITask child1 = children1.get(i);
113            ITask child2 = children2.get(i);
114
115            TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel);
116
117            if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) {
118                return TaskEquality.UNEQUAL;
119            }
120           
121            resultingEquality = resultingEquality.getCommonDenominator(taskEquality);
122        }
123
124        return resultingEquality;
125    }
126
127    /**
128     * <p>
129     * TODO: comment
130     * </p>
131     *
132     * @param child1
133     * @param child2
134     * @param requiredEqualityLevel
135     * @return
136     */
137    private TaskEquality callRuleManager(ITask        child1,
138                                         ITask        child2,
139                                         TaskEquality requiredEqualityLevel)
140    {
141        if (requiredEqualityLevel == null) {
142            return mRuleManager.compare(child1, child2);
143        }
144        else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) {
145            return requiredEqualityLevel;
146        }
147        else {
148            return TaskEquality.UNEQUAL;
149        }
150    }
151}
Note: See TracBrowser for help on using the repository browser.