[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 | |
---|
[1125] | 35 | /* (non-Javadoc) |
---|
[1146] | 36 | * @see NodeComparisonRule#isApplicable(ITask, ITask) |
---|
[557] | 37 | */ |
---|
| 38 | @Override |
---|
[1146] | 39 | public boolean isApplicable(ITask task1, ITask task2) { |
---|
| 40 | return (task1 instanceof ISequence) && (task2 instanceof ISequence); |
---|
[1125] | 41 | } |
---|
| 42 | |
---|
| 43 | /* (non-Javadoc) |
---|
[1146] | 44 | * @see NodeComparisonRule#areLexicallyEqual(ITask, ITask) |
---|
[1125] | 45 | */ |
---|
| 46 | @Override |
---|
[1146] | 47 | public boolean areLexicallyEqual(ITask task1, ITask task2) { |
---|
| 48 | TaskEquality equality = getEquality(task1, task2, TaskEquality.LEXICALLY_EQUAL); |
---|
| 49 | return (equality != null) && (equality.isAtLeast(TaskEquality.LEXICALLY_EQUAL)); |
---|
[1125] | 50 | } |
---|
| 51 | |
---|
| 52 | /* (non-Javadoc) |
---|
[1146] | 53 | * @see NodeComparisonRule#areSyntacticallyEqual(ITask, ITask) |
---|
[1125] | 54 | */ |
---|
| 55 | @Override |
---|
[1146] | 56 | public boolean areSyntacticallyEqual(ITask task1, ITask task2) { |
---|
| 57 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SYNTACTICALLY_EQUAL); |
---|
| 58 | return (equality != null) && (equality.isAtLeast(TaskEquality.SYNTACTICALLY_EQUAL)); |
---|
[1125] | 59 | } |
---|
| 60 | |
---|
| 61 | /* (non-Javadoc) |
---|
[1146] | 62 | * @see NodeComparisonRule#areSemanticallyEqual(ITask, ITask) |
---|
[1125] | 63 | */ |
---|
| 64 | @Override |
---|
[1146] | 65 | public boolean areSemanticallyEqual(ITask task1, ITask task2) { |
---|
| 66 | TaskEquality equality = getEquality(task1, task2, TaskEquality.SEMANTICALLY_EQUAL); |
---|
| 67 | return (equality != null) && (equality.isAtLeast(TaskEquality.SEMANTICALLY_EQUAL)); |
---|
[1125] | 68 | } |
---|
| 69 | |
---|
| 70 | /* (non-Javadoc) |
---|
[1146] | 71 | * @see NodeComparisonRule#compare(ITask, ITask) |
---|
[1125] | 72 | */ |
---|
| 73 | @Override |
---|
[1146] | 74 | public TaskEquality compare(ITask task1, ITask task2) { |
---|
| 75 | return getEquality(task1, task2, null); |
---|
[1125] | 76 | } |
---|
[439] | 77 | |
---|
[1125] | 78 | /** |
---|
[1154] | 79 | * <p> |
---|
| 80 | * compares two sequences with each other checking for the provided required level of |
---|
| 81 | * equality. If this level is ensured, the method immediately returns. The more concrete |
---|
| 82 | * the required equality level, the more checks this method performs. |
---|
| 83 | * </p> |
---|
[1125] | 84 | * |
---|
[1154] | 85 | * @param task1 the first task to be compared |
---|
| 86 | * @param task2 the second task to be compared |
---|
| 87 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 88 | * |
---|
| 89 | * @return the determined equality. |
---|
[1125] | 90 | */ |
---|
[1146] | 91 | private TaskEquality getEquality(ITask task1, ITask task2, TaskEquality requiredEqualityLevel) { |
---|
| 92 | List<ITask> children1 = ((ISequence) task1).getChildren(); |
---|
| 93 | List<ITask> children2 = ((ISequence) task2).getChildren(); |
---|
[807] | 94 | |
---|
[557] | 95 | // if both sequences do not have children, they are equal although this doesn't make sense |
---|
[1125] | 96 | if ((children1.size() == 0) && (children2.size() == 0)) { |
---|
[1146] | 97 | return TaskEquality.LEXICALLY_EQUAL; |
---|
[557] | 98 | } |
---|
| 99 | |
---|
[1125] | 100 | if (children1.size() != children2.size()) { |
---|
[1146] | 101 | return TaskEquality.UNEQUAL; |
---|
[557] | 102 | } |
---|
| 103 | |
---|
[1146] | 104 | TaskEquality resultingEquality = TaskEquality.LEXICALLY_EQUAL; |
---|
[1125] | 105 | for (int i = 0; i < children1.size(); i++) { |
---|
[1146] | 106 | ITask child1 = children1.get(i); |
---|
| 107 | ITask child2 = children2.get(i); |
---|
[557] | 108 | |
---|
[1146] | 109 | TaskEquality taskEquality = callRuleManager(child1, child2, requiredEqualityLevel); |
---|
[557] | 110 | |
---|
[1146] | 111 | if ((taskEquality == null) || (taskEquality == TaskEquality.UNEQUAL)) { |
---|
| 112 | return TaskEquality.UNEQUAL; |
---|
[557] | 113 | } |
---|
[807] | 114 | |
---|
[1146] | 115 | resultingEquality = resultingEquality.getCommonDenominator(taskEquality); |
---|
[557] | 116 | } |
---|
| 117 | |
---|
[807] | 118 | return resultingEquality; |
---|
[439] | 119 | } |
---|
| 120 | |
---|
[1125] | 121 | /** |
---|
| 122 | * <p> |
---|
[1154] | 123 | * used to to call the task equality rule manager for the comparison of the two provided |
---|
| 124 | * children. If no required equality level is provided, than the most concrete equality is |
---|
| 125 | * returned. Otherwise, the required equality is returned as long as the children are equal |
---|
| 126 | * on that level. |
---|
| 127 | * </p> |
---|
| 128 | * |
---|
| 129 | * @param child1 the first task to be compared |
---|
| 130 | * @param child2 the second task to be compared |
---|
| 131 | * @param requiredEqualityLevel the equality level to be checked for |
---|
| 132 | * |
---|
| 133 | * @return the determined equality |
---|
[1125] | 134 | */ |
---|
[1146] | 135 | private TaskEquality callRuleManager(ITask child1, |
---|
| 136 | ITask child2, |
---|
| 137 | TaskEquality requiredEqualityLevel) |
---|
[1125] | 138 | { |
---|
| 139 | if (requiredEqualityLevel == null) { |
---|
[1190] | 140 | return TaskEqualityRuleManager.getInstance().compare(child1, child2); |
---|
[1125] | 141 | } |
---|
[1190] | 142 | else if (TaskEqualityRuleManager.getInstance().areAtLeastEqual |
---|
| 143 | (child1, child2, requiredEqualityLevel)) |
---|
| 144 | { |
---|
[1125] | 145 | return requiredEqualityLevel; |
---|
| 146 | } |
---|
| 147 | else { |
---|
[1146] | 148 | return TaskEquality.UNEQUAL; |
---|
[1125] | 149 | } |
---|
| 150 | } |
---|
[439] | 151 | } |
---|