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

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