[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] | 15 | package de.ugoe.cs.autoquest.tasktrees.taskequality; |
---|
[439] | 16 | |
---|
[1125] | 17 | import java.util.List; |
---|
| 18 | |
---|
[922] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence; |
---|
[1146] | 20 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITask; |
---|
[439] | 21 | |
---|
| 22 | /** |
---|
[557] | 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> |
---|
[439] | 29 | * |
---|
| 30 | * @version $Revision: $ $Date: 19.02.2012$ |
---|
| 31 | * @author 2012, last modified by $Author: patrick$ |
---|
| 32 | */ |
---|
[1146] | 33 | public class SequenceComparisonRule implements TaskComparisonRule { |
---|
[439] | 34 | |
---|
[1146] | 35 | /** the rule manager for internally comparing tasks */ |
---|
| 36 | private TaskEqualityRuleManager mRuleManager; |
---|
[439] | 37 | |
---|
[557] | 38 | /** |
---|
| 39 | * <p> |
---|
[1146] | 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 |
---|
[557] | 42 | * </p> |
---|
| 43 | * |
---|
[1146] | 44 | * @param ruleManager the rule manager for comparing tasks |
---|
[557] | 45 | */ |
---|
[1146] | 46 | SequenceComparisonRule(TaskEqualityRuleManager ruleManager) { |
---|
[557] | 47 | super(); |
---|
| 48 | mRuleManager = ruleManager; |
---|
| 49 | } |
---|
[439] | 50 | |
---|
[1125] | 51 | /* (non-Javadoc) |
---|
[1146] | 52 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[557] | 53 | */ |
---|
| 54 | @Override |
---|
[1146] | 55 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 56 | return (task1 instanceof ISequence) && (task2 instanceof ISequence); |
---|
[1125] | 57 | } |
---|
| 58 | |
---|
| 59 | /* (non-Javadoc) |
---|
[1146] | 60 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 61 | */ |
---|
| 62 | @Override |
---|
[1146] | 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)); |
---|
[1125] | 66 | } |
---|
| 67 | |
---|
| 68 | /* (non-Javadoc) |
---|
[1146] | 69 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 70 | */ |
---|
| 71 | @Override |
---|
[1146] | 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)); |
---|
[1125] | 75 | } |
---|
| 76 | |
---|
| 77 | /* (non-Javadoc) |
---|
[1146] | 78 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 79 | */ |
---|
| 80 | @Override |
---|
[1146] | 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)); |
---|
[1125] | 84 | } |
---|
| 85 | |
---|
| 86 | /* (non-Javadoc) |
---|
[1146] | 87 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 88 | */ |
---|
| 89 | @Override |
---|
[1146] | 90 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 91 | return getEquality(task1, task2, null); |
---|
[1125] | 92 | } |
---|
[439] | 93 | |
---|
[1125] | 94 | /** |
---|
[1154] | 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> |
---|
[1125] | 100 | * |
---|
[1154] | 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. |
---|
[1125] | 106 | */ |
---|
[1146] | 107 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
| 108 | List<ITask> children1 = ((ISequence) task1).getChildren(); |
---|
| 109 | List<ITask> children2 = ((ISequence) task2).getChildren(); |
---|
[807] | 110 | |
---|
[557] | 111 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
[1125] | 112 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
[1146] | 113 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[557] | 114 | } |
---|
| 115 | |
---|
[1125] | 116 | if (children1.size() != children2.size()) { |
---|
[1146] | 117 | return TaskEquality.UNEQUAL; |
---|
[557] | 118 | } |
---|
| 119 | |
---|
[1146] | 120 | TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL; |
---|
[1125] | 121 | for (int i = 0; i < children1.size(); i++) { |
---|
[1146] | 122 | ITask child1 = children1.get(i); |
---|
| 123 | ITask child2 = children2.get(i); |
---|
[557] | 124 | |
---|
[1146] | 125 | TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
[557] | 126 | |
---|
[1146] | 127 | if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) { |
---|
| 128 | return TaskEquality.UNEQUAL; |
---|
[557] | 129 | } |
---|
[807] | 130 | |
---|
[1146] | 131 | resultingEquality = resultingEquality.getCommonDenominator(taskEquality); |
---|
[557] | 132 | } |
---|
| 133 | |
---|
[807] | 134 | return resultingEquality; |
---|
[439] | 135 | } |
---|
| 136 | |
---|
[1125] | 137 | /** |
---|
| 138 | * <p> |
---|
[1154] | 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 |
---|
[1125] | 150 | */ |
---|
[1146] | 151 | private TaskEquality callRuleManager(ITask child1, |
---|
| 152 | ITask child2, |
---|
| 153 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 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 { |
---|
[1146] | 162 | return TaskEquality.UNEQUAL; |
---|
[1125] | 163 | } |
---|
| 164 | } |
---|
[439] | 165 | } |
---|