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
.linuxtools
.tmf
.analysis
.xml
.core
.model
.readwrite
;
15 import java
.util
.ArrayList
;
16 import java
.util
.List
;
18 import org
.eclipse
.linuxtools
.internal
.tmf
.analysis
.xml
.core
.Activator
;
19 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
20 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystemBuilder
;
21 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
22 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateValueTypeException
;
23 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.TimeRangeException
;
24 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.ITmfStateValue
;
25 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.TmfStateValue
;
26 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
27 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
28 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.model
.TmfXmlStateValue
;
29 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
30 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
31 import org
.eclipse
.linuxtools
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
33 import org
.w3c
.dom
.Element
;
36 * Implements a state value in a read write mode. See {@link TmfXmlStateValue}
37 * for the syntax of the state value.
39 * In read/write mode, a state value can be considered as an assignation where
40 * the state value is assigned to the quark represented by the state attributes
42 * @author Geneviève Bastien
44 public class TmfXmlReadWriteStateValue
extends TmfXmlStateValue
{
47 * Constructor where the path to the value is a list of state attributes
50 * The factory used to create XML model elements
52 * The state value XML element
54 * The state system container this state value belongs to
56 * The attributes representing the path to this value
58 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
) {
59 this(modelFactory
, node
, container
, attributes
, null);
63 * Constructor where the path to the value is an event field
66 * The factory used to create XML model elements
68 * The state value XML element
70 * The state system container this state value belongs to
72 * The event field where to get the value
74 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, String eventField
) {
75 this(modelFactory
, node
, container
, new ArrayList
<ITmfXmlStateAttribute
>(), eventField
);
78 private TmfXmlReadWriteStateValue(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
, String eventField
) {
79 super(modelFactory
, node
, container
, attributes
, eventField
);
83 protected ITmfStateSystemBuilder
getStateSystem() {
84 return (ITmfStateSystemBuilder
) super.getStateSystem();
88 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
89 TmfXmlStateValueBase stateValueType
= null;
90 /* Process the XML Element state value */
91 String type
= node
.getAttribute(TmfXmlStrings
.TYPE
);
92 String value
= getSsContainer().getAttributeValue(node
.getAttribute(TmfXmlStrings
.VALUE
));
95 case TmfXmlStrings
.TYPE_INT
: {
97 ITmfStateValue stateValue
= TmfStateValue
.newValueInt(Integer
.parseInt(value
));
98 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
101 case TmfXmlStrings
.TYPE_LONG
: {
103 ITmfStateValue stateValue
= TmfStateValue
.newValueLong(Long
.parseLong(value
));
104 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
107 case TmfXmlStrings
.TYPE_STRING
: {
109 ITmfStateValue stateValue
= TmfStateValue
.newValueString(value
);
110 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
113 case TmfXmlStrings
.TYPE_NULL
: {
115 ITmfStateValue stateValue
= TmfStateValue
.nullValue();
116 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
119 case TmfXmlStrings
.EVENT_FIELD
:
121 stateValueType
= new TmfXmlStateValueEventField(value
);
123 case TmfXmlStrings
.TYPE_EVENT_NAME
:
124 /* The value is the event name */
125 stateValueType
= new TmfXmlStateValueEventName();
127 case TmfXmlStrings
.TYPE_DELETE
:
128 /* Deletes the value of an attribute */
129 stateValueType
= new TmfXmlStateValueDelete();
131 case TmfXmlStrings
.TYPE_QUERY
:
132 /* Value is the result of a query */
133 List
<Element
> children
= XmlUtils
.getChildElements(node
);
134 List
<ITmfXmlStateAttribute
> childAttributes
= new ArrayList
<>();
135 for (Element child
: children
) {
136 ITmfXmlStateAttribute queryAttribute
= modelFactory
.createStateAttribute(child
, getSsContainer());
137 childAttributes
.add(queryAttribute
);
139 stateValueType
= new TmfXmlStateValueQuery(childAttributes
);
142 throw new IllegalArgumentException(String
.format("TmfXmlStateValue constructor: unexpected element %s for stateValue type", type
)); //$NON-NLS-1$
144 return stateValueType
;
147 // ----------------------------------------------------------
148 // Internal state value classes for the different types
149 // ----------------------------------------------------------
152 * Base class for all state value. Contain default methods to handle event,
153 * process or increment the value
155 protected abstract class TmfXmlStateValueTypeReadWrite
extends TmfXmlStateValueBase
{
158 public final void handleEvent(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
160 incrementValue(event
, quark
, timestamp
);
162 ITmfStateValue value
= getValue(event
);
163 processValue(quark
, timestamp
, value
);
168 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws AttributeNotFoundException
, TimeRangeException
, StateValueTypeException
{
169 switch (getStackType()) {
171 getStateSystem().popAttribute(timestamp
, quark
);
174 getStateSystem().pushAttribute(timestamp
, value
, quark
);
179 getStateSystem().modifyAttribute(timestamp
, value
, quark
);
185 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
186 getStateSystem().incrementAttribute(timestamp
, quark
);
190 /* This state value uses a constant value, defined in the XML */
191 private class TmfXmlStateValueTmf
extends TmfXmlStateValueTypeReadWrite
{
193 private final ITmfStateValue fValue
;
195 public TmfXmlStateValueTmf(ITmfStateValue value
) {
200 public ITmfStateValue
getValue(ITmfEvent event
) {
205 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
206 ITmfStateSystem ss
= getStateSystem();
207 switch (fValue
.getType()) {
209 long incrementLong
= fValue
.unboxLong();
210 long currentValue
= ss
.queryOngoingState(quark
).unboxLong();
211 ITmfStateValue value
= TmfStateValue
.newValueLong(incrementLong
+ currentValue
);
212 processValue(quark
, timestamp
, value
);
216 int increment
= fValue
.unboxInt();
217 int currentValue
= ss
.queryOngoingState(quark
).unboxInt();
218 ITmfStateValue value
= TmfStateValue
.newValueInt(increment
+ currentValue
);
219 processValue(quark
, timestamp
, value
);
226 Activator
.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
232 /* The state value uses the value of an event field */
233 private class TmfXmlStateValueEventField
extends TmfXmlStateValueTypeReadWrite
{
235 private final String fFieldName
;
237 public TmfXmlStateValueEventField(String field
) {
242 public ITmfStateValue
getValue(ITmfEvent event
) {
244 Activator
.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
245 return TmfStateValue
.nullValue();
247 return getEventFieldValue(event
, fFieldName
);
251 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
252 ITmfStateSystem ss
= getSsContainer().getStateSystem();
253 ITmfStateValue incrementValue
= getValue(event
);
254 switch (incrementValue
.getType()) {
256 int increment
= incrementValue
.unboxInt();
257 int currentValue
= ss
.queryOngoingState(quark
).unboxInt();
258 ITmfStateValue value
= TmfStateValue
.newValueInt(increment
+ currentValue
);
259 processValue(quark
, timestamp
, value
);
263 long incrementLong
= incrementValue
.unboxLong();
264 long currentValue
= ss
.queryOngoingState(quark
).unboxLong();
265 ITmfStateValue value
= TmfStateValue
.newValueLong(incrementLong
+ currentValue
);
266 processValue(quark
, timestamp
, value
);
273 Activator
.logWarning(String
.format("TmfXmlStateValue: The event field increment %s is not a number type but a %s", fFieldName
, incrementValue
.getType())); //$NON-NLS-1$
279 /* The state value is the event name */
280 private class TmfXmlStateValueEventName
extends TmfXmlStateValueTypeReadWrite
{
283 public ITmfStateValue
getValue(ITmfEvent event
) {
285 Activator
.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
286 return TmfStateValue
.nullValue();
288 return TmfStateValue
.newValueString(event
.getType().getName());
293 /* The state value deletes an attribute */
294 private class TmfXmlStateValueDelete
extends TmfXmlStateValueTypeReadWrite
{
297 public ITmfStateValue
getValue(ITmfEvent event
) throws AttributeNotFoundException
{
298 return TmfStateValue
.nullValue();
302 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, AttributeNotFoundException
{
303 ITmfStateSystem ss
= getStateSystem();
304 if (!(ss
instanceof ITmfStateSystemBuilder
)) {
305 throw new IllegalStateException("incrementValue should never be called when not building the state system"); //$NON-NLS-1$
307 ITmfStateSystemBuilder builder
= (ITmfStateSystemBuilder
) ss
;
308 builder
.removeAttribute(timestamp
, quark
);
313 /* The state value uses the result of a query */
314 private class TmfXmlStateValueQuery
extends TmfXmlStateValueTypeReadWrite
{
316 private final List
<ITmfXmlStateAttribute
> fQueryValue
;
318 public TmfXmlStateValueQuery(List
<ITmfXmlStateAttribute
> childAttributes
) {
319 fQueryValue
= childAttributes
;
323 public ITmfStateValue
getValue(ITmfEvent event
) throws AttributeNotFoundException
{
324 /* Query the state system for the value */
325 ITmfStateValue value
= TmfStateValue
.nullValue();
326 int quarkQuery
= IXmlStateSystemContainer
.ROOT_QUARK
;
327 ITmfStateSystem ss
= getStateSystem();
329 for (ITmfXmlStateAttribute attribute
: fQueryValue
) {
330 quarkQuery
= attribute
.getAttributeQuark(event
, quarkQuery
);
331 if (quarkQuery
== IXmlStateSystemContainer
.ERROR_QUARK
) {
332 /* the query is not valid, we stop the state change */
337 * the query can fail : for example, if a value is requested but has
340 if (quarkQuery
!= IXmlStateSystemContainer
.ERROR_QUARK
) {
341 value
= ss
.queryOngoingState(quarkQuery
);
347 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
348 ITmfStateSystem ss
= getStateSystem();
349 ITmfStateValue incrementValue
= getValue(event
);
350 switch (incrementValue
.getType()) {
352 int increment
= incrementValue
.unboxInt();
353 int currentValue
= ss
.queryOngoingState(quark
).unboxInt();
354 ITmfStateValue value
= TmfStateValue
.newValueInt(increment
+ currentValue
);
355 processValue(quark
, timestamp
, value
);
359 long incrementLong
= incrementValue
.unboxLong();
360 long currentValue
= ss
.queryOngoingState(quark
).unboxLong();
361 ITmfStateValue value
= TmfStateValue
.newValueLong(incrementLong
+ currentValue
);
362 processValue(quark
, timestamp
, value
);
369 Activator
.logWarning("TmfXmlStateValue: The query result increment is not a number type"); //$NON-NLS-1$