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