1 /*******************************************************************************
2 * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others.
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
.NonNull
;
27 import org
.eclipse
.jdt
.annotation
.Nullable
;
28 import org
.eclipse
.swt
.widgets
.Composite
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.TmfXmlLocation
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
35 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
37 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
38 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.XmlViewInfo
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
40 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
41 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
42 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
43 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
48 import org
.w3c
.dom
.Element
;
51 * Main viewer to display XML-defined xy charts. It uses an XML
52 * {@link TmfXmlUiStrings#XY_VIEW} element from an XML file. This element
53 * defines which entries from the state system will be shown and also gives
54 * additional information on the presentation of the view.
56 * @author Geneviève Bastien
58 public class XmlXYViewer
extends TmfCommonXLineChartViewer
{
60 private static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
61 /** Timeout between updates in the updateData thread */
62 private static final long BUILD_UPDATE_TIMEOUT
= 500;
64 private static final Pattern WILDCARD_PATTERN
= Pattern
.compile("\\*"); //$NON-NLS-1$
66 private final ITmfXmlModelFactory fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
67 private final Map
<Integer
, SeriesData
> fSeriesData
= new HashMap
<>();
69 private final XmlViewInfo fViewInfo
;
71 /** XML Model elements to use to create the series */
72 private @Nullable ITmfXmlStateAttribute fDisplay
;
73 private @Nullable ITmfXmlStateAttribute fSeriesName
;
74 private @Nullable XmlXYEntry fEntry
;
76 private enum DisplayType
{
82 * The information related to one series on the chart
84 private class SeriesData
{
86 private final double[] fYValues
;
87 private final double @Nullable [] fYAbsoluteValues
;
88 private final Integer fDisplayQuark
;
89 private final String fName
;
90 private final DisplayType fType
;
92 public SeriesData(int length
, int attributeQuark
, String seriesName
, DisplayType type
) {
93 fYValues
= new double[length
];
94 fDisplayQuark
= attributeQuark
;
99 fYAbsoluteValues
= new double[length
];
103 fYAbsoluteValues
= null;
109 public double[] getYValues() {
113 public Integer
getDisplayQuark() {
114 return fDisplayQuark
;
117 public String
getSeriesName() {
121 public void setYValue(int i
, double yvalue
) {
124 double[] absoluteVals
= fYAbsoluteValues
;
125 if (absoluteVals
== null) {
126 throw new IllegalStateException();
128 absoluteVals
[i
] = yvalue
;
130 * At the first timestamp, the delta value should be 0 since we
131 * do not have the previous values
133 double prevValue
= yvalue
;
135 prevValue
= absoluteVals
[i
- 1];
137 fYValues
[i
] = yvalue
- prevValue
;
141 fYValues
[i
] = yvalue
;
148 private static class XmlXYEntry
implements IXmlStateSystemContainer
{
150 private final ITmfStateSystem fStateSystem
;
151 private final String fPath
;
152 private final DisplayType fType
;
154 public XmlXYEntry(ITmfStateSystem stateSystem
, String path
, Element entryElement
) {
155 fStateSystem
= stateSystem
;
157 switch (entryElement
.getAttribute(TmfXmlUiStrings
.DISPLAY_TYPE
)) {
158 case TmfXmlUiStrings
.DISPLAY_TYPE_DELTA
:
159 fType
= DisplayType
.DELTA
;
161 case TmfXmlUiStrings
.DISPLAY_TYPE_ABSOLUTE
:
163 fType
= DisplayType
.ABSOLUTE
;
169 public @Nullable String
getAttributeValue(@Nullable String name
) {
174 public ITmfStateSystem
getStateSystem() {
179 public @NonNull Iterable
<@NonNull TmfXmlLocation
> getLocations() {
180 return Collections
.EMPTY_SET
;
183 public DisplayType
getType() {
187 public List
<Integer
> getQuarks() {
188 /* Get the list of quarks to process with this path */
189 String
[] paths
= fPath
.split(SPLIT_STRING
);
190 List
<Integer
> quarks
= Collections
.singletonList(IXmlStateSystemContainer
.ROOT_QUARK
);
192 for (String path
: paths
) {
193 List
<Integer
> subQuarks
= new LinkedList
<>();
194 /* Replace * by .* to have a regex string */
195 String name
= WILDCARD_PATTERN
.matcher(path
).replaceAll(".*"); //$NON-NLS-1$
196 for (int relativeQuark
: quarks
) {
197 subQuarks
.addAll(fStateSystem
.getSubAttributes(relativeQuark
, false, name
));
211 * The view info object
213 public XmlXYViewer(@Nullable Composite parent
, XmlViewInfo viewInfo
) {
214 super(parent
, Messages
.XmlXYViewer_DefaultViewerTitle
, Messages
.XmlXYViewer_DefaultXAxis
, Messages
.XmlXYViewer_DefaultYAxis
);
215 fViewInfo
= viewInfo
;
219 protected void updateData(long start
, long end
, int nb
, @Nullable IProgressMonitor monitor
) {
221 ITmfXmlStateAttribute display
= fDisplay
;
222 ITmfXmlStateAttribute seriesNameAttrib
= fSeriesName
;
223 XmlXYEntry entry
= fEntry
;
224 if (getTrace() == null || display
== null || entry
== null) {
227 ITmfStateSystem ss
= entry
.getStateSystem();
229 double[] xvalues
= getXAxis(start
, end
, nb
);
232 boolean complete
= false;
233 long currentEnd
= start
;
235 while (!complete
&& currentEnd
< end
) {
236 if (monitor
!= null && monitor
.isCanceled()) {
240 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
241 currentEnd
= ss
.getCurrentEndTime();
243 List
<Integer
> quarks
= entry
.getQuarks();
244 long traceStart
= getStartTime();
245 long traceEnd
= getEndTime();
246 long offset
= this.getTimeOffset();
248 /* Initialize quarks and series names */
249 for (int quark
: quarks
) {
250 String seriesName
= null;
251 if (seriesNameAttrib
== null) {
252 seriesName
= ss
.getAttributeName(quark
);
254 int seriesNameQuark
= seriesNameAttrib
.getAttributeQuark(quark
, null);
256 ITmfStateValue seriesNameValue
= ss
.querySingleState(start
, seriesNameQuark
).getStateValue();
257 if (!seriesNameValue
.isNull()) {
258 seriesName
= seriesNameValue
.toString();
260 if (seriesName
== null || seriesName
.isEmpty()) {
261 seriesName
= ss
.getAttributeName(quark
);
263 } catch (TimeRangeException e
) {
265 * The attribute did not exist at this point, simply
266 * use attribute name as series name
268 seriesName
= ss
.getAttributeName(quark
);
271 fSeriesData
.put(quark
, new SeriesData(xvalues
.length
, display
.getAttributeQuark(quark
, null), seriesName
, entry
.getType()));
273 for (int i
= 0; i
< xvalues
.length
; i
++) {
274 if (monitor
!= null && monitor
.isCanceled()) {
277 double x
= xvalues
[i
];
278 long time
= (long) x
+ offset
;
279 // make sure that time is in the trace range after double to
281 time
= time
< traceStart ? traceStart
: time
;
282 time
= time
> traceEnd ? traceEnd
: time
;
284 for (int quark
: quarks
) {
286 SeriesData data
= checkNotNull(fSeriesData
.get(quark
));
288 ITmfStateValue value
= ss
.querySingleState(time
, data
.getDisplayQuark()).getStateValue();
289 switch (value
.getType()) {
291 yvalue
= value
.unboxDouble();
294 yvalue
= value
.unboxLong();
297 yvalue
= value
.unboxInt();
305 data
.setYValue(i
, yvalue
);
306 } catch (TimeRangeException e
) {
307 data
.setYValue(i
, 0);
311 for (int quark
: quarks
) {
312 SeriesData data
= checkNotNull(fSeriesData
.get(quark
));
313 setSeries(data
.getSeriesName(), data
.getYValues());
316 } catch (StateValueTypeException e
) {
317 Activator
.logError("Error updating the data of XML XY view", e
); //$NON-NLS-1$
318 } catch (StateSystemDisposedException e
) {
326 protected void initializeDataSource() {
327 super.initializeDataSource();
329 ITmfTrace trace
= this.getTrace();
334 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.XY_VIEW
);
335 if (viewElement
== null) {
339 Iterable
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
341 List
<ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new LinkedList
<>();
342 if (!analysisIds
.iterator().hasNext()) {
344 * No analysis specified, take all state system analysis modules
346 for (ITmfAnalysisModuleWithStateSystems module
: TmfTraceUtils
.getAnalysisModulesOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class)) {
347 stateSystemModules
.add(module
);
350 for (String moduleId
: analysisIds
) {
351 ITmfAnalysisModuleWithStateSystems module
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, ITmfAnalysisModuleWithStateSystems
.class, moduleId
);
352 if (module
!= null) {
353 stateSystemModules
.add(module
);
358 /** Initialize the data */
361 ITmfStateSystem ss
= null;
364 /* Schedule all state systems */
365 for (ITmfAnalysisModuleWithStateSystems module
: stateSystemModules
) {
366 IStatus status
= module
.schedule();
367 if (!status
.isOK()) {
370 if (!module
.waitForInitialization()) {
373 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
383 * Initialize state attributes. There should be only one entry element
386 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
387 Element entryElement
= entries
.get(0);
388 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
389 if (path
.isEmpty()) {
390 path
= TmfXmlStrings
.WILDCARD
;
392 XmlXYEntry entry
= new XmlXYEntry(ss
, path
, entryElement
);
395 /* Get the display element to use */
396 List
<@NonNull Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
397 if (displayElements
.isEmpty()) {
398 Activator
.logWarning(String
.format("XML view: entry for %s should have a display element", path
)); //$NON-NLS-1$
401 Element displayElement
= displayElements
.get(0);
402 fDisplay
= fFactory
.createStateAttribute(displayElement
, entry
);
404 /* Get the series name element to use */
405 List
<Element
> seriesNameElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.NAME_ELEMENT
);
406 if (!seriesNameElements
.isEmpty()) {
407 Element seriesNameElement
= seriesNameElements
.get(0);
408 fSeriesName
= fFactory
.createStateAttribute(seriesNameElement
, entry
);
414 * Tells the viewer that the view info has been updated and the viewer needs
415 * to be reinitialized
417 public void viewInfoUpdated() {