source: trunk/quest-core-tasktrees/src/main/java/de/ugoe/cs/quest/tasktrees/temporalrelation/TemporalRelationshipRuleManager.java @ 809

Last change on this file since 809 was 809, checked in by pharms, 12 years ago
  • added some java doc for the classes, which are expected to be quite final
  • Property svn:executable set to *
File size: 9.5 KB
Line 
1package de.ugoe.cs.quest.tasktrees.temporalrelation;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.logging.Level;
6
7import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEquality;
8import de.ugoe.cs.quest.tasktrees.nodeequality.NodeEqualityRuleManager;
9import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeBuilder;
10import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNode;
11import de.ugoe.cs.quest.tasktrees.treeifc.ITaskTreeNodeFactory;
12import de.ugoe.cs.util.console.Console;
13
14/**
15 * <p>
16 * This class is responsible for applying temporal relationship rules on a task tree. Through this,
17 * a flat task tree is restructured to have more depth but to include more temporal relationships
18 * between task tree nodes which are not only a major sequence. I.e. through the application of
19 * rule iterations and selections of task tree nodes are detected. Which kind of temporal relations
20 * between task tree nodes are detected depends on the {@link TemporalRelationshipRule}s known to
21 * this class.
22 * </p>
23 * <p>The class holds references to the appropriate {@link TemporalRelationshipRule}s and calls
24 * their {@link TemporalRelationshipRule#apply(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
25 * method for each node in the task tree it is needed for. The general behavior of this class is
26 * the following:
27 * <ol>
28 *   <li>
29 *     An instance of this class is created using the constructor and calling the
30 *     {@link #init()} method afterwards
31 *   </li>
32 *   <li>
33 *     then the {@link #applyRules(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
34 *     method is called for a so far unstructured task tree node
35 *   </li>
36 *   <li>
37 *     the class iterates its internal list of rules and calls their
38 *     {@link TemporalRelationshipRule#apply(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
39 *     method.
40 *   </li>
41 *   <li>
42 *     the class evaluates the rule application result
43 *     <ul>
44 *       <li>
45 *         if a rule returns a rule application result that is null, the next rule is tried
46 *       </li>
47 *       <li>
48 *         if a rule returns that it would be feasible if more data was available and the rule
49 *         application shall not be finalized (see finalize parameter of the applyRules method)
50 *         the rule application is broken up
51 *       </li>
52 *       <li>
53 *         if a rule returns, that it was applied, the same rule is applied again until it returns
54 *         null or feasible. For each newly created parent node provided in the rule application
55 *         result, the {@link #applyRules(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
56 *         method is called.
57 *       </li>
58 *     </ul>
59 *   </li>
60 * </ol>
61 * Through this, all rules are tried to be applied at least once to the provided parent node and
62 * all parent nodes created during the rule application.
63 * </p>
64 *
65 * @author Patrick Harms
66 */
67public class TemporalRelationshipRuleManager {
68   
69    /**
70     * <p>
71     * the node equality manager needed by the rules to compare task tree nodes with each other
72     * </p>
73     */
74    private NodeEqualityRuleManager nodeEqualityRuleManager;
75
76    /**
77     * <p>
78     * the temporal relationship rule known to the manager. The rules are applied in the order
79     * they occur in this list.
80     * </p>
81     */
82    private List<TemporalRelationshipRule> rules = new ArrayList<TemporalRelationshipRule>();
83
84    /**
85     * <p>
86     * initialize the manager with a node equality rule manager to be used by the known rules
87     * for task tree node comparison.
88     * </p>
89     */
90    public TemporalRelationshipRuleManager(NodeEqualityRuleManager nodeEqualityRuleManager) {
91        super();
92        this.nodeEqualityRuleManager = nodeEqualityRuleManager;
93    }
94
95    /**
96     * <p>
97     * initialized the temporal relationship rule manager by instantiating the known rules and
98     * providing them with a reference to the node equality manager or other information they need.
99     * </p>
100     */
101    public void init() {
102        rules.add(new DefaultGuiElementSequenceDetectionRule());
103        rules.add(new DefaultEventTargetSequenceDetectionRule());
104        rules.add(new TrackBarSelectionDetectionRule(nodeEqualityRuleManager));
105        rules.add(new DefaultGuiEventSequenceDetectionRule());
106       
107        rules.add(new DefaultIterationDetectionRule
108                      (nodeEqualityRuleManager, NodeEquality.LEXICALLY_EQUAL));
109        rules.add(new DefaultIterationDetectionRule
110                      (nodeEqualityRuleManager, NodeEquality.SYNTACTICALLY_EQUAL));
111        rules.add(new DefaultIterationDetectionRule
112                      (nodeEqualityRuleManager, NodeEquality.SEMANTICALLY_EQUAL));
113    }
114
115    /**
116     * <p>
117     * applies the known rules to the provided parent node. For the creation of further nodes,
118     * the provided builder and node factory are utilized. If the finalize parameter is true, the
119     * rule application is finalized as far as possible without waiting for further data. If it is
120     * false, the rule application is broken up at the first rule returning, that its application
121     * would be feasible.
122     * </p>
123     *
124     * @param parent       the parent node to apply the rules on
125     * @param builder      the task tree builder to be used for linking task tree nodes with each
126     *                     other
127     * @param nodeFactory  the node factory to be used for instantiating new task tree nodes.
128     * @param finalize     used to indicate, if the rule application shall break up if a rule would
129     *                     be feasible if further data was available, or not.
130     */
131    public void applyRules(ITaskTreeNode        parent,
132                           ITaskTreeBuilder     builder,
133                           ITaskTreeNodeFactory nodeFactory,
134                           boolean              finalize)
135    {
136        applyRules(parent, builder, nodeFactory, finalize, "");
137    }
138
139    /**
140     * <p>
141     * applies the known rules to the provided parent node. For the creation of further nodes,
142     * the provided builder and node factory are utilized. If the finalize parameter is true, the
143     * rule application is finalized as far as possible without waiting for further data. If it is
144     * false, the rule application is broken up at the first rule returning, that its application
145     * would be feasible. The method calls itself for each parent node created through the rule
146     * application. In this case, the finalize parameter is always true.
147     * </p>
148     *
149     * @param parent       the parent node to apply the rules on
150     * @param builder      the task tree builder to be used for linking task tree nodes with each
151     *                     other
152     * @param nodeFactory  the node factory to be used for instantiating new task tree nodes.
153     * @param finalize     used to indicate, if the rule application shall break up if a rule would
154     *                     be feasible if further data was available, or not.
155     * @param logIndent    simply used for loggin purposes to indent the log messages depending
156     *                     on the recursion depth of calling this method.
157     */
158    private int applyRules(ITaskTreeNode        parent,
159                           ITaskTreeBuilder     builder,
160                           ITaskTreeNodeFactory nodeFactory,
161                           boolean              finalize,
162                           String               logIndent)
163    {
164        Console.traceln(Level.FINER, logIndent + "applying rules for " + parent);
165
166        int noOfRuleApplications = 0;
167
168        for (TemporalRelationshipRule rule : rules) {
169            RuleApplicationResult result;
170            do {
171                // LOG.info(logIndent + "trying to apply rule " + rule + " on " + parent);
172                result = rule.apply(parent, builder, nodeFactory, finalize);
173
174                if ((result != null) &&
175                    (result.getRuleApplicationStatus() ==
176                     RuleApplicationStatus.RULE_APPLICATION_FINISHED))
177                {
178                    Console.traceln
179                        (Level.FINE, logIndent + "applied rule " + rule + " on " + parent);
180                    noOfRuleApplications++;
181
182                    for (ITaskTreeNode newParent : result.getNewlyCreatedParentNodes()) {
183                        noOfRuleApplications +=
184                            applyRules(newParent, builder, nodeFactory, true, logIndent + "  ");
185                    }
186                }
187            }
188            while ((result != null) &&
189                   (result.getRuleApplicationStatus() ==
190                    RuleApplicationStatus.RULE_APPLICATION_FINISHED));
191
192            if ((!finalize) &&
193                (result != null) &&
194                (result.getRuleApplicationStatus() ==
195                 RuleApplicationStatus.RULE_APPLICATION_FEASIBLE))
196            {
197                // in this case, don't go on applying rules, which should not be applied yet
198                break;
199            }
200        }
201
202        if (noOfRuleApplications <= 0) {
203            Console.traceln(Level.INFO, logIndent + "no rules applied --> no temporal " +
204                            "relationship generated");
205        }
206
207        return noOfRuleApplications;
208    }
209
210    /**
211     *
212     */
213    /*
214     * private void dumpTask(TaskTreeNode task, String indent) { System.err.print(indent);
215     * System.err.print(task); System.err.println(" ");
216     *
217     * if ((task.getChildren() != null) && (task.getChildren().size() > 0)) { for (TaskTreeNode
218     * child : task.getChildren()) { dumpTask(child, indent + "  "); } } }
219     */
220
221}
Note: See TracBrowser for help on using the repository browser.