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.ISequence; |
---|
20 | import 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 | */ |
---|
33 | public 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 | * <p> |
---|
96 | * compares two sequences with each other checking for the provided required level of |
---|
97 | * equality. If this level is ensured, the method immediately returns. The more concrete |
---|
98 | * the required equality level, the more checks this method performs. |
---|
99 | * </p> |
---|
100 | * |
---|
101 | * @param task1 the first task to be compared |
---|
102 | * @param task2 the second task to be compared |
---|
103 | * @param requiredEqualityLevel the equality level to be checked for |
---|
104 | * |
---|
105 | * @return the determined equality. |
---|
106 | */ |
---|
107 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
108 | List<ITask> children1 = ((ISequence) task1).getChildren(); |
---|
109 | List<ITask> children2 = ((ISequence) task2).getChildren(); |
---|
110 | |
---|
111 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
112 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
113 | return TaskEquality.LEXICALLY_EQUAL; |
---|
114 | } |
---|
115 | |
---|
116 | if (children1.size() != children2.size()) { |
---|
117 | return TaskEquality.UNEQUAL; |
---|
118 | } |
---|
119 | |
---|
120 | TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL; |
---|
121 | for (int i = 0; i < children1.size(); i++) { |
---|
122 | ITask child1 = children1.get(i); |
---|
123 | ITask child2 = children2.get(i); |
---|
124 | |
---|
125 | TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
126 | |
---|
127 | if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) { |
---|
128 | return TaskEquality.UNEQUAL; |
---|
129 | } |
---|
130 | |
---|
131 | resultingEquality = resultingEquality.getCommonDenominator(taskEquality); |
---|
132 | } |
---|
133 | |
---|
134 | return resultingEquality; |
---|
135 | } |
---|
136 | |
---|
137 | /** |
---|
138 | * <p> |
---|
139 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
140 | * children. If no required equality level is provided, than the most concrete equality is |
---|
141 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
142 | * on that level. |
---|
143 | * </p> |
---|
144 | * |
---|
145 | * @param child1 the first task to be compared |
---|
146 | * @param child2 the second task to be compared |
---|
147 | * @param requiredEqualityLevel the equality level to be checked for |
---|
148 | * |
---|
149 | * @return the determined equality |
---|
150 | */ |
---|
151 | private TaskEquality callRuleManager(ITask child1, |
---|
152 | ITask child2, |
---|
153 | TaskEquality requiredEqualityLevel) |
---|
154 | { |
---|
155 | if (requiredEqualityLevel == null) { |
---|
156 | return mRuleManager.compare(child1, child2); |
---|
157 | } |
---|
158 | else if (mRuleManager.areAtLeastEqual(child1, child2, requiredEqualityLevel)) { |
---|
159 | return requiredEqualityLevel; |
---|
160 | } |
---|
161 | else { |
---|
162 | return TaskEquality.UNEQUAL; |
---|
163 | } |
---|
164 | } |
---|
165 | } |
---|