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
.internal
.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
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlModelFactory
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.ITmfXmlStateAttribute
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.TmfXmlScenarioInfo
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.model
.TmfXmlStateValue
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.IXmlStateSystemContainer
;
26 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.module
.XmlUtils
;
27 import org
.eclipse
.tracecompass
.internal
.tmf
.analysis
.xml
.core
.stateprovider
.TmfXmlStrings
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystemBuilder
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemBuilderUtils
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.TmfStateValue
;
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
);
236 /* This state value uses a constant value, defined in the XML */
237 private class TmfXmlStateValueTmf
extends TmfXmlStateValueTypeReadWrite
{
239 private final ITmfStateValue fValue
;
241 public TmfXmlStateValueTmf(ITmfStateValue value
) {
246 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) {
251 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
252 ITmfStateSystem ss
= getStateSystem();
254 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
256 ITmfStateValue value
= incrementByType(quark
, ss
, fValue
);
258 processValue(quark
, timestamp
, value
);
260 Activator
.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
265 public String
toString() {
266 return "Value=" + fValue
; //$NON-NLS-1$
271 /* The state value uses the value of an event field */
272 private class TmfXmlStateValueEventField
extends TmfXmlStateValueTypeReadWrite
{
274 private final String fFieldName
;
276 public TmfXmlStateValueEventField(String field
) {
281 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) {
283 Activator
.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
284 return TmfStateValue
.nullValue();
286 return getEventFieldValue(event
, fFieldName
);
290 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
291 ITmfStateSystem ss
= getSsContainer().getStateSystem();
293 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
295 ITmfStateValue incrementValue
= getValue(event
, scenarioInfo
);
296 ITmfStateValue value
= incrementByType(quark
, ss
, incrementValue
);
298 processValue(quark
, timestamp
, value
);
300 Activator
.logWarning(String
.format("TmfXmlStateValue: The event field increment %s is not a number type but a %s", fFieldName
, incrementValue
.getType())); //$NON-NLS-1$
305 public String
toString() {
306 return "Event Field=" + fFieldName
; //$NON-NLS-1$
310 /* The state value is the event name */
311 private class TmfXmlStateValueEventName
extends TmfXmlStateValueTypeReadWrite
{
314 public @NonNull ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
316 Activator
.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
317 return TmfStateValue
.nullValue();
319 return TmfStateValue
.newValueString(event
.getName());
323 public String
toString() {
324 return "Event name"; //$NON-NLS-1$
328 /* The state value deletes an attribute */
329 private class TmfXmlStateValueDelete
extends TmfXmlStateValueTypeReadWrite
{
332 public @NonNull ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
333 return TmfStateValue
.nullValue();
337 protected void processValue(int quark
, long timestamp
, ITmfStateValue value
) throws TimeRangeException
, AttributeNotFoundException
{
338 ITmfStateSystem ss
= getStateSystem();
339 if (!(ss
instanceof ITmfStateSystemBuilder
)) {
340 throw new IllegalStateException("incrementValue should never be called when not building the state system"); //$NON-NLS-1$
342 ITmfStateSystemBuilder builder
= (ITmfStateSystemBuilder
) ss
;
343 builder
.removeAttribute(timestamp
, quark
);
347 public String
toString() {
348 return "Delete"; //$NON-NLS-1$
352 /* The state value uses the result of a query */
353 private class TmfXmlStateValueQuery
extends TmfXmlStateValueTypeReadWrite
{
355 private final List
<ITmfXmlStateAttribute
> fQueryValue
;
357 public TmfXmlStateValueQuery(List
<ITmfXmlStateAttribute
> childAttributes
) {
358 fQueryValue
= childAttributes
;
362 public ITmfStateValue
getValue(@Nullable ITmfEvent event
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws AttributeNotFoundException
{
363 /* Query the state system for the value */
364 ITmfStateValue value
= TmfStateValue
.nullValue();
365 int quarkQuery
= IXmlStateSystemContainer
.ROOT_QUARK
;
366 ITmfStateSystem ss
= getStateSystem();
368 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
371 for (ITmfXmlStateAttribute attribute
: fQueryValue
) {
372 quarkQuery
= attribute
.getAttributeQuark(event
, quarkQuery
, scenarioInfo
);
373 if (quarkQuery
== IXmlStateSystemContainer
.ERROR_QUARK
) {
374 /* the query is not valid, we stop the state change */
379 * the query can fail : for example, if a value is requested but has
382 if (quarkQuery
!= IXmlStateSystemContainer
.ERROR_QUARK
) {
383 value
= ss
.queryOngoingState(quarkQuery
);
389 public void incrementValue(ITmfEvent event
, int quark
, long timestamp
, @Nullable TmfXmlScenarioInfo scenarioInfo
) throws StateValueTypeException
, TimeRangeException
, AttributeNotFoundException
{
390 ITmfStateSystem ss
= getStateSystem();
392 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE
);
395 ITmfStateValue incrementValue
= getValue(event
, scenarioInfo
);
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$