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

Last change on this file since 1131 was 1131, checked in by pharms, 11 years ago
  • removed compiler warning
  • Property svn:executable set to *
File size: 13.0 KB
Line 
1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
15package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
16
17import java.util.ArrayList;
18import java.util.List;
19import java.util.logging.Level;
20
21import de.ugoe.cs.autoquest.eventcore.guimodel.IDialog;
22import de.ugoe.cs.autoquest.eventcore.guimodel.IFrame;
23import de.ugoe.cs.autoquest.eventcore.guimodel.IGUIElement;
24import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality;
25import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
29import de.ugoe.cs.util.console.Console;
30
31/**
32 * <p>
33 * This class is responsible for applying temporal relationship rules on a task tree. Through this,
34 * a flat task tree is restructured to have more depth but to include more temporal relationships
35 * between task tree nodes which are not only a major sequence. I.e. through the application of
36 * rule iterations and selections of task tree nodes are detected. Which kind of temporal relations
37 * between task tree nodes are detected depends on the {@link TemporalRelationshipRule}s known to
38 * this class.
39 * </p>
40 * <p>The class holds references to the appropriate {@link TemporalRelationshipRule}s and calls
41 * their {@link TemporalRelationshipRule#apply(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
42 * method for each node in the task tree it is needed for. The general behavior of this class is
43 * the following:
44 * <ol>
45 *   <li>
46 *     An instance of this class is created using the constructor and calling the
47 *     {@link #init()} method afterwards
48 *   </li>
49 *   <li>
50 *     then the {@link #applyRules(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
51 *     method is called for a so far unstructured task tree node
52 *   </li>
53 *   <li>
54 *     the class iterates its internal list of rules and calls their
55 *     {@link TemporalRelationshipRule#apply(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
56 *     method.
57 *   </li>
58 *   <li>
59 *     the class evaluates the rule application result
60 *     <ul>
61 *       <li>
62 *         if a rule returns a rule application result that is null, the next rule is tried
63 *       </li>
64 *       <li>
65 *         if a rule returns that it would be feasible if more data was available and the rule
66 *         application shall not be finalized (see finalize parameter of the applyRules method)
67 *         the rule application is broken up
68 *       </li>
69 *       <li>
70 *         if a rule returns, that it was applied, the same rule is applied again until it returns
71 *         null or feasible. For each newly created parent node provided in the rule application
72 *         result, the {@link #applyRules(ITaskTreeNode, ITaskTreeBuilder, ITaskTreeNodeFactory, boolean)}
73 *         method is called.
74 *       </li>
75 *     </ul>
76 *   </li>
77 * </ol>
78 * Through this, all rules are tried to be applied at least once to the provided parent node and
79 * all parent nodes created during the rule application.
80 * </p>
81 *
82 * @author Patrick Harms
83 */
84public class TemporalRelationshipRuleManager {
85   
86    /**
87     * <p>
88     * the node equality manager needed by the rules to compare task tree nodes with each other
89     * </p>
90     */
91    private NodeEqualityRuleManager nodeEqualityRuleManager;
92
93    /**
94     * <p>
95     * the task tree node factory to be used during rule application
96     * </p>
97     */
98    private ITaskTreeNodeFactory taskTreeNodeFactory;
99
100    /**
101     * <p>
102     * the task tree builder to be used during rule application
103     * </p>
104     */
105    private ITaskTreeBuilder taskTreeBuilder;
106
107    /**
108     * <p>
109     * the temporal relationship rules known to the manager that are executed on whole sub trees.
110     * The rules are applied in the order they occur in this list.
111     * </p>
112     */
113    private TemporalRelationshipRule[] treeScopeRules;
114
115    /**
116     * <p>
117     * the temporal relationship rules known to the manager that are executed on whole sub trees.
118     * The rules are applied in the order they occur in this list.
119     * </p>
120     */
121    private TemporalRelationshipRule[] nodeScopeRules;
122
123    /**
124     * <p>
125     * initialize the manager
126     * </p>
127     *
128     * @param nodeEqualityRuleManager the node equality rule manager to be used by the known rules
129     *                                for task tree node comparison during rule application
130     * @param taskTreeNodeFactory     the node factory to be used for instantiating new task tree
131     *                                nodes during rule application
132     * @param taskTreeBuilder         the task tree builder to be used for linking task tree nodes
133     *                                with each other during rule application
134     */
135    public TemporalRelationshipRuleManager(NodeEqualityRuleManager nodeEqualityRuleManager,
136                                           ITaskTreeNodeFactory    taskTreeNodeFactory,
137                                           ITaskTreeBuilder        taskTreeBuilder)
138    {
139        super();
140        this.nodeEqualityRuleManager = nodeEqualityRuleManager;
141        this.taskTreeNodeFactory = taskTreeNodeFactory;
142        this.taskTreeBuilder = taskTreeBuilder;
143    }
144
145    /**
146     * <p>
147     * initialized the temporal relationship rule manager by instantiating the known rules and
148     * providing them with a reference to the node equality manager or other information they need.
149     * </p>
150     */
151    public void init() {
152        List<Class<? extends IGUIElement>> frameFilter =
153            new ArrayList<Class<? extends IGUIElement>>();
154        frameFilter.add(IFrame.class);
155        frameFilter.add(IDialog.class);
156        //frameFilter.add(ICanvas.class);
157
158        treeScopeRules = new TemporalRelationshipRule[] {
159            new SequenceForTaskDetectionRule
160                (nodeEqualityRuleManager, NodeEquality.SEMANTICALLY_EQUAL,
161                 taskTreeNodeFactory, taskTreeBuilder),
162            /*new DefaultTaskSequenceDetectionRule
163                (nodeEqualityRuleManager, NodeEquality.SYNTACTICALLY_EQUAL,
164                 taskTreeNodeFactory, taskTreeBuilder),
165            new DefaultTaskSequenceDetectionRule
166                (nodeEqualityRuleManager, NodeEquality.LEXICALLY_EQUAL,
167                 taskTreeNodeFactory, taskTreeBuilder),*/
168            /*new TreeScopeWrapperRule
169                (new DefaultIterationDetectionRule
170                    (nodeEqualityRuleManager, NodeEquality.LEXICALLY_EQUAL,
171                     taskTreeNodeFactory, taskTreeBuilder)),
172            new TreeScopeWrapperRule
173                (new DefaultIterationDetectionRule
174                    (nodeEqualityRuleManager, NodeEquality.SYNTACTICALLY_EQUAL,
175                     taskTreeNodeFactory, taskTreeBuilder)),
176            new TreeScopeWrapperRule
177                (new DefaultIterationDetectionRule
178                    (nodeEqualityRuleManager, NodeEquality.SEMANTICALLY_EQUAL,
179                     taskTreeNodeFactory, taskTreeBuilder))*/
180        };
181       
182        //treeScopeRules.add(new DefaultGuiElementSequenceDetectionRule(frameFilter));
183
184        nodeScopeRules = new TemporalRelationshipRule[] {
185            //new SequenceOnGuiElementDetectionRule(taskTreeNodeFactory, taskTreeBuilder),
186            //new EventSequenceOnSameTargetDetectionRule(taskTreeNodeFactory, taskTreeBuilder),
187            new TrackBarSelectionDetectionRule
188                (nodeEqualityRuleManager, taskTreeNodeFactory, taskTreeBuilder),
189            //new DefaultGuiEventSequenceDetectionRule(taskTreeNodeFactory, taskTreeBuilder),
190        };
191
192    }
193
194    /**
195     * <p>
196     * applies the known rules to the provided parent node. For the creation of further nodes,
197     * the provided builder and node factory are utilized. The method expectes, that no more data
198     * is available and, therefore, finalizes the rule application.
199     * </p>
200     *
201     * @param nodeFactory  the node factory to be used for instantiating new task tree nodes.
202     */
203    public void applyRules(ITaskTreeNode parent) {
204        applyRules(parent, true);
205    }
206
207    /**
208     * <p>
209     * applies the known rules to the provided parent node. For the creation of further nodes,
210     * the provided builder and node factory are utilized. If the finalize parameter is true, the
211     * rule application is finalized as far as possible without waiting for further data. If it is
212     * false, the rule application is broken up at the first rule returning, that its application
213     * would be feasible.
214     * </p>
215     *
216     * @param parent       the parent node to apply the rules on
217     * @param finalize     used to indicate, if the rule application shall break up if a rule would
218     *                     be feasible if further data was available, or not.
219     */
220    public void applyRules(ITaskTreeNode parent, boolean finalize) {
221        applyRules(treeScopeRules, parent, finalize, "");
222    }
223
224    /**
225     * <p>
226     * applies the known rules to the provided parent node. For the creation of further nodes,
227     * the provided builder and node factory are utilized. If the finalize parameter is true, the
228     * rule application is finalized as far as possible without waiting for further data. If it is
229     * false, the rule application is broken up at the first rule returning, that its application
230     * would be feasible. The method calls itself for each parent node created through the rule
231     * application. In this case, the finalize parameter is always true.
232     * </p>
233     *
234     * @param parent       the parent node to apply the rules on
235     * @param finalize     used to indicate, if the rule application shall break up if a rule would
236     *                     be feasible if further data was available, or not.
237     * @param logIndent    simply used for logging purposes to indent the log messages depending
238     *                     on the recursion depth of calling this method.
239     */
240    private int applyRules(TemporalRelationshipRule[] rules,
241                           ITaskTreeNode              parent,
242                           boolean                    finalize,
243                           String                     logIndent)
244    {
245        Console.traceln(Level.FINER, logIndent + "applying rules for " + parent);
246
247        int noOfRuleApplications = 0;
248
249        for (TemporalRelationshipRule rule : rules) {
250            RuleApplicationResult result;
251            do {
252                Console.traceln(Level.FINER, logIndent + "trying rule " + rule + " on " + parent);
253                result = rule.apply(parent, finalize);
254
255                if ((result != null) &&
256                    (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED))
257                {
258                    Console.traceln
259                        (Level.FINE, logIndent + "applied rule " + rule + " on " + parent);
260                    noOfRuleApplications++;
261                   
262                    //dumpTask(parent, "");
263
264                    for (ITaskTreeNode newParent : result.getNewlyCreatedParentNodes()) {
265                        noOfRuleApplications +=
266                            applyRules(nodeScopeRules, newParent, true, logIndent + "  ");
267                    }
268                }
269            }
270            while ((result != null) &&
271                   (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED));
272
273            if ((!finalize) &&
274                (result != null) &&
275                (result.getRuleApplicationStatus() == RuleApplicationStatus.FEASIBLE))
276            {
277                // in this case, don't go on applying rules, which should not be applied yet
278                break;
279            }
280        }
281
282        if (noOfRuleApplications <= 0) {
283            Console.traceln(Level.FINE, logIndent + "no rules applied --> no temporal " +
284                            "relationship generated");
285        }
286
287        return noOfRuleApplications;
288    }
289
290    /**
291     *
292     */
293    /*private void dumpTask(ITaskTreeNode task, String indent) {
294        StringBuffer message = new StringBuffer();
295        message.append(indent);
296        message.append(task);
297        if (task.getDescription() != null) {
298            message.append('(');
299            message.append(task.getDescription());
300            message.append(')');
301        }
302       
303        Console.traceln(Level.FINER, message.toString());
304       
305        if ((task.getChildren() != null) && (task.getChildren().size() > 0)) {
306            for (ITaskTreeNode child : task.getChildren()) {
307                dumpTask(child, indent + "  ");
308            }
309        }
310    }*/
311
312}
Note: See TracBrowser for help on using the repository browser.