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

Last change on this file since 1127 was 1127, checked in by pharms, 11 years ago
  • complete refactoring of task detection
  • many performance improvements in task detection
  • improved merging of sequences using Myers diff algorithm
File size: 7.0 KB
RevLine 
[1113]1//   Copyright 2012 Georg-August-Universität Göttingen, Germany
2//
3//   Licensed under the Apache License, Version 2.0 (the "License");
4//   you may not use this file except in compliance with the License.
5//   You may obtain a copy of the License at
6//
7//       http://www.apache.org/licenses/LICENSE-2.0
8//
9//   Unless required by applicable law or agreed to in writing, software
10//   distributed under the License is distributed on an "AS IS" BASIS,
11//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//   See the License for the specific language governing permissions and
13//   limitations under the License.
14
[922]15package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
[439]16
[1127]17import java.util.List;
18
[922]19import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
20import de.ugoe.cs.autoquest.eventcore.guimodel.ITrackBar;
21import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
23import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
24import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
25import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
26import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
27import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
28import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
[439]29
30/**
31 * TODO comment
32 *
33 * @version $Revision: $ $Date: 28.04.2012$
34 * @author 2012, last modified by $Author: patrick$
35 */
[1107]36class TrackBarSelectionDetectionRule implements TemporalRelationshipRule {
37    /**
38     * <p>
39     * the task tree node factory to be used for creating substructures for the temporal
40     * relationships identified during rule
41     * </p>
42     */
43    private ITaskTreeNodeFactory taskTreeNodeFactory;
44    /**
45     * <p>
46     * the task tree builder to be used for creating substructures for the temporal relationships
47     * identified during rule application
48     * </p>
49     */
50    private ITaskTreeBuilder taskTreeBuilder;
[439]51
[1107]52    /**
53     * <p>
54     * the node equality manager needed for comparing task tree nodes with each other
55     * </p>
56     */
[557]57    private NodeEqualityRuleManager nodeEqualityRuleManager;
[439]58
[557]59    /**
[1107]60     * <p>
61     * instantiates the rule and initializes it with a node equality rule manager and the minimal
62     * node equality identified sublist must have to consider them as iterated.
63     * </p>
[557]64     */
[1107]65    TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager,
66                                   ITaskTreeNodeFactory    taskTreeNodeFactory,
67                                   ITaskTreeBuilder        taskTreeBuilder)
68    {
[557]69        this.nodeEqualityRuleManager = nodeEqualityRuleManager;
[1107]70        this.taskTreeNodeFactory = taskTreeNodeFactory;
71        this.taskTreeBuilder = taskTreeBuilder;
[557]72    }
[439]73
[1117]74    /* (non-Javadoc)
75     * @see java.lang.Object#toString()
76     */
77    @Override
78    public String toString() {
79        return "TrackBarSelectionDetectionRule";
80    }
81
[557]82    /*
83     * (non-Javadoc)
84     *
[1107]85     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
86     * boolean)
[557]87     */
88    @Override
[1107]89    public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) {
[557]90        if (!(parent instanceof ISequence)) {
91            return null;
[439]92        }
[557]93
94        RuleApplicationResult result = new RuleApplicationResult();
95
96        int valueSelectionStartIndex = -1;
[971]97        ITrackBar currentTrackBar = null;
[557]98
99        int index = 0;
[1127]100        List<ITaskTreeNode> children = parent.getChildren();
101       
102        while (index < children.size()) {
103            ITaskTreeNode child = children.get(index);
[557]104
105            if ((child instanceof IEventTask) &&
106                (((IEventTask) child).getEventTarget() instanceof ITrackBar) &&
[971]107                (((IEventTask) child).getEventType() instanceof ValueSelection) &&
108                ((currentTrackBar == null) ||
109                 (currentTrackBar.equals((((IEventTask) child).getEventTarget())))))
[557]110            {
111                if (valueSelectionStartIndex < 0) {
112                    // let the show begin
113                    valueSelectionStartIndex = index;
[971]114                    currentTrackBar = (ITrackBar) ((IEventTask) child).getEventTarget();
[557]115                }
116            }
117            else if (valueSelectionStartIndex >= 0) {
118                // current child is no more value selection. But the preceding tasks were.
119                // Therefore,
120                // create an iteration with the different selectable values as selection children
[1107]121                handleValueSelections
122                    (parent, currentTrackBar, valueSelectionStartIndex, index - 1, result);
[557]123
124                return result;
125            }
126
127            index++;
128        }
129
130        if (valueSelectionStartIndex >= 0) {
131            if (finalize) {
[986]132                handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex,
[1127]133                                      children.size() - 1, result);
[557]134            }
135            else {
[1127]136                result.setRuleApplicationStatus(RuleApplicationStatus.FEASIBLE);
[557]137            }
138        }
139
[439]140        return result;
141    }
[557]142
143    /**
[1099]144     *
[557]145     */
[986]146    private void handleValueSelections(ITaskTreeNode         parent,
147                                       ITrackBar             trackbar,
148                                       int                   startIndex,
[557]149                                       int                   endIndex,
150                                       RuleApplicationResult result)
[439]151    {
[1107]152        IIteration iteration = taskTreeNodeFactory.createNewIteration();
153        taskTreeBuilder.setDescription
154            (iteration, "value selection on " + trackbar.getStringIdentifier());
[557]155        result.addNewlyCreatedParentNode(iteration);
156
[1107]157        ISelection selection = taskTreeNodeFactory.createNewSelection();
[557]158        result.addNewlyCreatedParentNode(selection);
[1107]159        taskTreeBuilder.setChild(iteration, selection);
[557]160
[1127]161        List<ITaskTreeNode> children = parent.getChildren();
162       
[557]163        for (int i = endIndex - startIndex; i >= 0; i--) {
[1127]164            addChildIfNecessary(selection, children.get(startIndex), result);
[1107]165            taskTreeBuilder.removeChild((ISequence) parent, startIndex);
[557]166        }
167
[1107]168        taskTreeBuilder.addChild((ISequence) parent, startIndex, iteration);
[557]169
[1127]170        result.setRuleApplicationStatus(RuleApplicationStatus.FINISHED);
[439]171    }
172
[557]173    /**
[808]174     *
175     */
[557]176    private void addChildIfNecessary(ISelection            parentSelection,
177                                     ITaskTreeNode         node,
[439]178                                     RuleApplicationResult result)
179    {
[1127]180        List<ITaskTreeNode> children = parentSelection.getChildren();
181       
182        for (int i = 0; i < children.size(); i++) {
183            ITaskTreeNode child = children.get(i);
[439]184
[808]185            // check, if the new node is a variant for the current event task
[1127]186            if (nodeEqualityRuleManager.areSyntacticallyEqual(child, node)) {
[808]187                return;
[557]188            }
[439]189        }
190
[557]191        // if we did not return in the previous checks, then the node must be added
[1107]192        taskTreeBuilder.addChild(parentSelection, node);
[439]193    }
194
195}
Note: See TracBrowser for help on using the repository browser.