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