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
.common
.core
.NonNullUtils
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.analysis
.TmfAnalysisModuleSources
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.analysis
.TmfAnalysisParameterProviders
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
31 import com
.google
.common
.collect
.HashMultimap
;
32 import com
.google
.common
.collect
.ImmutableMap
;
33 import com
.google
.common
.collect
.ImmutableMultimap
;
34 import com
.google
.common
.collect
.Multimap
;
37 * Manages the available analysis helpers from different sources and their
38 * parameter providers.
40 * @author Geneviève Bastien
43 public class TmfAnalysisManager
{
45 private static final Multimap
<String
, IAnalysisModuleHelper
> fAnalysisModules
= NonNullUtils
.checkNotNull(HashMultimap
.<String
, IAnalysisModuleHelper
> create());
46 private static final Map
<String
, List
<Class
<?
extends IAnalysisParameterProvider
>>> fParameterProviders
= new HashMap
<>();
47 private static final Map
<Class
<?
extends IAnalysisParameterProvider
>, IAnalysisParameterProvider
> fParamProviderInstances
= new HashMap
<>();
48 private static final List
<IAnalysisModuleSource
> fSources
= new ArrayList
<>();
49 private static final List
<ITmfNewAnalysisModuleListener
> fListeners
= new ArrayList
<>();
52 * Constructor, not to be used
54 private TmfAnalysisManager() {
59 * Registers a new source of modules
62 * A {@link IAnalysisModuleSource} instance
64 public static synchronized void registerModuleSource(IAnalysisModuleSource source
) {
70 * Initializes sources and new module listeners from the extension point
72 public static synchronized void initialize() {
75 initializeModuleSources();
76 initializeNewModuleListeners();
80 * Cleans the module sources list and initialize it from the extension point
82 private static synchronized void initializeModuleSources() {
83 for (IAnalysisModuleSource source
: TmfAnalysisModuleSources
.getSources()) {
89 * Cleans the new module listeners list and initialize it from the extension
92 private static synchronized void initializeNewModuleListeners() {
93 for (ITmfNewAnalysisModuleListener output
: TmfAnalysisModuleOutputs
.getOutputListeners()) {
94 fListeners
.add(output
);
99 * Add a new module listener to the list of listeners
102 * The new module listener
104 public static synchronized void addNewModuleListener(ITmfNewAnalysisModuleListener listener
) {
105 fListeners
.add(listener
);
109 * Gets all available analysis module helpers
111 * This map is read-only
113 * @return The map of available {@link IAnalysisModuleHelper}
116 public static synchronized Multimap
<String
, IAnalysisModuleHelper
> getAnalysisModules() {
117 if (fAnalysisModules
.isEmpty()) {
118 for (IAnalysisModuleSource source
: fSources
) {
119 for (IAnalysisModuleHelper helper
: source
.getAnalysisModules()) {
120 fAnalysisModules
.put(helper
.getId(), helper
);
124 return checkNotNull(ImmutableMultimap
.copyOf(fAnalysisModules
));
128 * Gets all analysis module helpers that apply to a given trace type. For
129 * each analysis ID, only one helper will be returned if more than one
132 * This map is read-only
134 * TODO: This method is only used to populate the project view in the UI. It
135 * should be deprecated eventually, after some UI rework, so that the trace
136 * type does not drive whether the analysis module applies or not to a
137 * trace, but rather the content of the trace or experiment (once it is
141 * The trace class to get modules for
142 * @return The map of available {@link IAnalysisModuleHelper}
144 public static Map
<String
, IAnalysisModuleHelper
> getAnalysisModules(Class
<?
extends ITmfTrace
> traceclass
) {
145 Multimap
<String
, IAnalysisModuleHelper
> allModules
= getAnalysisModules();
146 Map
<String
, IAnalysisModuleHelper
> map
= new HashMap
<>();
147 for (IAnalysisModuleHelper module
: allModules
.values()) {
148 if (module
.appliesToTraceType(traceclass
)) {
149 map
.put(module
.getId(), module
);
152 return checkNotNull(ImmutableMap
.copyOf(map
));
156 * Register a new parameter provider for an analysis
159 * The id of the analysis
160 * @param paramProvider
161 * The class of the parameter provider
163 public static void registerParameterProvider(String analysisId
, Class
<?
extends IAnalysisParameterProvider
> paramProvider
) {
164 synchronized (fParameterProviders
) {
165 if (!fParameterProviders
.containsKey(analysisId
)) {
166 fParameterProviders
.put(analysisId
, new ArrayList
<Class
<?
extends IAnalysisParameterProvider
>>());
168 /* We checked via containsKey() above, get() should not return null */
169 checkNotNull(fParameterProviders
.get(analysisId
)).add(paramProvider
);
174 * Get the parameter providers that apply to the requested trace
180 * @return The set of parameter providers that apply to a trace for this module
181 * @deprecated Use the
182 * {@link #getParameterProvidersForModule(IAnalysisModule, ITmfTrace)}
183 * method that returns a set instead.
186 public static List
<IAnalysisParameterProvider
> getParameterProviders(IAnalysisModule module
, ITmfTrace trace
) {
187 /* Call the method that returns a set */
188 Set
<IAnalysisParameterProvider
> providerList
= getParameterProvidersForModule(module
, trace
);
189 return new ArrayList
<>(providerList
);
193 * Get the parameter providers that apply to the requested trace
199 * @return The set of parameter providers that apply to a trace for this module
202 public static Set
<IAnalysisParameterProvider
> getParameterProvidersForModule(IAnalysisModule module
, ITmfTrace trace
) {
203 /* First, get the parameter providers from the extension point */
204 Set
<IAnalysisParameterProvider
> providerSet
= TmfAnalysisParameterProviders
.getParameterProvidersFor(module
.getId());
205 /* Then add any new parameter provider coming from other sources */
206 synchronized (fParameterProviders
) {
207 if (!fParameterProviders
.containsKey(module
.getId())) {
210 /* We checked via containsKey, get() should not return null */
211 List
<Class
<?
extends IAnalysisParameterProvider
>> parameterProviders
= checkNotNull(fParameterProviders
.get(module
.getId()));
212 for (Class
<?
extends IAnalysisParameterProvider
> providerClass
: parameterProviders
) {
214 IAnalysisParameterProvider provider
= fParamProviderInstances
.get(providerClass
);
215 if (provider
== null) {
216 provider
= providerClass
.newInstance();
217 fParamProviderInstances
.put(providerClass
, provider
);
219 if (provider
!= null && provider
.appliesToTrace(trace
)) {
220 providerSet
.add(provider
);
222 } catch (IllegalArgumentException
| SecurityException
| InstantiationException
| IllegalAccessException e
) {
223 Activator
.logError(Messages
.TmfAnalysisManager_ErrorParameterProvider
, e
);
227 return NonNullUtils
.checkNotNull(Collections
.unmodifiableSet(providerSet
));
231 * Clear the list of modules so that next time, it is computed again from
234 public static synchronized void refreshModules() {
235 fAnalysisModules
.clear();
239 * This method should be called when new analysis modules have been created
240 * by module helpers to that the {@link ITmfNewAnalysisModuleListener} can
241 * be executed on the module instance.
244 * The newly created analysis module
246 public static synchronized void analysisModuleCreated(IAnalysisModule module
) {
247 for (ITmfNewAnalysisModuleListener listener
: fListeners
) {
248 listener
.moduleCreated(module
);