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

Last change on this file since 987 was 987, checked in by pharms, 12 years ago
  • improvement and documentation of the creation of sub sequences based on the GUI hierarchy
  • Property svn:executable set to *
File size: 6.1 KB
Line 
1package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
2
3import de.ugoe.cs.autoquest.eventcore.gui.IInteraction;
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 generates sequences of events depending on the event types, more concrete, the
12 * {@link IInteraction}s and the return values of their {@link IInteraction#startsLogicalSequence()}
13 * and {@link IInteraction#finishesLogicalSequence()}. If a new logical sequence is started by
14 * an interaction, then a real sequence is instantiated. The sequence is finished, if an
15 * interaction finishes a logical sequence. Examples include keyboard focus changes.
16 *
17 * @version $Revision: $ $Date: 18.03.2012$
18 * @author 2012, last modified by $Author: patrick$
19 */
20public class DefaultGuiEventSequenceDetectionRule implements TemporalRelationshipRule {
21
22    /*
23     * (non-Javadoc)
24     *
25     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
26     * TaskTreeBuilder, TaskTreeNodeFactory)
27     */
28    @Override
29    public RuleApplicationResult apply(ITaskTreeNode        parent,
30                                       ITaskTreeBuilder     builder,
31                                       ITaskTreeNodeFactory nodeFactory,
32                                       boolean              finalize)
33    {
34        if (!(parent instanceof ISequence)) {
35            return null;
36        }
37
38        RuleApplicationResult result = new RuleApplicationResult();
39        int sequenceStartingIndex = -1;
40
41        int index = 0;
42        while (index < parent.getChildren().size()) {
43            ITaskTreeNode child = parent.getChildren().get(index);
44
45            if ((child instanceof IEventTask) &&
46                (((IEventTask) child).getEventType() instanceof IInteraction))
47            {
48                IInteraction eventType = (IInteraction) ((IEventTask) child).getEventType();
49               
50                if (eventType.finishesLogicalSequence() && (sequenceStartingIndex > -1))
51                {
52                    // There are several situations in which this implementation may cause infinite
53                    // loops. This is because the rule manager will reapply rules until
54                    // no rule is applied anymore. A sequence identified in a first iteration will
55                    // be identified as a sequence also in a second iteration. As an example
56                    // many sequences start with an interaction starting that sequence and end
57                    // with an interaction ending that sequence. This will be reidentified as
58                    // further subsequence. It must therefore be assured, that a sequence, that
59                    // was once identified is not reidentified in a further application of the rule.
60                    // For this, the implementation performs a kind of dry run. It creates a list of
61                    // children that would belong to an identified sequence. Only if this list is
62                    // not a reidentification then a new sequence is created and added to the
63                    // parent. If it is a reidentification can be identified, if the list of
64                    // children will contain all children of the parent, or if the list of children
65                    // only consists of one sequence. Further, an identified sequence must at least
66                    // have one child.
67                    if (((sequenceStartingIndex != 0) ||
68                         (index != (parent.getChildren().size() - 1))) &&
69                        (((index - sequenceStartingIndex) > 0) ||
70                          (((index - sequenceStartingIndex) == 0) &&
71                           (!eventType.startsLogicalSequence()))))
72                    {
73                        boolean allNewChildrenAreSequences = true;
74
75                        for (int j = sequenceStartingIndex;
76                             ((allNewChildrenAreSequences) && (j < index)); j++)
77                        {
78                            allNewChildrenAreSequences &=
79                                (parent.getChildren().get(j) instanceof ISequence);
80                        }
81
82                        if (!allNewChildrenAreSequences) {
83                            ISequence sequence = nodeFactory.createNewSequence();
84
85                            for (int j = sequenceStartingIndex; j < index; j++) {
86                                builder.addChild
87                                    (sequence, parent.getChildren().get(sequenceStartingIndex));
88                                builder.removeChild((ISequence) parent, sequenceStartingIndex);
89                            }
90
91                            if (!eventType.startsLogicalSequence()) {
92                                builder.addChild
93                                    (sequence, parent.getChildren().get(sequenceStartingIndex));
94                                builder.removeChild((ISequence) parent, sequenceStartingIndex);
95                            }
96
97                            builder.addChild((ISequence) parent, sequenceStartingIndex, sequence);
98
99                            result.addNewlyCreatedParentNode(sequence);
100                           
101                            builder.setDescription
102                                (sequence, "logical sequence started by the first event");
103                           
104                            result.setRuleApplicationStatus
105                                (RuleApplicationStatus.RULE_APPLICATION_FINISHED);
106                            return result;
107                        }
108                    }
109                }
110
111                if (eventType.startsLogicalSequence()) {
112                    sequenceStartingIndex = index;
113                }
114            }
115
116            index++;
117        }
118
119        if (sequenceStartingIndex >= 0) {
120            result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
121        }
122
123        return result;
124    }
125
126}
Note: See TracBrowser for help on using the repository browser.