tmf: Bug 434646: Control Flow view not cleared when opening UST trace
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / event / matching / TmfEventMatching.java
1 /*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.event.matching;
14
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
20
21 import org.eclipse.tracecompass.internal.tmf.core.Activator;
22 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
23 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
24 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
25 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
26 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
27
28 import com.google.common.collect.HashMultimap;
29 import com.google.common.collect.ImmutableList;
30 import com.google.common.collect.Multimap;
31
32 /**
33 * Abstract class to extend to match certain type of events in a trace
34 *
35 * @author Geneviève Bastien
36 * @since 3.0
37 */
38 public abstract class TmfEventMatching implements ITmfEventMatching {
39
40 /**
41 * The matching type
42 *
43 * FIXME Not the best place to put this. Have an array of match types as a
44 * parameter of each trace?
45 */
46 public enum MatchingType {
47 /**
48 * NETWORK, match network events
49 */
50 NETWORK
51 }
52
53 /**
54 * The array of traces to match
55 */
56 private final Collection<ITmfTrace> fTraces;
57
58 /**
59 * The class to call once a match is found
60 */
61 private final IMatchProcessingUnit fMatches;
62
63 private static final Map<MatchingType, List<ITmfMatchEventDefinition>> fMatchDefinitions = new HashMap<>();
64
65 private final Multimap<ITmfTrace, ITmfMatchEventDefinition> fMatchMap = HashMultimap.create();
66
67 /**
68 * Constructor with multiple traces and a match processing object
69 *
70 * @param traces
71 * The set of traces for which to match events
72 * @param tmfEventMatches
73 * The match processing class
74 */
75 public TmfEventMatching(Collection<ITmfTrace> traces, IMatchProcessingUnit tmfEventMatches) {
76 if (tmfEventMatches == null) {
77 throw new IllegalArgumentException();
78 }
79 fTraces = traces;
80 fMatches = tmfEventMatches;
81 }
82
83 /**
84 * Returns the traces to process
85 *
86 * @return The traces
87 */
88 protected Collection<? extends ITmfTrace> getTraces() {
89 return fTraces;
90 }
91
92 /**
93 * Returns the match processing unit
94 *
95 * @return The match processing unit
96 */
97 protected IMatchProcessingUnit getProcessingUnit() {
98 return fMatches;
99 }
100
101 /**
102 * Returns the match event definitions corresponding to the trace
103 *
104 * @param trace
105 * The trace
106 * @return The match event definition object
107 */
108 protected Collection<ITmfMatchEventDefinition> getEventDefinitions(ITmfTrace trace) {
109 return ImmutableList.copyOf(fMatchMap.get(trace));
110 }
111
112 /**
113 * Method that initializes any data structure for the event matching. It
114 * also assigns to each trace an event matching definition instance that
115 * applies to the trace
116 */
117 protected void initMatching() {
118 fMatches.init(fTraces);
119 List<ITmfMatchEventDefinition> deflist = fMatchDefinitions.get(getMatchingType());
120 if (deflist == null) {
121 return;
122 }
123 for (ITmfTrace trace : fTraces) {
124 for (ITmfMatchEventDefinition def : deflist) {
125 if (def.canMatchTrace(trace)) {
126 fMatchMap.put(trace, def);
127 }
128 }
129 }
130 }
131
132 /**
133 * Calls any post matching methods of the processing class
134 */
135 protected void finalizeMatching() {
136 fMatches.matchingEnded();
137 }
138
139 /**
140 * Prints stats from the matching
141 *
142 * @return string of statistics
143 */
144 @SuppressWarnings("nls")
145 @Override
146 public String toString() {
147 return getClass().getSimpleName() + " [ " + fMatches + " ]";
148 }
149
150 /**
151 * Matches one event
152 *
153 * @param event
154 * The event to match
155 * @param trace
156 * The trace to which this event belongs
157 */
158 protected abstract void matchEvent(ITmfEvent event, ITmfTrace trace);
159
160 /**
161 * Returns the matching type this class implements
162 *
163 * @return A matching type
164 */
165 protected abstract MatchingType getMatchingType();
166
167 /**
168 * Method that start the process of matching events
169 *
170 * @return Whether the match was completed correctly or not
171 */
172 @Override
173 public boolean matchEvents() {
174
175 /* Are there traces to match? If no, return false */
176 if (!(fTraces.size() > 0)) {
177 return false;
178 }
179
180 // TODO Start a new thread here?
181 initMatching();
182
183 /**
184 * For each trace, get the events and for each event, call the
185 * MatchEvent method
186 *
187 * FIXME This would use a lot of memory if the traces are big, because
188 * all involved events from first trace will have to be kept before a
189 * first match is possible with second trace.
190 *
191 * <pre>
192 * Other possible matching strategy:
193 * Incremental:
194 * Sliding window:
195 * Other strategy: start with the shortest trace, take a few events
196 * at the beginning and at the end
197 * Experiment strategy: have the experiment do the request, then events will
198 * come from both traces chronologically, but then instead of ITmfTrace[], it
199 * would be preferable to have experiment
200 * </pre>
201 */
202 for (ITmfTrace trace : fTraces) {
203 EventMatchingBuildRequest request = new EventMatchingBuildRequest(this, trace);
204
205 /*
206 * Send the request to the trace here, since there is probably no
207 * experiment.
208 */
209 trace.sendRequest(request);
210 try {
211 request.waitForCompletion();
212 } catch (InterruptedException e) {
213 Activator.logInfo(e.getMessage());
214 }
215 }
216
217 finalizeMatching();
218
219 return true;
220 }
221
222 /**
223 * Registers an event match definition to be used for a certain match type
224 *
225 * @param match
226 * The event matching definition
227 */
228 public static void registerMatchObject(ITmfMatchEventDefinition match) {
229 for (MatchingType type : match.getApplicableMatchingTypes()) {
230 if (!fMatchDefinitions.containsKey(type)) {
231 fMatchDefinitions.put(type, new ArrayList<ITmfMatchEventDefinition>());
232 }
233 fMatchDefinitions.get(type).add(match);
234 }
235 }
236
237 }
238
239 class EventMatchingBuildRequest extends TmfEventRequest {
240
241 private final TmfEventMatching matching;
242 private final ITmfTrace trace;
243
244 EventMatchingBuildRequest(TmfEventMatching matching, ITmfTrace trace) {
245 super(ITmfEvent.class,
246 TmfTimeRange.ETERNITY,
247 0,
248 ITmfEventRequest.ALL_DATA,
249 ITmfEventRequest.ExecutionType.FOREGROUND);
250 this.matching = matching;
251 this.trace = trace;
252 }
253
254 @Override
255 public void handleData(final ITmfEvent event) {
256 super.handleData(event);
257 matching.matchEvent(event, trace);
258 }
259
260 @Override
261 public void handleSuccess() {
262 super.handleSuccess();
263 }
264
265 @Override
266 public void handleCancel() {
267 super.handleCancel();
268 }
269
270 @Override
271 public void handleFailure() {
272 super.handleFailure();
273 }
274 }
This page took 0.037142 seconds and 5 git commands to generate.