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
;
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
.linuxtools
.internal
.tmf
.core
.Activator
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.analysis
.TmfAnalysisRequirement
.ValuePriorityLevel
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.component
.TmfComponent
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfAnalysisException
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfStartAnalysisSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceClosedSignal
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
39 import org
.eclipse
.osgi
.util
.NLS
;
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 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() {
77 public void setName(String name
) {
82 public void setId(String id
) {
88 public String
getId() {
91 id
= new String(this.getClass().getCanonicalName());
98 public void setAutomatic(boolean auto
) {
103 public void setTrace(ITmfTrace trace
) throws TmfAnalysisException
{
105 throw new TmfAnalysisException(Messages
.TmfAbstractAnalysisModule_NullTrace
);
107 if (fTrace
!= null) {
108 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_TraceSetMoreThanOnce
, getName()));
111 /* Check that analysis can be executed */
112 if (!canExecute(trace
)) {
113 throw new TmfAnalysisException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, getName()));
117 /* Get the parameter providers for this trace */
118 fParameterProviders
= TmfAnalysisManager
.getParameterProviders(this, fTrace
);
119 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
120 provider
.registerModule(this);
131 protected ITmfTrace
getTrace() {
136 public void addParameter(String name
) {
137 fParameterNames
.add(name
);
141 public synchronized void setParameter(String name
, Object value
) {
142 if (!fParameterNames
.contains(name
)) {
143 throw new RuntimeException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, name
, getName()));
145 Object oldValue
= fParameters
.get(name
);
146 fParameters
.put(name
, value
);
147 if ((value
!= null) && !(value
.equals(oldValue
))) {
148 parameterChanged(name
);
153 public synchronized void notifyParameterChanged(String name
) {
154 if (!fParameterNames
.contains(name
)) {
155 throw new RuntimeException(NLS
.bind(Messages
.TmfAbstractAnalysisModule_InvalidParameter
, name
, getName()));
157 Object oldValue
= fParameters
.get(name
);
158 Object value
= getParameter(name
);
159 if ((value
!= null) && !(value
.equals(oldValue
))) {
160 parameterChanged(name
);
165 * Used to indicate that a parameter value has been changed
168 * The name of the modified parameter
170 protected void parameterChanged(String name
) {
175 public Object
getParameter(String name
) {
176 Object paramValue
= fParameters
.get(name
);
177 /* The parameter is not set, maybe it can be provided by someone else */
178 if ((paramValue
== null) && (fTrace
!= null)) {
179 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
180 paramValue
= provider
.getParameter(name
);
181 if (paramValue
!= null) {
190 public boolean canExecute(@NonNull ITmfTrace trace
) {
191 for (TmfAnalysisRequirement requirement
: getAnalysisRequirements()) {
192 if (!requirement
.isFulfilled(trace
)) {
200 * Set the countdown latch back to 1 so the analysis can be executed again
202 protected void resetAnalysis() {
203 fFinishedLatch
.countDown();
204 fFinishedLatch
= new CountDownLatch(1);
208 * Actually executes the analysis itself
212 * @return Whether the analysis was completed successfully or not
213 * @throws TmfAnalysisException
214 * Method may throw an analysis exception
216 protected abstract boolean executeAnalysis(final IProgressMonitor monitor
) throws TmfAnalysisException
;
219 * Indicate the analysis has been canceled. It is abstract to force
220 * implementing class to cleanup what they are running. This is called by
221 * the job's canceling. It does not need to be called directly.
223 protected abstract void canceling();
226 * To be called when the analysis is completed, whether normally or because
227 * it was cancelled or for any other reason.
229 * It has to be called inside a synchronized block
231 private void setAnalysisCompleted() {
234 fFinishedLatch
.countDown();
238 * Cancels the analysis if it is executing
241 public final void cancel() {
242 synchronized (syncObj
) {
245 fAnalysisCancelled
= true;
246 setAnalysisCompleted();
254 public void close() {
259 public void dispose() {
264 private void execute(@NonNull final ITmfTrace trace
) {
267 * TODO: The analysis in a job should be done at the analysis manager
268 * level instead of depending on this abstract class implementation,
269 * otherwise another analysis implementation may block the main thread
272 /* Do not execute if analysis has already run */
273 if (fFinishedLatch
.getCount() == 0) {
277 /* Do not execute if analysis already running */
278 synchronized (syncObj
) {
286 * Actual analysis will be run on a separate thread
288 fJob
= new Job(NLS
.bind(Messages
.TmfAbstractAnalysisModule_RunningAnalysis
, getName())) {
290 protected IStatus
run(final IProgressMonitor monitor
) {
292 monitor
.beginTask("", IProgressMonitor
.UNKNOWN
); //$NON-NLS-1$
293 broadcast(new TmfStartAnalysisSignal(TmfAbstractAnalysisModule
.this, TmfAbstractAnalysisModule
.this));
294 fAnalysisCancelled
= !executeAnalysis(monitor
);
295 } catch (TmfAnalysisException e
) {
296 Activator
.logError("Error executing analysis with trace " + getTrace().getName(), e
); //$NON-NLS-1$
298 synchronized (syncObj
) {
300 setAnalysisCompleted();
302 TmfTraceManager
.refreshSupplementaryFiles(trace
);
304 if (!fAnalysisCancelled
) {
305 return Status
.OK_STATUS
;
307 // Reset analysis so that it can be executed again.
309 return Status
.CANCEL_STATUS
;
313 protected void canceling() {
314 TmfAbstractAnalysisModule
.this.canceling();
322 public IStatus
schedule() {
323 final ITmfTrace trace
= fTrace
;
325 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, String
.format("No trace specified for analysis %s", getName())); //$NON-NLS-1$
329 return Status
.OK_STATUS
;
333 public Iterable
<IAnalysisOutput
> getOutputs() {
338 public void registerOutput(IAnalysisOutput output
) {
339 if (!fOutputs
.contains(output
)) {
340 fOutputs
.add(output
);
345 public boolean waitForCompletion() {
347 fFinishedLatch
.await();
348 } catch (InterruptedException e
) {
349 Activator
.logError("Error while waiting for module completion", e
); //$NON-NLS-1$
351 return !fAnalysisCancelled
;
355 public boolean waitForCompletion(IProgressMonitor monitor
) {
357 while (!fFinishedLatch
.await(500, TimeUnit
.MILLISECONDS
)) {
358 if (fAnalysisCancelled
|| monitor
.isCanceled()) {
359 fAnalysisCancelled
= true;
363 } catch (InterruptedException e
) {
364 Activator
.logError("Error while waiting for module completion", e
); //$NON-NLS-1$
366 return !fAnalysisCancelled
;
370 * Signal handler for trace closing
373 * Trace closed signal
376 public void traceClosed(TmfTraceClosedSignal signal
) {
377 /* Is the closing trace the one that was requested? */
378 if (signal
.getTrace() == fTrace
) {
385 * Signal handler for when the trace becomes active
388 * Trace selected signal
392 public void traceSelected(TmfTraceSelectedSignal signal
) {
394 * Since some parameter providers may handle many traces, we need to
395 * register the current trace to it
397 if (signal
.getTrace() == fTrace
) {
398 for (IAnalysisParameterProvider provider
: fParameterProviders
) {
399 provider
.registerModule(this);
405 * Returns a full help text to display
407 * @return Full help text for the module
409 protected String
getFullHelpText() {
410 return NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisModule
, getName());
414 * Gets a short help text, to display as header to other help text
417 * The trace to show help for
419 * @return Short help text describing the module
421 protected String
getShortHelpText(ITmfTrace trace
) {
422 return NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisForTrace
, getName(), trace
.getName());
426 * Gets the help text specific for a trace who does not have required
427 * characteristics for module to execute. The default implementation uses
428 * the analysis requirements.
431 * The trace to apply the analysis to
434 protected String
getTraceCannotExecuteHelpText(@NonNull ITmfTrace trace
) {
435 StringBuilder builder
= new StringBuilder();
436 builder
.append(NLS
.bind(Messages
.TmfAbstractAnalysisModule_AnalysisCannotExecute
, getName()));
437 for (TmfAnalysisRequirement requirement
: getAnalysisRequirements()) {
438 if (!requirement
.isFulfilled(trace
)) {
439 builder
.append("\n\n"); //$NON-NLS-1$
440 builder
.append(NLS
.bind(Messages
.TmfAnalysis_RequirementNotFulfilled
, requirement
.getType()));
441 builder
.append("\n"); //$NON-NLS-1$
442 builder
.append(NLS
.bind(Messages
.TmfAnalysis_RequirementMandatoryValues
, requirement
.getValues(ValuePriorityLevel
.MANDATORY
)));
443 Set
<String
> information
= requirement
.getInformation();
444 if (!information
.isEmpty()) {
445 builder
.append("\n"); //$NON-NLS-1$
446 builder
.append(NLS
.bind(Messages
.TmfAnalysis_RequirementInformation
, information
));
450 return builder
.toString();
454 public String
getHelpText() {
455 return getFullHelpText();
459 public String
getHelpText(ITmfTrace trace
) {
461 return getHelpText();
463 String text
= getShortHelpText(trace
);
464 if (!canExecute(trace
)) {
465 text
= text
+ "\n\n" + getTraceCannotExecuteHelpText(trace
); //$NON-NLS-1$
471 public Iterable
<TmfAnalysisRequirement
> getAnalysisRequirements() {
472 return Collections
.EMPTY_SET
;