1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are
5 * made 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 API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.core
.analysis
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.ArrayList
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
22 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
23 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.analysis
.TmfAnalysisModuleSources
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
28 import com
.google
.common
.collect
.HashMultimap
;
29 import com
.google
.common
.collect
.ImmutableMap
;
30 import com
.google
.common
.collect
.ImmutableMultimap
;
31 import com
.google
.common
.collect
.Multimap
;
34 * Manages the available analysis helpers from different sources and their
35 * parameter providers.
37 * @author Geneviève Bastien
40 public class TmfAnalysisManager
{
42 private static final Multimap
<String
, IAnalysisModuleHelper
> fAnalysisModules
= NonNullUtils
.checkNotNull(HashMultimap
.<String
, IAnalysisModuleHelper
> create());
43 private static final Map
<String
, List
<Class
<?
extends IAnalysisParameterProvider
>>> fParameterProviders
= new HashMap
<>();
44 private static final Map
<Class
<?
extends IAnalysisParameterProvider
>, IAnalysisParameterProvider
> fParamProviderInstances
= new HashMap
<>();
45 private static final List
<IAnalysisModuleSource
> fSources
= new ArrayList
<>();
46 private static final List
<ITmfNewAnalysisModuleListener
> fListeners
= new ArrayList
<>();
49 * Constructor, not to be used
51 private TmfAnalysisManager() {
56 * Registers a new source of modules
59 * A {@link IAnalysisModuleSource} instance
61 public static synchronized void registerModuleSource(IAnalysisModuleSource source
) {
67 * Initializes sources and new module listeners from the extension point
69 public static synchronized void initialize() {
72 initializeModuleSources();
73 initializeNewModuleListeners();
77 * Cleans the module sources list and initialize it from the extension point
79 private static synchronized void initializeModuleSources() {
80 for (IAnalysisModuleSource source
: TmfAnalysisModuleSources
.getSources()) {
86 * Cleans the new module listeners list and initialize it from the extension
89 private static synchronized void initializeNewModuleListeners() {
90 for (ITmfNewAnalysisModuleListener output
: TmfAnalysisModuleOutputs
.getOutputListeners()) {
91 fListeners
.add(output
);
96 * Add a new module listener to the list of listeners
99 * The new module listener
101 public static synchronized void addNewModuleListener(ITmfNewAnalysisModuleListener listener
) {
102 fListeners
.add(listener
);
106 * Gets all available analysis module helpers
108 * This map is read-only
110 * @return The map of available {@link IAnalysisModuleHelper}
113 public static synchronized Multimap
<String
, IAnalysisModuleHelper
> getAnalysisModules() {
114 if (fAnalysisModules
.isEmpty()) {
115 for (IAnalysisModuleSource source
: fSources
) {
116 for (IAnalysisModuleHelper helper
: source
.getAnalysisModules()) {
117 fAnalysisModules
.put(helper
.getId(), helper
);
121 return checkNotNull(ImmutableMultimap
.copyOf(fAnalysisModules
));
125 * Gets all analysis module helpers that apply to a given trace type. For
126 * each analysis ID, only one helper will be returned if more than one
129 * This map is read-only
131 * TODO: This method is only used to populate the project view in the UI. It
132 * should be deprecated eventually, after some UI rework, so that the trace
133 * type does not drive whether the analysis module applies or not to a
134 * trace, but rather the content of the trace or experiment (once it is
138 * The trace class to get modules for
139 * @return The map of available {@link IAnalysisModuleHelper}
141 public static Map
<String
, IAnalysisModuleHelper
> getAnalysisModules(Class
<?
extends ITmfTrace
> traceclass
) {
142 Multimap
<String
, IAnalysisModuleHelper
> allModules
= getAnalysisModules();
143 Map
<String
, IAnalysisModuleHelper
> map
= new HashMap
<>();
144 for (IAnalysisModuleHelper module
: allModules
.values()) {
145 if (module
.appliesToTraceType(traceclass
)) {
146 map
.put(module
.getId(), module
);
149 return checkNotNull(ImmutableMap
.copyOf(map
));
153 * Register a new parameter provider for an analysis
156 * The id of the analysis
157 * @param paramProvider
158 * The class of the parameter provider
160 public static void registerParameterProvider(String analysisId
, Class
<?
extends IAnalysisParameterProvider
> paramProvider
) {
161 synchronized (fParameterProviders
) {
162 if (!fParameterProviders
.containsKey(analysisId
)) {
163 fParameterProviders
.put(analysisId
, new ArrayList
<Class
<?
extends IAnalysisParameterProvider
>>());
165 fParameterProviders
.get(analysisId
).add(paramProvider
);
170 * Get a parameter provider that applies to the requested trace
176 * @return A parameter provider if one applies to the trace, null otherwise
178 public static List
<IAnalysisParameterProvider
> getParameterProviders(IAnalysisModule module
, ITmfTrace trace
) {
179 List
<IAnalysisParameterProvider
> providerList
= new ArrayList
<>();
180 synchronized (fParameterProviders
) {
181 if (!fParameterProviders
.containsKey(module
.getId())) {
184 for (Class
<?
extends IAnalysisParameterProvider
> providerClass
: fParameterProviders
.get(module
.getId())) {
186 IAnalysisParameterProvider provider
= fParamProviderInstances
.get(providerClass
);
187 if (provider
== null) {
188 provider
= providerClass
.newInstance();
189 fParamProviderInstances
.put(providerClass
, provider
);
191 if (provider
!= null && provider
.appliesToTrace(trace
)) {
192 providerList
.add(provider
);
194 } catch (IllegalArgumentException
| SecurityException
| InstantiationException
| IllegalAccessException e
) {
195 Activator
.logError(Messages
.TmfAnalysisManager_ErrorParameterProvider
, e
);
203 * Clear the list of modules so that next time, it is computed again from
206 public static synchronized void refreshModules() {
207 fAnalysisModules
.clear();
211 * This method should be called when new analysis modules have been created
212 * by module helpers to that the {@link ITmfNewAnalysisModuleListener} can
213 * be executed on the module instance.
216 * The newly created analysis module
218 public static synchronized void analysisModuleCreated(IAnalysisModule module
) {
219 for (ITmfNewAnalysisModuleListener listener
: fListeners
) {
220 listener
.moduleCreated(module
);