de83071fbd9a637df890c9897b241efc6705d510
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.ui / src / org / eclipse / linuxtools / tmf / analysis / xml / ui / views / timegraph / XmlTimeGraphView.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Florian Wininger - Initial API and implementation
11 * Geneviève Bastien - Review of the initial implementation
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph;
15
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.Comparator;
19 import java.util.HashMap;
20 import java.util.LinkedList;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import java.util.TreeSet;
25
26 import org.eclipse.core.runtime.IProgressMonitor;
27 import org.eclipse.jdt.annotation.NonNull;
28 import org.eclipse.jface.util.IPropertyChangeListener;
29 import org.eclipse.jface.util.PropertyChangeEvent;
30 import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.Activator;
31 import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.TmfXmlUiStrings;
32 import org.eclipse.linuxtools.internal.tmf.analysis.xml.ui.views.XmlViewInfo;
33 import org.eclipse.linuxtools.statesystem.core.ITmfStateSystem;
34 import org.eclipse.linuxtools.statesystem.core.exceptions.AttributeNotFoundException;
35 import org.eclipse.linuxtools.statesystem.core.exceptions.StateSystemDisposedException;
36 import org.eclipse.linuxtools.statesystem.core.exceptions.StateValueTypeException;
37 import org.eclipse.linuxtools.statesystem.core.exceptions.TimeRangeException;
38 import org.eclipse.linuxtools.statesystem.core.interval.ITmfStateInterval;
39 import org.eclipse.linuxtools.tmf.analysis.xml.core.model.ITmfXmlModelFactory;
40 import org.eclipse.linuxtools.tmf.analysis.xml.core.model.ITmfXmlStateAttribute;
41 import org.eclipse.linuxtools.tmf.analysis.xml.core.model.readonly.TmfXmlReadOnlyModelFactory;
42 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
43 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.XmlUtils;
44 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
45 import org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph.XmlEntry.EntryDisplayType;
46 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfAnalysisModuleWithStateSystems;
47 import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemAnalysisModule;
48 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
49 import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager;
50 import org.eclipse.linuxtools.tmf.ui.views.timegraph.AbstractTimeGraphView;
51 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.ITimeGraphPresentationProvider2;
52 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ILinkEvent;
53 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeEvent;
54 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
55 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.NullTimeEvent;
56 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeEvent;
57 import org.eclipse.linuxtools.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
58 import org.eclipse.swt.widgets.Display;
59 import org.w3c.dom.Element;
60
61 /**
62 * This view displays state system data in a time graph view. It uses an XML
63 * {@link TmfXmlUiStrings#TIME_GRAPH_VIEW} element from an XML file. This
64 * element defines which entries from the state system will be shown and also
65 * gives additional information on the presentation of the view (states, colors,
66 * etc)
67 *
68 * @author Florian Wininger
69 */
70 public class XmlTimeGraphView extends AbstractTimeGraphView {
71
72 /** View ID. */
73 public static final @NonNull String ID = "org.eclipse.linuxtools.tmf.analysis.xml.ui.views.timegraph"; //$NON-NLS-1$
74
75 private static final String[] DEFAULT_COLUMN_NAMES = new String[] {
76 Messages.XmlTimeGraphView_ColumnName,
77 Messages.XmlTimeGraphView_ColumnId,
78 Messages.XmlTimeGraphView_ColumnParentId,
79 };
80
81 private static final String[] DEFAULT_FILTER_COLUMN_NAMES = new String[] {
82 Messages.XmlTimeGraphView_ColumnName,
83 Messages.XmlTimeGraphView_ColumnId
84 };
85
86 /** The relative weight of the sash */
87 private static final int[] fWeight = { 1, 2 };
88
89 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
90 private static final String SPLIT_STRING = "/"; //$NON-NLS-1$
91
92 private final @NonNull XmlViewInfo fViewInfo = new XmlViewInfo(ID);
93 private final ITmfXmlModelFactory fFactory;
94
95 // ------------------------------------------------------------------------
96 // Constructors
97 // ------------------------------------------------------------------------
98
99 /**
100 * Default constructor
101 */
102 public XmlTimeGraphView() {
103 super(ID, new XmlPresentationProvider());
104 setWeight(fWeight);
105 setTreeColumns(DEFAULT_COLUMN_NAMES);
106 setTreeLabelProvider(new XmlTreeLabelProvider());
107 setFilterColumns(DEFAULT_FILTER_COLUMN_NAMES);
108 setFilterLabelProvider(new XmlTreeLabelProvider());
109 setEntryComparator(new XmlEntryComparator());
110 this.addPartPropertyListener(new IPropertyChangeListener() {
111 @Override
112 public void propertyChange(PropertyChangeEvent event) {
113 if (event.getProperty().equals(TmfXmlUiStrings.XML_OUTPUT_DATA)) {
114 Object newValue = event.getNewValue();
115 if (newValue instanceof String) {
116 String data = (String) newValue;
117 fViewInfo.setViewData(data);
118 loadNewXmlView();
119 }
120 }
121 }
122 });
123
124 fFactory = TmfXmlReadOnlyModelFactory.getInstance();
125 }
126
127 private void loadNewXmlView() {
128 rebuild();
129 }
130
131 private void setViewTitle(final String title) {
132 Display.getDefault().asyncExec(new Runnable() {
133 @Override
134 public void run() {
135 setPartName(title);
136 }
137 });
138
139 }
140
141 @Override
142 protected String getNextText() {
143 return Messages.XmlTimeGraphView_NextText;
144 }
145
146 @Override
147 protected String getNextTooltip() {
148 return Messages.XmlTimeGraphView_NextTooltip;
149 }
150
151 @Override
152 protected String getPrevText() {
153 return Messages.XmlTimeGraphView_PreviousText;
154 }
155
156 @Override
157 protected String getPrevTooltip() {
158 return Messages.XmlTimeGraphView_PreviousInterval;
159 }
160
161 /**
162 * Default label provider, it shows name, id and parent columns
163 *
164 * TODO: There should be a way to define columns in the XML
165 * */
166 private static class XmlTreeLabelProvider extends TreeLabelProvider {
167
168 @Override
169 public String getColumnText(Object element, int columnIndex) {
170 XmlEntry entry = (XmlEntry) element;
171
172 if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnName)) {
173 return entry.getName();
174 } else if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnId)) {
175 return entry.getId();
176 } else if (DEFAULT_COLUMN_NAMES[columnIndex].equals(Messages.XmlTimeGraphView_ColumnParentId)) {
177 return entry.getParentId();
178 }
179 return EMPTY_STRING;
180 }
181
182 }
183
184 private static class XmlEntryComparator implements Comparator<ITimeGraphEntry> {
185
186 @Override
187 public int compare(ITimeGraphEntry o1, ITimeGraphEntry o2) {
188
189 int result = 0;
190
191 if ((o1 instanceof XmlEntry) && (o2 instanceof XmlEntry)) {
192 XmlEntry entry1 = (XmlEntry) o1;
193 XmlEntry entry2 = (XmlEntry) o2;
194 result = entry1.getTrace().getStartTime().compareTo(entry2.getTrace().getStartTime());
195 if (result == 0) {
196 result = entry1.getTrace().getName().compareTo(entry2.getTrace().getName());
197 }
198 if (result == 0) {
199 result = entry1.getName().compareTo(entry2.getName());
200 }
201 }
202
203 if (result == 0) {
204 result = o1.getStartTime() < o2.getStartTime() ? -1 : o1.getStartTime() > o2.getStartTime() ? 1 : 0;
205 }
206
207 return result;
208 }
209 }
210
211 // ------------------------------------------------------------------------
212 // Internal
213 // ------------------------------------------------------------------------
214
215 @Override
216 protected void buildEventList(ITmfTrace trace, ITmfTrace parentTrace, IProgressMonitor monitor) {
217
218 /*
219 * Get the view element from the XML file. If the element can't be
220 * found, return.
221 */
222 Element viewElement = fViewInfo.getViewElement(TmfXmlUiStrings.TIME_GRAPH_VIEW);
223 if (viewElement == null) {
224 return;
225 }
226 ITimeGraphPresentationProvider2 pres = this.getPresentationProvider();
227 if (pres instanceof XmlPresentationProvider) {
228 /*
229 * TODO: Each entry of a line could have their own states/color.
230 * That will require an update to the presentation provider
231 */
232 ((XmlPresentationProvider) pres).loadNewStates(viewElement);
233 }
234
235 String title = fViewInfo.getViewTitle(viewElement);
236 if (title == null) {
237 title = Messages.XmlTimeGraphView_DefaultTitle;
238 }
239 setViewTitle(title);
240 Set<String> analysisIds = fViewInfo.getViewAnalysisIds(viewElement);
241
242 List<Element> entries = XmlUtils.getChildElements(viewElement, TmfXmlUiStrings.ENTRY_ELEMENT);
243 Set<XmlEntry> entryList = new TreeSet<>(getEntryComparator());
244 for (ITmfTrace aTrace : TmfTraceManager.getTraceSet(trace)) {
245 if (monitor.isCanceled()) {
246 return;
247 }
248
249 List<ITmfAnalysisModuleWithStateSystems> stateSystemModules = new LinkedList<>();
250 if (analysisIds.isEmpty()) {
251 /*
252 * No analysis specified, take all state system analysis modules
253 */
254 for (ITmfAnalysisModuleWithStateSystems module : aTrace.getAnalysisModulesOfClass(ITmfAnalysisModuleWithStateSystems.class)) {
255 stateSystemModules.add(module);
256 }
257 } else {
258 for (String moduleId : analysisIds) {
259 ITmfAnalysisModuleWithStateSystems module = aTrace.getAnalysisModuleOfClass(ITmfAnalysisModuleWithStateSystems.class, moduleId);
260 if (module != null) {
261 stateSystemModules.add(module);
262 }
263 }
264 }
265
266 for (ITmfAnalysisModuleWithStateSystems module : stateSystemModules) {
267 module.schedule();
268 if (module instanceof TmfStateSystemAnalysisModule) {
269 ((TmfStateSystemAnalysisModule) module).waitForInitialization();
270 }
271 for (ITmfStateSystem ssq : module.getStateSystems()) {
272 if (ssq == null) {
273 return;
274 }
275 ssq.waitUntilBuilt();
276
277 long startTime = ssq.getStartTime();
278 long endTime = ssq.getCurrentEndTime();
279 XmlEntry groupEntry = new XmlEntry(-1, aTrace, aTrace.getName(), ssq);
280 entryList.add(groupEntry);
281 setStartTime(Math.min(getStartTime(), startTime));
282 setEndTime(Math.max(getEndTime(), endTime));
283
284 /* Add children entry of this entry for each line */
285 for (Element entry : entries) {
286 buildEntry(entry, groupEntry, -1);
287 }
288 }
289 }
290 }
291 putEntryList(trace, new ArrayList<TimeGraphEntry>(entryList));
292
293 if (trace.equals(getTrace())) {
294 refresh();
295 }
296 for (XmlEntry traceEntry : entryList) {
297 if (monitor.isCanceled()) {
298 return;
299 }
300 long startTime = traceEntry.getStateSystem().getStartTime();
301 long endTime = traceEntry.getStateSystem().getCurrentEndTime() + 1;
302 buildStatusEvent(traceEntry, monitor, startTime, endTime);
303 }
304 }
305
306 private void buildEntry(Element entryElement, XmlEntry parentEntry, int baseQuark) {
307 /* Get the attribute string to display */
308 String path = entryElement.getAttribute(TmfXmlUiStrings.PATH);
309 if (path.isEmpty()) {
310 path = TmfXmlStrings.WILDCARD;
311 }
312
313 /*
314 * Make sure the XML element has either a display attribute or entries,
315 * otherwise issue a warning
316 */
317
318 List<Element> displayElements = XmlUtils.getChildElements(entryElement, TmfXmlUiStrings.DISPLAY_ELEMENT);
319 List<Element> entryElements = XmlUtils.getChildElements(entryElement, TmfXmlUiStrings.ENTRY_ELEMENT);
320
321 if (displayElements.isEmpty() && entryElements.isEmpty()) {
322 Activator.logWarning(String.format("XML view: entry for %s should have either a display element or entry elements", path)); //$NON-NLS-1$
323 return;
324 }
325
326 ITmfStateSystem ss = parentEntry.getStateSystem();
327
328 /* Get the list of quarks to process with this path */
329 String[] paths = path.split(SPLIT_STRING);
330 int i = 0;
331 List<Integer> quarks = Collections.singletonList(baseQuark);
332
333 try {
334 while (i < paths.length) {
335 List<Integer> subQuarks = new LinkedList<>();
336 /* Replace * by .* to have a regex string */
337 String name = paths[i].replaceAll("\\*", ".*"); //$NON-NLS-1$ //$NON-NLS-2$
338 for (int relativeQuark : quarks) {
339 for (int quark : ss.getSubAttributes(relativeQuark, false, name)) {
340 subQuarks.add(quark);
341 }
342 }
343 quarks = subQuarks;
344 i++;
345 }
346
347 /* Process each quark */
348 XmlEntry currentEntry = parentEntry;
349 Element displayElement = null;
350 Map<String, XmlEntry> entryMap = new HashMap<>();
351 if (!displayElements.isEmpty()) {
352 displayElement = displayElements.get(0);
353 }
354 for (int quark : quarks) {
355 currentEntry = parentEntry;
356 /* Process the current entry, if specified */
357 if (displayElement != null) {
358 currentEntry = processEntry(entryElement, displayElement, parentEntry, quark, ss);
359 entryMap.put(currentEntry.getId(), currentEntry);
360 }
361 /* Process the children entry of this entry */
362 for (Element subEntryEl : entryElements) {
363 buildEntry(subEntryEl, currentEntry, quark);
364 }
365 }
366 if (!entryMap.isEmpty()) {
367 buildTree(entryMap, parentEntry);
368 }
369 } catch (AttributeNotFoundException e) {
370 }
371 }
372
373 private XmlEntry processEntry(@NonNull Element entryElement, @NonNull Element displayEl,
374 XmlEntry parentEntry, int quark, ITmfStateSystem ss) {
375 /*
376 * Get the start time and end time of this entry from the display
377 * attribute
378 */
379 ITmfXmlStateAttribute display = fFactory.createStateAttribute(displayEl, parentEntry);
380 int displayQuark = display.getAttributeQuark(quark);
381 if (displayQuark == IXmlStateSystemContainer.ERROR_QUARK) {
382 return new XmlEntry(quark, parentEntry.getTrace(),
383 String.format("Unknown display quark for %s", ss.getAttributeName(quark)), ss); //$NON-NLS-1$
384 }
385
386 long entryStart = ss.getStartTime();
387 long entryEnd = ss.getCurrentEndTime();
388
389 try {
390
391 ITmfStateInterval oneInterval = ss.querySingleState(entryStart, displayQuark);
392
393 /* The entry start is the first non-null interval */
394 while (oneInterval.getStateValue().isNull()) {
395 long ts = oneInterval.getEndTime() + 1;
396 if (ts > ss.getCurrentEndTime()) {
397 break;
398 }
399 oneInterval = ss.querySingleState(ts, displayQuark);
400 }
401 entryStart = oneInterval.getStartTime();
402
403 /* The entry end is the last non-null interval */
404 oneInterval = ss.querySingleState(entryEnd, displayQuark);
405 while (oneInterval.getStateValue().isNull()) {
406 long ts = oneInterval.getStartTime() - 1;
407 if (ts < ss.getStartTime()) {
408 break;
409 }
410 oneInterval = ss.querySingleState(ts, displayQuark);
411 }
412 entryEnd = oneInterval.getEndTime();
413
414 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
415 }
416
417 return new XmlEntry(quark, displayQuark, parentEntry.getTrace(), ss.getAttributeName(quark),
418 entryStart, entryEnd, EntryDisplayType.DISPLAY, ss, entryElement);
419 }
420
421 private void buildStatusEvent(XmlEntry traceEntry, IProgressMonitor monitor, long start, long end) {
422 long resolution = (end - start) / getDisplayWidth();
423 long startTime = Math.max(start, traceEntry.getStartTime());
424 long endTime = Math.min(end + 1, traceEntry.getEndTime());
425 List<ITimeEvent> eventList = getEventList(traceEntry, startTime, endTime, resolution, monitor);
426 if (monitor.isCanceled()) {
427 return;
428 }
429 traceEntry.setEventList(eventList);
430 redraw();
431
432 for (ITimeGraphEntry entry : traceEntry.getChildren()) {
433 if (monitor.isCanceled()) {
434 return;
435 }
436 XmlEntry xmlEntry = (XmlEntry) entry;
437 buildStatusEvent(xmlEntry, monitor, start, end);
438 }
439 }
440
441 /** Build a tree using getParentId() and getId() */
442 private static void buildTree(Map<String, XmlEntry> entryMap, XmlEntry rootEntry) {
443 for (XmlEntry entry : entryMap.values()) {
444 boolean root = true;
445 if (!entry.getParentId().isEmpty()) {
446 XmlEntry parent = entryMap.get(entry.getParentId());
447 if (parent != null &&
448 entry.getStartTime() >= parent.getStartTime() &&
449 entry.getStartTime() <= parent.getEndTime()) {
450 parent.addChild(entry);
451 root = false;
452 }
453 }
454 if (root) {
455 rootEntry.addChild(entry);
456 }
457 }
458 }
459
460 @Override
461 protected List<ITimeEvent> getEventList(TimeGraphEntry entry, long startTime, long endTime, long resolution, IProgressMonitor monitor) {
462 if (!(entry instanceof XmlEntry)) {
463 return Collections.EMPTY_LIST;
464 }
465 XmlEntry xmlEntry = (XmlEntry) entry;
466 ITmfStateSystem ssq = xmlEntry.getStateSystem();
467 final long realStart = Math.max(startTime, entry.getStartTime());
468 final long realEnd = Math.min(endTime, entry.getEndTime());
469 if (realEnd <= realStart) {
470 return null;
471 }
472 List<ITimeEvent> eventList = null;
473 int quark = xmlEntry.getDisplayQuark();
474
475 try {
476 if (xmlEntry.getType() == EntryDisplayType.DISPLAY) {
477
478 List<ITmfStateInterval> statusIntervals = ssq.queryHistoryRange(quark, realStart, realEnd - 1, resolution, monitor);
479 eventList = new ArrayList<>(statusIntervals.size());
480 long lastEndTime = -1;
481 for (ITmfStateInterval statusInterval : statusIntervals) {
482 if (monitor.isCanceled()) {
483 return null;
484 }
485 int status = statusInterval.getStateValue().unboxInt();
486 long time = statusInterval.getStartTime();
487 long duration = statusInterval.getEndTime() - time + 1;
488 if (!statusInterval.getStateValue().isNull()) {
489 if (lastEndTime != time && lastEndTime != -1) {
490 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
491 }
492 eventList.add(new TimeEvent(entry, time, duration, status));
493 } else if (lastEndTime == -1 || time + duration >= endTime) {
494 // add null event if it intersects the start or end time
495 eventList.add(new NullTimeEvent(entry, time, duration));
496 }
497 lastEndTime = time + duration;
498 }
499 }
500 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException | StateSystemDisposedException e) {
501 /* Ignored */
502 }
503 return eventList;
504 }
505
506 @Override
507 protected List<ILinkEvent> getLinkList(long startTime, long endTime, long resolution, IProgressMonitor monitor) {
508 /* TODO: not implemented yet, need XML to go along */
509 return Collections.EMPTY_LIST;
510 }
511
512 }
This page took 0.042612 seconds and 4 git commands to generate.