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