1 /*******************************************************************************
2 * Copyright (c) 2014 Ecole Polytechnique de Montreal
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
10 * Florian Wininger - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
;
15 import java
.util
.List
;
17 import org
.eclipse
.jdt
.annotation
.NonNull
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
25 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
26 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
31 import org
.w3c
.dom
.Element
;
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
40 * <stateAttribute type="location" value="CurrentThread" />
41 * <stateAttribute type="constant" value="System_call" />
42 * <stateValue type="null" />
45 * @author Florian Wininger
47 public abstract class TmfXmlStateValue
implements ITmfXmlStateValue
{
49 private final TmfXmlStateValueBase fStateValue
;
51 /* Path in the State System */
52 private final List
<ITmfXmlStateAttribute
> fPath
;
53 /* Event field to match with this state value */
54 private final @Nullable String fEventField
;
56 /* Whether this state value is an increment of the previous value */
57 private final boolean fIncrement
;
59 private final ValueTypeStack fStackType
;
60 /* Forced value type */
61 private final ITmfStateValue
.Type fForcedType
;
63 private final IXmlStateSystemContainer fContainer
;
66 * Different behaviors of an attribute that is to be stacked
68 protected enum ValueTypeStack
{
71 /** Peek at the value at the top of the stack */
73 /** Take the value at the top of the stack */
75 /** Push the value on the stack */
79 * Get the type stack value corresponding to a string
82 * The string to match to a value
83 * @return The ValueTypeStack value
85 public static ValueTypeStack
getTypeFromString(String input
) {
87 case TmfXmlStrings
.STACK_PUSH
:
89 case TmfXmlStrings
.STACK_POP
:
91 case TmfXmlStrings
.STACK_PEEK
:
102 * @param modelFactory
103 * The factory used to create XML model elements
105 * The state value XML element
107 * The state system container this state value belongs to
109 * The event field where to get the value
111 * The attributes representing the path to this value
113 protected TmfXmlStateValue(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
, @Nullable String eventField
) {
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$
121 /* Check if there is an increment for the value */
122 fIncrement
= Boolean
.parseBoolean(node
.getAttribute(TmfXmlStrings
.INCREMENT
));
124 /* Process the XML Element state value */
125 fStateValue
= initializeStateValue(modelFactory
, node
);
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
132 switch (node
.getAttribute(TmfXmlStrings
.FORCED_TYPE
)) {
133 case TmfXmlStrings
.TYPE_STRING
:
134 fForcedType
= ITmfStateValue
.Type
.STRING
;
136 case TmfXmlStrings
.TYPE_INT
:
137 fForcedType
= ITmfStateValue
.Type
.INTEGER
;
139 case TmfXmlStrings
.TYPE_LONG
:
140 fForcedType
= ITmfStateValue
.Type
.LONG
;
143 fForcedType
= ITmfStateValue
.Type
.NULL
;
147 * Stack Actions : allow to define a stack with PUSH/POP/PEEK methods
149 @SuppressWarnings("null")
150 @NonNull String stack
= node
.getAttribute(TmfXmlStrings
.ATTRIBUTE_STACK
);
151 fStackType
= ValueTypeStack
.getTypeFromString(stack
);
155 * Initialize a {@link TmfXmlStateValueBase} object for the type and value
157 * @param modelFactory
158 * The factory used to create XML model elements
160 * The state value XML element
161 * @return The internal state value type corresponding to this state value
163 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
164 return new TmfXmlStateValueNull();
168 * Return the state system container this class is attached to
170 * @return The state system container
172 protected IXmlStateSystemContainer
getSsContainer() {
177 * Get the state system associated with this value's container
179 * @return The state system associated with the state system container
181 protected @Nullable ITmfStateSystem
getStateSystem() {
182 return fContainer
.getStateSystem();
186 * Return whether this value is an increment of the previous value
188 * @return <code>true</code> if the value is an increment
190 protected boolean isIncrement() {
195 * Get the stack type of this attribute. If the attribute is to be pushed or
196 * popped to a stack. The behavior of the stack attribute will depend on the
197 * implementation of the model.
199 * @return The stack type of the attribute
201 protected ValueTypeStack
getStackType() {
206 * Get the forced type of the value. For example, if the value obtained from
207 * the attributes is not in this forced type, it will be converted to this.
209 * @return The desired type of the value
211 protected ITmfStateValue
.Type
getForcedType() {
216 * Get the current {@link ITmfStateValue} of this state value for an event.
217 * It does not increment the value and does not any other processing of the
221 * The current event, or <code>null</code> if no event available.
222 * @return the {@link ITmfStateValue}
223 * @throws AttributeNotFoundException
224 * May be thrown by the state system during the query
227 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
228 return fStateValue
.getValue(event
);
232 * Get the value of the event field that is the path of this state value
236 * @return the value of the event field
239 public ITmfStateValue
getEventFieldValue(@NonNull ITmfEvent event
) {
240 String eventField
= fEventField
;
241 if (eventField
== null) {
242 throw new IllegalStateException();
244 return getEventFieldValue(event
, eventField
);
248 * Get the value of an event field
253 * The name of the field of which to get the value
254 * @return The value of the event field
256 protected ITmfStateValue
getEventFieldValue(ITmfEvent event
, String fieldName
) {
258 ITmfStateValue value
= TmfStateValue
.nullValue();
260 final ITmfEventField content
= event
.getContent();
262 /* Exception for "CPU", returns the source of this event */
263 /* FIXME : Nameclash if a eventfield have "cpu" for name. */
264 if (fieldName
.equals(TmfXmlStrings
.CPU
)) {
265 /* See if the event advertises a CPU aspect */
266 Iterable
<TmfCpuAspect
> cpuAspects
= TmfTraceUtils
.getEventAspectsOfClass(
267 event
.getTrace(), TmfCpuAspect
.class);
268 for (TmfCpuAspect aspect
: cpuAspects
) {
269 /* We will just pick the first valid one we find */
270 Integer cpu
= aspect
.resolve(event
);
271 if (!cpu
.equals(TmfCpuAspect
.CPU_UNAVAILABLE
)) {
272 return TmfStateValue
.newValueInt(cpu
.intValue());
276 /* Exception also for "TIMESTAMP", returns the timestamp of this event */
277 if (fieldName
.equals(TmfXmlStrings
.TIMESTAMP
)) {
278 return TmfStateValue
.newValueLong(event
.getTimestamp().getValue());
280 if (content
.getField(fieldName
) == null) {
284 Object field
= content
.getField(fieldName
).getValue();
287 * Try to find the right type. The type can be forced by
288 * "forcedType" argument.
291 if (field
instanceof String
) {
292 String fieldString
= (String
) field
;
294 switch (fForcedType
) {
296 value
= TmfStateValue
.newValueInt(Integer
.parseInt(fieldString
));
299 value
= TmfStateValue
.newValueLong(Long
.parseLong(fieldString
));
302 value
= TmfStateValue
.newValueDouble(Double
.parseDouble(fieldString
));
307 value
= TmfStateValue
.newValueString(fieldString
);
310 } else if (field
instanceof Long
) {
311 Long fieldLong
= (Long
) field
;
313 switch (fForcedType
) {
315 value
= TmfStateValue
.newValueInt(fieldLong
.intValue());
318 value
= TmfStateValue
.newValueString(fieldLong
.toString());
321 value
= TmfStateValue
.newValueDouble(fieldLong
.doubleValue());
326 value
= TmfStateValue
.newValueLong(fieldLong
);
329 } else if (field
instanceof Integer
) {
330 Integer fieldInteger
= (Integer
) field
;
332 switch (fForcedType
) {
334 value
= TmfStateValue
.newValueLong(fieldInteger
.longValue());
337 value
= TmfStateValue
.newValueString(fieldInteger
.toString());
340 value
= TmfStateValue
.newValueDouble(fieldInteger
.doubleValue());
345 value
= TmfStateValue
.newValueInt(fieldInteger
);
348 } else if (field
instanceof Double
) {
349 Double fieldDouble
= (Double
) field
;
351 switch (fForcedType
) {
353 value
= TmfStateValue
.newValueLong(fieldDouble
.longValue());
356 value
= TmfStateValue
.newValueString(fieldDouble
.toString());
359 value
= TmfStateValue
.newValueInt(fieldDouble
.intValue());
364 value
= TmfStateValue
.newValueDouble(fieldDouble
);
372 * Get the list of state attributes, the path to the state value
374 * @return the list of Attribute to have the path in the State System
377 public List
<ITmfXmlStateAttribute
> getAttributes() {
382 * Handles an event, by setting the value of the attribute described by the
383 * state attribute path in the state system.
386 * The event to process
387 * @throws AttributeNotFoundException
388 * Pass through the exception it received
389 * @throws TimeRangeException
390 * Pass through the exception it received
391 * @throws StateValueTypeException
392 * Pass through the exception it received
395 public void handleEvent(@NonNull ITmfEvent event
) throws AttributeNotFoundException
, StateValueTypeException
, TimeRangeException
{
396 int quark
= IXmlStateSystemContainer
.ROOT_QUARK
;
398 for (ITmfXmlStateAttribute attribute
: fPath
) {
399 quark
= attribute
.getAttributeQuark(event
, quark
);
400 /* the query is not valid, we stop the state change */
401 if (quark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
402 throw new AttributeNotFoundException("Not found XML attribute " + attribute
); //$NON-NLS-1$
406 long ts
= event
.getTimestamp().getValue();
407 fStateValue
.handleEvent(event
, quark
, ts
);
411 public String
toString() {
412 return "TmfXmlStateValue: " + fStateValue
; //$NON-NLS-1$
416 * Base class for all state values. Contain default methods to handle event,
417 * process or increment the value
419 protected abstract class TmfXmlStateValueBase
{
422 * Get the value associated with this state value.
425 * The event which can be used to retrieve the value if
426 * necessary. The event can be <code>null</code> if no event
428 * @return The state value corresponding to this XML state value
429 * @throws AttributeNotFoundException
430 * Pass through the exception it received
432 public abstract ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
;
435 * Do something with the state value, possibly using an event
438 * The event being handled. If there is no event is
439 * available, use <code>null</code>.
441 * The quark for this value
443 * The timestamp of the event
444 * @throws StateValueTypeException
445 * Pass through the exception it received
446 * @throws TimeRangeException
447 * Pass through the exception it received
448 * @throws AttributeNotFoundException
449 * Pass through the exception it received
451 public void handleEvent(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
453 incrementValue(event
, quark
, timestamp
);
455 ITmfStateValue value
= getValue(event
);
456 processValue(quark
, timestamp
, value
);
461 * Set the value of a quark at a given timestamp.
464 * The quark for this value
468 * The value of this state value
469 * @throws TimeRangeException
470 * Pass through the exception it received
471 * @throws StateValueTypeException
472 * Pass through the exception it received
473 * @throws AttributeNotFoundException
474 * Pass through the exception it received
476 @SuppressWarnings("unused")
477 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, StateValueTypeException
, AttributeNotFoundException
{
481 * Increments the value of the parameter
484 * The event being handled
486 * The quark for this value
488 * The timestamp of the event
489 * @throws StateValueTypeException
490 * Pass through the exception it received
491 * @throws TimeRangeException
492 * Pass through the exception it received
493 * @throws AttributeNotFoundException
494 * Pass through the exception it received
496 @SuppressWarnings("unused")
497 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
501 /* This state value uses a constant value, defined in the XML */
502 private class TmfXmlStateValueNull
extends TmfXmlStateValueBase
{
505 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
506 return TmfStateValue
.nullValue();