df59aaeeeba0d428a06150c5c8acf55779927b94
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / analysis / TmfAnalysisManager.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.core.analysis;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.List;
20 import java.util.Map;
21
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;
27
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;
32
33 /**
34 * Manages the available analysis helpers from different sources and their
35 * parameter providers.
36 *
37 * @author Geneviève Bastien
38 */
39 @NonNullByDefault
40 public class TmfAnalysisManager {
41
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<>();
47
48 /**
49 * Constructor, not to be used
50 */
51 private TmfAnalysisManager() {
52
53 }
54
55 /**
56 * Registers a new source of modules
57 *
58 * @param source
59 * A {@link IAnalysisModuleSource} instance
60 */
61 public static synchronized void registerModuleSource(IAnalysisModuleSource source) {
62 fSources.add(source);
63 refreshModules();
64 }
65
66 /**
67 * Initializes sources and new module listeners from the extension point
68 */
69 public static synchronized void initialize() {
70 fSources.clear();
71 fListeners.clear();
72 initializeModuleSources();
73 initializeNewModuleListeners();
74 }
75
76 /**
77 * Cleans the module sources list and initialize it from the extension point
78 */
79 private static synchronized void initializeModuleSources() {
80 for (IAnalysisModuleSource source : TmfAnalysisModuleSources.getSources()) {
81 fSources.add(source);
82 }
83 }
84
85 /**
86 * Cleans the new module listeners list and initialize it from the extension
87 * point
88 */
89 private static synchronized void initializeNewModuleListeners() {
90 for (ITmfNewAnalysisModuleListener output : TmfAnalysisModuleOutputs.getOutputListeners()) {
91 fListeners.add(output);
92 }
93 }
94
95 /**
96 * Add a new module listener to the list of listeners
97 *
98 * @param listener
99 * The new module listener
100 */
101 public static synchronized void addNewModuleListener(ITmfNewAnalysisModuleListener listener) {
102 fListeners.add(listener);
103 }
104
105 /**
106 * Gets all available analysis module helpers
107 *
108 * This map is read-only
109 *
110 * @return The map of available {@link IAnalysisModuleHelper}
111 * @since 1.0
112 */
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);
118 }
119 }
120 }
121 return checkNotNull(ImmutableMultimap.copyOf(fAnalysisModules));
122 }
123
124 /**
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
127 * applies.
128 *
129 * This map is read-only
130 *
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
135 * opened)
136 *
137 * @param traceclass
138 * The trace class to get modules for
139 * @return The map of available {@link IAnalysisModuleHelper}
140 */
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);
147 }
148 }
149 return checkNotNull(ImmutableMap.copyOf(map));
150 }
151
152 /**
153 * Register a new parameter provider for an analysis
154 *
155 * @param analysisId
156 * The id of the analysis
157 * @param paramProvider
158 * The class of the parameter provider
159 */
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>>());
164 }
165 fParameterProviders.get(analysisId).add(paramProvider);
166 }
167 }
168
169 /**
170 * Get a parameter provider that applies to the requested trace
171 *
172 * @param module
173 * Analysis module
174 * @param trace
175 * The trace
176 * @return A parameter provider if one applies to the trace, null otherwise
177 */
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())) {
182 return providerList;
183 }
184 for (Class<? extends IAnalysisParameterProvider> providerClass : fParameterProviders.get(module.getId())) {
185 try {
186 IAnalysisParameterProvider provider = fParamProviderInstances.get(providerClass);
187 if (provider == null) {
188 provider = providerClass.newInstance();
189 fParamProviderInstances.put(providerClass, provider);
190 }
191 if (provider != null && provider.appliesToTrace(trace)) {
192 providerList.add(provider);
193 }
194 } catch (IllegalArgumentException | SecurityException | InstantiationException | IllegalAccessException e) {
195 Activator.logError(Messages.TmfAnalysisManager_ErrorParameterProvider, e);
196 }
197 }
198 }
199 return providerList;
200 }
201
202 /**
203 * Clear the list of modules so that next time, it is computed again from
204 * sources
205 */
206 public static synchronized void refreshModules() {
207 fAnalysisModules.clear();
208 }
209
210 /**
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.
214 *
215 * @param module
216 * The newly created analysis module
217 */
218 public static synchronized void analysisModuleCreated(IAnalysisModule module) {
219 for (ITmfNewAnalysisModuleListener listener : fListeners) {
220 listener.moduleCreated(module);
221 }
222 }
223
224 }
This page took 0.040145 seconds and 4 git commands to generate.