Commit | Line | Data |
---|---|---|
1a23419e | 1 | /******************************************************************************* |
36299425 | 2 | * Copyright (c) 2014, 2016 École Polytechnique de Montréal and others. |
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 | ||
2bdf0193 | 14 | package org.eclipse.tracecompass.tmf.analysis.xml.ui.views.timegraph; |
1a23419e FW |
15 | |
16 | import java.util.Collections; | |
df2597e0 | 17 | import java.util.Iterator; |
1a23419e | 18 | import java.util.List; |
36299425 | 19 | import java.util.regex.Pattern; |
1a23419e FW |
20 | |
21 | import org.eclipse.jdt.annotation.NonNull; | |
2bdf0193 | 22 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.ui.TmfXmlUiStrings; |
e894a508 | 23 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
1dd75589 | 24 | import org.eclipse.tracecompass.statesystem.core.StateSystemUtils; |
e894a508 | 25 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; |
2bdf0193 AM |
26 | import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlModelFactory; |
27 | import org.eclipse.tracecompass.tmf.analysis.xml.core.model.ITmfXmlStateAttribute; | |
28 | import org.eclipse.tracecompass.tmf.analysis.xml.core.model.TmfXmlLocation; | |
29 | import org.eclipse.tracecompass.tmf.analysis.xml.core.model.readonly.TmfXmlReadOnlyModelFactory; | |
30 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer; | |
31 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; | |
32 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; | |
df2597e0 | 33 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent; |
2bdf0193 | 34 | import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry; |
1a23419e FW |
35 | import org.w3c.dom.Element; |
36 | ||
37 | /** | |
38 | * An XML-defined entry, or row, to display in the XML state system view | |
39 | * | |
40 | * @author Florian Wininger | |
41 | */ | |
42 | public class XmlEntry extends TimeGraphEntry implements IXmlStateSystemContainer { | |
43 | ||
44 | private static final String EMPTY_STRING = ""; //$NON-NLS-1$ | |
45 | ||
46 | /** Type of resource */ | |
47 | public static enum EntryDisplayType { | |
48 | /** Entries without events to display (filler rows, etc.) */ | |
49 | NULL, | |
50 | /** Entries with time events */ | |
51 | DISPLAY | |
52 | } | |
53 | ||
54 | private final ITmfTrace fTrace; | |
55 | private final EntryDisplayType fType; | |
56 | private final int fBaseQuark; | |
57 | private final int fDisplayQuark; | |
58 | private final String fParentId; | |
59 | private final String fId; | |
60 | private final @NonNull ITmfStateSystem fSs; | |
61 | private final Element fElement; | |
62 | ||
63 | /** | |
64 | * Constructor | |
65 | * | |
66 | * @param baseQuark | |
67 | * The quark matching this entry, or <code>-1</code> if no quark | |
68 | * @param displayQuark | |
69 | * The quark containing the value to display. It was needed by | |
70 | * the caller to get the start and end time of this entry, so we | |
71 | * receive it as parameter from him. | |
72 | * @param trace | |
73 | * The trace on which we are working (FIXME: is this parameter | |
74 | * useful?) | |
75 | * @param name | |
76 | * The name of this entry. It will be overridden if a "name" XML | |
77 | * tag is specified in the entryElement. It will also be used as | |
78 | * the ID of this entry if no "id" XML tag is specified. It | |
79 | * typically is the attribute name corresponding the the base | |
80 | * quark. | |
81 | * @param startTime | |
82 | * The start time of this entry lifetime | |
83 | * @param endTime | |
84 | * The end time of this entry | |
85 | * @param type | |
86 | * The display type of this entry | |
87 | * @param ss | |
88 | * The state system this entry belongs to | |
89 | * @param entryElement | |
90 | * The XML element describing this entry. This element will be | |
91 | * used to determine, if available, the parent, ID, name and | |
92 | * other display option of this entry | |
93 | */ | |
94 | public XmlEntry(int baseQuark, int displayQuark, ITmfTrace trace, String name, long startTime, long endTime, EntryDisplayType type, @NonNull ITmfStateSystem ss, Element entryElement) { | |
95 | super(name, startTime, endTime); | |
96 | fTrace = trace; | |
97 | fType = type; | |
98 | fBaseQuark = baseQuark; | |
99 | fDisplayQuark = displayQuark; | |
100 | fSs = ss; | |
101 | fElement = entryElement; | |
102 | ||
103 | /* Get the parent if specified */ | |
104 | List<Element> elements = XmlUtils.getChildElements(fElement, TmfXmlUiStrings.PARENT_ELEMENT); | |
105 | if (elements.size() > 0) { | |
106 | fParentId = getFirstValue(elements.get(0)); | |
107 | } else { | |
108 | fParentId = EMPTY_STRING; | |
109 | } | |
110 | ||
111 | /* Get the name of this entry */ | |
112 | elements = XmlUtils.getChildElements(fElement, TmfXmlUiStrings.NAME_ELEMENT); | |
113 | if (elements.size() > 0) { | |
114 | String nameFromSs = getFirstValue(elements.get(0)); | |
115 | if (!nameFromSs.isEmpty()) { | |
116 | setName(nameFromSs); | |
117 | } | |
118 | } | |
119 | ||
120 | /* Get the id of this entry */ | |
121 | elements = XmlUtils.getChildElements(fElement, TmfXmlUiStrings.ID_ELEMENT); | |
122 | if (elements.size() > 0) { | |
123 | fId = getFirstValue(elements.get(0)); | |
124 | } else { | |
125 | fId = name; | |
126 | } | |
127 | ||
128 | } | |
129 | ||
130 | /** | |
131 | * Constructor | |
132 | * | |
133 | * @param baseQuark | |
134 | * The quark matching this entry, or <code>-1</code> if no quark | |
135 | * @param trace | |
136 | * The trace on which we are working | |
137 | * @param name | |
138 | * The exec_name of this entry | |
139 | * @param ss | |
140 | * The state system this entry belongs to | |
141 | */ | |
142 | public XmlEntry(int baseQuark, ITmfTrace trace, String name, @NonNull ITmfStateSystem ss) { | |
143 | super(name, ss.getStartTime(), ss.getCurrentEndTime()); | |
144 | fTrace = trace; | |
145 | fType = EntryDisplayType.NULL; | |
146 | fBaseQuark = baseQuark; | |
147 | fDisplayQuark = baseQuark; | |
148 | fSs = ss; | |
149 | fElement = null; | |
150 | fParentId = EMPTY_STRING; | |
151 | fId = name; | |
152 | } | |
153 | ||
154 | /** Return the state value of the first interval with a non-null value */ | |
155 | private String getFirstValue(Element stateAttribute) { | |
12685851 GB |
156 | if (stateAttribute == null) { |
157 | throw new IllegalArgumentException(); | |
158 | } | |
0a35a36f | 159 | |
1a23419e FW |
160 | ITmfXmlModelFactory factory = TmfXmlReadOnlyModelFactory.getInstance(); |
161 | ITmfXmlStateAttribute display = factory.createStateAttribute(stateAttribute, this); | |
0b563c20 | 162 | int quark = display.getAttributeQuark(fBaseQuark, null); |
1a23419e | 163 | if (quark != IXmlStateSystemContainer.ERROR_QUARK) { |
0a35a36f GB |
164 | ITmfStateInterval firstInterval = StateSystemUtils.queryUntilNonNullValue(fSs, quark, getStartTime(), getEndTime()); |
165 | if (firstInterval != null) { | |
166 | return firstInterval.getStateValue().toString(); | |
1a23419e FW |
167 | } |
168 | } | |
169 | return EMPTY_STRING; | |
170 | } | |
171 | ||
172 | /** | |
173 | * Get the trace this entry was taken from | |
174 | * | |
175 | * @return the entry's trace | |
176 | */ | |
177 | public ITmfTrace getTrace() { | |
178 | return fTrace; | |
179 | } | |
180 | ||
181 | /** | |
182 | * Get the entry Type of this entry. Uses the inner EntryDisplayType enum. | |
183 | * | |
184 | * @return The entry type | |
185 | */ | |
186 | public EntryDisplayType getType() { | |
187 | return fType; | |
188 | } | |
189 | ||
190 | /** | |
191 | * Get the quark from which to get the time event intervals for this entry. | |
192 | * | |
193 | * @return The attribute quark containing the intervals to display | |
194 | */ | |
195 | public int getDisplayQuark() { | |
196 | return fDisplayQuark; | |
197 | } | |
198 | ||
199 | /** | |
200 | * Get this entry's ID | |
201 | * | |
202 | * @return The id of the entry. | |
203 | */ | |
204 | public String getId() { | |
205 | return fId; | |
206 | } | |
207 | ||
208 | /** | |
209 | * Return the entry's parent ID. It corresponds to another entry's ID | |
210 | * received from the {@link #getId()} method. | |
211 | * | |
212 | * @return The parent ID of this entry | |
213 | */ | |
214 | public String getParentId() { | |
215 | return fParentId; | |
216 | } | |
217 | ||
218 | @Override | |
219 | public boolean hasTimeEvents() { | |
220 | if (fType == EntryDisplayType.NULL) { | |
221 | return false; | |
222 | } | |
223 | return true; | |
224 | } | |
225 | ||
226 | /** | |
227 | * Add a child to this entry of type XmlEntry | |
228 | * | |
229 | * @param entry | |
230 | * The entry to add | |
231 | */ | |
232 | public void addChild(XmlEntry entry) { | |
233 | int index; | |
234 | for (index = 0; index < getChildren().size(); index++) { | |
235 | XmlEntry other = (XmlEntry) getChildren().get(index); | |
236 | if (entry.getType().compareTo(other.getType()) < 0) { | |
237 | break; | |
238 | } else if (entry.getType().equals(other.getType())) { | |
239 | if (entry.getName().compareTo(other.getName()) < 0) { | |
240 | break; | |
241 | } | |
242 | } | |
243 | } | |
244 | ||
245 | entry.setParent(this); | |
a3188982 | 246 | addChild(index, entry); |
1a23419e FW |
247 | } |
248 | ||
249 | /** | |
250 | * Return the state system this entry is associated to | |
251 | * | |
252 | * @return The state system, or <code>null</code> if the state system can't | |
253 | * be found. | |
254 | */ | |
255 | @Override | |
256 | @NonNull | |
257 | public ITmfStateSystem getStateSystem() { | |
258 | return fSs; | |
259 | } | |
260 | ||
261 | @Override | |
262 | public String getAttributeValue(String name) { | |
263 | return name; | |
264 | } | |
265 | ||
266 | @Override | |
267 | public Iterable<TmfXmlLocation> getLocations() { | |
268 | return Collections.EMPTY_SET; | |
269 | } | |
270 | ||
df2597e0 AM |
271 | @Override |
272 | public Iterator<@NonNull ITimeEvent> getTimeEventsIterator() { | |
273 | return super.getTimeEventsIterator(); | |
274 | } | |
275 | ||
36299425 JCK |
276 | @Override |
277 | public boolean matches(@NonNull Pattern pattern) { | |
278 | if (pattern.matcher(getName()).find()) { | |
279 | return true; | |
280 | } | |
281 | if (pattern.matcher(fId).find()) { | |
282 | return true; | |
283 | } | |
284 | return pattern.matcher(fParentId).find(); | |
285 | } | |
1a23419e | 286 | } |