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
.NonNull
;
19 import org
.eclipse
.jdt
.annotation
.Nullable
;
20 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.Activator
;
21 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
22 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
23 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemBuilderUtils
;
24 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
26 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
27 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
29 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
30 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
31 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.TmfXmlScenarioInfo
;
32 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
.TmfXmlStateValue
;
33 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
34 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
35 import org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
37 import org
.w3c
.dom
.Element
;
40 * Implements a state value in a read write mode. See {@link TmfXmlStateValue}
41 * for the syntax of the state value.
43 * In read/write mode, a state value can be considered as an assignation where
44 * the state value is assigned to the quark represented by the state attributes
46 * @author Geneviève Bastien
48 public class TmfXmlReadWriteStateValue
extends TmfXmlStateValue
{
50 private static final String ILLEGAL_STATE_EXCEPTION_MESSAGE
= "The state system hasn't been initialized yet"; //$NON-NLS-1$
53 * Constructor where the path to the value is a list of state attributes
56 * The factory used to create XML model elements
58 * The state value XML element
60 * The state system container this state value belongs to
62 * The attributes representing the path to this value
64 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
) {
65 this(modelFactory
, node
, container
, attributes
, null);
69 * Constructor where the path to the value is an event field
72 * The factory used to create XML model elements
74 * The state value XML element
76 * The state system container this state value belongs to
78 * The event field where to get the value
80 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, String eventField
) {
81 this(modelFactory
, node
, container
, new ArrayList
<ITmfXmlStateAttribute
>(), eventField
);
84 private TmfXmlReadWriteStateValue(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
, List
<ITmfXmlStateAttribute
> attributes
, @Nullable String eventField
) {
85 super(modelFactory
, node
, container
, attributes
, eventField
);
89 protected @Nullable ITmfStateSystemBuilder
getStateSystem() {
90 return (ITmfStateSystemBuilder
) super.getStateSystem();
94 protected TmfXmlStateValueBase
initializeStateValue(ITmfXmlModelFactory modelFactory
, Element node
) {
95 TmfXmlStateValueBase stateValueType
= null;
96 /* Process the XML Element state value */
97 String type
= node
.getAttribute(TmfXmlStrings
.TYPE
);
98 String value
= getSsContainer().getAttributeValue(node
.getAttribute(TmfXmlStrings
.VALUE
));
100 throw new IllegalStateException();
104 case TmfXmlStrings
.TYPE_INT
: {
106 ITmfStateValue stateValue
= TmfStateValue
.newValueInt(Integer
.parseInt(value
));
107 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
110 case TmfXmlStrings
.TYPE_LONG
: {
112 ITmfStateValue stateValue
= TmfStateValue
.newValueLong(Long
.parseLong(value
));
113 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
116 case TmfXmlStrings
.TYPE_STRING
: {
118 ITmfStateValue stateValue
= TmfStateValue
.newValueString(value
);
119 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
122 case TmfXmlStrings
.TYPE_NULL
: {
124 ITmfStateValue stateValue
= TmfStateValue
.nullValue();
125 stateValueType
= new TmfXmlStateValueTmf(stateValue
);
128 case TmfXmlStrings
.EVENT_FIELD
:
130 stateValueType
= new TmfXmlStateValueEventField(value
);
132 case TmfXmlStrings
.TYPE_EVENT_NAME
:
133 /* The value is the event name */
134 stateValueType
= new TmfXmlStateValueEventName();
136 case TmfXmlStrings
.TYPE_DELETE
:
137 /* Deletes the value of an attribute */
138 stateValueType
= new TmfXmlStateValueDelete();
140 case TmfXmlStrings
.TYPE_QUERY
:
141 /* Value is the result of a query */
142 List
<@Nullable Element
> children
= XmlUtils
.getChildElements(node
);
143 List
<ITmfXmlStateAttribute
> childAttributes
= new ArrayList
<>();
144 for (Element child
: children
) {
148 ITmfXmlStateAttribute queryAttribute
= modelFactory
.createStateAttribute(child
, getSsContainer());
149 childAttributes
.add(queryAttribute
);
151 stateValueType
= new TmfXmlStateValueQuery(childAttributes
);
154 throw new IllegalArgumentException(String
.format("TmfXmlStateValue constructor: unexpected element %s for stateValue type", type
)); //$NON-NLS-1$
156 return stateValueType
;
159 // ----------------------------------------------------------
160 // Internal state value classes for the different types
161 // ----------------------------------------------------------
164 * Base class for all state value. Contain default methods to handle event,
165 * process or increment the value
167 protected abstract class TmfXmlStateValueTypeReadWrite
extends TmfXmlStateValueBase
{
170 public final void handleEvent(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
172 incrementValue(event
, quark
, timestamp
, scenarioInfo
);
174 ITmfStateValue value
= getValue(event
, scenarioInfo
);
175 processValue(quark
, timestamp
, value
);
180 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws AttributeNotFoundException
, TimeRangeException
, StateValueTypeException
{
181 ITmfStateSystemBuilder ss
= getStateSystem();
183 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
185 switch (getStackType()) {
187 ss
.popAttribute(timestamp
, quark
);
190 ss
.pushAttribute(timestamp
, value
, quark
);
195 ss
.modifyAttribute(timestamp
, value
, quark
);
201 protected void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
202 ITmfStateSystemBuilder ss
= getStateSystem();
204 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
206 StateSystemBuilderUtils
.incrementAttributeInt(ss
, timestamp
, quark
, 1);
210 private static @Nullable ITmfStateValue
incrementByType(int quark
, ITmfStateSystem ss
, ITmfStateValue stateValue
) throws AttributeNotFoundException
{
211 ITmfStateValue value
= null;
212 switch (stateValue
.getType()) {
214 long incrementLong
= stateValue
.unboxLong();
215 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
216 long currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxLong());
217 value
= TmfStateValue
.newValueLong(incrementLong
+ currentValue
);
221 int increment
= stateValue
.unboxInt();
222 ITmfStateValue currentState
= ss
.queryOngoingState(quark
);
223 int currentValue
= (currentState
.isNull() ?
0 : currentState
.unboxInt());
224 value
= TmfStateValue
.newValueInt(increment
+ currentValue
);
235 /* This state value uses a constant value, defined in the XML */
236 private class TmfXmlStateValueTmf
extends TmfXmlStateValueTypeReadWrite
{
238 private final ITmfStateValue fValue
;
240 public TmfXmlStateValueTmf(ITmfStateValue value
) {
245 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) {
250 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
251 ITmfStateSystem ss
= getStateSystem();
253 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
255 ITmfStateValue value
= incrementByType(quark
, ss
, fValue
);
257 processValue(quark
, timestamp
, value
);
259 Activator
.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
264 public String
toString() {
265 return "Value=" + fValue
; //$NON-NLS-1$
270 /* The state value uses the value of an event field */
271 private class TmfXmlStateValueEventField
extends TmfXmlStateValueTypeReadWrite
{
273 private final String fFieldName
;
275 public TmfXmlStateValueEventField(String field
) {
280 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) {
282 Activator
.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
283 return TmfStateValue
.nullValue();
285 return getEventFieldValue(event
, fFieldName
);
289 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
290 ITmfStateSystem ss
= getSsContainer().getStateSystem();
292 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
294 ITmfStateValue incrementValue
= getValue(event
, scenarioInfo
);
295 ITmfStateValue value
= incrementByType(quark
, ss
, incrementValue
);
297 processValue(quark
, timestamp
, value
);
299 Activator
.logWarning(String
.format("TmfXmlStateValue: The event field increment %s is not a number type but a %s", fFieldName
, incrementValue
.getType())); //$NON-NLS-1$
304 public String
toString() {
305 return "Event Field=" + fFieldName
; //$NON-NLS-1$
309 /* The state value is the event name */
310 private class TmfXmlStateValueEventName
extends TmfXmlStateValueTypeReadWrite
{
313 public @NonNull ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
315 Activator
.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
316 return TmfStateValue
.nullValue();
318 return TmfStateValue
.newValueString(event
.getName());
322 public String
toString() {
323 return "Event name"; //$NON-NLS-1$
327 /* The state value deletes an attribute */
328 private class TmfXmlStateValueDelete
extends TmfXmlStateValueTypeReadWrite
{
331 public @NonNull ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
332 return TmfStateValue
.nullValue();
336 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, AttributeNotFoundException
{
337 ITmfStateSystem ss
= getStateSystem();
338 if (!(ss
instanceof ITmfStateSystemBuilder
)) {
339 throw new IllegalStateException("incrementValue should never be called when not building the state system"); //$NON-NLS-1$
341 ITmfStateSystemBuilder builder
= (ITmfStateSystemBuilder
) ss
;
342 builder
.removeAttribute(timestamp
, quark
);
346 public String
toString() {
347 return "Delete"; //$NON-NLS-1$
351 /* The state value uses the result of a query */
352 private class TmfXmlStateValueQuery
extends TmfXmlStateValueTypeReadWrite
{
354 private final List
<ITmfXmlStateAttribute
> fQueryValue
;
356 public TmfXmlStateValueQuery(List
<ITmfXmlStateAttribute
> childAttributes
) {
357 fQueryValue
= childAttributes
;
361 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
362 /* Query the state system for the value */
363 ITmfStateValue value
= TmfStateValue
.nullValue();
364 int quarkQuery
= IXmlStateSystemContainer
.ROOT_QUARK
;
365 ITmfStateSystem ss
= getStateSystem();
367 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
370 for (ITmfXmlStateAttribute attribute
: fQueryValue
) {
371 quarkQuery
= attribute
.getAttributeQuark(event
, quarkQuery
, scenarioInfo
);
372 if (quarkQuery
== IXmlStateSystemContainer
.ERROR_QUARK
) {
373 /* the query is not valid, we stop the state change */
378 * the query can fail : for example, if a value is requested but has
381 if (quarkQuery
!= IXmlStateSystemContainer
.ERROR_QUARK
) {
382 value
= ss
.queryOngoingState(quarkQuery
);
388 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
389 ITmfStateSystem ss
= getStateSystem();
391 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
394 ITmfStateValue incrementValue
= getValue(event
, scenarioInfo
);
395 ITmfStateValue value
= incrementByType(quark
, ss
, incrementValue
);
397 processValue(quark
, timestamp
, value
);
399 Activator
.logWarning("TmfXmlStateValue: The query result increment is not a number type"); //$NON-NLS-1$
404 public String
toString() {
405 return "Query=" + fQueryValue
; //$NON-NLS-1$