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

Last change on this file since 1127 was 1127, checked in by pharms, 11 years ago
  • complete refactoring of task detection
  • many performance improvements in task detection
  • improved merging of sequences using Myers diff algorithm
File size: 7.3 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;
18
19import de.ugoe.cs.autoquest.eventcore.IEventTarget;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
25
26/**
27 * This rule subdivides long sequences into subsequences of tasks on the same event target.
28 * Event targets are compared using the equals method. A more complex GUI model is ignored.
29 *
30 * @version $Revision: $ $Date: 18.03.2012$
31 * @author 2012, last modified by $Author: patrick$
32 */
33class SequenceOnSameTargetDetectionRule implements TemporalRelationshipRule {
34
35    /**
36     * <p>
37     * the task tree node factory to be used for creating substructures for the temporal
38     * relationships identified during rule
39     * </p>
40     */
41    private ITaskTreeNodeFactory taskTreeNodeFactory;
42    /**
43     * <p>
44     * the task tree builder to be used for creating substructures for the temporal relationships
45     * identified during rule application
46     * </p>
47     */
48    private ITaskTreeBuilder taskTreeBuilder;
49   
50    /**
51     * <p>
52     * instantiates the rule with a task tree node factory and builder to be used during rule
53     * application.
54     * </p>
55     *
56     * @param taskTreeNodeFactory the task tree node factory to be used for creating substructures
57     *                            for the temporal relationships identified during rule
58     *                            application
59     * @param taskTreeBuilder     the task tree builder to be used for creating substructures for
60     *                            the temporal relationships identified during rule application
61     */
62    SequenceOnSameTargetDetectionRule(ITaskTreeNodeFactory taskTreeNodeFactory,
63                                      ITaskTreeBuilder     taskTreeBuilder)
64    {
65        this.taskTreeNodeFactory = taskTreeNodeFactory;
66        this.taskTreeBuilder = taskTreeBuilder;
67    }
68   
69    /* (non-Javadoc)
70     * @see java.lang.Object#toString()
71     */
72    @Override
73    public String toString() {
74        return "SequenceOnSameTargetDetectionRule";
75    }
76
77    /*
78     * (non-Javadoc)
79     *
80     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
81     * boolean)
82     */
83    @Override
84    public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) {
85        if (!(parent instanceof ISequence)) {
86            return null;
87        }
88
89        RuleApplicationResult result = new RuleApplicationResult();
90
91        IEventTarget currentEventTarget = null;
92        int startingIndex = -1;
93
94        int index = 0;
95        List<ITaskTreeNode> children = parent.getChildren();
96       
97        while (index < children.size()) {
98            ITaskTreeNode child = children.get(index);
99
100            IEventTarget eventTarget = determineEventTarget(child);
101
102            if (((eventTarget == null) && (currentEventTarget != null)) ||
103                ((eventTarget != null) && (!eventTarget.equals(currentEventTarget))))
104            {
105                if (startingIndex < 0) {
106                    startingIndex = index;
107                    currentEventTarget = eventTarget;
108                }
109                else {
110                    int endIndex = index - 1;
111                   
112                    // only reduce to a sequence, if it is not a sequence with only one child
113                    // or if this child is not a sequence itself
114                    if ((startingIndex != endIndex) ||
115                        (!(children.get(startingIndex) instanceof ISequence)))
116                    {
117                        handleEventTargetSequence
118                            (parent, currentEventTarget, startingIndex, endIndex, result);
119
120                        result.setRuleApplicationStatus(RuleApplicationStatus.FINISHED);
121                        return result;
122                    }
123                    else if (eventTarget != null) {
124                        // here a new sequence on a new target begins
125                        startingIndex = index;
126                        currentEventTarget = eventTarget;
127                    }
128                    else {
129                        startingIndex = -1;
130                        currentEventTarget = null;
131                    }
132                }
133            }
134
135            index++;
136        }
137
138        if (startingIndex > -1) {
139            int endIndex = children.size() - 1;
140           
141            if (finalize) {
142                // only reduce to a sequence, if it is not a sequence with only one child
143                // or if this child is not a sequence itself
144                if ((startingIndex > 0) &&
145                    ((startingIndex != endIndex) ||
146                     (!(children.get(startingIndex) instanceof ISequence))))
147                {
148                    handleEventTargetSequence
149                        (parent, currentEventTarget, startingIndex, endIndex, result);
150               
151                    result.setRuleApplicationStatus(RuleApplicationStatus.FINISHED);
152                }
153            }
154            else {
155                result.setRuleApplicationStatus(RuleApplicationStatus.FEASIBLE);
156            }
157        }
158
159        return result;
160    }
161
162    /**
163     *
164     */
165    private IEventTarget determineEventTarget(ITaskTreeNode node) {
166        if (node instanceof IEventTask) {
167            return ((IEventTask) node).getEventTarget();
168        }
169        else {
170            IEventTarget commonTarget = null;
171           
172            for (ITaskTreeNode child : node.getChildren()) {
173                if (commonTarget == null) {
174                    commonTarget = determineEventTarget(child);
175                }
176                else {
177                    if (!commonTarget.equals(determineEventTarget(child))) {
178                        return null;
179                    }
180                }
181            }
182           
183            return commonTarget;
184        }
185    }
186
187    /**
188     *
189     */
190    private void handleEventTargetSequence(ITaskTreeNode         parent,
191                                           IEventTarget          target,
192                                           int                   startIndex,
193                                           int                   endIndex,
194                                           RuleApplicationResult result)
195    {
196        String description = "interactions on " + target.getStringIdentifier();
197       
198        ISequence sequence = RuleUtils.createNewSubSequenceInRange
199            (parent, startIndex, endIndex, description, taskTreeNodeFactory, taskTreeBuilder);
200
201        result.addNewlyCreatedParentNode(sequence);
202    }
203
204}
Note: See TracBrowser for help on using the repository browser.