tmf: bug 493661 Return analyses who apply to experiment
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / analysis / TmfAnalysisModuleHelperConfigElement.java
CommitLineData
c068a752 1/*******************************************************************************
60ae41e1 2 * Copyright (c) 2013, 2014 École Polytechnique de Montréal
c068a752
GB
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
8c736b3c 11 * Mathieu Rail - Added functionality for getting a module's requirements
c068a752
GB
12 *******************************************************************************/
13
2bdf0193 14package org.eclipse.tracecompass.tmf.core.analysis;
c068a752 15
63c43609 16import java.util.Collections;
8c736b3c
MR
17import java.util.HashSet;
18import java.util.Set;
63c43609 19
c068a752
GB
20import org.eclipse.core.runtime.ContributorFactoryOSGi;
21import org.eclipse.core.runtime.CoreException;
22import org.eclipse.core.runtime.IConfigurationElement;
23import org.eclipse.core.runtime.InvalidRegistryObjectException;
54eae41f 24import org.eclipse.jdt.annotation.NonNull;
2bdf0193
AM
25import org.eclipse.tracecompass.internal.tmf.core.Activator;
26import org.eclipse.tracecompass.internal.tmf.core.analysis.TmfAnalysisModuleSourceConfigElement;
58080002 27import org.eclipse.tracecompass.tmf.core.analysis.requirements.TmfAbstractAnalysisRequirement;
2bdf0193
AM
28import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
29import org.eclipse.tracecompass.tmf.core.project.model.TmfTraceType;
30import org.eclipse.tracecompass.tmf.core.project.model.TraceTypeHelper;
31import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
ff7b95a5
GB
32import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
33import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
c068a752
GB
34import org.osgi.framework.Bundle;
35
36/**
37 * Analysis module helper for modules provided by a plugin's configuration
38 * elements.
39 *
40 * @author Geneviève Bastien
41 */
b3b03da0 42public class TmfAnalysisModuleHelperConfigElement implements IAnalysisModuleHelper {
c068a752
GB
43
44 private final IConfigurationElement fCe;
45
46 /**
47 * Constructor
48 *
49 * @param ce
50 * The source {@link IConfigurationElement} of this module helper
51 */
b3b03da0 52 public TmfAnalysisModuleHelperConfigElement(IConfigurationElement ce) {
c068a752
GB
53 fCe = ce;
54 }
55
56 // ----------------------------------------
57 // Wrappers to {@link IAnalysisModule} methods
58 // ----------------------------------------
59
60 @Override
61 public String getId() {
ba27dd38
GB
62 String id = fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ID_ATTR);
63 if (id == null) {
64 throw new IllegalStateException();
65 }
66 return id;
c068a752
GB
67 }
68
69 @Override
70 public String getName() {
ba27dd38
GB
71 String name = fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR);
72 if (name == null) {
73 throw new IllegalStateException();
74 }
75 return name;
c068a752
GB
76 }
77
78 @Override
79 public boolean isAutomatic() {
0126a8ca 80 return Boolean.parseBoolean(fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.AUTOMATIC_ATTR));
c068a752
GB
81 }
82
ff7b95a5
GB
83 /**
84 * @since 1.0
85 */
86 @Override
87 public boolean appliesToExperiment() {
88 return Boolean.parseBoolean(fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.APPLIES_EXP_ATTR));
89 }
90
c068a752
GB
91 @Override
92 public String getHelpText() {
4bc53929
GB
93 /*
94 * FIXME: No need to externalize this. A better solution will be found
95 * soon and this string is just temporary
96 */
1124938f 97 return "The trace must be opened to get the help message"; //$NON-NLS-1$
c068a752
GB
98 }
99
100 @Override
101 public String getIcon() {
b3b03da0 102 return fCe.getAttribute(TmfAnalysisModuleSourceConfigElement.ICON_ATTR);
c068a752
GB
103 }
104
105 @Override
106 public Bundle getBundle() {
107 return ContributorFactoryOSGi.resolve(fCe.getContributor());
108 }
109
6fbe12fd 110 private boolean appliesToTraceClass(Class<? extends ITmfTrace> traceclass) {
c068a752
GB
111 boolean applies = false;
112
113 /* Get the module's applying tracetypes */
b3b03da0 114 final IConfigurationElement[] tracetypeCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.TRACETYPE_ELEM);
c068a752
GB
115 for (IConfigurationElement element : tracetypeCE) {
116 Class<?> applyclass;
117 try {
b3b03da0
GB
118 applyclass = getBundle().loadClass(element.getAttribute(TmfAnalysisModuleSourceConfigElement.CLASS_ATTR));
119 String classAppliesVal = element.getAttribute(TmfAnalysisModuleSourceConfigElement.APPLIES_ATTR);
c068a752
GB
120 boolean classApplies = true;
121 if (classAppliesVal != null) {
0126a8ca 122 classApplies = Boolean.parseBoolean(classAppliesVal);
c068a752
GB
123 }
124 if (classApplies) {
04da5385 125 applies |= applyclass.isAssignableFrom(traceclass);
c068a752 126 } else {
ff7b95a5
GB
127 /*
128 * If the trace type does not apply, reset the applies
129 * variable to false
130 */
04da5385
GB
131 if (applyclass.isAssignableFrom(traceclass)) {
132 applies = false;
133 }
c068a752 134 }
ff7b95a5 135 } catch (ClassNotFoundException | InvalidRegistryObjectException e) {
c068a752
GB
136 Activator.logError("Error in applies to trace", e); //$NON-NLS-1$
137 }
138 }
139 return applies;
140 }
141
6fbe12fd
GB
142 @Override
143 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceclass) {
144 boolean applies = appliesToTraceClass(traceclass);
145
146 /* Check if it applies to an experiment */
147 if (!applies && TmfExperiment.class.isAssignableFrom(traceclass)) {
148 applies = appliesToExperiment();
149 }
150 return applies;
151 }
152
63c43609
MR
153 @Override
154 public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
8c736b3c
MR
155 Set<Class<? extends ITmfTrace>> traceTypes = new HashSet<>();
156
a4a116c3 157 for (TraceTypeHelper tth : TmfTraceType.getTraceTypeHelpers()) {
8c736b3c
MR
158 if (appliesToTraceType(tth.getTraceClass())) {
159 traceTypes.add(tth.getTraceClass());
160 }
161 }
162
163 return traceTypes;
63c43609
MR
164 }
165
166 @Override
58080002 167 public Iterable<TmfAbstractAnalysisRequirement> getAnalysisRequirements() {
54eae41f
GB
168 IAnalysisModule module = createModule();
169 if (module != null) {
170 return module.getAnalysisRequirements();
8c736b3c 171 }
63c43609 172 return Collections.EMPTY_SET;
04da5385 173
63c43609
MR
174 }
175
c068a752
GB
176 // ---------------------------------------
177 // Functionalities
178 // ---------------------------------------
179
54eae41f
GB
180 private IAnalysisModule createModule() {
181 IAnalysisModule module = null;
182 try {
183 module = (IAnalysisModule) fCe.createExecutableExtension(TmfAnalysisModuleSourceConfigElement.ANALYSIS_MODULE_ATTR);
184 module.setName(getName());
185 module.setId(getId());
186 } catch (CoreException e) {
187 Activator.logError("Error getting analysis modules from configuration files", e); //$NON-NLS-1$
188 }
189 return module;
190 }
191
c068a752
GB
192 @Override
193 public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
194
ff7b95a5 195 /* Check if it applies to trace itself */
6fbe12fd 196 boolean applies = appliesToTraceClass(trace.getClass());
ff7b95a5
GB
197 /*
198 * If the trace is an experiment, check if this module would apply to an
199 * experiment should it apply to one of its traces.
200 */
201 if (!applies && (trace instanceof TmfExperiment) && appliesToExperiment()) {
202 for (ITmfTrace expTrace : TmfTraceManager.getTraceSet(trace)) {
6fbe12fd 203 if (appliesToTraceClass(expTrace.getClass())) {
ff7b95a5
GB
204 applies = true;
205 break;
206 }
207 }
208 }
209
ff7b95a5 210 if (!applies) {
f479550c 211 return null;
c068a752
GB
212 }
213
54eae41f
GB
214 IAnalysisModule module = createModule();
215 if (module == null) {
216 return null;
217 }
218
219 module.setAutomatic(isAutomatic());
220
221 /* Get the module's parameters */
222 final IConfigurationElement[] parametersCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.PARAMETER_ELEM);
223 for (IConfigurationElement element : parametersCE) {
ba27dd38
GB
224 String paramName = element.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR);
225 if (paramName == null) {
226 continue;
227 }
228 module.addParameter(paramName);
54eae41f
GB
229 String defaultValue = element.getAttribute(TmfAnalysisModuleSourceConfigElement.DEFAULT_VALUE_ATTR);
230 if (defaultValue != null) {
ba27dd38 231 module.setParameter(paramName, defaultValue);
c068a752 232 }
c068a752 233 }
f479550c
GB
234 if (module.setTrace(trace)) {
235 TmfAnalysisManager.analysisModuleCreated(module);
236 } else {
237 module.dispose();
238 module = null;
239 }
54eae41f 240
c068a752
GB
241 return module;
242
243 }
54eae41f
GB
244
245 @Override
246 public String getHelpText(@NonNull ITmfTrace trace) {
247 IAnalysisModule module = createModule();
248 if (module != null) {
249 String ret = module.getHelpText(trace);
250 module.dispose();
251 return ret;
252 }
253 return getHelpText();
04da5385 254
54eae41f 255 }
c068a752 256}
This page took 0.089483 seconds and 5 git commands to generate.