1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.List
;
19 import org
.eclipse
.jdt
.annotation
.NonNull
;
20 import org
.eclipse
.jdt
.annotation
.Nullable
;
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
;
27 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
28 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.TmfCpuAspect
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
33 import org
.w3c
.dom
.Element
;
36 * This Class implements a State Value in the XML-defined state system, along
37 * with the path to get to the value (either a list of state attributes or an
42 * <stateAttribute type="location" value="CurrentThread" />
43 * <stateAttribute type="constant" value="System_call" />
44 * <stateValue type="null" />
47 * @author Florian Wininger
49 public abstract class TmfXmlStateValue
implements ITmfXmlStateValue
{
51 private final TmfXmlStateValueBase fStateValue
;
53 /* Path in the State System */
54 private final List
<ITmfXmlStateAttribute
> fPath
;
55 /* Event field to match with this state value */
56 private final @Nullable String fEventField
;
58 /* Whether this state value is an increment of the previous value */
59 private final boolean fIncrement
;
61 private final ValueTypeStack fStackType
;
62 /* Forced value type */
63 private final ITmfStateValue
.Type fForcedType
;
65 private final IXmlStateSystemContainer fContainer
;
68 * Different behaviors of an attribute that is to be stacked
70 protected enum ValueTypeStack
{
73 /** Peek at the value at the top of the stack */
75 /** Take the value at the top of the stack */
77 /** Push the value on the stack */
81 * Get the type stack value corresponding to a string
84 * The string to match to a value
85 * @return The ValueTypeStack value
87 public static ValueTypeStack
getTypeFromString(String input
) {
89 case TmfXmlStrings
.STACK_PUSH
:
91 case TmfXmlStrings
.STACK_POP
:
93 case TmfXmlStrings
.STACK_PEEK
:
104 * @param modelFactory
105 * The factory used to create XML model elements
107 * The state value XML element
109 * The state system container this state value belongs to
111 * The event field where to get the value
113 * The attributes representing the path to this value
115 protected TmfXmlStateValue(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
, @Nullable String eventField
) {
117 fContainer
= container
;
118 fEventField
= eventField
;
119 if (!node
.getNodeName().equals(TmfXmlStrings
.STATE_VALUE
)) {
120 throw new IllegalArgumentException("TmfXmlStateValue constructor: Element is not a stateValue"); //$NON-NLS-1$
123 /* Check if there is an increment for the value */
124 fIncrement
= Boolean
.parseBoolean(node
.getAttribute(TmfXmlStrings
.INCREMENT
));
126 /* Process the XML Element state value */
127 fStateValue
= initializeStateValue(modelFactory
, node
);
130 * Forced type allows to convert the value to a certain type : For
131 * example, a process's TID in an event field may arrive with a LONG
132 * format but we want to store the data in an INT
134 switch (node
.getAttribute(TmfXmlStrings
.FORCED_TYPE
)) {
135 case TmfXmlStrings
.TYPE_STRING
:
136 fForcedType
= ITmfStateValue
.Type
.STRING
;
138 case TmfXmlStrings
.TYPE_INT
:
139 fForcedType
= ITmfStateValue
.Type
.INTEGER
;
141 case TmfXmlStrings
.TYPE_LONG
:
142 fForcedType
= ITmfStateValue
.Type
.LONG
;
145 fForcedType
= ITmfStateValue
.Type
.NULL
;
149 * Stack Actions : allow to define a stack with PUSH/POP/PEEK methods
151 String stack
= checkNotNull(node
.getAttribute(TmfXmlStrings
.ATTRIBUTE_STACK
));
152 fStackType
= ValueTypeStack
.getTypeFromString(stack
);
156 * Initialize a {@link TmfXmlStateValueBase} object for the type and value
158 * @param modelFactory
159 * The factory used to create XML model elements
161 * The state value XML element
162 * @return The internal state value type corresponding to this state value
164 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
165 return new TmfXmlStateValueNull();
169 * Return the state system container this class is attached to
171 * @return The state system container
173 protected IXmlStateSystemContainer
getSsContainer() {
178 * Get the state system associated with this value's container
180 * @return The state system associated with the state system container
182 protected @Nullable ITmfStateSystem
getStateSystem() {
183 return fContainer
.getStateSystem();
187 * Return whether this value is an increment of the previous value
189 * @return <code>true</code> if the value is an increment
191 protected boolean isIncrement() {
196 * Get the stack type of this attribute. If the attribute is to be pushed or
197 * popped to a stack. The behavior of the stack attribute will depend on the
198 * implementation of the model.
200 * @return The stack type of the attribute
202 protected ValueTypeStack
getStackType() {
207 * Get the forced type of the value. For example, if the value obtained from
208 * the attributes is not in this forced type, it will be converted to this.
210 * @return The desired type of the value
212 protected ITmfStateValue
.Type
getForcedType() {
217 * Get the current {@link ITmfStateValue} of this state value for an event.
218 * It does not increment the value and does not any other processing of the
222 * The current event, or <code>null</code> if no event available.
223 * @return the {@link ITmfStateValue}
224 * @throws AttributeNotFoundException
225 * May be thrown by the state system during the query
228 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
229 return fStateValue
.getValue(event
);
233 * Get the value of the event field that is the path of this state value
237 * @return the value of the event field
240 public ITmfStateValue
getEventFieldValue(@NonNull ITmfEvent event
) {
241 String eventField
= fEventField
;
242 if (eventField
== null) {
243 throw new IllegalStateException();
245 return getEventFieldValue(event
, eventField
);
249 * Get the value of an event field
254 * The name of the field of which to get the value
255 * @return The value of the event field
257 protected ITmfStateValue
getEventFieldValue(ITmfEvent event
, String fieldName
) {
259 ITmfStateValue value
= TmfStateValue
.nullValue();
261 final ITmfEventField content
= event
.getContent();
263 /* Exception for "CPU", returns the source of this event */
264 /* FIXME : Nameclash if a eventfield have "cpu" for name. */
265 if (fieldName
.equals(TmfXmlStrings
.CPU
)) {
266 Integer cpu
= TmfTraceUtils
.resolveIntEventAspectOfClassForEvent(event
.getTrace(), TmfCpuAspect
.class, event
);
268 return TmfStateValue
.newValueInt(cpu
.intValue());
271 /* Exception also for "TIMESTAMP", returns the timestamp of this event */
272 if (fieldName
.equals(TmfXmlStrings
.TIMESTAMP
)) {
273 return TmfStateValue
.newValueLong(event
.getTimestamp().getValue());
275 if (content
.getField(fieldName
) == null) {
279 Object field
= content
.getField(fieldName
).getValue();
282 * Try to find the right type. The type can be forced by
283 * "forcedType" argument.
286 if (field
instanceof String
) {
287 String fieldString
= (String
) field
;
289 switch (fForcedType
) {
291 value
= TmfStateValue
.newValueInt(Integer
.parseInt(fieldString
));
294 value
= TmfStateValue
.newValueLong(Long
.parseLong(fieldString
));
297 value
= TmfStateValue
.newValueDouble(Double
.parseDouble(fieldString
));
302 value
= TmfStateValue
.newValueString(fieldString
);
305 } else if (field
instanceof Long
) {
306 Long fieldLong
= (Long
) field
;
308 switch (fForcedType
) {
310 value
= TmfStateValue
.newValueInt(fieldLong
.intValue());
313 value
= TmfStateValue
.newValueString(fieldLong
.toString());
316 value
= TmfStateValue
.newValueDouble(fieldLong
.doubleValue());
321 value
= TmfStateValue
.newValueLong(fieldLong
);
324 } else if (field
instanceof Integer
) {
325 Integer fieldInteger
= (Integer
) field
;
327 switch (fForcedType
) {
329 value
= TmfStateValue
.newValueLong(fieldInteger
.longValue());
332 value
= TmfStateValue
.newValueString(fieldInteger
.toString());
335 value
= TmfStateValue
.newValueDouble(fieldInteger
.doubleValue());
340 value
= TmfStateValue
.newValueInt(fieldInteger
);
343 } else if (field
instanceof Double
) {
344 Double fieldDouble
= (Double
) field
;
346 switch (fForcedType
) {
348 value
= TmfStateValue
.newValueLong(fieldDouble
.longValue());
351 value
= TmfStateValue
.newValueString(fieldDouble
.toString());
354 value
= TmfStateValue
.newValueInt(fieldDouble
.intValue());
359 value
= TmfStateValue
.newValueDouble(fieldDouble
);
367 * Get the list of state attributes, the path to the state value
369 * @return the list of Attribute to have the path in the State System
372 public List
<ITmfXmlStateAttribute
> getAttributes() {
377 * Handles an event, by setting the value of the attribute described by the
378 * state attribute path in the state system.
381 * The event to process
382 * @throws AttributeNotFoundException
383 * Pass through the exception it received
384 * @throws TimeRangeException
385 * Pass through the exception it received
386 * @throws StateValueTypeException
387 * Pass through the exception it received
390 public void handleEvent(@NonNull ITmfEvent event
) throws AttributeNotFoundException
, StateValueTypeException
, TimeRangeException
{
391 int quark
= IXmlStateSystemContainer
.ROOT_QUARK
;
393 for (ITmfXmlStateAttribute attribute
: fPath
) {
394 quark
= attribute
.getAttributeQuark(event
, quark
);
395 /* the query is not valid, we stop the state change */
396 if (quark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
397 throw new AttributeNotFoundException("Not found XML attribute " + attribute
); //$NON-NLS-1$
401 long ts
= event
.getTimestamp().getValue();
402 fStateValue
.handleEvent(event
, quark
, ts
);
406 public String
toString() {
407 return "TmfXmlStateValue: " + fStateValue
; //$NON-NLS-1$
411 * Base class for all state values. Contain default methods to handle event,
412 * process or increment the value
414 protected abstract class TmfXmlStateValueBase
{
417 * Get the value associated with this state value.
420 * The event which can be used to retrieve the value if
421 * necessary. The event can be <code>null</code> if no event
423 * @return The state value corresponding to this XML state value
424 * @throws AttributeNotFoundException
425 * Pass through the exception it received
427 public abstract ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
;
430 * Do something with the state value, possibly using an event
433 * The event being handled. If there is no event is
434 * available, use <code>null</code>.
436 * The quark for this value
438 * The timestamp of the event
439 * @throws StateValueTypeException
440 * Pass through the exception it received
441 * @throws TimeRangeException
442 * Pass through the exception it received
443 * @throws AttributeNotFoundException
444 * Pass through the exception it received
446 public void handleEvent(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
448 incrementValue(event
, quark
, timestamp
);
450 ITmfStateValue value
= getValue(event
);
451 processValue(quark
, timestamp
, value
);
456 * Set the value of a quark at a given timestamp.
459 * The quark for this value
463 * The value of this state value
464 * @throws TimeRangeException
465 * Pass through the exception it received
466 * @throws StateValueTypeException
467 * Pass through the exception it received
468 * @throws AttributeNotFoundException
469 * Pass through the exception it received
471 @SuppressWarnings("unused")
472 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, StateValueTypeException
, AttributeNotFoundException
{
476 * Increments the value of the parameter
479 * The event being handled
481 * The quark for this value
483 * The timestamp of the event
484 * @throws StateValueTypeException
485 * Pass through the exception it received
486 * @throws TimeRangeException
487 * Pass through the exception it received
488 * @throws AttributeNotFoundException
489 * Pass through the exception it received
491 @SuppressWarnings("unused")
492 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
496 /* This state value uses a constant value, defined in the XML */
497 private class TmfXmlStateValueNull
extends TmfXmlStateValueBase
{
500 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
501 return TmfStateValue
.nullValue();