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
.internal
.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
.internal
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
20 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
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
.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
;
58 /* Whether to update the current attribute or create a new state */
59 private final boolean fUpdate
;
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 /* Check if this value is an update of the ongoing state */
127 fUpdate
= Boolean
.parseBoolean(node
.getAttribute(TmfXmlStrings
.UPDATE
));
129 /* Process the XML Element state value */
130 fStateValue
= initializeStateValue(modelFactory
, node
);
133 * Forced type allows to convert the value to a certain type : For
134 * example, a process's TID in an event field may arrive with a LONG
135 * format but we want to store the data in an INT
137 switch (node
.getAttribute(TmfXmlStrings
.FORCED_TYPE
)) {
138 case TmfXmlStrings
.TYPE_STRING
:
139 fForcedType
= ITmfStateValue
.Type
.STRING
;
141 case TmfXmlStrings
.TYPE_INT
:
142 fForcedType
= ITmfStateValue
.Type
.INTEGER
;
144 case TmfXmlStrings
.TYPE_LONG
:
145 fForcedType
= ITmfStateValue
.Type
.LONG
;
147 case TmfXmlStrings
.TYPE_DOUBLE
:
148 fForcedType
= ITmfStateValue
.Type
.DOUBLE
;
151 fForcedType
= ITmfStateValue
.Type
.NULL
;
155 * Stack Actions : allow to define a stack with PUSH/POP/PEEK methods
157 String stack
= node
.getAttribute(TmfXmlStrings
.ATTRIBUTE_STACK
);
158 fStackType
= ValueTypeStack
.getTypeFromString(stack
);
162 * Initialize a {@link TmfXmlStateValueBase} object for the type and value
164 * @param modelFactory
165 * The factory used to create XML model elements
167 * The state value XML element
168 * @return The internal state value type corresponding to this state value
170 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
171 return new TmfXmlStateValueNull();
175 * Return the state system container this class is attached to
177 * @return The state system container
179 protected IXmlStateSystemContainer
getSsContainer() {
184 * Get the state system associated with this value's container
186 * @return The state system associated with the state system container
188 protected @Nullable ITmfStateSystem
getStateSystem() {
189 return fContainer
.getStateSystem();
193 * Return whether this value is an increment of the previous value
195 * @return <code>true</code> if the value is an increment
197 protected boolean isIncrement() {
202 * Return whether this value should replace the current value of the
203 * attribute or if a new state should be created.
205 * @return <code>true</code> if the value is to replace the current one
207 protected boolean isUpdate() {
212 * Get the stack type of this attribute. If the attribute is to be pushed or
213 * popped to a stack. The behavior of the stack attribute will depend on the
214 * implementation of the model.
216 * @return The stack type of the attribute
218 protected ValueTypeStack
getStackType() {
223 * Get the forced type of the value. For example, if the value obtained from
224 * the attributes is not in this forced type, it will be converted to this.
226 * @return The desired type of the value
228 protected ITmfStateValue
.Type
getForcedType() {
233 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
234 return fStateValue
.getValue(event
, scenarioInfo
);
238 * Get the value of the event field that is the path of this state value
242 * @return the value of the event field
245 public ITmfStateValue
getEventFieldValue(@NonNull ITmfEvent event
) {
246 String eventField
= fEventField
;
247 if (eventField
== null) {
248 throw new IllegalStateException();
250 return getEventFieldValue(event
, eventField
);
254 * Get the value of an event field
259 * The name of the field of which to get the value
260 * @return The value of the event field
262 protected ITmfStateValue
getEventFieldValue(ITmfEvent event
, String fieldName
) {
264 ITmfStateValue value
= TmfStateValue
.nullValue();
266 final ITmfEventField field
= event
.getContent().getField(fieldName
);
268 /* If the field does not exist, see if it's a special case */
271 if (fieldName
.equalsIgnoreCase(TmfXmlStrings
.CPU
)) {
272 /* A "CPU" field will return the CPU aspect if available */
273 Integer cpu
= TmfTraceUtils
.resolveIntEventAspectOfClassForEvent(event
.getTrace(), TmfCpuAspect
.class, event
);
275 return TmfStateValue
.newValueInt(cpu
.intValue());
277 } else if (fieldName
.equalsIgnoreCase(TmfXmlStrings
.TIMESTAMP
)) {
279 * Exception also for "TIMESTAMP", returns the timestamp of this
282 return TmfStateValue
.newValueLong(event
.getTimestamp().getValue());
287 Object fieldValue
= field
.getValue();
290 * Try to find the right type. The type can be forced by
291 * "forcedType" argument.
294 if (fieldValue
instanceof String
) {
295 String fieldString
= (String
) fieldValue
;
297 switch (fForcedType
) {
299 value
= TmfStateValue
.newValueInt(Integer
.parseInt(fieldString
));
302 value
= TmfStateValue
.newValueLong(Long
.parseLong(fieldString
));
305 value
= TmfStateValue
.newValueDouble(Double
.parseDouble(fieldString
));
308 throw new IllegalStateException("Custom type cannot be forced"); //$NON-NLS-1$
312 value
= TmfStateValue
.newValueString(fieldString
);
315 } else if (fieldValue
instanceof Long
) {
316 Long fieldLong
= (Long
) fieldValue
;
318 switch (fForcedType
) {
320 value
= TmfStateValue
.newValueInt(fieldLong
.intValue());
323 value
= TmfStateValue
.newValueString(fieldLong
.toString());
326 value
= TmfStateValue
.newValueDouble(fieldLong
.doubleValue());
329 throw new IllegalStateException("Custom type cannot be forced"); //$NON-NLS-1$
333 value
= TmfStateValue
.newValueLong(fieldLong
);
336 } else if (fieldValue
instanceof Integer
) {
337 Integer fieldInteger
= (Integer
) fieldValue
;
339 switch (fForcedType
) {
341 value
= TmfStateValue
.newValueLong(fieldInteger
.longValue());
344 value
= TmfStateValue
.newValueString(fieldInteger
.toString());
347 value
= TmfStateValue
.newValueDouble(fieldInteger
.doubleValue());
350 throw new IllegalStateException("Custom type cannot be forced"); //$NON-NLS-1$
354 value
= TmfStateValue
.newValueInt(fieldInteger
);
357 } else if (fieldValue
instanceof Double
) {
358 Double fieldDouble
= (Double
) fieldValue
;
360 switch (fForcedType
) {
362 value
= TmfStateValue
.newValueLong(fieldDouble
.longValue());
365 value
= TmfStateValue
.newValueString(fieldDouble
.toString());
368 value
= TmfStateValue
.newValueInt(fieldDouble
.intValue());
371 throw new IllegalStateException("Custom type cannot be forced"); //$NON-NLS-1$
375 value
= TmfStateValue
.newValueDouble(fieldDouble
);
383 * Get the list of state attributes, the path to the state value
385 * @return the list of Attribute to have the path in the State System
388 public List
<ITmfXmlStateAttribute
> getAttributes() {
393 public void handleEvent(@NonNull ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
, StateValueTypeException
, TimeRangeException
{
394 int quark
= IXmlStateSystemContainer
.ROOT_QUARK
;
396 for (ITmfXmlStateAttribute attribute
: fPath
) {
397 quark
= attribute
.getAttributeQuark(event
, quark
, scenarioInfo
);
398 /* the query is not valid, we stop the state change */
399 if (quark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
400 throw new AttributeNotFoundException("Not found XML attribute " + attribute
); //$NON-NLS-1$
404 long ts
= event
.getTimestamp().getValue();
405 fStateValue
.handleEvent(event
, quark
, ts
, scenarioInfo
);
409 public String
toString() {
410 StringBuilder builder
= new StringBuilder("TmfXmlStateValue: "); //$NON-NLS-1$
411 if (fEventField
!= null) {
412 builder
.append("Field=").append(fEventField
).append("; "); //$NON-NLS-1$ //$NON-NLS-2$
413 } else if (!fPath
.isEmpty()) {
414 builder
.append("Path=").append(fPath
).append("; "); //$NON-NLS-1$ //$NON-NLS-2$
416 builder
.append(fStateValue
);
417 return builder
.toString();
421 * Base class for all state values. Contain default methods to handle event,
422 * process or increment the value
424 protected abstract class TmfXmlStateValueBase
{
427 * Get the value associated with this state value.
430 * The event which can be used to retrieve the value if
431 * necessary. The event can be <code>null</code> if no event
433 * @param scenarioInfo
434 * The active scenario details. The value should be null if
436 * @return The state value corresponding to this XML state value
437 * @throws AttributeNotFoundException
438 * Pass through the exception it received
440 public abstract ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
;
443 * Do something with the state value, possibly using an event
446 * The event being handled. If there is no event is
447 * available, use <code>null</code>.
449 * The quark for this value
451 * The timestamp of the event
452 * @param scenarioInfo
453 * The active scenario details. The value should be null if
455 * @throws StateValueTypeException
456 * Pass through the exception it received
457 * @throws TimeRangeException
458 * Pass through the exception it received
459 * @throws AttributeNotFoundException
460 * Pass through the exception it received
462 public void handleEvent(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
464 incrementValue(event
, quark
, timestamp
, scenarioInfo
);
466 ITmfStateValue value
= getValue(event
, scenarioInfo
);
467 processValue(quark
, timestamp
, value
);
472 * Set the value of a quark at a given timestamp.
475 * The quark for this value
479 * The value of this state value
480 * @throws TimeRangeException
481 * Pass through the exception it received
482 * @throws StateValueTypeException
483 * Pass through the exception it received
484 * @throws AttributeNotFoundException
485 * Pass through the exception it received
487 @SuppressWarnings("unused")
488 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, StateValueTypeException
, AttributeNotFoundException
{
492 * Increments the value of the parameter
495 * The event being handled
497 * The quark for this value
499 * The timestamp of the event
500 * @param scenarioInfo
501 * The active scenario details. The value should be null if
503 * @throws StateValueTypeException
504 * Pass through the exception it received
505 * @throws TimeRangeException
506 * Pass through the exception it received
507 * @throws AttributeNotFoundException
508 * Pass through the exception it received
510 @SuppressWarnings("unused")
511 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
515 /* This state value uses a constant value, defined in the XML */
516 private class TmfXmlStateValueNull
extends TmfXmlStateValueBase
{
519 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
520 return TmfStateValue
.nullValue();
524 public String
toString() {
525 return "NULL"; //$NON-NLS-1$