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

Last change on this file since 1044 was 985, checked in by pharms, 12 years ago
  • small bugfix
File size: 6.0 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 */
17public class DefaultEventTargetSequenceDetectionRule implements TemporalRelationshipRule {
18
19    /*
20     * (non-Javadoc)
21     *
22     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
23     * TaskTreeBuilder, TaskTreeNodeFactory)
24     */
25    @Override
26    public RuleApplicationResult apply(ITaskTreeNode        parent,
27                                       ITaskTreeBuilder     builder,
28                                       ITaskTreeNodeFactory nodeFactory,
29                                       boolean              finalize)
30    {
31        if (!(parent instanceof ISequence)) {
32            return null;
33        }
34
35        RuleApplicationResult result = new RuleApplicationResult();
36
37        IEventTarget currentEventTarget = null;
38        int startingIndex = -1;
39
40        int index = 0;
41        while (index < parent.getChildren().size()) {
42            ITaskTreeNode child = parent.getChildren().get(index);
43
44            IEventTarget eventTarget = determineEventTarget(child);
45
46            if (((eventTarget == null) && (currentEventTarget != null)) ||
47                ((eventTarget != null) && (!eventTarget.equals(currentEventTarget))))
48            {
49                if (startingIndex < 0) {
50                    startingIndex = index;
51                    currentEventTarget = eventTarget;
52                }
53                else {
54                    int endIndex = index - 1;
55                   
56                    // only reduce to a sequence, if it is not a sequence with only one child
57                    // or if this child is not a sequence itself
58                    if ((startingIndex != endIndex) ||
59                        (!(parent.getChildren().get(startingIndex) instanceof ISequence)))
60                    {
61                        handleEventTargetSequence(parent, currentEventTarget, startingIndex,
62                                                  endIndex, builder, nodeFactory, result);
63
64                        result.setRuleApplicationStatus
65                            (RuleApplicationStatus.RULE_APPLICATION_FINISHED);
66                        return result;
67                    }
68                    else if (eventTarget != null) {
69                        // here a new sequence on a new target begins
70                        startingIndex = index;
71                        currentEventTarget = eventTarget;
72                    }
73                    else {
74                        startingIndex = -1;
75                        currentEventTarget = null;
76                    }
77                }
78            }
79
80            index++;
81        }
82
83        if (startingIndex > -1) {
84            int endIndex = parent.getChildren().size() - 1;
85           
86            if (finalize) {
87                // only reduce to a sequence, if it is not a sequence with only one child
88                // or if this child is not a sequence itself
89                if ((startingIndex > 0) &&
90                    ((startingIndex != endIndex) ||
91                     (!(parent.getChildren().get(startingIndex) instanceof ISequence))))
92                {
93                    handleEventTargetSequence(parent, currentEventTarget, startingIndex, endIndex,
94                                              builder, nodeFactory, result);
95               
96                    result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
97                }
98            }
99            else {
100                result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
101            }
102        }
103
104        return result;
105    }
106
107    /**
108     * TODO: comment
109     *
110     * @param child
111     * @return
112     */
113    private IEventTarget determineEventTarget(ITaskTreeNode node) {
114        if (node instanceof IEventTask) {
115            return ((IEventTask) node).getEventTarget();
116        }
117        else {
118            IEventTarget commonTarget = null;
119           
120            for (ITaskTreeNode child : node.getChildren()) {
121                if (commonTarget == null) {
122                    commonTarget = determineEventTarget(child);
123                }
124                else {
125                    if (!commonTarget.equals(determineEventTarget(child))) {
126                        return null;
127                    }
128                }
129            }
130           
131            return commonTarget;
132        }
133    }
134
135    /**
136     * TODO: comment
137     *
138     */
139    private void handleEventTargetSequence(ITaskTreeNode         parent,
140                                           IEventTarget          target,
141                                           int                   startIndex,
142                                           int                   endIndex,
143                                           ITaskTreeBuilder      builder,
144                                           ITaskTreeNodeFactory  nodeFactory,
145                                           RuleApplicationResult result)
146    {
147        ISequence sequence = nodeFactory.createNewSequence();
148        builder.setDescription(sequence, "interactions on " + target.getStringIdentifier());
149
150        for (int i = startIndex; i <= endIndex; i++) {
151            builder.addChild(sequence, parent.getChildren().get(startIndex));
152            builder.removeChild((ISequence) parent, startIndex);
153        }
154
155        builder.addChild((ISequence) parent, startIndex, sequence);
156
157        result.addNewlyCreatedParentNode(sequence);
158    }
159
160}
Note: See TracBrowser for help on using the repository browser.