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 |
|
---|
15 | package de.ugoe.cs.autoquest.plugin.mfc;
|
---|
16 |
|
---|
17 | import java.util.LinkedList;
|
---|
18 | import java.util.List;
|
---|
19 | import java.util.logging.Level;
|
---|
20 |
|
---|
21 | import de.ugoe.cs.autoquest.eventcore.Event;
|
---|
22 | import de.ugoe.cs.autoquest.eventcore.guimodel.GUIElementTree;
|
---|
23 | import de.ugoe.cs.autoquest.plugin.mfc.eventcore.WindowsMessage;
|
---|
24 | import de.ugoe.cs.autoquest.plugin.mfc.eventcore.WindowsMessageType;
|
---|
25 | import de.ugoe.cs.util.console.Console;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * <p>
|
---|
29 | * Responsible to split sequences into subsequences, such that each subsequences contains exactly
|
---|
30 | * one event.
|
---|
31 | * </p>
|
---|
32 | *
|
---|
33 | * @author Steffen Herbold
|
---|
34 | * @version 1.0
|
---|
35 | */
|
---|
36 | public class SequenceSplitter {
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * <p>
|
---|
40 | * Contains the current subsequence.
|
---|
41 | * </p>
|
---|
42 | */
|
---|
43 | private List<WindowsMessage> currentSequence;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * <p>
|
---|
47 | * Number of messages in the current sequences, that signal that a key or mouse button has been
|
---|
48 | * pressed down to which not yet a message has been found, that signals that the button has been
|
---|
49 | * released.
|
---|
50 | * </p>
|
---|
51 | */
|
---|
52 | private int openDowns;
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * <p>
|
---|
56 | * Internal flag that signals if {@link #currentSequence} needs to be initialized.
|
---|
57 | * </p>
|
---|
58 | */
|
---|
59 | private boolean initMessages;
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * <p>
|
---|
63 | * The {@link EventGenerator} used to convert the subsequences into {@link Event}s
|
---|
64 | * </p>
|
---|
65 | */
|
---|
66 | private EventGenerator tokenGenerator;
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * <p>
|
---|
70 | * The event sequence generated.
|
---|
71 | * </p>
|
---|
72 | */
|
---|
73 | private List<Event> actionSequence;
|
---|
74 |
|
---|
75 | /**
|
---|
76 | * <p>
|
---|
77 | * Type of the previous message.
|
---|
78 | * </p>
|
---|
79 | */
|
---|
80 | private WindowsMessageType prevMsg;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * <p>
|
---|
84 | * Constructor. Creates a new SequenceSplitter.
|
---|
85 | * </p>
|
---|
86 | */
|
---|
87 | public SequenceSplitter(GUIElementTree<Long> guiElementTree) {
|
---|
88 | currentSequence = new LinkedList<WindowsMessage>();
|
---|
89 | openDowns = 0;
|
---|
90 | initMessages = true;
|
---|
91 | tokenGenerator = new EventGenerator(guiElementTree);
|
---|
92 | actionSequence = new LinkedList<Event>();
|
---|
93 | prevMsg = null;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * <p>
|
---|
98 | * Called by the {@link MFCLogParser} every time a message is parsed.
|
---|
99 | * </p>
|
---|
100 | *
|
---|
101 | * @param msg
|
---|
102 | * message to be added
|
---|
103 | */
|
---|
104 | public void addMessage(WindowsMessage msg) {
|
---|
105 | if (startOfSequence(msg)) {
|
---|
106 | if (!initMessages) {
|
---|
107 | Event currentAction = tokenGenerator.generateEvent(currentSequence);
|
---|
108 | if (currentAction != null) {
|
---|
109 | actionSequence.add(currentAction);
|
---|
110 | }
|
---|
111 | if (msg.getType().isKeyMessage() && openDowns > 0) {
|
---|
112 | Console.traceln(Level.SEVERE, "Key message found with open down mouse " +
|
---|
113 | "messages - will probabably result in a faulty sequence.");
|
---|
114 | }
|
---|
115 | }
|
---|
116 | else {
|
---|
117 | initMessages = false;
|
---|
118 | }
|
---|
119 | currentSequence = new LinkedList<WindowsMessage>();
|
---|
120 | }
|
---|
121 | if (msg.getType().isUpMessage()) {
|
---|
122 | if (openDowns > 0) {
|
---|
123 | openDowns--;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | // this fix checks if there are two consecutive mouse-down messages.
|
---|
128 | // This sometimes occurs due to incorrect filtering in the monitoring
|
---|
129 | // dll.
|
---|
130 | if (!(prevMsg == WindowsMessageType.WM_LBUTTONDOWN && prevMsg == msg.getType())) {
|
---|
131 | currentSequence.add(msg);
|
---|
132 | }
|
---|
133 | else {
|
---|
134 | openDowns--;
|
---|
135 | }
|
---|
136 | prevMsg = msg.getType();
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * <p>
|
---|
141 | * Returns the event sequence generated from the message that have been added.
|
---|
142 | * </p>
|
---|
143 | *
|
---|
144 | * @return generated event sequence
|
---|
145 | */
|
---|
146 | public List<Event> getSequence() {
|
---|
147 | return actionSequence;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * <p>
|
---|
152 | * Called when a session in the log file is finished, i.e., a closing session-node is found.
|
---|
153 | * </p>
|
---|
154 | */
|
---|
155 | public void endSession() {
|
---|
156 | Event currentAction = tokenGenerator.generateEvent(currentSequence);
|
---|
157 | if (currentAction != null) {
|
---|
158 | actionSequence.add(currentAction);
|
---|
159 | }
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * <p>
|
---|
164 | * Checks if the message starts a new subsequence and returns the result.
|
---|
165 | * </p>
|
---|
166 | *
|
---|
167 | * @param msg
|
---|
168 | * message that is checked
|
---|
169 | * @return true, if a new subsequence begins
|
---|
170 | */
|
---|
171 | private boolean startOfSequence(WindowsMessage msg) {
|
---|
172 | boolean isStart = false;
|
---|
173 | WindowsMessageType msgType = msg.getType();
|
---|
174 | if (msgType.isKeyMessage()) {
|
---|
175 | isStart = true;
|
---|
176 | }
|
---|
177 | if (msgType.isDownMessage()) {
|
---|
178 | openDowns++;
|
---|
179 | if (openDowns == 1) {
|
---|
180 | isStart = true;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | if (msgType.isDblclkMessage()) {
|
---|
184 | openDowns++;
|
---|
185 | }
|
---|
186 | return isStart;
|
---|
187 | }
|
---|
188 |
|
---|
189 | }
|
---|