1 /*******************************************************************************
2 * Copyright (c) 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
.internal
.tmf
.analysis
.xml
.ui
.views
.xychart
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.Collections
;
18 import java
.util
.HashMap
;
19 import java
.util
.LinkedList
;
20 import java
.util
.List
;
22 import java
.util
.regex
.Pattern
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.core
.runtime
.IStatus
;
26 import org
.eclipse
.jdt
.annotation
.Nullable
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.XmlViewInfo
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
37 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
38 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
39 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.TmfXmlLocation
;
40 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
41 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
42 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
43 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
49 import org
.w3c
.dom
.Element
;
52 * Main viewer to display XML-defined xy charts. It uses an XML
53 * {@link TmfXmlUiStrings#XY_VIEW} element from an XML file. This element
54 * defines which entries from the state system will be shown and also gives
55 * additional information on the presentation of the view.
57 * @author Geneviève Bastien
59 public class XmlXYViewer
extends TmfCommonXLineChartViewer
{
61 private static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
62 /** Timeout between updates in the updateData thread */
63 private static final long BUILD_UPDATE_TIMEOUT
= 500;
65 private static final Pattern WILDCARD_PATTERN
= checkNotNull(Pattern
.compile("\\*")); //$NON-NLS-1$
67 private final ITmfXmlModelFactory fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
68 private final Map
<Integer
, SeriesData
> fSeriesData
= new HashMap
<>();
70 private final XmlViewInfo fViewInfo
;
72 /** XML Model elements to use to create the series */
73 private @Nullable ITmfXmlStateAttribute fDisplay
;
74 private @Nullable ITmfXmlStateAttribute fSeriesName
;
75 private @Nullable XmlXYEntry fEntry
;
77 private enum DisplayType
{
83 * The information related to one series on the chart
85 private class SeriesData
{
87 private final double[] fYValues
;
88 private final @Nullable double[] fYAbsoluteValues
;
89 private final Integer fDisplayQuark
;
90 private final String fName
;
91 private final DisplayType fType
;
93 public SeriesData(int length
, int attributeQuark
, String seriesName
, DisplayType type
) {
94 fYValues
= new double[length
];
95 fDisplayQuark
= attributeQuark
;
100 fYAbsoluteValues
= new double[length
];
104 fYAbsoluteValues
= null;
110 public double[] getYValues() {
114 public Integer
getDisplayQuark() {
115 return fDisplayQuark
;
118 public String
getSeriesName() {
122 public void setYValue(int i
, double yvalue
) {
125 double[] absoluteVals
= fYAbsoluteValues
;
126 if (absoluteVals
== null) {
127 throw new IllegalStateException();
129 absoluteVals
[i
] = yvalue
;
131 * At the first timestamp, the delta value should be 0 since we
132 * do not have the previous values
134 double prevValue
= yvalue
;
136 prevValue
= absoluteVals
[i
- 1];
138 fYValues
[i
] = yvalue
- prevValue
;
142 fYValues
[i
] = yvalue
;
149 private static class XmlXYEntry
implements IXmlStateSystemContainer
{
151 private final ITmfStateSystem fStateSystem
;
152 private final String fPath
;
153 private final DisplayType fType
;
155 public XmlXYEntry(ITmfStateSystem stateSystem
, String path
, Element entryElement
) {
156 fStateSystem
= stateSystem
;
158 switch (entryElement
.getAttribute(TmfXmlUiStrings
.DISPLAY_TYPE
)) {
159 case TmfXmlUiStrings
.DISPLAY_TYPE_DELTA
:
160 fType
= DisplayType
.DELTA
;
162 case TmfXmlUiStrings
.DISPLAY_TYPE_ABSOLUTE
:
164 fType
= DisplayType
.ABSOLUTE
;
170 public @Nullable String
getAttributeValue(@Nullable String name
) {
175 public ITmfStateSystem
getStateSystem() {
180 public @Nullable Iterable
<TmfXmlLocation
> getLocations() {
181 return Collections
.EMPTY_SET
;
184 public DisplayType
getType() {
188 public List
<Integer
> getQuarks() {
189 /* Get the list of quarks to process with this path */
190 String
[] paths
= fPath
.split(SPLIT_STRING
);
191 List
<Integer
> quarks
= checkNotNull(Collections
.singletonList(IXmlStateSystemContainer
.ROOT_QUARK
));
194 for (String path
: paths
) {
195 List
<Integer
> subQuarks
= new LinkedList
<>();
196 /* Replace * by .* to have a regex string */
197 String name
= WILDCARD_PATTERN
.matcher(path
).replaceAll(".*"); //$NON-NLS-1$
198 for (int relativeQuark
: quarks
) {
199 subQuarks
.addAll(fStateSystem
.getSubAttributes(relativeQuark
, false, name
));
203 } catch (AttributeNotFoundException e
) {
205 * We get all attributes from the state system itself, this
208 throw new IllegalStateException();
220 * The view info object
222 public XmlXYViewer(@Nullable Composite parent
, XmlViewInfo viewInfo
) {
223 super(parent
, Messages
.XmlXYViewer_DefaultViewerTitle
, Messages
.XmlXYViewer_DefaultXAxis
, Messages
.XmlXYViewer_DefaultYAxis
);
224 fViewInfo
= viewInfo
;
228 protected void updateData(long start
, long end
, int nb
, @Nullable IProgressMonitor monitor
) {
230 ITmfXmlStateAttribute display
= fDisplay
;
231 ITmfXmlStateAttribute seriesNameAttrib
= fSeriesName
;
232 XmlXYEntry entry
= fEntry
;
233 if (getTrace() == null || display
== null || entry
== null) {
236 ITmfStateSystem ss
= entry
.getStateSystem();
238 double[] xvalues
= getXAxis(start
, end
, nb
);
241 boolean complete
= false;
242 long currentEnd
= start
;
244 while (!complete
&& currentEnd
< end
) {
245 if (monitor
!= null && monitor
.isCanceled()) {
249 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
250 currentEnd
= ss
.getCurrentEndTime();
252 List
<Integer
> quarks
= entry
.getQuarks();
253 long traceStart
= getStartTime();
254 long traceEnd
= getEndTime();
255 long offset
= this.getTimeOffset();
257 /* Initialize quarks and series names */
258 for (int quark
: quarks
) {
259 String seriesName
= null;
260 if (seriesNameAttrib
== null) {
261 seriesName
= ss
.getAttributeName(quark
);
263 int seriesNameQuark
= seriesNameAttrib
.getAttributeQuark(quark
);
265 ITmfStateValue seriesNameValue
= ss
.querySingleState(start
, seriesNameQuark
).getStateValue();
266 if (!seriesNameValue
.isNull()) {
267 seriesName
= seriesNameValue
.toString();
269 if (seriesName
== null || seriesName
.isEmpty()) {
270 seriesName
= ss
.getAttributeName(quark
);
272 } catch (TimeRangeException e
) {
274 * The attribute did not exist at this point, simply
275 * use attribute name as series name
277 seriesName
= ss
.getAttributeName(quark
);
280 if (seriesName
== null) {
281 throw new IllegalStateException();
283 fSeriesData
.put(quark
, new SeriesData(xvalues
.length
, display
.getAttributeQuark(quark
), seriesName
, entry
.getType()));
286 for (int i
= 0; i
< xvalues
.length
; i
++) {
287 if (monitor
!= null && monitor
.isCanceled()) {
290 double x
= xvalues
[i
];
291 long time
= (long) x
+ offset
;
292 // make sure that time is in the trace range after double to
294 time
= time
< traceStart ? traceStart
: time
;
295 time
= time
> traceEnd ? traceEnd
: time
;
297 for (int quark
: quarks
) {
299 yvalue
= ss
.querySingleState(time
, fSeriesData
.get(quark
).getDisplayQuark()).getStateValue().unboxLong();
300 fSeriesData
.get(quark
).setYValue(i
, yvalue
);
301 } catch (TimeRangeException e
) {
302 fSeriesData
.get(quark
).setYValue(i
, 0);
306 for (int quark
: quarks
) {
307 setSeries(fSeriesData
.get(quark
).getSeriesName(), fSeriesData
.get(quark
).getYValues());
310 } catch (AttributeNotFoundException
| StateValueTypeException e
) {
311 Activator
.logError("Error updating the data of XML XY view", e
); //$NON-NLS-1$
312 } catch (StateSystemDisposedException e
) {
320 protected void initializeDataSource() {
321 super.initializeDataSource();
323 ITmfTrace trace
= this.getTrace();
328 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.XY_VIEW
);
329 if (viewElement
== null) {
333 Iterable
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
335 List
<ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new LinkedList
<>();
336 if (!analysisIds
.iterator().hasNext()) {
338 * No analysis specified, take all state system analysis modules
340 for (ITmfAnalysisModuleWithStateSystems module
: TmfTraceUtils
.getAnalysisModulesOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class)) {
341 stateSystemModules
.add(module
);
344 for (String moduleId
: analysisIds
) {
345 ITmfAnalysisModuleWithStateSystems module
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
346 if (module
!= null) {
347 stateSystemModules
.add(module
);
352 /** Initialize the data */
355 ITmfStateSystem ss
= null;
358 /* Schedule all state systems */
359 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
360 IStatus status
= module
.schedule();
361 if (!status
.isOK()) {
364 if (module
instanceof TmfStateSystemAnalysisModule
) {
365 ((TmfStateSystemAnalysisModule
) module
).waitForInitialization();
367 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
379 * Initialize state attributes. There should be only one entry element
382 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
383 Element entryElement
= entries
.get(0);
384 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
385 if (path
.isEmpty()) {
386 path
= TmfXmlStrings
.WILDCARD
;
388 XmlXYEntry entry
= new XmlXYEntry(ss
, path
, entryElement
);
391 /* Get the display element to use */
392 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
393 if (displayElements
.isEmpty()) {
394 Activator
.logWarning(String
.format("XML view: entry for %s should have a display element", path
)); //$NON-NLS-1$
397 Element displayElement
= displayElements
.get(0);
398 if (displayElement
== null) {
399 throw new IllegalStateException();
401 fDisplay
= fFactory
.createStateAttribute(displayElement
, entry
);
403 /* Get the series name element to use */
404 List
<Element
> seriesNameElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.NAME_ELEMENT
);
405 if (!seriesNameElements
.isEmpty()) {
406 Element seriesNameElement
= seriesNameElements
.get(0);
407 if (seriesNameElement
== null) {
408 throw new IllegalStateException();
410 fSeriesName
= fFactory
.createStateAttribute(seriesNameElement
, entry
);
416 * Tells the viewer that the view info has been updated and the viewer needs
417 * to be reinitialized
419 public void viewInfoUpdated() {