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
.Collection
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimeRange
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
29 * Abstract class to extend to match certain type of events in a trace
31 * @author Geneviève Bastien
34 public abstract class TmfEventMatching
implements ITmfEventMatching
{
39 * FIXME Not the best place to put this. Have an array of match types as a
40 * parameter of each trace?
42 public enum MatchingType
{
44 * NETWORK, match network events
50 * The array of traces to match
52 private final Collection
<ITmfTrace
> fTraces
;
55 * The class to call once a match is found
57 private final IMatchProcessingUnit fMatches
;
59 private static final Map
<MatchingType
, List
<ITmfMatchEventDefinition
>> fMatchDefinitions
= new HashMap
<>();
61 private final Map
<ITmfTrace
, ITmfMatchEventDefinition
> fMatchMap
= new HashMap
<>();
64 * Constructor with multiple traces and a match processing object
67 * The set of traces for which to match events
68 * @param tmfEventMatches
69 * The match processing class
71 public TmfEventMatching(Collection
<ITmfTrace
> traces
, IMatchProcessingUnit tmfEventMatches
) {
72 if (tmfEventMatches
== null) {
73 throw new IllegalArgumentException();
76 fMatches
= tmfEventMatches
;
80 * Returns the traces to process
84 protected Collection
<?
extends ITmfTrace
> getTraces() {
89 * Returns the match processing unit
91 * @return The match processing unit
93 protected IMatchProcessingUnit
getProcessingUnit() {
98 * Returns the match event definition corresponding to the trace
102 * @return The match event definition object
104 protected ITmfMatchEventDefinition
getEventDefinition(ITmfTrace trace
) {
105 return fMatchMap
.get(trace
);
109 * Method that initializes any data structure for the event matching. It
110 * also assigns to each trace an event matching definition instance that
111 * applies to the trace
113 protected void initMatching() {
114 fMatches
.init(fTraces
);
115 List
<ITmfMatchEventDefinition
> deflist
= fMatchDefinitions
.get(getMatchingType());
116 if (deflist
== null) {
119 for (ITmfTrace trace
: fTraces
) {
120 for (ITmfMatchEventDefinition def
: deflist
) {
121 if (def
.canMatchTrace(trace
)) {
122 fMatchMap
.put(trace
, def
);
130 * Calls any post matching methods of the processing class
132 protected void finalizeMatching() {
133 fMatches
.matchingEnded();
137 * Prints stats from the matching
139 * @return string of statistics
141 @SuppressWarnings("nls")
143 public String
toString() {
144 return getClass().getSimpleName() + " [ " + fMatches
+ " ]";
153 * The trace to which this event belongs
155 protected abstract void matchEvent(ITmfEvent event
, ITmfTrace trace
);
158 * Returns the matching type this class implements
160 * @return A matching type
162 protected abstract MatchingType
getMatchingType();
165 * Method that start the process of matching events
167 * @return Whether the match was completed correctly or not
170 public boolean matchEvents() {
172 /* Are there traces to match? If no, return false */
173 if (!(fTraces
.size() > 0)) {
177 // TODO Start a new thread here?
181 * For each trace, get the events and for each event, call the
184 * FIXME This would use a lot of memory if the traces are big, because
185 * all involved events from first trace will have to be kept before a
186 * first match is possible with second trace.
189 * Other possible matching strategy:
192 * Other strategy: start with the shortest trace, take a few events
193 * at the beginning and at the end
194 * Experiment strategy: have the experiment do the request, then events will
195 * come from both traces chronologically, but then instead of ITmfTrace[], it
196 * would be preferable to have experiment
199 for (ITmfTrace trace
: fTraces
) {
200 EventMatchingBuildRequest request
= new EventMatchingBuildRequest(this, trace
);
203 * Send the request to the trace here, since there is probably no
206 trace
.sendRequest(request
);
208 request
.waitForCompletion();
209 } catch (InterruptedException e
) {
210 Activator
.logInfo(e
.getMessage());
220 * Registers an event match definition to be used for a certain match type
223 * The event matching definition
225 public static void registerMatchObject(ITmfMatchEventDefinition match
) {
226 for (MatchingType type
: match
.getApplicableMatchingTypes()) {
227 if (!fMatchDefinitions
.containsKey(type
)) {
228 fMatchDefinitions
.put(type
, new ArrayList
<ITmfMatchEventDefinition
>());
230 fMatchDefinitions
.get(type
).add(match
);
236 class EventMatchingBuildRequest
extends TmfEventRequest
{
238 private final TmfEventMatching matching
;
239 private final ITmfTrace trace
;
241 EventMatchingBuildRequest(TmfEventMatching matching
, ITmfTrace trace
) {
242 super(ITmfEvent
.class,
243 TmfTimeRange
.ETERNITY
,
245 ITmfEventRequest
.ALL_DATA
,
246 ITmfEventRequest
.ExecutionType
.FOREGROUND
);
247 this.matching
= matching
;
252 public void handleData(final ITmfEvent event
) {
253 super.handleData(event
);
255 matching
.matchEvent(event
, trace
);
260 public void handleSuccess() {
261 super.handleSuccess();
265 public void handleCancel() {
266 super.handleCancel();
270 public void handleFailure() {
271 super.handleFailure();