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