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
.linuxtools
.tmf
.core
.analysis
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Collections
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
20 import java
.util
.concurrent
.CountDownLatch
;
21 import java
.util
.concurrent
.TimeUnit
;
23 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
24 import org
.eclipse
.core
.runtime
.IStatus
;
25 import org
.eclipse
.core
.runtime
.Status
;
26 import org
.eclipse
.core
.runtime
.jobs
.Job
;
27 import org
.eclipse
.jdt
.annotation
.NonNull
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfAnalysisException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStartAnalysisSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
36 import org
.eclipse
.osgi
.util
.NLS
;
39 * Base class that analysis modules main class may extend. It provides default
40 * behavior to some methods of the analysis module
42 * @author Geneviève Bastien
45 public abstract class TmfAbstractAnalysisModule
extends TmfComponent
implements IAnalysisModule
{
47 @NonNull private static final String UNDEFINED_ID
= "undefined"; //$NON-NLS-1$
49 private String fName
, fId
;
50 private boolean fAutomatic
= false, fStarted
= false;
51 private ITmfTrace fTrace
;
52 private final Map
<String
, Object
> fParameters
= new HashMap
<>();
53 private final List
<String
> fParameterNames
= new ArrayList
<>();
54 private final List
<IAnalysisOutput
> fOutputs
= new ArrayList
<>();
55 private List
<IAnalysisParameterProvider
> fParameterProviders
= new ArrayList
<>();
56 private Job fJob
= null;
58 private final Object syncObj
= new Object();
60 /* Latch tracking if the analysis is completed or not */
61 private CountDownLatch fFinishedLatch
= new CountDownLatch(0);
63 private boolean fAnalysisCancelled
= false;
66 public boolean isAutomatic() {
71 public String
getName() {
76 public void setName(String name
) {
81 public void setId(String id
) {
87 public String
getId() {
90 Activator
.logError("Analysis module getId(): the id should not be null in class " + this.getClass().getSimpleName()); //$NON-NLS-1$
97 public void setAutomatic(boolean auto
) {
102 public void setTrace(ITmfTrace trace
) throws TmfAnalysisException
{
103 if (fTrace
!= null) {
104 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_TraceSetMoreThanOnce
, getName()));
107 /* Check that analysis can be executed */
108 if (!canExecute(trace
)) {
109 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, getName()));
112 /* Get the parameter providers for this trace */
113 fParameterProviders
= TmfAnalysisManager
.getParameterProviders(this, fTrace
);
114 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
115 provider
.registerModule(this);
126 protected ITmfTrace
getTrace() {
131 public void addParameter(String name
) {
132 fParameterNames
.add(name
);
136 public synchronized void setParameter(String name
, Object value
) {
137 if (!fParameterNames
.contains(name
)) {
138 throw new RuntimeException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, name
, getName()));
140 Object oldValue
= fParameters
.get(name
);
141 fParameters
.put(name
, value
);
142 if ((value
!= null) && !(value
.equals(oldValue
))) {
143 parameterChanged(name
);
148 public synchronized void notifyParameterChanged(String name
) {
149 if (!fParameterNames
.contains(name
)) {
150 throw new RuntimeException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, name
, getName()));
152 Object oldValue
= fParameters
.get(name
);
153 Object value
= getParameter(name
);
154 if ((value
!= null) && !(value
.equals(oldValue
))) {
155 parameterChanged(name
);
160 * Used to indicate that a parameter value has been changed
163 * The name of the modified parameter
165 protected void parameterChanged(String name
) {
170 public Object
getParameter(String name
) {
171 Object paramValue
= fParameters
.get(name
);
172 /* The parameter is not set, maybe it can be provided by someone else */
173 if ((paramValue
== null) && (fTrace
!= null)) {
174 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
175 paramValue
= provider
.getParameter(name
);
176 if (paramValue
!= null) {
185 public boolean canExecute(ITmfTrace trace
) {
190 * Set the countdown latch back to 1 so the analysis can be executed again
192 protected void resetAnalysis() {
193 fFinishedLatch
.countDown();
194 fFinishedLatch
= new CountDownLatch(1);
198 * Actually executes the analysis itself
202 * @return Whether the analysis was completed successfully or not
203 * @throws TmfAnalysisException
204 * Method may throw an analysis exception
206 protected abstract boolean executeAnalysis(final IProgressMonitor monitor
) throws TmfAnalysisException
;
209 * Indicate the analysis has been canceled. It is abstract to force
210 * implementing class to cleanup what they are running. This is called by
211 * the job's canceling. It does not need to be called directly.
213 protected abstract void canceling();
216 * To be called when the analysis is completed, whether normally or because
217 * it was cancelled or for any other reason.
219 * It has to be called inside a synchronized block
221 private void setAnalysisCompleted() {
224 fFinishedLatch
.countDown();
228 * Cancels the analysis if it is executing
231 public final void cancel() {
232 synchronized (syncObj
) {
235 fAnalysisCancelled
= true;
236 setAnalysisCompleted();
244 public void close() {
249 public void dispose() {
254 private void execute() {
257 * TODO: The analysis in a job should be done at the analysis manager
258 * level instead of depending on this abstract class implementation,
259 * otherwise another analysis implementation may block the main thread
262 /* Do not execute if analysis has already run */
263 if (fFinishedLatch
.getCount() == 0) {
267 /* Do not execute if analysis already running */
268 synchronized (syncObj
) {
275 final ITmfTrace trace
= fTrace
;
277 * Actual analysis will be run on a separate thread
279 fJob
= new Job(NLS
.bind(Messages
.TmfAbstractAnalysisModule_RunningAnalysis
, getName())) {
281 protected IStatus
run(final IProgressMonitor monitor
) {
283 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
284 broadcast(new TmfStartAnalysisSignal(TmfAbstractAnalysisModule
.this, TmfAbstractAnalysisModule
.this));
285 fAnalysisCancelled
= !executeAnalysis(monitor
);
286 } catch (TmfAnalysisException e
) {
287 Activator
.logError("Error executing analysis with trace " + getTrace().getName(), e
); //$NON-NLS-1$
289 synchronized (syncObj
) {
291 setAnalysisCompleted();
293 TmfTraceManager
.refreshSupplementaryFiles(trace
);
295 if (!fAnalysisCancelled
) {
296 return Status
.OK_STATUS
;
298 // Reset analysis so that it can be executed again.
300 return Status
.CANCEL_STATUS
;
304 protected void canceling() {
305 TmfAbstractAnalysisModule
.this.canceling();
313 public IStatus
schedule() {
314 if (fTrace
== null) {
315 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, String
.format("No trace specified for analysis %s", getName())); //$NON-NLS-1$
319 return Status
.OK_STATUS
;
323 public Iterable
<IAnalysisOutput
> getOutputs() {
328 public void registerOutput(IAnalysisOutput output
) {
329 if (!fOutputs
.contains(output
)) {
330 fOutputs
.add(output
);
335 public boolean waitForCompletion() {
337 fFinishedLatch
.await();
338 } catch (InterruptedException e
) {
339 Activator
.logError("Error while waiting for module completion", e
); //$NON-NLS-1$
341 return !fAnalysisCancelled
;
345 public boolean waitForCompletion(IProgressMonitor monitor
) {
347 while (!fFinishedLatch
.await(500, TimeUnit
.MILLISECONDS
)) {
348 if (fAnalysisCancelled
|| monitor
.isCanceled()) {
349 fAnalysisCancelled
= true;
353 } catch (InterruptedException e
) {
354 Activator
.logError("Error while waiting for module completion", e
); //$NON-NLS-1$
356 return !fAnalysisCancelled
;
360 * Signal handler for trace closing
363 * Trace closed signal
366 public void traceClosed(TmfTraceClosedSignal signal
) {
367 /* Is the closing trace the one that was requested? */
368 if (signal
.getTrace() == fTrace
) {
375 * Returns a full help text to display
377 * @return Full help text for the module
379 protected String
getFullHelpText() {
380 return NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisModule
, getName());
384 * Gets a short help text, to display as header to other help text
387 * The trace to show help for
389 * @return Short help text describing the module
391 protected String
getShortHelpText(ITmfTrace trace
) {
392 return NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisForTrace
, getName(), trace
.getName());
396 * Gets the help text specific for a trace who does not have required
397 * characteristics for module to execute
400 * The trace to apply the analysis to
403 protected String
getTraceCannotExecuteHelpText(ITmfTrace trace
) {
404 return Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
;
408 public String
getHelpText() {
409 return getFullHelpText();
413 public String
getHelpText(ITmfTrace trace
) {
415 return getHelpText();
417 String text
= getShortHelpText(trace
);
418 if (!canExecute(trace
)) {
419 text
= text
+ getTraceCannotExecuteHelpText(trace
);
425 public Iterable
<TmfAnalysisRequirement
> getAnalysisRequirements() {
426 return Collections
.EMPTY_SET
;