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
;
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
;
142 case TmfXmlStrings
.TYPE_DOUBLE
:
143 fForcedType
= ITmfStateValue
.Type
.DOUBLE
;
146 fForcedType
= ITmfStateValue
.Type
.NULL
;
150 * Stack Actions : allow to define a stack with PUSH/POP/PEEK methods
152 String stack
= node
.getAttribute(TmfXmlStrings
.ATTRIBUTE_STACK
);
153 fStackType
= ValueTypeStack
.getTypeFromString(stack
);
157 * Initialize a {@link TmfXmlStateValueBase} object for the type and value
159 * @param modelFactory
160 * The factory used to create XML model elements
162 * The state value XML element
163 * @return The internal state value type corresponding to this state value
165 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
166 return new TmfXmlStateValueNull();
170 * Return the state system container this class is attached to
172 * @return The state system container
174 protected IXmlStateSystemContainer
getSsContainer() {
179 * Get the state system associated with this value's container
181 * @return The state system associated with the state system container
183 protected @Nullable ITmfStateSystem
getStateSystem() {
184 return fContainer
.getStateSystem();
188 * Return whether this value is an increment of the previous value
190 * @return <code>true</code> if the value is an increment
192 protected boolean isIncrement() {
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.
201 * @return The stack type of the attribute
203 protected ValueTypeStack
getStackType() {
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.
211 * @return The desired type of the value
213 protected ITmfStateValue
.Type
getForcedType() {
218 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
219 return fStateValue
.getValue(event
, scenarioInfo
);
223 * Get the value of the event field that is the path of this state value
227 * @return the value of the event field
230 public ITmfStateValue
getEventFieldValue(@NonNull ITmfEvent event
) {
231 String eventField
= fEventField
;
232 if (eventField
== null) {
233 throw new IllegalStateException();
235 return getEventFieldValue(event
, eventField
);
239 * Get the value of an event field
244 * The name of the field of which to get the value
245 * @return The value of the event field
247 protected ITmfStateValue
getEventFieldValue(ITmfEvent event
, String fieldName
) {
249 ITmfStateValue value
= TmfStateValue
.nullValue();
251 final ITmfEventField content
= event
.getContent();
253 /* Exception for "CPU", returns the source of this event */
254 /* FIXME : Nameclash if a eventfield have "cpu" for name. */
255 if (fieldName
.equals(TmfXmlStrings
.CPU
)) {
256 Integer cpu
= TmfTraceUtils
.resolveIntEventAspectOfClassForEvent(event
.getTrace(), TmfCpuAspect
.class, event
);
258 return TmfStateValue
.newValueInt(cpu
.intValue());
261 /* Exception also for "TIMESTAMP", returns the timestamp of this event */
262 if (fieldName
.equals(TmfXmlStrings
.TIMESTAMP
)) {
263 return TmfStateValue
.newValueLong(event
.getTimestamp().getValue());
265 if (content
.getField(fieldName
) == null) {
269 Object field
= content
.getField(fieldName
).getValue();
272 * Try to find the right type. The type can be forced by
273 * "forcedType" argument.
276 if (field
instanceof String
) {
277 String fieldString
= (String
) field
;
279 switch (fForcedType
) {
281 value
= TmfStateValue
.newValueInt(Integer
.parseInt(fieldString
));
284 value
= TmfStateValue
.newValueLong(Long
.parseLong(fieldString
));
287 value
= TmfStateValue
.newValueDouble(Double
.parseDouble(fieldString
));
292 value
= TmfStateValue
.newValueString(fieldString
);
295 } else if (field
instanceof Long
) {
296 Long fieldLong
= (Long
) field
;
298 switch (fForcedType
) {
300 value
= TmfStateValue
.newValueInt(fieldLong
.intValue());
303 value
= TmfStateValue
.newValueString(fieldLong
.toString());
306 value
= TmfStateValue
.newValueDouble(fieldLong
.doubleValue());
311 value
= TmfStateValue
.newValueLong(fieldLong
);
314 } else if (field
instanceof Integer
) {
315 Integer fieldInteger
= (Integer
) field
;
317 switch (fForcedType
) {
319 value
= TmfStateValue
.newValueLong(fieldInteger
.longValue());
322 value
= TmfStateValue
.newValueString(fieldInteger
.toString());
325 value
= TmfStateValue
.newValueDouble(fieldInteger
.doubleValue());
330 value
= TmfStateValue
.newValueInt(fieldInteger
);
333 } else if (field
instanceof Double
) {
334 Double fieldDouble
= (Double
) field
;
336 switch (fForcedType
) {
338 value
= TmfStateValue
.newValueLong(fieldDouble
.longValue());
341 value
= TmfStateValue
.newValueString(fieldDouble
.toString());
344 value
= TmfStateValue
.newValueInt(fieldDouble
.intValue());
349 value
= TmfStateValue
.newValueDouble(fieldDouble
);
357 * Get the list of state attributes, the path to the state value
359 * @return the list of Attribute to have the path in the State System
362 public List
<ITmfXmlStateAttribute
> getAttributes() {
367 public void handleEvent(@NonNull ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
, StateValueTypeException
, TimeRangeException
{
368 int quark
= IXmlStateSystemContainer
.ROOT_QUARK
;
370 for (ITmfXmlStateAttribute attribute
: fPath
) {
371 quark
= attribute
.getAttributeQuark(event
, quark
, scenarioInfo
);
372 /* the query is not valid, we stop the state change */
373 if (quark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
374 throw new AttributeNotFoundException("Not found XML attribute " + attribute
); //$NON-NLS-1$
378 long ts
= event
.getTimestamp().getValue();
379 fStateValue
.handleEvent(event
, quark
, ts
, scenarioInfo
);
383 public String
toString() {
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$
390 builder
.append(fStateValue
);
391 return builder
.toString();
395 * Base class for all state values. Contain default methods to handle event,
396 * process or increment the value
398 protected abstract class TmfXmlStateValueBase
{
401 * Get the value associated with this state value.
404 * The event which can be used to retrieve the value if
405 * necessary. The event can be <code>null</code> if no event
407 * @param scenarioInfo
408 * The active scenario details. The value should be null if
410 * @return The state value corresponding to this XML state value
411 * @throws AttributeNotFoundException
412 * Pass through the exception it received
414 public abstract ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
;
417 * Do something with the state value, possibly using an event
420 * The event being handled. If there is no event is
421 * available, use <code>null</code>.
423 * The quark for this value
425 * The timestamp of the event
426 * @param scenarioInfo
427 * The active scenario details. The value should be null if
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
436 public void handleEvent(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
438 incrementValue(event
, quark
, timestamp
, scenarioInfo
);
440 ITmfStateValue value
= getValue(event
, scenarioInfo
);
441 processValue(quark
, timestamp
, value
);
446 * Set the value of a quark at a given timestamp.
449 * The quark for this 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
461 @SuppressWarnings("unused")
462 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, StateValueTypeException
, AttributeNotFoundException
{
466 * Increments the value of the parameter
469 * The event being handled
471 * The quark for this value
473 * The timestamp of the event
474 * @param scenarioInfo
475 * The active scenario details. The value should be null if
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
484 @SuppressWarnings("unused")
485 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
489 /* This state value uses a constant value, defined in the XML */
490 private class TmfXmlStateValueNull
extends TmfXmlStateValueBase
{
493 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
494 return TmfStateValue
.nullValue();
498 public String
toString() {
499 return "NULL"; //$NON-NLS-1$