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

Last change on this file since 1113 was 1113, checked in by pharms, 11 years ago
  • added license statement
File size: 7.2 KB
Line 
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
15package de.ugoe.cs.autoquest.tasktrees.temporalrelation;
16
17import de.ugoe.cs.autoquest.eventcore.IEventTarget;
18import de.ugoe.cs.autoquest.tasktrees.treeifc.IEventTask;
19import de.ugoe.cs.autoquest.tasktrees.treeifc.ISequence;
20import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeBuilder;
21import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNode;
22import de.ugoe.cs.autoquest.tasktrees.treeifc.ITaskTreeNodeFactory;
23
24/**
25 * This rule subdivides long sequences into subsequences of tasks on the same event target.
26 * Event targets are compared using the equals method. A more complex GUI model is ignored.
27 *
28 * @version $Revision: $ $Date: 18.03.2012$
29 * @author 2012, last modified by $Author: patrick$
30 */
31class DefaultEventTargetSequenceDetectionRule implements TemporalRelationshipRule {
32
33    /**
34     * <p>
35     * the task tree node factory to be used for creating substructures for the temporal
36     * relationships identified during rule
37     * </p>
38     */
39    private ITaskTreeNodeFactory taskTreeNodeFactory;
40    /**
41     * <p>
42     * the task tree builder to be used for creating substructures for the temporal relationships
43     * identified during rule application
44     * </p>
45     */
46    private ITaskTreeBuilder taskTreeBuilder;
47   
48    /**
49     * <p>
50     * instantiates the rule with a task tree node factory and builder to be used during rule
51     * application.
52     * </p>
53     *
54     * @param taskTreeNodeFactory the task tree node factory to be used for creating substructures
55     *                            for the temporal relationships identified during rule
56     *                            application
57     * @param taskTreeBuilder     the task tree builder to be used for creating substructures for
58     *                            the temporal relationships identified during rule application
59     */
60    DefaultEventTargetSequenceDetectionRule(ITaskTreeNodeFactory taskTreeNodeFactory,
61                                            ITaskTreeBuilder     taskTreeBuilder)
62    {
63        this.taskTreeNodeFactory = taskTreeNodeFactory;
64        this.taskTreeBuilder = taskTreeBuilder;
65    }
66   
67    /*
68     * (non-Javadoc)
69     *
70     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
71     * boolean)
72     */
73    @Override
74    public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) {
75        if (!(parent instanceof ISequence)) {
76            return null;
77        }
78
79        RuleApplicationResult result = new RuleApplicationResult();
80
81        IEventTarget currentEventTarget = null;
82        int startingIndex = -1;
83
84        int index = 0;
85        while (index < parent.getChildren().size()) {
86            ITaskTreeNode child = parent.getChildren().get(index);
87
88            IEventTarget eventTarget = determineEventTarget(child);
89
90            if (((eventTarget == null) && (currentEventTarget != null)) ||
91                ((eventTarget != null) && (!eventTarget.equals(currentEventTarget))))
92            {
93                if (startingIndex < 0) {
94                    startingIndex = index;
95                    currentEventTarget = eventTarget;
96                }
97                else {
98                    int endIndex = index - 1;
99                   
100                    // only reduce to a sequence, if it is not a sequence with only one child
101                    // or if this child is not a sequence itself
102                    if ((startingIndex != endIndex) ||
103                        (!(parent.getChildren().get(startingIndex) instanceof ISequence)))
104                    {
105                        handleEventTargetSequence
106                            (parent, currentEventTarget, startingIndex, endIndex, result);
107
108                        result.setRuleApplicationStatus
109                            (RuleApplicationStatus.RULE_APPLICATION_FINISHED);
110                        return result;
111                    }
112                    else if (eventTarget != null) {
113                        // here a new sequence on a new target begins
114                        startingIndex = index;
115                        currentEventTarget = eventTarget;
116                    }
117                    else {
118                        startingIndex = -1;
119                        currentEventTarget = null;
120                    }
121                }
122            }
123
124            index++;
125        }
126
127        if (startingIndex > -1) {
128            int endIndex = parent.getChildren().size() - 1;
129           
130            if (finalize) {
131                // only reduce to a sequence, if it is not a sequence with only one child
132                // or if this child is not a sequence itself
133                if ((startingIndex > 0) &&
134                    ((startingIndex != endIndex) ||
135                     (!(parent.getChildren().get(startingIndex) instanceof ISequence))))
136                {
137                    handleEventTargetSequence
138                        (parent, currentEventTarget, startingIndex, endIndex, result);
139               
140                    result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
141                }
142            }
143            else {
144                result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
145            }
146        }
147
148        return result;
149    }
150
151    /**
152     *
153     */
154    private IEventTarget determineEventTarget(ITaskTreeNode node) {
155        if (node instanceof IEventTask) {
156            return ((IEventTask) node).getEventTarget();
157        }
158        else {
159            IEventTarget commonTarget = null;
160           
161            for (ITaskTreeNode child : node.getChildren()) {
162                if (commonTarget == null) {
163                    commonTarget = determineEventTarget(child);
164                }
165                else {
166                    if (!commonTarget.equals(determineEventTarget(child))) {
167                        return null;
168                    }
169                }
170            }
171           
172            return commonTarget;
173        }
174    }
175
176    /**
177     *
178     */
179    private void handleEventTargetSequence(ITaskTreeNode         parent,
180                                           IEventTarget          target,
181                                           int                   startIndex,
182                                           int                   endIndex,
183                                           RuleApplicationResult result)
184    {
185        String description = "interactions on " + target.getStringIdentifier();
186       
187        ISequence sequence = RuleUtils.createNewSubSequenceInRange
188            (parent, startIndex, endIndex, description, taskTreeNodeFactory, taskTreeBuilder);
189
190        result.addNewlyCreatedParentNode(sequence);
191    }
192
193}
Note: See TracBrowser for help on using the repository browser.