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
.tracecompass
.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
.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
;
28 import com
.google
.common
.collect
.HashMultimap
;
29 import com
.google
.common
.collect
.ImmutableList
;
30 import com
.google
.common
.collect
.Multimap
;
33 * Abstract class to extend to match certain type of events in a trace
35 * @author Geneviève Bastien
38 public abstract class TmfEventMatching
implements ITmfEventMatching
{
43 * FIXME Not the best place to put this. Have an array of match types as a
44 * parameter of each trace?
46 public enum MatchingType
{
48 * NETWORK, match network events
54 * The array of traces to match
56 private final Collection
<ITmfTrace
> fTraces
;
59 * The class to call once a match is found
61 private final IMatchProcessingUnit fMatches
;
63 private static final Map
<MatchingType
, List
<ITmfMatchEventDefinition
>> fMatchDefinitions
= new HashMap
<>();
65 private final Multimap
<ITmfTrace
, ITmfMatchEventDefinition
> fMatchMap
= HashMultimap
.create();
68 * Constructor with multiple traces and a match processing object
71 * The set of traces for which to match events
72 * @param tmfEventMatches
73 * The match processing class
75 public TmfEventMatching(Collection
<ITmfTrace
> traces
, IMatchProcessingUnit tmfEventMatches
) {
76 if (tmfEventMatches
== null) {
77 throw new IllegalArgumentException();
80 fMatches
= tmfEventMatches
;
84 * Returns the traces to process
88 protected Collection
<?
extends ITmfTrace
> getTraces() {
93 * Returns the match processing unit
95 * @return The match processing unit
97 protected IMatchProcessingUnit
getProcessingUnit() {
102 * Returns the match event definitions corresponding to the trace
106 * @return The match event definition object
108 protected Collection
<ITmfMatchEventDefinition
> getEventDefinitions(ITmfTrace trace
) {
109 return ImmutableList
.copyOf(fMatchMap
.get(trace
));
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
117 protected void initMatching() {
118 fMatches
.init(fTraces
);
119 List
<ITmfMatchEventDefinition
> deflist
= fMatchDefinitions
.get(getMatchingType());
120 if (deflist
== null) {
123 for (ITmfTrace trace
: fTraces
) {
124 for (ITmfMatchEventDefinition def
: deflist
) {
125 if (def
.canMatchTrace(trace
)) {
126 fMatchMap
.put(trace
, def
);
133 * Calls any post matching methods of the processing class
135 protected void finalizeMatching() {
136 fMatches
.matchingEnded();
140 * Prints stats from the matching
142 * @return string of statistics
144 @SuppressWarnings("nls")
146 public String
toString() {
147 return getClass().getSimpleName() + " [ " + fMatches
+ " ]";
156 * The trace to which this event belongs
158 protected abstract void matchEvent(ITmfEvent event
, ITmfTrace trace
);
161 * Returns the matching type this class implements
163 * @return A matching type
165 protected abstract MatchingType
getMatchingType();
168 * Method that start the process of matching events
170 * @return Whether the match was completed correctly or not
173 public boolean matchEvents() {
175 /* Are there traces to match? If no, return false */
176 if (!(fTraces
.size() > 0)) {
180 // TODO Start a new thread here?
184 * For each trace, get the events and for each event, call the
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.
192 * Other possible matching strategy:
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
202 for (ITmfTrace trace
: fTraces
) {
203 EventMatchingBuildRequest request
= new EventMatchingBuildRequest(this, trace
);
206 * Send the request to the trace here, since there is probably no
209 trace
.sendRequest(request
);
211 request
.waitForCompletion();
212 } catch (InterruptedException e
) {
213 Activator
.logInfo(e
.getMessage());
223 * Registers an event match definition to be used for a certain match type
226 * The event matching definition
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
>());
233 fMatchDefinitions
.get(type
).add(match
);
239 class EventMatchingBuildRequest
extends TmfEventRequest
{
241 private final TmfEventMatching matching
;
242 private final ITmfTrace trace
;
244 EventMatchingBuildRequest(TmfEventMatching matching
, ITmfTrace trace
) {
245 super(ITmfEvent
.class,
246 TmfTimeRange
.ETERNITY
,
248 ITmfEventRequest
.ALL_DATA
,
249 ITmfEventRequest
.ExecutionType
.FOREGROUND
);
250 this.matching
= matching
;
255 public void handleData(final ITmfEvent event
) {
256 super.handleData(event
);
257 matching
.matchEvent(event
, trace
);
261 public void handleSuccess() {
262 super.handleSuccess();
266 public void handleCancel() {
267 super.handleCancel();
271 public void handleFailure() {
272 super.handleFailure();