1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
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 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.readwrite
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
20 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
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
.model
.ITmfXmlModelFactory
;
28 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
29 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.TmfXmlStateValue
;
30 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
31 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
32 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
34 import org
.w3c
.dom
.Element
;
37 * Implements a state value in a read write mode. See {@link TmfXmlStateValue}
38 * for the syntax of the state value.
40 * In read/write mode, a state value can be considered as an assignation where
41 * the state value is assigned to the quark represented by the state attributes
43 * @author Geneviève Bastien
45 public class TmfXmlReadWriteStateValue
extends TmfXmlStateValue
{
48 * Constructor where the path to the value is a list of state attributes
51 * The factory used to create XML model elements
53 * The state value XML element
55 * The state system container this state value belongs to
57 * The attributes representing the path to this value
59 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
) {
60 this(modelFactory
, node
, container
, attributes
, null);
64 * Constructor where the path to the value is an event field
67 * The factory used to create XML model elements
69 * The state value XML element
71 * The state system container this state value belongs to
73 * The event field where to get the value
75 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, String eventField
) {
76 this(modelFactory
, node
, container
, new ArrayList
<ITmfXmlStateAttribute
>(), eventField
);
79 private TmfXmlReadWriteStateValue(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
, String eventField
) {
80 super(modelFactory
, node
, container
, attributes
, eventField
);
84 protected ITmfStateSystemBuilder
getStateSystem() {
85 return (ITmfStateSystemBuilder
) super.getStateSystem();
89 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
90 TmfXmlStateValueBase stateValueType
= null;
91 /* Process the XML Element state value */
92 String type
= node
.getAttribute(TmfXmlStrings
.TYPE
);
93 String value
= getSsContainer().getAttributeValue(node
.getAttribute(TmfXmlStrings
.VALUE
));
96 case TmfXmlStrings
.TYPE_INT
: {
98 ITmfStateValue stateValue
= TmfStateValue
.newValueInt(Integer
.parseInt(value
));
99 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
102 case TmfXmlStrings
.TYPE_LONG
: {
104 ITmfStateValue stateValue
= TmfStateValue
.newValueLong(Long
.parseLong(value
));
105 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
108 case TmfXmlStrings
.TYPE_STRING
: {
110 ITmfStateValue stateValue
= TmfStateValue
.newValueString(value
);
111 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
114 case TmfXmlStrings
.TYPE_NULL
: {
116 ITmfStateValue stateValue
= TmfStateValue
.nullValue();
117 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
120 case TmfXmlStrings
.EVENT_FIELD
:
122 stateValueType
= new TmfXmlStateValueEventField(value
);
124 case TmfXmlStrings
.TYPE_EVENT_NAME
:
125 /* The value is the event name */
126 stateValueType
= new TmfXmlStateValueEventName();
128 case TmfXmlStrings
.TYPE_DELETE
:
129 /* Deletes the value of an attribute */
130 stateValueType
= new TmfXmlStateValueDelete();
132 case TmfXmlStrings
.TYPE_QUERY
:
133 /* Value is the result of a query */
134 List
<Element
> children
= XmlUtils
.getChildElements(node
);
135 List
<ITmfXmlStateAttribute
> childAttributes
= new ArrayList
<>();
136 for (Element child
: children
) {
137 ITmfXmlStateAttribute queryAttribute
= modelFactory
.createStateAttribute(child
, getSsContainer());
138 childAttributes
.add(queryAttribute
);
140 stateValueType
= new TmfXmlStateValueQuery(childAttributes
);
143 throw new IllegalArgumentException(String
.format("TmfXmlStateValue constructor: unexpected element %s for stateValue type", type
)); //$NON-NLS-1$
145 return stateValueType
;
148 // ----------------------------------------------------------
149 // Internal state value classes for the different types
150 // ----------------------------------------------------------
153 * Base class for all state value. Contain default methods to handle event,
154 * process or increment the value
156 protected abstract class TmfXmlStateValueTypeReadWrite
extends TmfXmlStateValueBase
{
159 public final void handleEvent(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
161 incrementValue(event
, quark
, timestamp
);
163 ITmfStateValue value
= getValue(event
);
164 processValue(quark
, timestamp
, value
);
169 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws AttributeNotFoundException
, TimeRangeException
, StateValueTypeException
{
170 switch (getStackType()) {
172 getStateSystem().popAttribute(timestamp
, quark
);
175 getStateSystem().pushAttribute(timestamp
, value
, quark
);
180 getStateSystem().modifyAttribute(timestamp
, value
, quark
);
186 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
187 getStateSystem().incrementAttribute(timestamp
, quark
);
191 private static @Nullable ITmfStateValue
incrementByType(int quark
, long timestamp
, ITmfStateSystem ss
, ITmfStateValue stateValue
) throws AttributeNotFoundException
{
192 ITmfStateValue value
= null;
193 switch (stateValue
.getType()) {
195 long incrementLong
= stateValue
.unboxLong();
196 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
197 long currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxLong());
198 value
= TmfStateValue
.newValueLong(incrementLong
+ currentValue
);
202 int increment
= stateValue
.unboxInt();
203 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
204 int currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxInt());
205 value
= TmfStateValue
.newValueInt(increment
+ currentValue
);
216 /* This state value uses a constant value, defined in the XML */
217 private class TmfXmlStateValueTmf
extends TmfXmlStateValueTypeReadWrite
{
219 private final ITmfStateValue fValue
;
221 public TmfXmlStateValueTmf(ITmfStateValue value
) {
226 public ITmfStateValue
getValue(ITmfEvent event
) {
231 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
232 ITmfStateSystem ss
= getStateSystem();
233 ITmfStateValue value
= incrementByType(quark
, timestamp
, ss
, fValue
);
235 processValue(quark
, timestamp
, value
);
237 Activator
.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
243 /* The state value uses the value of an event field */
244 private class TmfXmlStateValueEventField
extends TmfXmlStateValueTypeReadWrite
{
246 private final String fFieldName
;
248 public TmfXmlStateValueEventField(String field
) {
253 public ITmfStateValue
getValue(ITmfEvent event
) {
255 Activator
.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
256 return TmfStateValue
.nullValue();
258 return getEventFieldValue(event
, fFieldName
);
262 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
263 ITmfStateSystem ss
= getSsContainer().getStateSystem();
264 ITmfStateValue incrementValue
= getValue(event
);
265 ITmfStateValue value
= incrementByType(quark
, timestamp
, ss
, incrementValue
);
267 processValue(quark
, timestamp
, value
);
269 Activator
.logWarning(String
.format("TmfXmlStateValue: The event field increment %s is not a number type but a %s", fFieldName
, incrementValue
.getType())); //$NON-NLS-1$
274 /* The state value is the event name */
275 private class TmfXmlStateValueEventName
extends TmfXmlStateValueTypeReadWrite
{
278 public ITmfStateValue
getValue(ITmfEvent event
) {
280 Activator
.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
281 return TmfStateValue
.nullValue();
283 return TmfStateValue
.newValueString(event
.getType().getName());
288 /* The state value deletes an attribute */
289 private class TmfXmlStateValueDelete
extends TmfXmlStateValueTypeReadWrite
{
292 public ITmfStateValue
getValue(ITmfEvent event
) throws AttributeNotFoundException
{
293 return TmfStateValue
.nullValue();
297 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, AttributeNotFoundException
{
298 ITmfStateSystem ss
= getStateSystem();
299 if (!(ss
instanceof ITmfStateSystemBuilder
)) {
300 throw new IllegalStateException("incrementValue should never be called when not building the state system"); //$NON-NLS-1$
302 ITmfStateSystemBuilder builder
= (ITmfStateSystemBuilder
) ss
;
303 builder
.removeAttribute(timestamp
, quark
);
308 /* The state value uses the result of a query */
309 private class TmfXmlStateValueQuery
extends TmfXmlStateValueTypeReadWrite
{
311 private final List
<ITmfXmlStateAttribute
> fQueryValue
;
313 public TmfXmlStateValueQuery(List
<ITmfXmlStateAttribute
> childAttributes
) {
314 fQueryValue
= childAttributes
;
318 public ITmfStateValue
getValue(ITmfEvent event
) throws AttributeNotFoundException
{
319 /* Query the state system for the value */
320 ITmfStateValue value
= TmfStateValue
.nullValue();
321 int quarkQuery
= IXmlStateSystemContainer
.ROOT_QUARK
;
322 ITmfStateSystem ss
= getStateSystem();
324 for (ITmfXmlStateAttribute attribute
: fQueryValue
) {
325 quarkQuery
= attribute
.getAttributeQuark(event
, quarkQuery
);
326 if (quarkQuery
== IXmlStateSystemContainer
.ERROR_QUARK
) {
327 /* the query is not valid, we stop the state change */
332 * the query can fail : for example, if a value is requested but has
335 if (quarkQuery
!= IXmlStateSystemContainer
.ERROR_QUARK
) {
336 value
= ss
.queryOngoingState(quarkQuery
);
342 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
343 ITmfStateSystem ss
= getStateSystem();
344 ITmfStateValue incrementValue
= getValue(event
);
345 ITmfStateValue value
= incrementByType(quark
, timestamp
, ss
, incrementValue
);
347 processValue(quark
, timestamp
, value
);
349 Activator
.logWarning("TmfXmlStateValue: The query result increment is not a number type"); //$NON-NLS-1$