1 | package de.ugoe.cs.quest.tasktrees.temporalrelation; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | import java.util.logging.Level; |
---|
6 | |
---|
7 | import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager; |
---|
8 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder; |
---|
9 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode; |
---|
10 | import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory; |
---|
11 | import de.ugoe.cs.util.console.Console; |
---|
12 | |
---|
13 | /** |
---|
14 | * TODO comment |
---|
15 | * |
---|
16 | * @version $Revision: $ $Date: 12.02.2012$ |
---|
17 | * @author 2012, last modified by $Author: patrick$ |
---|
18 | */ |
---|
19 | public class TemporalRelationshipRuleManager { |
---|
20 | |
---|
21 | /** */ |
---|
22 | private NodeEqualityRuleManager nodeEqualityRuleManager; |
---|
23 | |
---|
24 | /** */ |
---|
25 | private List<TemporalRelationshipRule> ruleIndex = new ArrayList<TemporalRelationshipRule>(); |
---|
26 | |
---|
27 | /** |
---|
28 | * TODO: comment |
---|
29 | * |
---|
30 | */ |
---|
31 | public TemporalRelationshipRuleManager(NodeEqualityRuleManager nodeEqualityRuleManager) { |
---|
32 | super(); |
---|
33 | this.nodeEqualityRuleManager = nodeEqualityRuleManager; |
---|
34 | } |
---|
35 | |
---|
36 | /** |
---|
37 | * TODO: comment |
---|
38 | * |
---|
39 | */ |
---|
40 | public void init() { |
---|
41 | ruleIndex.add(new DefaultEventTargetSequenceDetectionRule()); |
---|
42 | ruleIndex.add(new TrackBarSelectionDetectionRule(nodeEqualityRuleManager)); |
---|
43 | ruleIndex.add(new DefaultGuiEventSequenceDetectionRule()); |
---|
44 | ruleIndex.add(new DefaultIterationDetectionRule(nodeEqualityRuleManager)); |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * returns true, if there is a rule that matches the current situation and if, therefore, a new |
---|
49 | * temporal relationship has been added to the tree. |
---|
50 | * |
---|
51 | * @param parent |
---|
52 | * @param newChild |
---|
53 | * @return |
---|
54 | */ |
---|
55 | public void applyRules(ITaskTreeNode parent, |
---|
56 | ITaskTreeBuilder builder, |
---|
57 | ITaskTreeNodeFactory nodeFactory, |
---|
58 | boolean finalize) |
---|
59 | { |
---|
60 | applyRules(parent, builder, nodeFactory, finalize, ""); |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * returns true, if there is a rule that matches the current situation and if, therefore, a new |
---|
65 | * temporal relationship has been added to the tree. |
---|
66 | * |
---|
67 | * @param parent |
---|
68 | * @param newChild |
---|
69 | * @return |
---|
70 | */ |
---|
71 | private int applyRules(ITaskTreeNode parent, |
---|
72 | ITaskTreeBuilder builder, |
---|
73 | ITaskTreeNodeFactory nodeFactory, |
---|
74 | boolean finalize, |
---|
75 | String logIndent) |
---|
76 | { |
---|
77 | Console.traceln(Level.FINER, logIndent + "applying rules for " + parent); |
---|
78 | |
---|
79 | int noOfRuleApplications = 0; |
---|
80 | |
---|
81 | for (TemporalRelationshipRule rule : ruleIndex) { |
---|
82 | RuleApplicationResult result; |
---|
83 | do { |
---|
84 | // LOG.info(logIndent + "trying to apply rule " + rule + " on " + parent); |
---|
85 | result = rule.apply(parent, builder, nodeFactory, finalize); |
---|
86 | |
---|
87 | if ((result != null) && |
---|
88 | (result.getRuleApplicationStatus() == |
---|
89 | RuleApplicationStatus.RULE_APPLICATION_FINISHED)) |
---|
90 | { |
---|
91 | Console.traceln |
---|
92 | (Level.FINE, logIndent + "applied rule " + rule + " on " + parent); |
---|
93 | noOfRuleApplications++; |
---|
94 | |
---|
95 | for (ITaskTreeNode newParent : result.getNewlyCreatedParentNodes()) { |
---|
96 | noOfRuleApplications += |
---|
97 | applyRules(newParent, builder, nodeFactory, true, logIndent + " "); |
---|
98 | } |
---|
99 | } |
---|
100 | } |
---|
101 | while ((result != null) && |
---|
102 | (result.getRuleApplicationStatus() == |
---|
103 | RuleApplicationStatus.RULE_APPLICATION_FINISHED)); |
---|
104 | |
---|
105 | if ((!finalize) && |
---|
106 | (result != null) && |
---|
107 | (result.getRuleApplicationStatus() == |
---|
108 | RuleApplicationStatus.RULE_APPLICATION_FEASIBLE)) |
---|
109 | { |
---|
110 | // in this case, don't go on applying rules, which should not be applied yet |
---|
111 | break; |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | if (noOfRuleApplications <= 0) { |
---|
116 | Console.traceln(Level.INFO, logIndent + "no rules applied --> no temporal " + |
---|
117 | "relationship generated"); |
---|
118 | } |
---|
119 | |
---|
120 | return noOfRuleApplications; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * |
---|
125 | */ |
---|
126 | /* |
---|
127 | * private void dumpTask(TaskTreeNode task, String indent) { System.err.print(indent); |
---|
128 | * System.err.print(task); System.err.println(" "); |
---|
129 | * |
---|
130 | * if ((task.getChildren() != null) && (task.getChildren().size() > 0)) { for (TaskTreeNode |
---|
131 | * child : task.getChildren()) { dumpTask(child, indent + " "); } } } |
---|
132 | */ |
---|
133 | |
---|
134 | } |
---|