TMF: Specify if an analysis applies differently to an experiment
[deliverable/tracecompass.git] / 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;
c068a752 25import org.eclipse.osgi.util.NLS;
2bdf0193
AM
26import org.eclipse.tracecompass.internal.tmf.core.Activator;
27import org.eclipse.tracecompass.internal.tmf.core.analysis.TmfAnalysisModuleSourceConfigElement;
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 */
97 return new String("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
110 @Override
111 public boolean appliesToTraceType(Class<? extends ITmfTrace> traceclass) {
112 boolean applies = false;
113
114 /* Get the module's applying tracetypes */
b3b03da0 115 final IConfigurationElement[] tracetypeCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.TRACETYPE_ELEM);
c068a752
GB
116 for (IConfigurationElement element : tracetypeCE) {
117 Class<?> applyclass;
118 try {
b3b03da0
GB
119 applyclass = getBundle().loadClass(element.getAttribute(TmfAnalysisModuleSourceConfigElement.CLASS_ATTR));
120 String classAppliesVal = element.getAttribute(TmfAnalysisModuleSourceConfigElement.APPLIES_ATTR);
c068a752
GB
121 boolean classApplies = true;
122 if (classAppliesVal != null) {
0126a8ca 123 classApplies = Boolean.parseBoolean(classAppliesVal);
c068a752
GB
124 }
125 if (classApplies) {
04da5385 126 applies |= applyclass.isAssignableFrom(traceclass);
c068a752 127 } else {
ff7b95a5
GB
128 /*
129 * If the trace type does not apply, reset the applies
130 * variable to false
131 */
04da5385
GB
132 if (applyclass.isAssignableFrom(traceclass)) {
133 applies = false;
134 }
c068a752 135 }
ff7b95a5 136 } catch (ClassNotFoundException | InvalidRegistryObjectException e) {
c068a752
GB
137 Activator.logError("Error in applies to trace", e); //$NON-NLS-1$
138 }
139 }
140 return applies;
141 }
142
63c43609
MR
143 @Override
144 public Iterable<Class<? extends ITmfTrace>> getValidTraceTypes() {
8c736b3c
MR
145 Set<Class<? extends ITmfTrace>> traceTypes = new HashSet<>();
146
a4a116c3 147 for (TraceTypeHelper tth : TmfTraceType.getTraceTypeHelpers()) {
8c736b3c
MR
148 if (appliesToTraceType(tth.getTraceClass())) {
149 traceTypes.add(tth.getTraceClass());
150 }
151 }
152
153 return traceTypes;
63c43609
MR
154 }
155
156 @Override
157 public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
54eae41f
GB
158 IAnalysisModule module = createModule();
159 if (module != null) {
160 return module.getAnalysisRequirements();
8c736b3c 161 }
63c43609 162 return Collections.EMPTY_SET;
04da5385 163
63c43609
MR
164 }
165
c068a752
GB
166 // ---------------------------------------
167 // Functionalities
168 // ---------------------------------------
169
54eae41f
GB
170 private IAnalysisModule createModule() {
171 IAnalysisModule module = null;
172 try {
173 module = (IAnalysisModule) fCe.createExecutableExtension(TmfAnalysisModuleSourceConfigElement.ANALYSIS_MODULE_ATTR);
174 module.setName(getName());
175 module.setId(getId());
176 } catch (CoreException e) {
177 Activator.logError("Error getting analysis modules from configuration files", e); //$NON-NLS-1$
178 }
179 return module;
180 }
181
c068a752
GB
182 @Override
183 public IAnalysisModule newModule(ITmfTrace trace) throws TmfAnalysisException {
184
ff7b95a5
GB
185 /* Check if it applies to trace itself */
186 boolean applies = appliesToTraceType(trace.getClass());
187 /*
188 * If the trace is an experiment, check if this module would apply to an
189 * experiment should it apply to one of its traces.
190 */
191 if (!applies && (trace instanceof TmfExperiment) && appliesToExperiment()) {
192 for (ITmfTrace expTrace : TmfTraceManager.getTraceSet(trace)) {
193 if (appliesToTraceType(expTrace.getClass())) {
194 applies = true;
195 break;
196 }
197 }
198 }
199
c068a752 200 /* Check that analysis can be executed */
ff7b95a5 201 if (!applies) {
c068a752
GB
202 throw new TmfAnalysisException(NLS.bind(Messages.TmfAnalysisModuleHelper_AnalysisDoesNotApply, getName()));
203 }
204
54eae41f
GB
205 IAnalysisModule module = createModule();
206 if (module == null) {
207 return null;
208 }
209
210 module.setAutomatic(isAutomatic());
211
212 /* Get the module's parameters */
213 final IConfigurationElement[] parametersCE = fCe.getChildren(TmfAnalysisModuleSourceConfigElement.PARAMETER_ELEM);
214 for (IConfigurationElement element : parametersCE) {
ba27dd38
GB
215 String paramName = element.getAttribute(TmfAnalysisModuleSourceConfigElement.NAME_ATTR);
216 if (paramName == null) {
217 continue;
218 }
219 module.addParameter(paramName);
54eae41f
GB
220 String defaultValue = element.getAttribute(TmfAnalysisModuleSourceConfigElement.DEFAULT_VALUE_ATTR);
221 if (defaultValue != null) {
ba27dd38 222 module.setParameter(paramName, defaultValue);
c068a752 223 }
c068a752 224 }
54eae41f
GB
225 module.setTrace(trace);
226 TmfAnalysisManager.analysisModuleCreated(module);
227
c068a752
GB
228 return module;
229
230 }
54eae41f
GB
231
232 @Override
233 public String getHelpText(@NonNull ITmfTrace trace) {
234 IAnalysisModule module = createModule();
235 if (module != null) {
236 String ret = module.getHelpText(trace);
237 module.dispose();
238 return ret;
239 }
240 return getHelpText();
04da5385 241
54eae41f 242 }
c068a752 243}
This page took 0.081229 seconds and 5 git commands to generate.