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

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