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