1 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
2 | |
---|
3 | import de.ugoe.cs.quest.eventcore.IEventTarget; |
---|
4 | import de.ugoe.cs.quest.tasktrees.treeifc.IEventTask; |
---|
5 | import de.ugoe.cs.quest.tasktrees.treeifc.ISequence; |
---|
6 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
7 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
8 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
9 | |
---|
10 | /** |
---|
11 | * TODO comment |
---|
12 | * |
---|
13 | * @version $Revision: $ $Date: 18.03.2012$ |
---|
14 | * @author 2012, last modified by $Author: patrick$ |
---|
15 | */ |
---|
16 | public class DefaultEventTargetSequenceDetectionRule implements TemporalRelationshipRule { |
---|
17 | |
---|
18 | /* |
---|
19 | * (non-Javadoc) |
---|
20 | * |
---|
21 | * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode, |
---|
22 | * TaskTreeBuilder, TaskTreeNodeFactory) |
---|
23 | */ |
---|
24 | @Override |
---|
25 | public RuleApplicationResult apply(ITaskTreeNode parent, |
---|
26 | ITaskTreeBuilder builder, |
---|
27 | ITaskTreeNodeFactory nodeFactory, |
---|
28 | boolean finalize) |
---|
29 | { |
---|
30 | if (!(parent instanceof ISequence)) { |
---|
31 | return null; |
---|
32 | } |
---|
33 | |
---|
34 | RuleApplicationResult result = new RuleApplicationResult(); |
---|
35 | |
---|
36 | IEventTarget currentEventTarget = null; |
---|
37 | int startingIndex = -1; |
---|
38 | |
---|
39 | int index = 0; |
---|
40 | while (index < parent.getChildren().size()) { |
---|
41 | ITaskTreeNode child = parent.getChildren().get(index); |
---|
42 | |
---|
43 | IEventTarget eventTarget = determineEventTarget(child); |
---|
44 | |
---|
45 | if (((eventTarget == null) && (currentEventTarget != null)) || |
---|
46 | ((eventTarget != null) && (!eventTarget.equals(currentEventTarget)))) |
---|
47 | { |
---|
48 | if (startingIndex < 0) { |
---|
49 | startingIndex = index; |
---|
50 | currentEventTarget = eventTarget; |
---|
51 | } |
---|
52 | else { |
---|
53 | int endIndex = index - 1; |
---|
54 | |
---|
55 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
56 | // or if this child is not a sequence itself |
---|
57 | if ((startingIndex != endIndex) || |
---|
58 | (!(parent.getChildren().get(startingIndex) instanceof ISequence))) |
---|
59 | { |
---|
60 | handleEventTargetSequence |
---|
61 | (parent, startingIndex, endIndex, builder, nodeFactory, result); |
---|
62 | |
---|
63 | result.setRuleApplicationStatus |
---|
64 | (RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
65 | return result; |
---|
66 | } |
---|
67 | else { |
---|
68 | // here a new sequence on a new target begins |
---|
69 | startingIndex = index; |
---|
70 | currentEventTarget = eventTarget; |
---|
71 | } |
---|
72 | } |
---|
73 | } |
---|
74 | |
---|
75 | index++; |
---|
76 | } |
---|
77 | |
---|
78 | if (startingIndex > -1) { |
---|
79 | int endIndex = parent.getChildren().size() - 1; |
---|
80 | |
---|
81 | if (finalize) { |
---|
82 | // only reduce to a sequence, if it is not a sequence with only one child |
---|
83 | // or if this child is not a sequence itself |
---|
84 | if ((startingIndex > 0) && |
---|
85 | ((startingIndex != endIndex) || |
---|
86 | (!(parent.getChildren().get(startingIndex) instanceof ISequence)))) |
---|
87 | { |
---|
88 | handleEventTargetSequence |
---|
89 | (parent, startingIndex, endIndex, builder, nodeFactory, result); |
---|
90 | |
---|
91 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
92 | } |
---|
93 | } |
---|
94 | else { |
---|
95 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | return result; |
---|
100 | } |
---|
101 | |
---|
102 | /** |
---|
103 | * TODO: comment |
---|
104 | * |
---|
105 | * @param child |
---|
106 | * @return |
---|
107 | */ |
---|
108 | private IEventTarget determineEventTarget(ITaskTreeNode node) { |
---|
109 | if (node instanceof IEventTask) { |
---|
110 | return ((IEventTask) node).getEventTarget(); |
---|
111 | } |
---|
112 | else { |
---|
113 | IEventTarget commonTarget = null; |
---|
114 | |
---|
115 | for (ITaskTreeNode child : node.getChildren()) { |
---|
116 | if (commonTarget == null) { |
---|
117 | commonTarget = determineEventTarget(child); |
---|
118 | } |
---|
119 | else { |
---|
120 | if (!commonTarget.equals(determineEventTarget(child))) { |
---|
121 | return null; |
---|
122 | } |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | return commonTarget; |
---|
127 | } |
---|
128 | } |
---|
129 | |
---|
130 | /** |
---|
131 | * TODO: comment |
---|
132 | * |
---|
133 | */ |
---|
134 | private void handleEventTargetSequence(ITaskTreeNode parent, |
---|
135 | int startIndex, |
---|
136 | int endIndex, |
---|
137 | ITaskTreeBuilder builder, |
---|
138 | ITaskTreeNodeFactory nodeFactory, |
---|
139 | RuleApplicationResult result) |
---|
140 | { |
---|
141 | ISequence sequence = nodeFactory.createNewSequence(); |
---|
142 | |
---|
143 | for (int i = startIndex; i <= endIndex; i++) { |
---|
144 | builder.addChild(sequence, parent.getChildren().get(startIndex)); |
---|
145 | builder.removeChild((ISequence) parent, startIndex); |
---|
146 | } |
---|
147 | |
---|
148 | builder.addChild((ISequence) parent, startIndex, sequence); |
---|
149 | |
---|
150 | result.addNewlyCreatedParentNode(sequence); |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|