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

Last change on this file since 2218 was 2218, checked in by pharms, 7 years ago
  • java doc issues removal
  • Property svn:executable set to *
File size: 9.1 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.IUserSession;
28import de.ugoe.cs.util.console.Console;
29
30/**
31 * TODO update comment
32 *
33 * <p>
34 * This class is responsible for applying temporal relationship rules on a task tree. Through this,
35 * a flat task tree is restructured to have more depth but to include more temporal relationships
36 * between tasks which are not only a major sequence. I.e. through the application of the
37 * rules iterations and selections of tasks are detected. Which kind of temporal relations
38 * between tasks are detected depends on the {@link ITaskInstanceScopeRule}s known to
39 * this class.
40 * </p>
41 * <p>The class holds references to the appropriate {@link ITaskInstanceScopeRule}s and calls
42 * their {@link ITaskInstanceScopeRule#apply(de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskInstance)}
43 * method for each task in the task tree it is needed for. The general behavior of this class is
44 * the following:
45 * </p>
46 * <ol>
47 *   <li>
48 *     An instance of this class is created using the constructor and calling the
49 *     {@link #init()} method afterwards
50 *   </li>
51 *   <li>
52 *     then the {@link #applyTaskDetectionRule(List, TaskEquality, int)}
53 *     method is called for a so far unstructured task
54 *   </li>
55 *   <li>
56 *     the class iterates its internal list of rules and calls their
57 *     {@link ITaskInstanceScopeRule#apply(ITask, ITaskBuilder, ITaskFactory, boolean)}
58 *     method.
59 *   </li>
60 *   <li>
61 *     the class evaluates the rule application result
62 *     <ul>
63 *       <li>
64 *         if a rule returns a rule application result that is null, the next rule is tried
65 *       </li>
66 *       <li>
67 *         if a rule returns that it would be feasible if more data was available and the rule
68 *         application shall not be finalized (see finalize parameter of the applyRules method)
69 *         the rule application is broken up
70 *       </li>
71 *       <li>
72 *         if a rule returns, that it was applied, the same rule is applied again until it returns
73 *         null or feasible. For each newly created parent task provided in the rule application
74 *         result, the {@link #applyRules(ITask, ITaskBuilder, ITaskFactory, boolean)}
75 *         method is called.
76 *       </li>
77 *     </ul>
78 *   </li>
79 * </ol>
80 * <p>
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     * initialize the manager
106     * </p>
107     *
108     * @param taskFactory             the task factory to be used for instantiating new task tree
109     *                                tasks during rule application
110     * @param taskBuilder             the task builder to be used for linking tasks
111     *                                with each other during rule application
112     */
113    public TemporalRelationshipRuleManager(ITaskFactory taskFactory, ITaskBuilder taskBuilder) {
114        super();
115        this.taskFactory = taskFactory;
116        this.taskBuilder = taskBuilder;
117    }
118
119    /**
120     * <p>
121     * initialized the temporal relationship rule manager by instantiating the known rules and
122     * providing them with a reference to the task equality manager or other information they need.
123     * </p>
124     */
125    public void init() {
126        List<Class<? extends IGUIElement>> frameFilter =
127            new ArrayList<Class<? extends IGUIElement>>();
128        frameFilter.add(IFrame.class);
129        frameFilter.add(IDialog.class);
130        //frameFilter.add(ICanvas.class);
131    }
132
133    /**
134     * <p>
135     * applies only the known rules for task detection, i.e., currently only the sequence for task
136     * detection rule
137     * </p>
138     *
139     * @param sessions  the sessions to be processed
140     */
141    public void applyTaskDetectionRule(List<IUserSession> sessions,
142                                       TaskEquality       minimalTaskEquality,
143                                       int                minimumSequenceCoverage)
144    {
145        ISessionScopeRule[] rules = new ISessionScopeRule[] {
146            new SequenceForTaskDetectionRule(minimalTaskEquality, taskFactory, taskBuilder,
147                                             minimumSequenceCoverage)
148        };
149       
150        applyRules(rules, sessions, "");
151    }
152
153    /**
154     * <p>
155     * applies only the known rules for task merging, i.e., currently only the condense similar task
156     * rule
157     * </p>
158     *
159     * @param sessions  the sessions to be processed
160     */
161    public void applyTaskMergingRule(List<IUserSession> sessions) {
162        ISessionScopeRule[] rules = new ISessionScopeRule[] {
163            new CondenseSimilarTasksRule
164                (TaskEquality.SEMANTICALLY_EQUAL, taskFactory, taskBuilder)
165        };
166       
167        applyRules(rules, sessions, "");
168    }
169
170    /**
171     * <p>
172     * applies the known rules to the provided parent task. For the creation of further tasks,
173     * the provided builder and task factory are utilized. If the finalize parameter is true, the
174     * rule application is finalized as far as possible without waiting for further data. If it is
175     * false, the rule application is broken up at the first rule returning, that its application
176     * would be feasible. The method calls itself for each parent task created through the rule
177     * application. In this case, the finalize parameter is always true.
178     * </p>
179     *
180     * @param parent       the parent task to apply the rules on
181     * @param finalize     used to indicate, if the rule application shall break up if a rule would
182     *                     be feasible if further data was available, or not.
183     * @param logIndent    simply used for logging purposes to indent the log messages depending
184     *                     on the recursion depth of calling this method.
185     */
186    private int applyRules(ISessionScopeRule[] rules,
187                           List<IUserSession>  sessions,
188                           String              logIndent)
189    {
190        Console.traceln
191            (Level.FINER, logIndent + "applying rules for " + sessions.size() + " sessions");
192
193        int noOfRuleApplications = 0;
194
195        for (ISessionScopeRule rule : rules) {
196            RuleApplicationResult result;
197            do {
198                Console.traceln(Level.FINER, logIndent + "trying rule " + rule);
199                result = rule.apply(sessions);
200
201                if ((result != null) &&
202                    (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED))
203                {
204                    Console.traceln(Level.FINE, logIndent + "applied rule " + rule);
205                    noOfRuleApplications++;
206                   
207                    //dumpTask(parent, "");
208                }
209            }
210            while ((result != null) &&
211                   (result.getRuleApplicationStatus() == RuleApplicationStatus.FINISHED));
212
213        }
214
215        if (noOfRuleApplications <= 0) {
216            Console.traceln(Level.FINE, logIndent + "no rules applied --> no temporal " +
217                            "relationship generated");
218        }
219
220        return noOfRuleApplications;
221    }
222
223    /**
224     *
225     */
226    /*private void dumpTask(ITask task, String indent) {
227        StringBuffer message = new StringBuffer();
228        message.append(indent);
229        message.append(task);
230        if (task.getDescription() != null) {
231            message.append('(');
232            message.append(task.getDescription());
233            message.append(')');
234        }
235       
236        Console.traceln(Level.FINER, message.toString());
237       
238        if ((task.getChildren() != null) && (task.getChildren().size() > 0)) {
239            for (ITask child : task.getChildren()) {
240                dumpTask(child, indent + "  ");
241            }
242        }
243    }*/
244
245}
Note: See TracBrowser for help on using the repository browser.