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

Last change on this file since 1107 was 1107, checked in by pharms, 11 years ago
  • changed rules to be testable on their own
  • added first version for a task detection rule
  • refactored rules to have a simpler interface
File size: 6.3 KB
Line 
1package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
2
3import de.ugoe.cs.autoquest.eventcore.gui.ValueSelection;
4import de.ugoe.cs.autoquest.eventcore.guimodel.ITrackBar;
5import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEquality;
6import de.ugoe.cs.autoquest.tasktrees.nodeequality.NodeEqualityRuleManager;
7import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
8import de.ugoe.cs.autoquest.tasktrees.treeifc.IIteration;
9import de.ugoe.cs.autoquest.tasktrees.treeifc.ISelection;
10import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
11import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
12import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
13import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
14
15/**
16 * TODO comment
17 *
18 * @version $Revision: $ $Date: 28.04.2012$
19 * @author 2012, last modified by $Author: patrick$
20 */
21class TrackBarSelectionDetectionRule implements TemporalRelationshipRule {
22    /**
23     * <p>
24     * the task tree node factory to be used for creating substructures for the temporal
25     * relationships identified during rule
26     * </p>
27     */
28    private ITaskTreeNodeFactory taskTreeNodeFactory;
29    /**
30     * <p>
31     * the task tree builder to be used for creating substructures for the temporal relationships
32     * identified during rule application
33     * </p>
34     */
35    private ITaskTreeBuilder taskTreeBuilder;
36
37    /**
38     * <p>
39     * the node equality manager needed for comparing task tree nodes with each other
40     * </p>
41     */
42    private NodeEqualityRuleManager nodeEqualityRuleManager;
43
44    /**
45     * <p>
46     * instantiates the rule and initializes it with a node equality rule manager and the minimal
47     * node equality identified sublist must have to consider them as iterated.
48     * </p>
49     */
50    TrackBarSelectionDetectionRule(NodeEqualityRuleManager nodeEqualityRuleManager,
51                                   ITaskTreeNodeFactory    taskTreeNodeFactory,
52                                   ITaskTreeBuilder        taskTreeBuilder)
53    {
54        this.nodeEqualityRuleManager = nodeEqualityRuleManager;
55        this.taskTreeNodeFactory = taskTreeNodeFactory;
56        this.taskTreeBuilder = taskTreeBuilder;
57    }
58
59    /*
60     * (non-Javadoc)
61     *
62     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
63     * boolean)
64     */
65    @Override
66    public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) {
67        if (!(parent instanceof ISequence)) {
68            return null;
69        }
70
71        RuleApplicationResult result = new RuleApplicationResult();
72
73        int valueSelectionStartIndex = -1;
74        ITrackBar currentTrackBar = null;
75
76        int index = 0;
77        while (index < parent.getChildren().size()) {
78            ITaskTreeNode child = parent.getChildren().get(index);
79
80            if ((child instanceof IEventTask) &&
81                (((IEventTask) child).getEventTarget() instanceof ITrackBar) &&
82                (((IEventTask) child).getEventType() instanceof ValueSelection) &&
83                ((currentTrackBar == null) ||
84                 (currentTrackBar.equals((((IEventTask) child).getEventTarget())))))
85            {
86                if (valueSelectionStartIndex < 0) {
87                    // let the show begin
88                    valueSelectionStartIndex = index;
89                    currentTrackBar = (ITrackBar) ((IEventTask) child).getEventTarget();
90                }
91            }
92            else if (valueSelectionStartIndex >= 0) {
93                // current child is no more value selection. But the preceding tasks were.
94                // Therefore,
95                // create an iteration with the different selectable values as selection children
96                handleValueSelections
97                    (parent, currentTrackBar, valueSelectionStartIndex, index - 1, result);
98
99                return result;
100            }
101
102            index++;
103        }
104
105        if (valueSelectionStartIndex >= 0) {
106            if (finalize) {
107                handleValueSelections(parent, currentTrackBar, valueSelectionStartIndex,
108                                      parent.getChildren().size() - 1, result);
109            }
110            else {
111                result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
112            }
113        }
114
115        return result;
116    }
117
118    /**
119     *
120     */
121    private void handleValueSelections(ITaskTreeNode         parent,
122                                       ITrackBar             trackbar,
123                                       int                   startIndex,
124                                       int                   endIndex,
125                                       RuleApplicationResult result)
126    {
127        IIteration iteration = taskTreeNodeFactory.createNewIteration();
128        taskTreeBuilder.setDescription
129            (iteration, "value selection on " + trackbar.getStringIdentifier());
130        result.addNewlyCreatedParentNode(iteration);
131
132        ISelection selection = taskTreeNodeFactory.createNewSelection();
133        result.addNewlyCreatedParentNode(selection);
134        taskTreeBuilder.setChild(iteration, selection);
135
136        for (int i = endIndex - startIndex; i >= 0; i--) {
137            addChildIfNecessary(selection, parent.getChildren().get(startIndex), result);
138            taskTreeBuilder.removeChild((ISequence) parent, startIndex);
139        }
140
141        taskTreeBuilder.addChild((ISequence) parent, startIndex, iteration);
142
143        result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
144    }
145
146    /**
147     *
148     */
149    private void addChildIfNecessary(ISelection            parentSelection,
150                                     ITaskTreeNode         node,
151                                     RuleApplicationResult result)
152    {
153        for (int i = 0; i < parentSelection.getChildren().size(); i++) {
154            ITaskTreeNode child = parentSelection.getChildren().get(i);
155
156            // check, if the new node is a variant for the current event task
157            NodeEquality nodeEquality = nodeEqualityRuleManager.applyRules(child, node);
158            if (nodeEquality.isAtLeast(NodeEquality.SYNTACTICALLY_EQUAL)) {
159                return;
160            }
161        }
162
163        // if we did not return in the previous checks, then the node must be added
164        taskTreeBuilder.addChild(parentSelection, node);
165    }
166
167}
Note: See TracBrowser for help on using the repository browser.