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

Last change on this file since 1117 was 1117, checked in by pharms, 11 years ago
  • added toString methods
File size: 7.4 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    /* (non-Javadoc)
68     * @see java.lang.Object#toString()
69     */
70    @Override
71    public String toString() {
72        return "DefaultEventTargetSequenceDetectionRule";
73    }
74
75    /*
76     * (non-Javadoc)
77     *
78     * @see de.ugoe.cs.tasktree.temporalrelation.TemporalRelationshipRule#apply(TaskTreeNode,
79     * boolean)
80     */
81    @Override
82    public RuleApplicationResult apply(ITaskTreeNode parent, boolean finalize) {
83        if (!(parent instanceof ISequence)) {
84            return null;
85        }
86
87        RuleApplicationResult result = new RuleApplicationResult();
88
89        IEventTarget currentEventTarget = null;
90        int startingIndex = -1;
91
92        int index = 0;
93        while (index < parent.getChildren().size()) {
94            ITaskTreeNode child = parent.getChildren().get(index);
95
96            IEventTarget eventTarget = determineEventTarget(child);
97
98            if (((eventTarget == null) && (currentEventTarget != null)) ||
99                ((eventTarget != null) && (!eventTarget.equals(currentEventTarget))))
100            {
101                if (startingIndex < 0) {
102                    startingIndex = index;
103                    currentEventTarget = eventTarget;
104                }
105                else {
106                    int endIndex = index - 1;
107                   
108                    // only reduce to a sequence, if it is not a sequence with only one child
109                    // or if this child is not a sequence itself
110                    if ((startingIndex != endIndex) ||
111                        (!(parent.getChildren().get(startingIndex) instanceof ISequence)))
112                    {
113                        handleEventTargetSequence
114                            (parent, currentEventTarget, startingIndex, endIndex, result);
115
116                        result.setRuleApplicationStatus
117                            (RuleApplicationStatus.RULE_APPLICATION_FINISHED);
118                        return result;
119                    }
120                    else if (eventTarget != null) {
121                        // here a new sequence on a new target begins
122                        startingIndex = index;
123                        currentEventTarget = eventTarget;
124                    }
125                    else {
126                        startingIndex = -1;
127                        currentEventTarget = null;
128                    }
129                }
130            }
131
132            index++;
133        }
134
135        if (startingIndex > -1) {
136            int endIndex = parent.getChildren().size() - 1;
137           
138            if (finalize) {
139                // only reduce to a sequence, if it is not a sequence with only one child
140                // or if this child is not a sequence itself
141                if ((startingIndex > 0) &&
142                    ((startingIndex != endIndex) ||
143                     (!(parent.getChildren().get(startingIndex) instanceof ISequence))))
144                {
145                    handleEventTargetSequence
146                        (parent, currentEventTarget, startingIndex, endIndex, result);
147               
148                    result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FINISHED);
149                }
150            }
151            else {
152                result.setRuleApplicationStatus(RuleApplicationStatus.RULE_APPLICATION_FEASIBLE);
153            }
154        }
155
156        return result;
157    }
158
159    /**
160     *
161     */
162    private IEventTarget determineEventTarget(ITaskTreeNode node) {
163        if (node instanceof IEventTask) {
164            return ((IEventTask) node).getEventTarget();
165        }
166        else {
167            IEventTarget commonTarget = null;
168           
169            for (ITaskTreeNode child : node.getChildren()) {
170                if (commonTarget == null) {
171                    commonTarget = determineEventTarget(child);
172                }
173                else {
174                    if (!commonTarget.equals(determineEventTarget(child))) {
175                        return null;
176                    }
177                }
178            }
179           
180            return commonTarget;
181        }
182    }
183
184    /**
185     *
186     */
187    private void handleEventTargetSequence(ITaskTreeNode         parent,
188                                           IEventTarget          target,
189                                           int                   startIndex,
190                                           int                   endIndex,
191                                           RuleApplicationResult result)
192    {
193        String description = "interactions on " + target.getStringIdentifier();
194       
195        ISequence sequence = RuleUtils.createNewSubSequenceInRange
196            (parent, startIndex, endIndex, description, taskTreeNodeFactory, taskTreeBuilder);
197
198        result.addNewlyCreatedParentNode(sequence);
199    }
200
201}
Note: See TracBrowser for help on using the repository browser.