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
{
47 private static final String ILLEGAL_STATE_EXCEPTION_MESSAGE
= "The state system hasn't been initialized yet"; //$NON-NLS-1$
50 * Constructor where the path to the value is a list of state attributes
53 * The factory used to create XML model elements
55 * The state value XML element
57 * The state system container this state value belongs to
59 * The attributes representing the path to this value
61 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
) {
62 this(modelFactory
, node
, container
, attributes
, null);
66 * Constructor where the path to the value is an event field
69 * The factory used to create XML model elements
71 * The state value XML element
73 * The state system container this state value belongs to
75 * The event field where to get the value
77 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, String eventField
) {
78 this(modelFactory
, node
, container
, new ArrayList
<ITmfXmlStateAttribute
>(), eventField
);
81 private TmfXmlReadWriteStateValue(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
, @Nullable String eventField
) {
82 super(modelFactory
, node
, container
, attributes
, eventField
);
86 protected @Nullable ITmfStateSystemBuilder
getStateSystem() {
87 return (ITmfStateSystemBuilder
) super.getStateSystem();
91 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
92 TmfXmlStateValueBase stateValueType
= null;
93 /* Process the XML Element state value */
94 String type
= node
.getAttribute(TmfXmlStrings
.TYPE
);
95 String value
= getSsContainer().getAttributeValue(node
.getAttribute(TmfXmlStrings
.VALUE
));
97 throw new IllegalStateException();
101 case TmfXmlStrings
.TYPE_INT
: {
103 ITmfStateValue stateValue
= TmfStateValue
.newValueInt(Integer
.parseInt(value
));
104 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
107 case TmfXmlStrings
.TYPE_LONG
: {
109 ITmfStateValue stateValue
= TmfStateValue
.newValueLong(Long
.parseLong(value
));
110 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
113 case TmfXmlStrings
.TYPE_STRING
: {
115 ITmfStateValue stateValue
= TmfStateValue
.newValueString(value
);
116 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
119 case TmfXmlStrings
.TYPE_NULL
: {
121 ITmfStateValue stateValue
= TmfStateValue
.nullValue();
122 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
125 case TmfXmlStrings
.EVENT_FIELD
:
127 stateValueType
= new TmfXmlStateValueEventField(value
);
129 case TmfXmlStrings
.TYPE_EVENT_NAME
:
130 /* The value is the event name */
131 stateValueType
= new TmfXmlStateValueEventName();
133 case TmfXmlStrings
.TYPE_DELETE
:
134 /* Deletes the value of an attribute */
135 stateValueType
= new TmfXmlStateValueDelete();
137 case TmfXmlStrings
.TYPE_QUERY
:
138 /* Value is the result of a query */
139 List
<@Nullable Element
> children
= XmlUtils
.getChildElements(node
);
140 List
<ITmfXmlStateAttribute
> childAttributes
= new ArrayList
<>();
141 for (Element child
: children
) {
145 ITmfXmlStateAttribute queryAttribute
= modelFactory
.createStateAttribute(child
, getSsContainer());
146 childAttributes
.add(queryAttribute
);
148 stateValueType
= new TmfXmlStateValueQuery(childAttributes
);
151 throw new IllegalArgumentException(String
.format("TmfXmlStateValue constructor: unexpected element %s for stateValue type", type
)); //$NON-NLS-1$
153 return stateValueType
;
156 // ----------------------------------------------------------
157 // Internal state value classes for the different types
158 // ----------------------------------------------------------
161 * Base class for all state value. Contain default methods to handle event,
162 * process or increment the value
164 protected abstract class TmfXmlStateValueTypeReadWrite
extends TmfXmlStateValueBase
{
167 public final void handleEvent(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
169 incrementValue(event
, quark
, timestamp
);
171 ITmfStateValue value
= getValue(event
);
172 processValue(quark
, timestamp
, value
);
177 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws AttributeNotFoundException
, TimeRangeException
, StateValueTypeException
{
178 ITmfStateSystemBuilder ss
= getStateSystem();
180 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
182 switch (getStackType()) {
184 ss
.popAttribute(timestamp
, quark
);
187 ss
.pushAttribute(timestamp
, value
, quark
);
192 ss
.modifyAttribute(timestamp
, value
, quark
);
198 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
199 ITmfStateSystemBuilder ss
= getStateSystem();
201 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
203 ss
.incrementAttribute(timestamp
, quark
);
207 private static @Nullable ITmfStateValue
incrementByType(int quark
, ITmfStateSystem ss
, ITmfStateValue stateValue
) throws AttributeNotFoundException
{
208 ITmfStateValue value
= null;
209 switch (stateValue
.getType()) {
211 long incrementLong
= stateValue
.unboxLong();
212 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
213 long currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxLong());
214 value
= TmfStateValue
.newValueLong(incrementLong
+ currentValue
);
218 int increment
= stateValue
.unboxInt();
219 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
220 int currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxInt());
221 value
= TmfStateValue
.newValueInt(increment
+ currentValue
);
232 /* This state value uses a constant value, defined in the XML */
233 private class TmfXmlStateValueTmf
extends TmfXmlStateValueTypeReadWrite
{
235 private final ITmfStateValue fValue
;
237 public TmfXmlStateValueTmf(ITmfStateValue value
) {
242 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) {
247 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
248 ITmfStateSystem ss
= getStateSystem();
250 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
252 ITmfStateValue value
= incrementByType(quark
, ss
, fValue
);
254 processValue(quark
, timestamp
, value
);
256 Activator
.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
261 public String
toString() {
262 return "Value=" + fValue
; //$NON-NLS-1$
267 /* The state value uses the value of an event field */
268 private class TmfXmlStateValueEventField
extends TmfXmlStateValueTypeReadWrite
{
270 private final String fFieldName
;
272 public TmfXmlStateValueEventField(String field
) {
277 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) {
279 Activator
.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
280 return TmfStateValue
.nullValue();
282 return getEventFieldValue(event
, fFieldName
);
286 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
287 ITmfStateSystem ss
= getSsContainer().getStateSystem();
289 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
291 ITmfStateValue incrementValue
= getValue(event
);
292 ITmfStateValue value
= incrementByType(quark
, ss
, incrementValue
);
294 processValue(quark
, timestamp
, value
);
296 Activator
.logWarning(String
.format("TmfXmlStateValue: The event field increment %s is not a number type but a %s", fFieldName
, incrementValue
.getType())); //$NON-NLS-1$
301 public String
toString() {
302 return "Event Field=" + fFieldName
; //$NON-NLS-1$
306 /* The state value is the event name */
307 private class TmfXmlStateValueEventName
extends TmfXmlStateValueTypeReadWrite
{
310 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) {
312 Activator
.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
313 return TmfStateValue
.nullValue();
315 return TmfStateValue
.newValueString(event
.getName());
319 public String
toString() {
320 return "Event name"; //$NON-NLS-1$
325 /* The state value deletes an attribute */
326 private class TmfXmlStateValueDelete
extends TmfXmlStateValueTypeReadWrite
{
329 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
330 return TmfStateValue
.nullValue();
334 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, AttributeNotFoundException
{
335 ITmfStateSystem ss
= getStateSystem();
336 if (!(ss
instanceof ITmfStateSystemBuilder
)) {
337 throw new IllegalStateException("incrementValue should never be called when not building the state system"); //$NON-NLS-1$
339 ITmfStateSystemBuilder builder
= (ITmfStateSystemBuilder
) ss
;
340 builder
.removeAttribute(timestamp
, quark
);
344 public String
toString() {
345 return "Delete"; //$NON-NLS-1$
349 /* The state value uses the result of a query */
350 private class TmfXmlStateValueQuery
extends TmfXmlStateValueTypeReadWrite
{
352 private final List
<ITmfXmlStateAttribute
> fQueryValue
;
354 public TmfXmlStateValueQuery(List
<ITmfXmlStateAttribute
> childAttributes
) {
355 fQueryValue
= childAttributes
;
359 public ITmfStateValue
getValue(@Nullable ITmfEvent event
) throws AttributeNotFoundException
{
360 /* Query the state system for the value */
361 ITmfStateValue value
= TmfStateValue
.nullValue();
362 int quarkQuery
= IXmlStateSystemContainer
.ROOT_QUARK
;
363 ITmfStateSystem ss
= getStateSystem();
365 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
368 for (ITmfXmlStateAttribute attribute
: fQueryValue
) {
369 quarkQuery
= attribute
.getAttributeQuark(event
, quarkQuery
);
370 if (quarkQuery
== IXmlStateSystemContainer
.ERROR_QUARK
) {
371 /* the query is not valid, we stop the state change */
376 * the query can fail : for example, if a value is requested but has
379 if (quarkQuery
!= IXmlStateSystemContainer
.ERROR_QUARK
) {
380 value
= ss
.queryOngoingState(quarkQuery
);
382 throw new IllegalStateException();
389 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
390 ITmfStateSystem ss
= getStateSystem();
392 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
395 ITmfStateValue incrementValue
= getValue(event
);
396 ITmfStateValue value
= incrementByType(quark
, ss
, incrementValue
);
398 processValue(quark
, timestamp
, value
);
400 Activator
.logWarning("TmfXmlStateValue: The query result increment is not a number type"); //$NON-NLS-1$
405 public String
toString() {
406 return "Query=" + fQueryValue
; //$NON-NLS-1$