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 java
.util
.Collections
;
16 import java
.util
.HashMap
;
17 import java
.util
.LinkedList
;
18 import java
.util
.List
;
20 import java
.util
.regex
.Pattern
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.jdt
.annotation
.NonNull
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.Activator
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.TmfXmlUiStrings
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.ui
.views
.XmlViewInfo
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
35 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
36 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
37 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.TmfXmlLocation
;
38 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.readonly
.TmfXmlReadOnlyModelFactory
;
39 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
40 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
41 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfAnalysisModuleWithStateSystems
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
46 import org
.w3c
.dom
.Element
;
49 * Main viewer to display XML-defined xy charts. It uses an XML
50 * {@link TmfXmlUiStrings#XY_VIEW} element from an XML file. This element
51 * defines which entries from the state system will be shown and also gives
52 * additional information on the presentation of the view.
54 * @author Geneviève Bastien
56 public class XmlXYViewer
extends TmfCommonXLineChartViewer
{
58 private static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
59 /** Timeout between updates in the updateData thread */
60 private static final long BUILD_UPDATE_TIMEOUT
= 500;
62 @SuppressWarnings("null")
63 private static final @NonNull Pattern WILDCARD_PATTERN
= Pattern
.compile("\\*"); //$NON-NLS-1$
65 private final ITmfXmlModelFactory fFactory
= TmfXmlReadOnlyModelFactory
.getInstance();
66 private final Map
<Integer
, SeriesData
> fSeriesData
= new HashMap
<>();
68 private final XmlViewInfo fViewInfo
;
70 /** XML Model elements to use to create the series */
71 private @Nullable ITmfXmlStateAttribute fDisplay
;
72 private @Nullable ITmfXmlStateAttribute fSeriesName
;
73 private @Nullable XmlXYEntry fEntry
;
75 private enum DisplayType
{
81 * The information related to one series on the chart
83 private class SeriesData
{
85 private final double[] fYValues
;
86 private final @Nullable double[] fYAbsoluteValues
;
87 private final Integer fDisplayQuark
;
88 private final String fName
;
89 private final DisplayType fType
;
91 public SeriesData(int length
, int attributeQuark
, String seriesName
, DisplayType type
) {
92 fYValues
= new double[length
];
93 fDisplayQuark
= attributeQuark
;
98 fYAbsoluteValues
= new double[length
];
102 fYAbsoluteValues
= null;
108 public double[] getYValues() {
112 public Integer
getDisplayQuark() {
113 return fDisplayQuark
;
116 public String
getSeriesName() {
120 public void setYValue(int i
, double yvalue
) {
123 double[] absoluteVals
= fYAbsoluteValues
;
124 if (absoluteVals
== null) {
125 throw new IllegalStateException();
127 absoluteVals
[i
] = yvalue
;
129 * At the first timestamp, the delta value should be 0 since we
130 * do not have the previous values
132 double prevValue
= yvalue
;
134 prevValue
= absoluteVals
[i
- 1];
136 fYValues
[i
] = yvalue
- prevValue
;
140 fYValues
[i
] = yvalue
;
147 private static class XmlXYEntry
implements IXmlStateSystemContainer
{
149 private final ITmfStateSystem fStateSystem
;
150 private final String fPath
;
151 private final DisplayType fType
;
153 public XmlXYEntry(ITmfStateSystem stateSystem
, String path
, Element entryElement
) {
154 fStateSystem
= stateSystem
;
156 switch (entryElement
.getAttribute(TmfXmlUiStrings
.DISPLAY_TYPE
)) {
157 case TmfXmlUiStrings
.DISPLAY_TYPE_DELTA
:
158 fType
= DisplayType
.DELTA
;
160 case TmfXmlUiStrings
.DISPLAY_TYPE_ABSOLUTE
:
162 fType
= DisplayType
.ABSOLUTE
;
168 public @Nullable String
getAttributeValue(@Nullable String name
) {
173 public ITmfStateSystem
getStateSystem() {
178 public @Nullable Iterable
<TmfXmlLocation
> getLocations() {
179 return Collections
.EMPTY_SET
;
182 public DisplayType
getType() {
186 public List
<Integer
> getQuarks() {
187 /* Get the list of quarks to process with this path */
188 String
[] paths
= fPath
.split(SPLIT_STRING
);
189 @SuppressWarnings("null")
190 @NonNull List
<Integer
> quarks
= Collections
.singletonList(IXmlStateSystemContainer
.ROOT_QUARK
);
193 for (String path
: paths
) {
194 List
<Integer
> subQuarks
= new LinkedList
<>();
195 /* Replace * by .* to have a regex string */
196 String name
= WILDCARD_PATTERN
.matcher(path
).replaceAll(".*"); //$NON-NLS-1$
197 for (int relativeQuark
: quarks
) {
198 subQuarks
.addAll(fStateSystem
.getSubAttributes(relativeQuark
, false, name
));
202 } catch (AttributeNotFoundException e
) {
204 * We get all attributes from the state system itself, this
207 throw new IllegalStateException();
219 * The view info object
221 public XmlXYViewer(@Nullable Composite parent
, XmlViewInfo viewInfo
) {
222 super(parent
, Messages
.XmlXYViewer_DefaultViewerTitle
, Messages
.XmlXYViewer_DefaultXAxis
, Messages
.XmlXYViewer_DefaultYAxis
);
223 fViewInfo
= viewInfo
;
227 protected void updateData(long start
, long end
, int nb
, @Nullable IProgressMonitor monitor
) {
229 ITmfXmlStateAttribute display
= fDisplay
;
230 ITmfXmlStateAttribute seriesNameAttrib
= fSeriesName
;
231 XmlXYEntry entry
= fEntry
;
232 if (getTrace() == null || display
== null || entry
== null) {
235 ITmfStateSystem ss
= entry
.getStateSystem();
237 double[] xvalues
= getXAxis(start
, end
, nb
);
240 boolean complete
= false;
241 long currentEnd
= start
;
243 while (!complete
&& currentEnd
< end
) {
244 if (monitor
!= null && monitor
.isCanceled()) {
248 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
249 currentEnd
= ss
.getCurrentEndTime();
251 List
<Integer
> quarks
= entry
.getQuarks();
252 long traceStart
= getStartTime();
253 long traceEnd
= getEndTime();
254 long offset
= this.getTimeOffset();
256 /* Initialize quarks and series names */
257 for (int quark
: quarks
) {
258 String seriesName
= null;
259 if (seriesNameAttrib
== null) {
260 seriesName
= ss
.getAttributeName(quark
);
262 int seriesNameQuark
= seriesNameAttrib
.getAttributeQuark(quark
);
264 ITmfStateValue seriesNameValue
= ss
.querySingleState(start
, seriesNameQuark
).getStateValue();
265 if (!seriesNameValue
.isNull()) {
266 seriesName
= seriesNameValue
.toString();
268 if (seriesName
== null || seriesName
.isEmpty()) {
269 seriesName
= ss
.getAttributeName(quark
);
271 } catch (TimeRangeException e
) {
273 * The attribute did not exist at this point, simply
274 * use attribute name as series name
276 seriesName
= ss
.getAttributeName(quark
);
279 if (seriesName
== null) {
280 throw new IllegalStateException();
282 fSeriesData
.put(quark
, new SeriesData(xvalues
.length
, display
.getAttributeQuark(quark
), seriesName
, entry
.getType()));
285 for (int i
= 0; i
< xvalues
.length
; i
++) {
286 if (monitor
!= null && monitor
.isCanceled()) {
289 double x
= xvalues
[i
];
290 long time
= (long) x
+ offset
;
291 // make sure that time is in the trace range after double to
293 time
= time
< traceStart ? traceStart
: time
;
294 time
= time
> traceEnd ? traceEnd
: time
;
296 for (int quark
: quarks
) {
298 yvalue
= ss
.querySingleState(time
, fSeriesData
.get(quark
).getDisplayQuark()).getStateValue().unboxLong();
299 fSeriesData
.get(quark
).setYValue(i
, yvalue
);
300 } catch (TimeRangeException e
) {
301 fSeriesData
.get(quark
).setYValue(i
, 0);
305 for (int quark
: quarks
) {
306 setSeries(fSeriesData
.get(quark
).getSeriesName(), fSeriesData
.get(quark
).getYValues());
309 } catch (AttributeNotFoundException
| StateValueTypeException e
) {
310 Activator
.logError("Error updating the data of XML XY view", e
); //$NON-NLS-1$
311 } catch (StateSystemDisposedException e
) {
319 protected void initializeDataSource() {
320 super.initializeDataSource();
322 ITmfTrace trace
= this.getTrace();
327 Element viewElement
= fViewInfo
.getViewElement(TmfXmlUiStrings
.XY_VIEW
);
328 if (viewElement
== null) {
332 Iterable
<String
> analysisIds
= fViewInfo
.getViewAnalysisIds(viewElement
);
334 List
<ITmfAnalysisModuleWithStateSystems
> stateSystemModules
= new LinkedList
<>();
335 if (!analysisIds
.iterator().hasNext()) {
337 * No analysis specified, take all state system analysis modules
339 for (ITmfAnalysisModuleWithStateSystems module
: trace
.getAnalysisModulesOfClass(ITmfAnalysisModuleWithStateSystems
.class)) {
340 stateSystemModules
.add(module
);
343 for (String moduleId
: analysisIds
) {
344 @SuppressWarnings("resource")
345 ITmfAnalysisModuleWithStateSystems module
= trace
.getAnalysisModuleOfClass(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
) {
361 if (module
instanceof TmfStateSystemAnalysisModule
) {
362 ((TmfStateSystemAnalysisModule
) module
).waitForInitialization();
364 for (ITmfStateSystem ssq
: module
.getStateSystems()) {
376 * Initialize state attributes. There should be only one entry element
379 List
<Element
> entries
= XmlUtils
.getChildElements(viewElement
, TmfXmlUiStrings
.ENTRY_ELEMENT
);
380 Element entryElement
= entries
.get(0);
381 String path
= entryElement
.getAttribute(TmfXmlUiStrings
.PATH
);
382 if (path
.isEmpty()) {
383 path
= TmfXmlStrings
.WILDCARD
;
385 XmlXYEntry entry
= new XmlXYEntry(ss
, path
, entryElement
);
388 /* Get the display element to use */
389 List
<Element
> displayElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.DISPLAY_ELEMENT
);
390 if (displayElements
.isEmpty()) {
391 Activator
.logWarning(String
.format("XML view: entry for %s should have a display element", path
)); //$NON-NLS-1$
394 Element displayElement
= displayElements
.get(0);
395 fDisplay
= fFactory
.createStateAttribute(displayElement
, entry
);
397 /* Get the series name element to use */
398 List
<Element
> seriesNameElements
= XmlUtils
.getChildElements(entryElement
, TmfXmlUiStrings
.NAME_ELEMENT
);
399 if (!seriesNameElements
.isEmpty()) {
400 Element seriesNameElement
= seriesNameElements
.get(0);
401 fSeriesName
= fFactory
.createStateAttribute(seriesNameElement
, entry
);
407 * Tells the viewer that the view info has been updated and the viewer needs
408 * to be reinitialized
410 public void viewInfoUpdated() {