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
, @Nullable String eventField
) {
80 super(modelFactory
, node
, container
, attributes
, eventField
);
84 protected @Nullable 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
));
95 throw new IllegalStateException();
99 case TmfXmlStrings
.TYPE_INT
: {
101 ITmfStateValue stateValue
= TmfStateValue
.newValueInt(Integer
.parseInt(value
));
102 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
105 case TmfXmlStrings
.TYPE_LONG
: {
107 ITmfStateValue stateValue
= TmfStateValue
.newValueLong(Long
.parseLong(value
));
108 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
111 case TmfXmlStrings
.TYPE_STRING
: {
113 ITmfStateValue stateValue
= TmfStateValue
.newValueString(value
);
114 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
117 case TmfXmlStrings
.TYPE_NULL
: {
119 ITmfStateValue stateValue
= TmfStateValue
.nullValue();
120 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
123 case TmfXmlStrings
.EVENT_FIELD
:
125 stateValueType
= new TmfXmlStateValueEventField(value
);
127 case TmfXmlStrings
.TYPE_EVENT_NAME
:
128 /* The value is the event name */
129 stateValueType
= new TmfXmlStateValueEventName();
131 case TmfXmlStrings
.TYPE_DELETE
:
132 /* Deletes the value of an attribute */
133 stateValueType
= new TmfXmlStateValueDelete();
135 case TmfXmlStrings
.TYPE_QUERY
:
136 /* Value is the result of a query */
137 List
<@Nullable Element
> children
= XmlUtils
.getChildElements(node
);
138 List
<ITmfXmlStateAttribute
> childAttributes
= new ArrayList
<>();
139 for (Element child
: children
) {
143 ITmfXmlStateAttribute queryAttribute
= modelFactory
.createStateAttribute(child
, getSsContainer());
144 childAttributes
.add(queryAttribute
);
146 stateValueType
= new TmfXmlStateValueQuery(childAttributes
);
149 throw new IllegalArgumentException(String
.format("TmfXmlStateValue constructor: unexpected element %s for stateValue type", type
)); //$NON-NLS-1$
151 return stateValueType
;
154 // ----------------------------------------------------------
155 // Internal state value classes for the different types
156 // ----------------------------------------------------------
159 * Base class for all state value. Contain default methods to handle event,
160 * process or increment the value
162 protected abstract class TmfXmlStateValueTypeReadWrite
extends TmfXmlStateValueBase
{
165 public final void handleEvent(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
167 incrementValue(event
, quark
, timestamp
);
169 ITmfStateValue value
= getValue(event
);
170 processValue(quark
, timestamp
, value
);
175 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws AttributeNotFoundException
, TimeRangeException
, StateValueTypeException
{
176 ITmfStateSystemBuilder ss
= getStateSystem();
178 throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
180 switch (getStackType()) {
182 ss
.popAttribute(timestamp
, quark
);
185 ss
.pushAttribute(timestamp
, value
, quark
);
190 ss
.modifyAttribute(timestamp
, value
, quark
);
196 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
197 ITmfStateSystemBuilder ss
= getStateSystem();
199 throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
201 ss
.incrementAttribute(timestamp
, quark
);
205 private static @Nullable ITmfStateValue
incrementByType(int quark
, ITmfStateSystem ss
, ITmfStateValue stateValue
) throws AttributeNotFoundException
{
206 ITmfStateValue value
= null;
207 switch (stateValue
.getType()) {
209 long incrementLong
= stateValue
.unboxLong();
210 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
211 long currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxLong());
212 value
= TmfStateValue
.newValueLong(incrementLong
+ currentValue
);
216 int increment
= stateValue
.unboxInt();
217 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
218 int currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxInt());
219 value
= TmfStateValue
.newValueInt(increment
+ currentValue
);
230 /* This state value uses a constant value, defined in the XML */
231 private class TmfXmlStateValueTmf
extends TmfXmlStateValueTypeReadWrite
{
233 private final ITmfStateValue fValue
;
235 public TmfXmlStateValueTmf(ITmfStateValue value
) {
240 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) {
245 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
246 ITmfStateSystem ss
= getStateSystem();
248 throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
250 ITmfStateValue value
= incrementByType(quark
, ss
, fValue
);
252 processValue(quark
, timestamp
, value
);
254 Activator
.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
260 /* The state value uses the value of an event field */
261 private class TmfXmlStateValueEventField
extends TmfXmlStateValueTypeReadWrite
{
263 private final String fFieldName
;
265 public TmfXmlStateValueEventField(String field
) {
270 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) {
272 Activator
.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
273 return TmfStateValue
.nullValue();
275 return getEventFieldValue(event
, fFieldName
);
279 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
280 ITmfStateSystem ss
= getSsContainer().getStateSystem();
282 throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
284 ITmfStateValue incrementValue
= getValue(event
);
285 ITmfStateValue value
= incrementByType(quark
, ss
, incrementValue
);
287 processValue(quark
, timestamp
, value
);
289 Activator
.logWarning(String
.format("TmfXmlStateValue: The event field increment %s is not a number type but a %s", fFieldName
, incrementValue
.getType())); //$NON-NLS-1$
294 /* The state value is the event name */
295 private class TmfXmlStateValueEventName
extends TmfXmlStateValueTypeReadWrite
{
298 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) {
300 Activator
.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
301 return TmfStateValue
.nullValue();
303 return TmfStateValue
.newValueString(event
.getName());
308 /* The state value deletes an attribute */
309 private class TmfXmlStateValueDelete
extends TmfXmlStateValueTypeReadWrite
{
312 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
313 return TmfStateValue
.nullValue();
317 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, AttributeNotFoundException
{
318 ITmfStateSystem ss
= getStateSystem();
319 if (!(ss
instanceof ITmfStateSystemBuilder
)) {
320 throw new IllegalStateException("incrementValue should never be called when not building the state system"); //$NON-NLS-1$
322 ITmfStateSystemBuilder builder
= (ITmfStateSystemBuilder
) ss
;
323 builder
.removeAttribute(timestamp
, quark
);
328 /* The state value uses the result of a query */
329 private class TmfXmlStateValueQuery
extends TmfXmlStateValueTypeReadWrite
{
331 private final List
<ITmfXmlStateAttribute
> fQueryValue
;
333 public TmfXmlStateValueQuery(List
<ITmfXmlStateAttribute
> childAttributes
) {
334 fQueryValue
= childAttributes
;
338 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
339 /* Query the state system for the value */
340 ITmfStateValue value
= TmfStateValue
.nullValue();
341 int quarkQuery
= IXmlStateSystemContainer
.ROOT_QUARK
;
342 ITmfStateSystem ss
= getStateSystem();
344 throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
347 for (ITmfXmlStateAttribute attribute
: fQueryValue
) {
348 quarkQuery
= attribute
.getAttributeQuark(event
, quarkQuery
);
349 if (quarkQuery
== IXmlStateSystemContainer
.ERROR_QUARK
) {
350 /* the query is not valid, we stop the state change */
355 * the query can fail : for example, if a value is requested but has
358 if (quarkQuery
!= IXmlStateSystemContainer
.ERROR_QUARK
) {
359 value
= ss
.queryOngoingState(quarkQuery
);
361 throw new IllegalStateException();
368 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
369 ITmfStateSystem ss
= getStateSystem();
371 throw new IllegalStateException("The state system hasn't been initialized yet"); //$NON-NLS-1$
374 ITmfStateValue incrementValue
= getValue(event
);
375 ITmfStateValue value
= incrementByType(quark
, ss
, incrementValue
);
377 processValue(quark
, timestamp
, value
);
379 Activator
.logWarning("TmfXmlStateValue: The query result increment is not a number type"); //$NON-NLS-1$