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

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