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
.Collections
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
24 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.analysis
.TmfAnalysisModuleSources
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.analysis
.TmfAnalysisParameterProviders
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
30 import com
.google
.common
.collect
.HashMultimap
;
31 import com
.google
.common
.collect
.ImmutableMap
;
32 import com
.google
.common
.collect
.ImmutableMultimap
;
33 import com
.google
.common
.collect
.Multimap
;
36 * Manages the available analysis helpers from different sources and their
37 * parameter providers.
39 * @author Geneviève Bastien
42 public class TmfAnalysisManager
{
44 private static final Multimap
<String
, IAnalysisModuleHelper
> fAnalysisModules
= HashMultimap
.create();
45 private static final Map
<String
, List
<Class
<?
extends IAnalysisParameterProvider
>>> fParameterProviders
= new HashMap
<>();
46 private static final Map
<Class
<?
extends IAnalysisParameterProvider
>, IAnalysisParameterProvider
> fParamProviderInstances
= new HashMap
<>();
47 private static final List
<IAnalysisModuleSource
> fSources
= new ArrayList
<>();
48 private static final List
<ITmfNewAnalysisModuleListener
> fListeners
= new ArrayList
<>();
51 * Constructor, not to be used
53 private TmfAnalysisManager() {
58 * Disposes the analysis manager
62 public static void dispose() {
63 TmfAnalysisParameterProviders
.dispose();
64 synchronized (fParameterProviders
) {
65 fParamProviderInstances
.values().forEach(provider
-> provider
.dispose());
70 * Registers a new source of modules
73 * A {@link IAnalysisModuleSource} instance
75 public static synchronized void registerModuleSource(IAnalysisModuleSource source
) {
81 * Initializes sources and new module listeners from the extension point
83 public static synchronized void initialize() {
86 initializeModuleSources();
87 initializeNewModuleListeners();
91 * Cleans the module sources list and initialize it from the extension point
93 private static synchronized void initializeModuleSources() {
94 for (IAnalysisModuleSource source
: TmfAnalysisModuleSources
.getSources()) {
100 * Cleans the new module listeners list and initialize it from the extension
103 private static synchronized void initializeNewModuleListeners() {
104 for (ITmfNewAnalysisModuleListener output
: TmfAnalysisModuleOutputs
.getOutputListeners()) {
105 fListeners
.add(output
);
110 * Add a new module listener to the list of listeners
113 * The new module listener
115 public static synchronized void addNewModuleListener(ITmfNewAnalysisModuleListener listener
) {
116 fListeners
.add(listener
);
120 * Gets all available analysis module helpers
122 * This map is read-only
124 * @return The map of available {@link IAnalysisModuleHelper}
127 public static synchronized Multimap
<String
, IAnalysisModuleHelper
> getAnalysisModules() {
128 if (fAnalysisModules
.isEmpty()) {
129 for (IAnalysisModuleSource source
: fSources
) {
130 for (IAnalysisModuleHelper helper
: source
.getAnalysisModules()) {
131 fAnalysisModules
.put(helper
.getId(), helper
);
135 return checkNotNull(ImmutableMultimap
.copyOf(fAnalysisModules
));
139 * Gets all analysis module helpers that apply to a given trace type. For
140 * each analysis ID, only one helper will be returned if more than one
143 * This map is read-only
145 * TODO: This method is only used to populate the project view in the UI. It
146 * should be deprecated eventually, after some UI rework, so that the trace
147 * type does not drive whether the analysis module applies or not to a
148 * trace, but rather the content of the trace or experiment (once it is
152 * The trace class to get modules for
153 * @return The map of available {@link IAnalysisModuleHelper}
155 public static Map
<String
, IAnalysisModuleHelper
> getAnalysisModules(Class
<?
extends ITmfTrace
> traceclass
) {
156 Multimap
<String
, IAnalysisModuleHelper
> allModules
= getAnalysisModules();
157 Map
<String
, IAnalysisModuleHelper
> map
= new HashMap
<>();
158 for (IAnalysisModuleHelper module
: allModules
.values()) {
159 if (module
.appliesToTraceType(traceclass
)) {
160 map
.put(module
.getId(), module
);
163 return ImmutableMap
.copyOf(map
);
167 * Register a new parameter provider for an analysis
170 * The id of the analysis
171 * @param paramProvider
172 * The class of the parameter provider
174 public static void registerParameterProvider(String analysisId
, Class
<?
extends IAnalysisParameterProvider
> paramProvider
) {
175 synchronized (fParameterProviders
) {
176 if (!fParameterProviders
.containsKey(analysisId
)) {
177 fParameterProviders
.put(analysisId
, new ArrayList
<Class
<?
extends IAnalysisParameterProvider
>>());
179 /* We checked via containsKey() above, get() should not return null */
180 checkNotNull(fParameterProviders
.get(analysisId
)).add(paramProvider
);
185 * Get the parameter providers that apply to the requested trace
191 * @return The set of parameter providers that apply to a trace for this module
194 public static Set
<IAnalysisParameterProvider
> getParameterProvidersForModule(IAnalysisModule module
, ITmfTrace trace
) {
195 /* First, get the parameter providers from the extension point */
196 Set
<IAnalysisParameterProvider
> providerSet
= TmfAnalysisParameterProviders
.getParameterProvidersFor(module
.getId());
197 /* Then add any new parameter provider coming from other sources */
198 synchronized (fParameterProviders
) {
199 if (!fParameterProviders
.containsKey(module
.getId())) {
202 /* We checked via containsKey, get() should not return null */
203 List
<Class
<?
extends IAnalysisParameterProvider
>> parameterProviders
= checkNotNull(fParameterProviders
.get(module
.getId()));
204 for (Class
<?
extends IAnalysisParameterProvider
> providerClass
: parameterProviders
) {
206 IAnalysisParameterProvider provider
= fParamProviderInstances
.get(providerClass
);
207 if (provider
== null) {
208 provider
= providerClass
.newInstance();
209 fParamProviderInstances
.put(providerClass
, provider
);
211 if (provider
.appliesToTrace(trace
)) {
212 providerSet
.add(provider
);
214 } catch (IllegalArgumentException
| SecurityException
| InstantiationException
| IllegalAccessException e
) {
215 Activator
.logError(Messages
.TmfAnalysisManager_ErrorParameterProvider
, e
);
219 return Collections
.unmodifiableSet(providerSet
);
223 * Clear the list of modules so that next time, it is computed again from
226 public static synchronized void refreshModules() {
227 fAnalysisModules
.clear();
231 * This method should be called when new analysis modules have been created
232 * by module helpers to that the {@link ITmfNewAnalysisModuleListener} can
233 * be executed on the module instance.
236 * The newly created analysis module
238 public static synchronized void analysisModuleCreated(IAnalysisModule module
) {
239 for (ITmfNewAnalysisModuleListener listener
: fListeners
) {
240 listener
.moduleCreated(module
);