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 DefaultGuiElementSequenceDetectionRule()); |
---|
42 | ruleIndex.add(new DefaultEventTargetSequenceDetectionRule()); |
---|
43 | ruleIndex.add(new TrackBarSelectionDetectionRule(nodeEqualityRuleManager)); |
---|
44 | ruleIndex.add(new DefaultGuiEventSequenceDetectionRule()); |
---|
45 | ruleIndex.add(new DefaultIterationDetectionRule(nodeEqualityRuleManager)); |
---|
46 | } |
---|
47 | |
---|
48 | /** |
---|
49 | * returns true, if there is a rule that matches the current situation and if, therefore, a new |
---|
50 | * temporal relationship has been added to the tree. |
---|
51 | * |
---|
52 | * @param parent |
---|
53 | * @param newChild |
---|
54 | * @return |
---|
55 | */ |
---|
56 | public void applyRules(ITaskTreeNode parent, |
---|
57 | ITaskTreeBuilder builder, |
---|
58 | ITaskTreeNodeFactory nodeFactory, |
---|
59 | boolean finalize) |
---|
60 | { |
---|
61 | applyRules(parent, builder, nodeFactory, finalize, ""); |
---|
62 | } |
---|
63 | |
---|
64 | /** |
---|
65 | * returns true, if there is a rule that matches the current situation and if, therefore, a new |
---|
66 | * temporal relationship has been added to the tree. |
---|
67 | * |
---|
68 | * @param parent |
---|
69 | * @param newChild |
---|
70 | * @return |
---|
71 | */ |
---|
72 | private int applyRules(ITaskTreeNode parent, |
---|
73 | ITaskTreeBuilder builder, |
---|
74 | ITaskTreeNodeFactory nodeFactory, |
---|
75 | boolean finalize, |
---|
76 | String logIndent) |
---|
77 | { |
---|
78 | Console.traceln(Level.FINER, logIndent + "applying rules for " + parent); |
---|
79 | |
---|
80 | int noOfRuleApplications = 0; |
---|
81 | |
---|
82 | for (TemporalRelationshipRule rule : ruleIndex) { |
---|
83 | RuleApplicationResult result; |
---|
84 | do { |
---|
85 | // LOG.info(logIndent + "trying to apply rule " + rule + " on " + parent); |
---|
86 | result = rule.apply(parent, builder, nodeFactory, finalize); |
---|
87 | |
---|
88 | if ((result != null) && |
---|
89 | (result.getRuleApplicationStatus() == |
---|
90 | RuleApplicationStatus.RULE_APPLICATION_FINISHED)) |
---|
91 | { |
---|
92 | Console.traceln |
---|
93 | (Level.FINE, logIndent + "applied rule " + rule + " on " + parent); |
---|
94 | noOfRuleApplications++; |
---|
95 | |
---|
96 | for (ITaskTreeNode newParent : result.getNewlyCreatedParentNodes()) { |
---|
97 | noOfRuleApplications += |
---|
98 | applyRules(newParent, builder, nodeFactory, true, logIndent + " "); |
---|
99 | } |
---|
100 | } |
---|
101 | } |
---|
102 | while ((result != null) && |
---|
103 | (result.getRuleApplicationStatus() == |
---|
104 | RuleApplicationStatus.RULE_APPLICATION_FINISHED)); |
---|
105 | |
---|
106 | if ((!finalize) && |
---|
107 | (result != null) && |
---|
108 | (result.getRuleApplicationStatus() == |
---|
109 | RuleApplicationStatus.RULE_APPLICATION_FEASIBLE)) |
---|
110 | { |
---|
111 | // in this case, don't go on applying rules, which should not be applied yet |
---|
112 | break; |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | if (noOfRuleApplications <= 0) { |
---|
117 | Console.traceln(Level.INFO, logIndent + "no rules applied --> no temporal " + |
---|
118 | "relationship generated"); |
---|
119 | } |
---|
120 | |
---|
121 | return noOfRuleApplications; |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * |
---|
126 | */ |
---|
127 | /* |
---|
128 | * private void dumpTask(TaskTreeNode task, String indent) { System.err.print(indent); |
---|
129 | * System.err.print(task); System.err.println(" "); |
---|
130 | * |
---|
131 | * if ((task.getChildren() != null) && (task.getChildren().size() > 0)) { for (TaskTreeNode |
---|
132 | * child : task.getChildren()) { dumpTask(child, indent + " "); } } } |
---|
133 | */ |
---|
134 | |
---|
135 | } |
---|