1 /*******************************************************************************
2 * Copyright (c) 2013 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial implementation and API
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.event
.matching
;
15 import java
.util
.ArrayList
;
16 import java
.util
.HashMap
;
17 import java
.util
.List
;
20 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfEvent
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
30 * Abstract class to extend to match certain type of events in a trace
32 * @author Geneviève Bastien
35 public abstract class TmfEventMatching
implements ITmfEventMatching
{
40 * FIXME Not the best place to put this. Have an array of match types as a
41 * parameter of each trace?
43 public enum MatchingType
{
45 * NETWORK, match network events
51 * The array of traces to match
53 private final ITmfTrace
[] fTraces
;
56 * The class to call once a match is found
58 private final IMatchProcessingUnit fMatches
;
60 private static final Map
<MatchingType
, List
<ITmfMatchEventDefinition
>> fMatchDefinitions
= new HashMap
<MatchingType
, List
<ITmfMatchEventDefinition
>>();
62 private final Map
<ITmfTrace
, ITmfMatchEventDefinition
> fMatchMap
= new HashMap
<ITmfTrace
, ITmfMatchEventDefinition
>();
65 * Constructor with multiple traces and a match processing object
68 * The set of traces for which to match events
69 * @param tmfEventMatches
70 * The match processing class
72 public TmfEventMatching(ITmfTrace
[] traces
, IMatchProcessingUnit tmfEventMatches
) {
73 if (tmfEventMatches
== null) {
74 throw new IllegalArgumentException();
77 fMatches
= tmfEventMatches
;
81 * Returns the traces to process
85 protected ITmfTrace
[] getTraces() {
90 * Returns the match processing unit
92 * @return The match processing unit
94 protected IMatchProcessingUnit
getProcessingUnit() {
99 * Returns the match event definition corresponding to the trace
103 * @return The match event definition object
105 protected ITmfMatchEventDefinition
getEventDefinition(ITmfTrace trace
) {
106 return fMatchMap
.get(trace
);
110 * Method that initializes any data structure for the event matching. It
111 * also assigns to each trace an event matching definition instance that
112 * applies to the trace
114 protected void initMatching() {
115 fMatches
.init(fTraces
);
116 List
<ITmfMatchEventDefinition
> deflist
= fMatchDefinitions
.get(getMatchingType());
117 if (deflist
== null) {
120 for (ITmfTrace trace
: fTraces
) {
121 for (ITmfMatchEventDefinition def
: deflist
) {
122 if (def
.canMatchTrace(trace
)) {
123 fMatchMap
.put(trace
, def
);
131 * Calls any post matching methods of the processing class
133 protected void finalizeMatching() {
134 fMatches
.matchingEnded();
138 * Prints stats from the matching
140 * @return string of statistics
142 @SuppressWarnings("nls")
144 public String
toString() {
145 return getClass().getSimpleName() + " [ " + fMatches
+ " ]";
154 * The number of the trace this event belongs to
156 protected abstract void matchEvent(ITmfEvent event
, int traceno
);
159 * Returns the matching type this class implements
161 * @return A matching type
163 protected abstract MatchingType
getMatchingType();
166 * Method that start the process of matching events
168 * @return Whether the match was completed correctly or not
171 public boolean matchEvents() {
173 /* Are there traces to match? If no, return false */
174 if (!(fTraces
.length
> 0)) {
178 // TODO Start a new thread here?
182 * For each trace, get the events and for each event, call the
185 * FIXME This would use a lot of memory if the traces are big, because
186 * all involved events from first trace will have to be kept before a
187 * first match is possible with second trace.
190 * Other possible matching strategy:
193 * Other strategy: start with the shortest trace, take a few events
194 * at the beginning and at the end
195 * Experiment strategy: have the experiment do the request, then events will
196 * come from both traces chronologically, but then instead of ITmfTrace[], it
197 * would be preferable to have experiment
200 for (int i
= 0; i
< fTraces
.length
; i
++) {
202 EventMatchingBuildRequest request
= new EventMatchingBuildRequest(this, i
);
205 * Send the request to the trace here, since there is probably no
208 fTraces
[i
].sendRequest(request
);
210 request
.waitForCompletion();
211 } catch (InterruptedException e
) {
212 Activator
.logInfo(e
.getMessage());
222 * Registers an event match definition to be used for a certain match type
225 * The event matching definition
227 public static void registerMatchObject(ITmfMatchEventDefinition match
) {
228 for (MatchingType type
: match
.getApplicableMatchingTypes()) {
229 if (!fMatchDefinitions
.containsKey(type
)) {
230 fMatchDefinitions
.put(type
, new ArrayList
<ITmfMatchEventDefinition
>());
232 fMatchDefinitions
.get(type
).add(match
);
238 class EventMatchingBuildRequest
extends TmfEventRequest
{
240 private final TmfEventMatching matching
;
241 private final int traceno
;
243 EventMatchingBuildRequest(TmfEventMatching matching
, int traceno
) {
244 super(CtfTmfEvent
.class,
245 TmfTimeRange
.ETERNITY
,
247 TmfDataRequest
.ALL_DATA
,
248 ITmfDataRequest
.ExecutionType
.FOREGROUND
);
249 this.matching
= matching
;
250 this.traceno
= traceno
;
254 public void handleData(final ITmfEvent event
) {
255 super.handleData(event
);
257 matching
.matchEvent(event
, traceno
);
262 public void handleSuccess() {
263 super.handleSuccess();
267 public void handleCancel() {
268 super.handleCancel();
272 public void handleFailure() {
273 super.handleFailure();