Commit | Line | Data |
---|---|---|
1d7e62f9 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 Ecole Polytechnique de Montreal |
1d7e62f9 GB |
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 | ******************************************************************************/ | |
12 | ||
6eca054d | 13 | package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model; |
1d7e62f9 GB |
14 | |
15 | import java.util.List; | |
16 | ||
17 | import org.eclipse.jdt.annotation.NonNull; | |
18 | import org.eclipse.jdt.annotation.Nullable; | |
6eca054d GB |
19 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.module.IXmlStateSystemContainer; |
20 | import org.eclipse.tracecompass.internal.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
e894a508 AM |
21 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
22 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
23 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
24 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; | |
25 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
26 | import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue; | |
2bdf0193 AM |
27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
28 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
35f39420 AM |
29 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; |
30 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; | |
1d7e62f9 GB |
31 | import org.w3c.dom.Element; |
32 | ||
33 | /** | |
34 | * This Class implements a State Value in the XML-defined state system, along | |
35 | * with the path to get to the value (either a list of state attributes or an | |
36 | * event field) | |
37 | * | |
38 | * <pre> | |
39 | * Example: | |
40 | * <stateAttribute type="location" value="CurrentThread" /> | |
41 | * <stateAttribute type="constant" value="System_call" /> | |
42 | * <stateValue type="null" /> | |
43 | * </pre> | |
44 | * | |
45 | * @author Florian Wininger | |
46 | */ | |
47 | public abstract class TmfXmlStateValue implements ITmfXmlStateValue { | |
48 | ||
49 | private final TmfXmlStateValueBase fStateValue; | |
50 | ||
51 | /* Path in the State System */ | |
52 | private final List<ITmfXmlStateAttribute> fPath; | |
53 | /* Event field to match with this state value */ | |
12685851 | 54 | private final @Nullable String fEventField; |
1d7e62f9 GB |
55 | |
56 | /* Whether this state value is an increment of the previous value */ | |
57 | private final boolean fIncrement; | |
58 | /* Stack value */ | |
59 | private final ValueTypeStack fStackType; | |
60 | /* Forced value type */ | |
61 | private final ITmfStateValue.Type fForcedType; | |
62 | ||
63 | private final IXmlStateSystemContainer fContainer; | |
64 | ||
65 | /** | |
66 | * Different behaviors of an attribute that is to be stacked | |
67 | */ | |
68 | protected enum ValueTypeStack { | |
69 | /** Not stacked */ | |
70 | NULL, | |
71 | /** Peek at the value at the top of the stack */ | |
72 | PEEK, | |
73 | /** Take the value at the top of the stack */ | |
74 | POP, | |
75 | /** Push the value on the stack */ | |
76 | PUSH; | |
77 | ||
78 | /** | |
79 | * Get the type stack value corresponding to a string | |
80 | * | |
81 | * @param input | |
82 | * The string to match to a value | |
83 | * @return The ValueTypeStack value | |
84 | */ | |
85 | public static ValueTypeStack getTypeFromString(String input) { | |
86 | switch (input) { | |
87 | case TmfXmlStrings.STACK_PUSH: | |
88 | return PUSH; | |
89 | case TmfXmlStrings.STACK_POP: | |
90 | return POP; | |
91 | case TmfXmlStrings.STACK_PEEK: | |
92 | return PEEK; | |
93 | default: | |
94 | return NULL; | |
95 | } | |
96 | } | |
97 | } | |
98 | ||
99 | /** | |
100 | * Constructor | |
101 | * | |
102 | * @param modelFactory | |
103 | * The factory used to create XML model elements | |
104 | * @param node | |
105 | * The state value XML element | |
106 | * @param container | |
107 | * The state system container this state value belongs to | |
108 | * @param eventField | |
109 | * The event field where to get the value | |
110 | * @param attributes | |
111 | * The attributes representing the path to this value | |
112 | */ | |
12685851 | 113 | protected TmfXmlStateValue(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container, List<ITmfXmlStateAttribute> attributes, @Nullable String eventField) { |
1d7e62f9 GB |
114 | fPath = attributes; |
115 | fContainer = container; | |
116 | fEventField = eventField; | |
117 | if (!node.getNodeName().equals(TmfXmlStrings.STATE_VALUE)) { | |
118 | throw new IllegalArgumentException("TmfXmlStateValue constructor: Element is not a stateValue"); //$NON-NLS-1$ | |
119 | } | |
120 | ||
121 | /* Check if there is an increment for the value */ | |
122 | fIncrement = Boolean.parseBoolean(node.getAttribute(TmfXmlStrings.INCREMENT)); | |
123 | ||
124 | /* Process the XML Element state value */ | |
125 | fStateValue = initializeStateValue(modelFactory, node); | |
126 | ||
127 | /* | |
128 | * Forced type allows to convert the value to a certain type : For | |
129 | * example, a process's TID in an event field may arrive with a LONG | |
130 | * format but we want to store the data in an INT | |
131 | */ | |
132 | switch (node.getAttribute(TmfXmlStrings.FORCED_TYPE)) { | |
133 | case TmfXmlStrings.TYPE_STRING: | |
134 | fForcedType = ITmfStateValue.Type.STRING; | |
135 | break; | |
136 | case TmfXmlStrings.TYPE_INT: | |
137 | fForcedType = ITmfStateValue.Type.INTEGER; | |
138 | break; | |
139 | case TmfXmlStrings.TYPE_LONG: | |
140 | fForcedType = ITmfStateValue.Type.LONG; | |
141 | break; | |
425e1cfa BH |
142 | case TmfXmlStrings.TYPE_DOUBLE: |
143 | fForcedType = ITmfStateValue.Type.DOUBLE; | |
144 | break; | |
1d7e62f9 GB |
145 | default: |
146 | fForcedType = ITmfStateValue.Type.NULL; | |
147 | } | |
148 | ||
149 | /* | |
150 | * Stack Actions : allow to define a stack with PUSH/POP/PEEK methods | |
151 | */ | |
0e4f957e | 152 | String stack = node.getAttribute(TmfXmlStrings.ATTRIBUTE_STACK); |
1d7e62f9 GB |
153 | fStackType = ValueTypeStack.getTypeFromString(stack); |
154 | } | |
155 | ||
156 | /** | |
157 | * Initialize a {@link TmfXmlStateValueBase} object for the type and value | |
158 | * | |
159 | * @param modelFactory | |
160 | * The factory used to create XML model elements | |
161 | * @param node | |
162 | * The state value XML element | |
163 | * @return The internal state value type corresponding to this state value | |
164 | */ | |
165 | protected TmfXmlStateValueBase initializeStateValue(ITmfXmlModelFactory modelFactory, Element node) { | |
166 | return new TmfXmlStateValueNull(); | |
167 | } | |
168 | ||
169 | /** | |
170 | * Return the state system container this class is attached to | |
171 | * | |
172 | * @return The state system container | |
173 | */ | |
174 | protected IXmlStateSystemContainer getSsContainer() { | |
175 | return fContainer; | |
176 | } | |
177 | ||
178 | /** | |
179 | * Get the state system associated with this value's container | |
180 | * | |
181 | * @return The state system associated with the state system container | |
182 | */ | |
12685851 | 183 | protected @Nullable ITmfStateSystem getStateSystem() { |
1d7e62f9 GB |
184 | return fContainer.getStateSystem(); |
185 | } | |
186 | ||
187 | /** | |
188 | * Return whether this value is an increment of the previous value | |
189 | * | |
190 | * @return <code>true</code> if the value is an increment | |
191 | */ | |
192 | protected boolean isIncrement() { | |
193 | return fIncrement; | |
194 | } | |
195 | ||
196 | /** | |
197 | * Get the stack type of this attribute. If the attribute is to be pushed or | |
198 | * popped to a stack. The behavior of the stack attribute will depend on the | |
199 | * implementation of the model. | |
200 | * | |
201 | * @return The stack type of the attribute | |
202 | */ | |
203 | protected ValueTypeStack getStackType() { | |
204 | return fStackType; | |
205 | } | |
206 | ||
207 | /** | |
208 | * Get the forced type of the value. For example, if the value obtained from | |
209 | * the attributes is not in this forced type, it will be converted to this. | |
210 | * | |
211 | * @return The desired type of the value | |
212 | */ | |
213 | protected ITmfStateValue.Type getForcedType() { | |
214 | return fForcedType; | |
215 | } | |
216 | ||
1d7e62f9 | 217 | @Override |
0b563c20 JCK |
218 | public ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) throws AttributeNotFoundException { |
219 | return fStateValue.getValue(event, scenarioInfo); | |
1d7e62f9 GB |
220 | } |
221 | ||
222 | /** | |
223 | * Get the value of the event field that is the path of this state value | |
224 | * | |
225 | * @param event | |
226 | * The current event | |
227 | * @return the value of the event field | |
228 | */ | |
229 | @Override | |
230 | public ITmfStateValue getEventFieldValue(@NonNull ITmfEvent event) { | |
12685851 GB |
231 | String eventField = fEventField; |
232 | if (eventField == null) { | |
233 | throw new IllegalStateException(); | |
234 | } | |
235 | return getEventFieldValue(event, eventField); | |
1d7e62f9 GB |
236 | } |
237 | ||
238 | /** | |
239 | * Get the value of an event field | |
240 | * | |
241 | * @param event | |
242 | * The current event | |
243 | * @param fieldName | |
244 | * The name of the field of which to get the value | |
245 | * @return The value of the event field | |
246 | */ | |
12685851 | 247 | protected ITmfStateValue getEventFieldValue(ITmfEvent event, String fieldName) { |
1d7e62f9 GB |
248 | |
249 | ITmfStateValue value = TmfStateValue.nullValue(); | |
250 | ||
251 | final ITmfEventField content = event.getContent(); | |
252 | ||
12685851 GB |
253 | /* Exception for "CPU", returns the source of this event */ |
254 | /* FIXME : Nameclash if a eventfield have "cpu" for name. */ | |
35f39420 | 255 | if (fieldName.equals(TmfXmlStrings.CPU)) { |
b3867ecc MAL |
256 | Integer cpu = TmfTraceUtils.resolveIntEventAspectOfClassForEvent(event.getTrace(), TmfCpuAspect.class, event); |
257 | if (cpu != null) { | |
b1aad44e | 258 | return TmfStateValue.newValueInt(cpu.intValue()); |
35f39420 AM |
259 | } |
260 | } | |
6e9040a7 NE |
261 | /* Exception also for "TIMESTAMP", returns the timestamp of this event */ |
262 | if (fieldName.equals(TmfXmlStrings.TIMESTAMP)) { | |
263 | return TmfStateValue.newValueLong(event.getTimestamp().getValue()); | |
264 | } | |
1d7e62f9 GB |
265 | if (content.getField(fieldName) == null) { |
266 | return value; | |
267 | } | |
268 | ||
269 | Object field = content.getField(fieldName).getValue(); | |
270 | ||
271 | /* | |
272 | * Try to find the right type. The type can be forced by | |
273 | * "forcedType" argument. | |
274 | */ | |
275 | ||
276 | if (field instanceof String) { | |
277 | String fieldString = (String) field; | |
278 | ||
279 | switch (fForcedType) { | |
280 | case INTEGER: | |
281 | value = TmfStateValue.newValueInt(Integer.parseInt(fieldString)); | |
282 | break; | |
283 | case LONG: | |
284 | value = TmfStateValue.newValueLong(Long.parseLong(fieldString)); | |
285 | break; | |
286 | case DOUBLE: | |
287 | value = TmfStateValue.newValueDouble(Double.parseDouble(fieldString)); | |
288 | break; | |
289 | case NULL: | |
290 | case STRING: | |
291 | default: | |
292 | value = TmfStateValue.newValueString(fieldString); | |
293 | break; | |
294 | } | |
295 | } else if (field instanceof Long) { | |
296 | Long fieldLong = (Long) field; | |
297 | ||
298 | switch (fForcedType) { | |
299 | case INTEGER: | |
300 | value = TmfStateValue.newValueInt(fieldLong.intValue()); | |
301 | break; | |
302 | case STRING: | |
303 | value = TmfStateValue.newValueString(fieldLong.toString()); | |
304 | break; | |
305 | case DOUBLE: | |
306 | value = TmfStateValue.newValueDouble(fieldLong.doubleValue()); | |
307 | break; | |
308 | case LONG: | |
309 | case NULL: | |
310 | default: | |
311 | value = TmfStateValue.newValueLong(fieldLong); | |
312 | break; | |
313 | } | |
314 | } else if (field instanceof Integer) { | |
315 | Integer fieldInteger = (Integer) field; | |
316 | ||
317 | switch (fForcedType) { | |
318 | case LONG: | |
319 | value = TmfStateValue.newValueLong(fieldInteger.longValue()); | |
320 | break; | |
321 | case STRING: | |
322 | value = TmfStateValue.newValueString(fieldInteger.toString()); | |
323 | break; | |
324 | case DOUBLE: | |
325 | value = TmfStateValue.newValueDouble(fieldInteger.doubleValue()); | |
326 | break; | |
327 | case INTEGER: | |
328 | case NULL: | |
329 | default: | |
330 | value = TmfStateValue.newValueInt(fieldInteger); | |
331 | break; | |
332 | } | |
333 | } else if (field instanceof Double) { | |
334 | Double fieldDouble = (Double) field; | |
335 | ||
336 | switch (fForcedType) { | |
337 | case LONG: | |
338 | value = TmfStateValue.newValueLong(fieldDouble.longValue()); | |
339 | break; | |
340 | case STRING: | |
341 | value = TmfStateValue.newValueString(fieldDouble.toString()); | |
342 | break; | |
343 | case INTEGER: | |
344 | value = TmfStateValue.newValueInt(fieldDouble.intValue()); | |
345 | break; | |
346 | case DOUBLE: | |
347 | case NULL: | |
348 | default: | |
349 | value = TmfStateValue.newValueDouble(fieldDouble); | |
350 | break; | |
351 | } | |
352 | } | |
353 | return value; | |
354 | } | |
355 | ||
356 | /** | |
357 | * Get the list of state attributes, the path to the state value | |
358 | * | |
359 | * @return the list of Attribute to have the path in the State System | |
360 | */ | |
361 | @Override | |
362 | public List<ITmfXmlStateAttribute> getAttributes() { | |
363 | return fPath; | |
364 | } | |
365 | ||
1d7e62f9 | 366 | @Override |
0b563c20 | 367 | public void handleEvent(@NonNull ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) throws AttributeNotFoundException, StateValueTypeException, TimeRangeException { |
1d7e62f9 GB |
368 | int quark = IXmlStateSystemContainer.ROOT_QUARK; |
369 | ||
370 | for (ITmfXmlStateAttribute attribute : fPath) { | |
0b563c20 | 371 | quark = attribute.getAttributeQuark(event, quark, scenarioInfo); |
1d7e62f9 GB |
372 | /* the query is not valid, we stop the state change */ |
373 | if (quark == IXmlStateSystemContainer.ERROR_QUARK) { | |
446598f9 | 374 | throw new AttributeNotFoundException("Not found XML attribute " + attribute); //$NON-NLS-1$ |
1d7e62f9 GB |
375 | } |
376 | } | |
377 | ||
378 | long ts = event.getTimestamp().getValue(); | |
0b563c20 | 379 | fStateValue.handleEvent(event, quark, ts, scenarioInfo); |
1d7e62f9 GB |
380 | } |
381 | ||
446598f9 GB |
382 | @Override |
383 | public String toString() { | |
cbac1ac1 JCK |
384 | StringBuilder builder = new StringBuilder("TmfXmlStateValue: "); //$NON-NLS-1$ |
385 | if (fEventField != null) { | |
386 | builder.append("Field=").append(fEventField).append("; "); //$NON-NLS-1$ //$NON-NLS-2$ | |
387 | } else if (!fPath.isEmpty()) { | |
388 | builder.append("Path=").append(fPath).append("; "); //$NON-NLS-1$ //$NON-NLS-2$ | |
389 | } | |
390 | builder.append(fStateValue); | |
391 | return builder.toString(); | |
446598f9 GB |
392 | } |
393 | ||
1d7e62f9 GB |
394 | /** |
395 | * Base class for all state values. Contain default methods to handle event, | |
396 | * process or increment the value | |
397 | */ | |
398 | protected abstract class TmfXmlStateValueBase { | |
399 | ||
400 | /** | |
401 | * Get the value associated with this state value. | |
402 | * | |
403 | * @param event | |
404 | * The event which can be used to retrieve the value if | |
405 | * necessary. The event can be <code>null</code> if no event | |
406 | * is required. | |
0b563c20 JCK |
407 | * @param scenarioInfo |
408 | * The active scenario details. The value should be null if | |
409 | * there no scenario. | |
1d7e62f9 GB |
410 | * @return The state value corresponding to this XML state value |
411 | * @throws AttributeNotFoundException | |
412 | * Pass through the exception it received | |
413 | */ | |
0b563c20 | 414 | public abstract ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) throws AttributeNotFoundException; |
1d7e62f9 GB |
415 | |
416 | /** | |
417 | * Do something with the state value, possibly using an event | |
418 | * | |
419 | * @param event | |
420 | * The event being handled. If there is no event is | |
421 | * available, use <code>null</code>. | |
422 | * @param quark | |
423 | * The quark for this value | |
424 | * @param timestamp | |
425 | * The timestamp of the event | |
0b563c20 JCK |
426 | * @param scenarioInfo |
427 | * The active scenario details. The value should be null if | |
428 | * there no scenario. | |
1d7e62f9 GB |
429 | * @throws StateValueTypeException |
430 | * Pass through the exception it received | |
431 | * @throws TimeRangeException | |
432 | * Pass through the exception it received | |
433 | * @throws AttributeNotFoundException | |
434 | * Pass through the exception it received | |
435 | */ | |
0b563c20 | 436 | public void handleEvent(ITmfEvent event, int quark, long timestamp, @Nullable TmfXmlScenarioInfo scenarioInfo) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException { |
1d7e62f9 | 437 | if (fIncrement) { |
0b563c20 | 438 | incrementValue(event, quark, timestamp, scenarioInfo); |
1d7e62f9 | 439 | } else { |
0b563c20 | 440 | ITmfStateValue value = getValue(event, scenarioInfo); |
1d7e62f9 GB |
441 | processValue(quark, timestamp, value); |
442 | } | |
443 | } | |
444 | ||
445 | /** | |
446 | * Set the value of a quark at a given timestamp. | |
447 | * | |
448 | * @param quark | |
449 | * The quark for this value | |
450 | * @param timestamp | |
451 | * The timestamp | |
452 | * @param value | |
453 | * The value of this state value | |
454 | * @throws TimeRangeException | |
455 | * Pass through the exception it received | |
456 | * @throws StateValueTypeException | |
457 | * Pass through the exception it received | |
458 | * @throws AttributeNotFoundException | |
459 | * Pass through the exception it received | |
460 | */ | |
461 | @SuppressWarnings("unused") | |
462 | protected void processValue(int quark, long timestamp, ITmfStateValue value) throws TimeRangeException, StateValueTypeException, AttributeNotFoundException { | |
463 | } | |
464 | ||
465 | /** | |
466 | * Increments the value of the parameter | |
467 | * | |
468 | * @param event | |
469 | * The event being handled | |
470 | * @param quark | |
471 | * The quark for this value | |
472 | * @param timestamp | |
473 | * The timestamp of the event | |
0b563c20 JCK |
474 | * @param scenarioInfo |
475 | * The active scenario details. The value should be null if | |
476 | * there no scenario. | |
1d7e62f9 GB |
477 | * @throws StateValueTypeException |
478 | * Pass through the exception it received | |
479 | * @throws TimeRangeException | |
480 | * Pass through the exception it received | |
481 | * @throws AttributeNotFoundException | |
482 | * Pass through the exception it received | |
483 | */ | |
484 | @SuppressWarnings("unused") | |
0b563c20 | 485 | protected void incrementValue(ITmfEvent event, int quark, long timestamp, @Nullable TmfXmlScenarioInfo scenarioInfo) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException { |
1d7e62f9 GB |
486 | } |
487 | } | |
488 | ||
489 | /* This state value uses a constant value, defined in the XML */ | |
490 | private class TmfXmlStateValueNull extends TmfXmlStateValueBase { | |
491 | ||
492 | @Override | |
0b563c20 | 493 | public ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) throws AttributeNotFoundException { |
1d7e62f9 GB |
494 | return TmfStateValue.nullValue(); |
495 | } | |
496 | ||
cbac1ac1 JCK |
497 | @Override |
498 | public String toString() { | |
499 | return "NULL"; //$NON-NLS-1$ | |
500 | } | |
1d7e62f9 GB |
501 | } |
502 | ||
52293ccd | 503 | } |