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 | handleEventTargetSequence(parent, startingIndex, index - 1, builder, |
---|
54 | nodeFactory, result); |
---|
55 | |
---|
56 | return result; |
---|
57 | } |
---|
58 | } |
---|
59 | |
---|
60 | index++; |
---|
61 | } |
---|
62 | |
---|
63 | if (startingIndex > -1) { |
---|
64 | if (finalize && (startingIndex > 0)) { |
---|
65 | handleEventTargetSequence(parent, startingIndex, parent.getChildren().size() - 1, |
---|
66 | builder, nodeFactory, result); |
---|
67 | } |
---|
68 | else { |
---|
69 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE); |
---|
70 | } |
---|
71 | } |
---|
72 | |
---|
73 | return result; |
---|
74 | } |
---|
75 | |
---|
76 | /** |
---|
77 | * TODO: comment |
---|
78 | * |
---|
79 | * @param child |
---|
80 | * @return |
---|
81 | */ |
---|
82 | private IEventTarget determineEventTarget(ITaskTreeNode node) { |
---|
83 | if (node instanceof IEventTask) { |
---|
84 | return ((IEventTask) node).getEventTarget(); |
---|
85 | } |
---|
86 | else { |
---|
87 | return null; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * TODO: comment |
---|
93 | * |
---|
94 | */ |
---|
95 | private void handleEventTargetSequence(ITaskTreeNode parent, |
---|
96 | int startIndex, |
---|
97 | int endIndex, |
---|
98 | ITaskTreeBuilder builder, |
---|
99 | ITaskTreeNodeFactory nodeFactory, |
---|
100 | RuleApplicationResult result) |
---|
101 | { |
---|
102 | ISequence sequence = nodeFactory.createNewSequence(); |
---|
103 | |
---|
104 | for (int i = startIndex; i <= endIndex; i++) { |
---|
105 | builder.addChild(sequence, parent.getChildren().get(startIndex)); |
---|
106 | builder.removeChild((ISequence) parent, startIndex); |
---|
107 | } |
---|
108 | |
---|
109 | builder.addChild((ISequence) parent, startIndex, sequence); |
---|
110 | |
---|
111 | result.addNewlyCreatedParentNode(sequence); |
---|
112 | result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED); |
---|
113 | } |
---|
114 | |
---|
115 | } |
---|