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

Last change on this file since 1189 was 1189, checked in by pharms, 11 years ago
  • remove a find bugs warning
  • Property svn:executable set to *
File size: 13.4 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.taskequality.TaskEquality;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskBuilder;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskFactory;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstanceList;
29import de.ugoe.cs.autoquest.tasktrees.treeifc.IUserSession;
30import de.ugoe.cs.util.console.Console;
31
32/**
33 * TODO update comment
34 *
35 * <p>
36 * This class is responsible for applying temporal relationship rules on a task tree. Through this,
37 * a flat task tree is restructured to have more depth but to include more temporal relationships
38 * between tasks which are not only a major sequence. I.e. through the application of the
39 * rules iterations and selections of tasks are detected. Which kind of temporal relations
40 * between tasks are detected depends on the {@link ITaskInstanceListScopeRule}s known to
41 * this class.
42 * </p>
43 * <p>The class holds references to the appropriate {@link ITaskInstanceListScopeRule}s and calls
44 * their {@link ITaskInstanceListScopeRule#apply(ITask, ITaskBuilder, ITaskFactory, boolean)}
45 * method for each task in the task tree it is needed for. The general behavior of this class is
46 * the following:
47 * <ol>
48 *   <li>
49 *     An instance of this class is created using the constructor and calling the
50 *     {@link #init()} method afterwards
51 *   </li>
52 *   <li>
53 *     then the {@link #applyRules(ITask, ITaskBuilder, ITaskFactory, boolean)}
54 *     method is called for a so far unstructured task
55 *   </li>
56 *   <li>
57 *     the class iterates its internal list of rules and calls their
58 *     {@link ITaskInstanceListScopeRule#apply(ITask, ITaskBuilder, ITaskFactory, boolean)}
59 *     method.
60 *   </li>
61 *   <li>
62 *     the class evaluates the rule application result
63 *     <ul>
64 *       <li>
65 *         if a rule returns a rule application result that is null, the next rule is tried
66 *       </li>
67 *       <li>
68 *         if a rule returns that it would be feasible if more data was available and the rule
69 *         application shall not be finalized (see finalize parameter of the applyRules method)
70 *         the rule application is broken up
71 *       </li>
72 *       <li>
73 *         if a rule returns, that it was applied, the same rule is applied again until it returns
74 *         null or feasible. For each newly created parent task provided in the rule application
75 *         result, the {@link #applyRules(ITask, ITaskBuilder, ITaskFactory, boolean)}
76 *         method is called.
77 *       </li>
78 *     </ul>
79 *   </li>
80 * </ol>
81 * Through this, all rules are tried to be applied at least once to the provided parent task and
82 * all parent tasks created during the rule application.
83 * </p>
84 *
85 * @author Patrick Harms
86 */
87public class TemporalRelationshipRuleManager {
88   
89    /**
90     * <p>
91     * the task factory to be used during rule application
92     * </p>
93     */
94    private ITaskFactory taskFactory;
95
96    /**
97     * <p>
98     * the task builder to be used during rule application
99     * </p>
100     */
101    private ITaskBuilder taskBuilder;
102
103    /**
104     * <p>
105     * the temporal relationship rules known to the manager that are executed on whole sessions.
106     * The rules are applied in the order they occur in this list.
107     * </p>
108     */
109    private ISessionScopeRule[] sessionScopeRules;
110
111    /**
112     * <p>
113     * the temporal relationship rules known to the manager that are executed on whole sub trees.
114     * The rules are applied in the order they occur in this list.
115     * </p>
116     */
117    private ITaskInstanceListScopeRule[] taskScopeRules;
118
119    /**
120     * <p>
121     * initialize the manager
122     * </p>
123     *
124     * @param taskFactory             the task factory to be used for instantiating new task tree
125     *                                tasks during rule application
126     * @param taskBuilder             the task builder to be used for linking tasks
127     *                                with each other during rule application
128     */
129    public TemporalRelationshipRuleManager(ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
130        super();
131        this.taskFactory = taskFactory;
132        this.taskBuilder = taskBuilder;
133    }
134
135    /**
136     * <p>
137     * initialized the temporal relationship rule manager by instantiating the known rules and
138     * providing them with a reference to the task equality manager or other information they need.
139     * </p>
140     */
141    public void init() {
142        List<Class<? extends IGUIElement>> frameFilter =
143            new ArrayList<Class<? extends IGUIElement>>();
144        frameFilter.add(IFrame.class);
145        frameFilter.add(IDialog.class);
146        //frameFilter.add(ICanvas.class);
147
148        sessionScopeRules = new ISessionScopeRule[] {
149            new SequenceForTaskDetectionRule
150                (TaskEquality.SEMANTICALLY_EQUAL, taskFactory, taskBuilder),
151            /*new DefaultTaskSequenceDetectionRule
152                (NodeEquality.SYNTACTICALLY_EQUAL, taskFactory, taskTreeBuilder),
153            new DefaultTaskSequenceDetectionRule
154                (NodeEquality.LEXICALLY_EQUAL, taskFactory, taskTreeBuilder),*/
155            /*new TreeScopeWrapperRule
156                (new DefaultIterationDetectionRule
157                    (NodeEquality.LEXICALLY_EQUAL, taskFactory, taskTreeBuilder)),
158            new TreeScopeWrapperRule
159                (new DefaultIterationDetectionRule
160                    (NodeEquality.SYNTACTICALLY_EQUAL, taskFactory, taskTreeBuilder)),
161            new TreeScopeWrapperRule
162                (new DefaultIterationDetectionRule
163                    (NodeEquality.SEMANTICALLY_EQUAL, taskFactory, taskTreeBuilder))*/
164        };
165       
166        //treeScopeRules.add(new DefaultGuiElementSequenceDetectionRule(frameFilter));
167
168        taskScopeRules = new ITaskInstanceListScopeRule[] {
169            //new SequenceOnGuiElementDetectionRule(taskFactory, taskTreeBuilder),
170            //new EventSequenceOnSameTargetDetectionRule(taskFactory, taskTreeBuilder),
171            //new TrackBarSelectionDetectionRule(taskFactory, taskBuilder),
172            //new DefaultGuiEventSequenceDetectionRule(taskFactory, taskTreeBuilder),
173        };
174
175    }
176
177    /**
178     * <p>
179     * applies the known rules to the provided sessions. For the creation of further tasks,
180     * the provided builder and task factory are utilized. The method expects, that no more data
181     * is available and, therefore, finalizes the rule application.
182     * </p>
183     *
184     * @param taskFactory  the task factory to be used for instantiating new tasks.
185     */
186    public void applyRules(List<IUserSession> sessions) {
187        applyRules(sessionScopeRules, sessions, "");
188    }
189
190    /**
191     * <p>
192     * applies the known rules to the provided parent task. For the creation of further tasks,
193     * the provided builder and task factory are utilized. If the finalize parameter is true, the
194     * rule application is finalized as far as possible without waiting for further data. If it is
195     * false, the rule application is broken up at the first rule returning, that its application
196     * would be feasible. The method calls itself for each parent task created through the rule
197     * application. In this case, the finalize parameter is always true.
198     * </p>
199     *
200     * @param parent       the parent task to apply the rules on
201     * @param finalize     used to indicate, if the rule application shall break up if a rule would
202     *                     be feasible if further data was available, or not.
203     * @param logIndent    simply used for logging purposes to indent the log messages depending
204     *                     on the recursion depth of calling this method.
205     */
206    private int applyRules(ISessionScopeRule[] rules,
207                           List<IUserSession>  sessions,
208                           String              logIndent)
209    {
210        Console.traceln
211            (Level.FINER, logIndent + "applying rules for " + sessions.size() + " sessions");
212
213        int noOfRuleApplications = 0;
214
215        for (ISessionScopeRule rule : rules) {
216            RuleApplicationResult result;
217            do {
218                Console.traceln(Level.FINER, logIndent + "trying rule " + rule);
219                result = rule.apply(sessions);
220
221                if ((result != null) &&
222                    (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED))
223                {
224                    Console.traceln(Level.FINE, logIndent + "applied rule " + rule);
225                    noOfRuleApplications++;
226                   
227                    //dumpTask(parent, "");
228
229                    for (ITaskInstance newParent : result.getNewlyCreatedTaskInstances()) {
230                        noOfRuleApplications +=
231                            applyRules(taskScopeRules, newParent, logIndent + "  ");
232                    }
233                }
234            }
235            while ((result != null) &&
236                   (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED));
237
238        }
239
240        if (noOfRuleApplications <= 0) {
241            Console.traceln(Level.FINE, logIndent + "no rules applied --> no temporal " +
242                            "relationship generated");
243        }
244
245        return noOfRuleApplications;
246    }
247
248    /**
249     * <p>
250     * applies the known rules to the provided parent task. For the creation of further tasks,
251     * the provided builder and task factory are utilized. If the finalize parameter is true, the
252     * rule application is finalized as far as possible without waiting for further data. If it is
253     * false, the rule application is broken up at the first rule returning, that its application
254     * would be feasible. The method calls itself for each parent task created through the rule
255     * application. In this case, the finalize parameter is always true.
256     * </p>
257     *
258     * @param parent       the parent task to apply the rules on
259     * @param finalize     used to indicate, if the rule application shall break up if a rule would
260     *                     be feasible if further data was available, or not.
261     * @param logIndent    simply used for logging purposes to indent the log messages depending
262     *                     on the recursion depth of calling this method.
263     */
264    private int applyRules(ITaskInstanceListScopeRule[] rules,
265                           ITaskInstanceList            taskInstances,
266                           String                       logIndent)
267    {
268        Console.traceln(Level.FINER, logIndent + "applying rules for " + taskInstances.size() +
269                        " task instances");
270
271        int noOfRuleApplications = 0;
272
273        for (ITaskInstanceListScopeRule rule : rules) {
274            RuleApplicationResult result;
275            do {
276                Console.traceln
277                    (Level.FINER, logIndent + "trying rule " + rule + " on " + taskInstances);
278                result = rule.apply(taskInstances);
279
280                if ((result != null) &&
281                    (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED))
282                {
283                    Console.traceln
284                        (Level.FINE, logIndent + "applied rule " + rule + " on " + taskInstances);
285                    noOfRuleApplications++;
286                   
287                    //dumpTask(parent, "");
288
289                    for (ITaskInstance newParent : result.getNewlyCreatedTaskInstances()) {
290                        noOfRuleApplications +=
291                            applyRules(taskScopeRules, newParent, logIndent + "  ");
292                    }
293                }
294            }
295            while ((result != null) &&
296                   (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED));
297        }
298
299        if (noOfRuleApplications <= 0) {
300            Console.traceln(Level.FINE, logIndent + "no rules applied --> no temporal " +
301                            "relationship generated");
302        }
303
304        return noOfRuleApplications;
305    }
306
307    /**
308     *
309     */
310    /*private void dumpTask(ITask task, String indent) {
311        StringBuffer message = new StringBuffer();
312        message.append(indent);
313        message.append(task);
314        if (task.getDescription() != null) {
315            message.append('(');
316            message.append(task.getDescription());
317            message.append(')');
318        }
319       
320        Console.traceln(Level.FINER, message.toString());
321       
322        if ((task.getChildren() != null) && (task.getChildren().size() > 0)) {
323            for (ITask child : task.getChildren()) {
324                dumpTask(child, indent + "  ");
325            }
326        }
327    }*/
328
329}
Note: See TracBrowser for help on using the repository browser.