[1119] | 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.temporalrelation; |
---|
| 16 | |
---|
[1127] | 17 | import java.util.List; |
---|
| 18 | |
---|
[1119] | 19 | import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode; |
---|
| 20 | |
---|
| 21 | /** |
---|
| 22 | * This rule applies a node scope rule (working only in the scope of the provided parent node) |
---|
| 23 | * as a tree scope rule, i.e. it applies the provided wrapped rule recursively on the subtree |
---|
| 24 | * provided by the parent node. It traverses the nodes in post order. |
---|
| 25 | * |
---|
| 26 | * @author 2013, last modified by $Author: patrick$ |
---|
| 27 | */ |
---|
| 28 | class TreeScopeWrapperRule implements TemporalRelationshipRule { |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * <p> |
---|
| 32 | * the node scope rule wrapped by this wrapper |
---|
| 33 | * </p> |
---|
| 34 | */ |
---|
| 35 | private TemporalRelationshipRule rule; |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * <p> |
---|
| 39 | * instantiates the wrapper with the rule to be wrapped |
---|
| 40 | * </p> |
---|
| 41 | * |
---|
| 42 | * @param rule the rule to be wrapped and applied recursively |
---|
| 43 | */ |
---|
| 44 | TreeScopeWrapperRule(TemporalRelationshipRule rule) { |
---|
| 45 | if (rule == null) { |
---|
| 46 | throw new IllegalArgumentException("rule must not be null"); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | this.rule = rule; |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /* (non-Javadoc) |
---|
| 53 | * @see java.lang.Object#toString() |
---|
| 54 | */ |
---|
| 55 | @Override |
---|
| 56 | public String toString() { |
---|
| 57 | return this.rule.toString() + " (tree scope)"; |
---|
| 58 | } |
---|
| 59 | |
---|
| 60 | /* |
---|
| 61 | * (non-Javadoc) |
---|
| 62 | * |
---|
| 63 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
| 64 | * boolean) |
---|
| 65 | */ |
---|
| 66 | @Override |
---|
| 67 | public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) { |
---|
| 68 | RuleApplicationResult result = null; |
---|
| 69 | |
---|
| 70 | if (parent != null) { |
---|
| 71 | result = new RuleApplicationResult(); |
---|
| 72 | |
---|
[1127] | 73 | List<ITaskTreeNode> children = parent.getChildren(); |
---|
| 74 | if (children != null) { |
---|
| 75 | for (ITaskTreeNode child : children) { |
---|
[1119] | 76 | merge(result, apply(child, finalize)); |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | merge(result, rule.apply(parent, finalize)); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | return result; |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * <p> |
---|
| 88 | * merges the overall rule application result with an intermediate result |
---|
| 89 | * </p> |
---|
| 90 | * |
---|
| 91 | * @param overallResult the current overall result |
---|
| 92 | * @param intermediate the intermediate result to be merged into the overall result |
---|
| 93 | */ |
---|
| 94 | private void merge(RuleApplicationResult overallResult, RuleApplicationResult intermediate) { |
---|
| 95 | if (intermediate == null) { |
---|
| 96 | return; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | RuleApplicationStatus overallStatus = overallResult.getRuleApplicationStatus(); |
---|
| 100 | RuleApplicationStatus intermediateStatus = intermediate.getRuleApplicationStatus(); |
---|
| 101 | |
---|
[1127] | 102 | if ((intermediateStatus == RuleApplicationStatus.FINISHED) || |
---|
| 103 | ((intermediateStatus == RuleApplicationStatus.FEASIBLE) && |
---|
| 104 | (overallStatus == RuleApplicationStatus.NOT_APPLIED))) |
---|
[1119] | 105 | { |
---|
| 106 | overallResult.setRuleApplicationStatus(intermediateStatus); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | for (ITaskTreeNode newlyCreatedNode : intermediate.getNewlyCreatedParentNodes()) { |
---|
| 110 | overallResult.addNewlyCreatedParentNode(newlyCreatedNode); |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | } |
---|