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

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