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 java
.util
.ArrayList
;
16 import java
.util
.Collections
;
17 import java
.util
.HashMap
;
18 import java
.util
.List
;
21 import java
.util
.concurrent
.CountDownLatch
;
22 import java
.util
.concurrent
.TimeUnit
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.core
.runtime
.Status
;
27 import org
.eclipse
.core
.runtime
.jobs
.Job
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.osgi
.util
.NLS
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.Activator
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.TmfAnalysisRequirement
.ValuePriorityLevel
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.component
.TmfComponent
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfAnalysisException
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfStartAnalysisSignal
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
42 * Base class that analysis modules main class may extend. It provides default
43 * behavior to some methods of the analysis module
45 * @author Geneviève Bastien
48 public abstract class TmfAbstractAnalysisModule
extends TmfComponent
implements IAnalysisModule
{
50 private String fName
, fId
;
51 private boolean fAutomatic
= false, fStarted
= false;
52 private volatile ITmfTrace fTrace
;
53 private final Map
<String
, Object
> fParameters
= new HashMap
<>();
54 private final List
<String
> fParameterNames
= new ArrayList
<>();
55 private final List
<IAnalysisOutput
> fOutputs
= new ArrayList
<>();
56 private List
<IAnalysisParameterProvider
> fParameterProviders
= new ArrayList
<>();
57 private Job fJob
= null;
59 private final Object syncObj
= new Object();
61 /* Latch tracking if the analysis is completed or not */
62 private CountDownLatch fFinishedLatch
= new CountDownLatch(0);
64 private boolean fAnalysisCancelled
= false;
67 public boolean isAutomatic() {
72 public String
getName() {
75 return ""; //$NON-NLS-1$
81 public void setName(String name
) {
86 public void setId(String id
) {
92 public String
getId() {
95 id
= new String(this.getClass().getCanonicalName());
102 public void setAutomatic(boolean auto
) {
107 public void setTrace(ITmfTrace trace
) throws TmfAnalysisException
{
109 throw new TmfAnalysisException(Messages
.TmfAbstractAnalysisModule_NullTrace
);
111 if (fTrace
!= null) {
112 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_TraceSetMoreThanOnce
, getName()));
115 /* Check that analysis can be executed */
116 if (!canExecute(trace
)) {
117 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, getName()));
121 /* Get the parameter providers for this trace */
122 fParameterProviders
= TmfAnalysisManager
.getParameterProviders(this, trace
);
123 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
124 provider
.registerModule(this);
135 protected ITmfTrace
getTrace() {
140 public void addParameter(String name
) {
141 fParameterNames
.add(name
);
145 public synchronized void setParameter(String name
, Object value
) {
146 if (!fParameterNames
.contains(name
)) {
147 throw new RuntimeException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, name
, getName()));
149 Object oldValue
= fParameters
.get(name
);
150 fParameters
.put(name
, value
);
151 if ((value
!= null) && !(value
.equals(oldValue
))) {
152 parameterChanged(name
);
157 public synchronized void notifyParameterChanged(String name
) {
158 if (!fParameterNames
.contains(name
)) {
159 throw new RuntimeException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, name
, getName()));
161 Object oldValue
= fParameters
.get(name
);
162 Object value
= getParameter(name
);
163 if ((value
!= null) && !(value
.equals(oldValue
))) {
164 parameterChanged(name
);
169 * Used to indicate that a parameter value has been changed
172 * The name of the modified parameter
174 protected void parameterChanged(String name
) {
179 public Object
getParameter(String name
) {
180 Object paramValue
= fParameters
.get(name
);
181 /* The parameter is not set, maybe it can be provided by someone else */
182 if ((paramValue
== null) && (fTrace
!= null)) {
183 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
184 paramValue
= provider
.getParameter(name
);
185 if (paramValue
!= null) {
194 public boolean canExecute(@NonNull ITmfTrace trace
) {
195 for (TmfAnalysisRequirement requirement
: getAnalysisRequirements()) {
196 if (!requirement
.isFulfilled(trace
)) {
204 * Set the countdown latch back to 1 so the analysis can be executed again
206 protected void resetAnalysis() {
207 fFinishedLatch
.countDown();
208 fFinishedLatch
= new CountDownLatch(1);
212 * Actually executes the analysis itself
216 * @return Whether the analysis was completed successfully or not
217 * @throws TmfAnalysisException
218 * Method may throw an analysis exception
220 protected abstract boolean executeAnalysis(final IProgressMonitor monitor
) throws TmfAnalysisException
;
223 * Indicate the analysis has been canceled. It is abstract to force
224 * implementing class to cleanup what they are running. This is called by
225 * the job's canceling. It does not need to be called directly.
227 protected abstract void canceling();
230 * To be called when the analysis is completed, whether normally or because
231 * it was cancelled or for any other reason.
233 * It has to be called inside a synchronized block
235 private void setAnalysisCompleted() {
238 fFinishedLatch
.countDown();
242 * Cancels the analysis if it is executing
245 public final void cancel() {
246 synchronized (syncObj
) {
249 fAnalysisCancelled
= true;
250 setAnalysisCompleted();
258 public void dispose() {
263 private void execute(@NonNull final ITmfTrace trace
) {
266 * TODO: The analysis in a job should be done at the analysis manager
267 * level instead of depending on this abstract class implementation,
268 * otherwise another analysis implementation may block the main thread
271 /* Do not execute if analysis has already run */
272 if (fFinishedLatch
.getCount() == 0) {
276 /* Do not execute if analysis already running */
277 synchronized (syncObj
) {
285 * Actual analysis will be run on a separate thread
287 fJob
= new Job(NLS
.bind(Messages
.TmfAbstractAnalysisModule_RunningAnalysis
, getName())) {
289 protected IStatus
run(final IProgressMonitor monitor
) {
291 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
292 broadcast(new TmfStartAnalysisSignal(TmfAbstractAnalysisModule
.this, TmfAbstractAnalysisModule
.this));
293 fAnalysisCancelled
= !executeAnalysis(monitor
);
294 } catch (TmfAnalysisException e
) {
295 Activator
.logError("Error executing analysis with trace " + trace
.getName(), e
); //$NON-NLS-1$
297 synchronized (syncObj
) {
299 setAnalysisCompleted();
301 TmfTraceManager
.refreshSupplementaryFiles(trace
);
303 if (!fAnalysisCancelled
) {
304 return Status
.OK_STATUS
;
306 // Reset analysis so that it can be executed again.
308 return Status
.CANCEL_STATUS
;
312 protected void canceling() {
313 TmfAbstractAnalysisModule
.this.canceling();
321 public IStatus
schedule() {
322 synchronized (syncObj
) {
323 final ITmfTrace trace
= getTrace();
325 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, String
.format("No trace specified for analysis %s", getName())); //$NON-NLS-1$
330 return Status
.OK_STATUS
;
334 public Iterable
<IAnalysisOutput
> getOutputs() {
339 public void registerOutput(IAnalysisOutput output
) {
340 if (!fOutputs
.contains(output
)) {
341 fOutputs
.add(output
);
346 public boolean waitForCompletion() {
348 fFinishedLatch
.await();
349 } catch (InterruptedException e
) {
350 Activator
.logError("Error while waiting for module completion", e
); //$NON-NLS-1$
352 return !fAnalysisCancelled
;
356 public boolean waitForCompletion(IProgressMonitor monitor
) {
358 while (!fFinishedLatch
.await(500, TimeUnit
.MILLISECONDS
)) {
359 if (fAnalysisCancelled
|| monitor
.isCanceled()) {
360 fAnalysisCancelled
= true;
364 } catch (InterruptedException e
) {
365 Activator
.logError("Error while waiting for module completion", e
); //$NON-NLS-1$
367 return !fAnalysisCancelled
;
371 * Signal handler for trace closing
374 * Trace closed signal
377 public void traceClosed(TmfTraceClosedSignal signal
) {
378 /* Is the closing trace the one that was requested? */
379 synchronized (syncObj
) {
380 if (signal
.getTrace() == fTrace
) {
388 * Signal handler for when the trace becomes active
391 * Trace selected signal
395 public void traceSelected(TmfTraceSelectedSignal signal
) {
397 * Since some parameter providers may handle many traces, we need to
398 * register the current trace to it
400 if (signal
.getTrace() == fTrace
) {
401 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
402 provider
.registerModule(this);
408 * Returns a full help text to display
410 * @return Full help text for the module
412 protected String
getFullHelpText() {
413 return NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisModule
, getName());
417 * Gets a short help text, to display as header to other help text
420 * The trace to show help for
422 * @return Short help text describing the module
424 protected String
getShortHelpText(ITmfTrace trace
) {
425 return NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisForTrace
, getName(), trace
.getName());
429 * Gets the help text specific for a trace who does not have required
430 * characteristics for module to execute. The default implementation uses
431 * the analysis requirements.
434 * The trace to apply the analysis to
437 protected String
getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace
) {
438 StringBuilder builder
= new StringBuilder();
439 builder
.append(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, getName()));
440 for (TmfAnalysisRequirement requirement
: getAnalysisRequirements()) {
441 if (!requirement
.isFulfilled(trace
)) {
442 builder
.append("\n\n"); //$NON-NLS-1$
443 builder
.append(NLS
.bind(Messages
.TmfAnalysis_RequirementNotFulfilled
, requirement
.getType()));
444 builder
.append("\n"); //$NON-NLS-1$
445 builder
.append(NLS
.bind(Messages
.TmfAnalysis_RequirementMandatoryValues
, requirement
.getValues(ValuePriorityLevel
.MANDATORY
)));
446 Set
<String
> information
= requirement
.getInformation();
447 if (!information
.isEmpty()) {
448 builder
.append("\n"); //$NON-NLS-1$
449 builder
.append(NLS
.bind(Messages
.TmfAnalysis_RequirementInformation
, information
));
453 return builder
.toString();
457 public String
getHelpText() {
458 return getFullHelpText();
462 public String
getHelpText(ITmfTrace trace
) {
464 return getHelpText();
466 String text
= getShortHelpText(trace
);
467 if (!canExecute(trace
)) {
468 text
= text
+ "\n\n" + getTraceCannotExecuteHelpText(trace
); //$NON-NLS-1$
474 public Iterable
<TmfAnalysisRequirement
> getAnalysisRequirements() {
475 return Collections
.EMPTY_SET
;