ss: Add a custom state value type
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / internal / tmf / analysis / xml / core / model / readwrite / TmfXmlReadWriteStateValue.java
1 /*******************************************************************************
2 * Copyright (c) 2014 École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.internal.tmf.analysis.xml.core.model.readwrite;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
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;
38
39 /**
40 * Implements a state value in a read write mode. See {@link TmfXmlStateValue}
41 * for the syntax of the state value.
42 *
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
45 *
46 * @author Geneviève Bastien
47 */
48 public class TmfXmlReadWriteStateValue extends TmfXmlStateValue {
49
50 private static final String ILLEGAL_STATE_EXCEPTION_MESSAGE = "The state system hasn't been initialized yet"; //$NON-NLS-1$
51
52 /**
53 * Constructor where the path to the value is a list of state attributes
54 *
55 * @param modelFactory
56 * The factory used to create XML model elements
57 * @param node
58 * The state value XML element
59 * @param container
60 * The state system container this state value belongs to
61 * @param attributes
62 * The attributes representing the path to this value
63 */
64 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory, Element node, IXmlStateSystemContainer container, List<ITmfXmlStateAttribute> attributes) {
65 this(modelFactory, node, container, attributes, null);
66 }
67
68 /**
69 * Constructor where the path to the value is an event field
70 *
71 * @param modelFactory
72 * The factory used to create XML model elements
73 * @param node
74 * The state value XML element
75 * @param container
76 * The state system container this state value belongs to
77 * @param eventField
78 * The event field where to get the value
79 */
80 public TmfXmlReadWriteStateValue(TmfXmlReadWriteModelFactory modelFactory, Element node, IXmlStateSystemContainer container, String eventField) {
81 this(modelFactory, node, container, new ArrayList<ITmfXmlStateAttribute>(), eventField);
82 }
83
84 private TmfXmlReadWriteStateValue(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container, List<ITmfXmlStateAttribute> attributes, @Nullable String eventField) {
85 super(modelFactory, node, container, attributes, eventField);
86 }
87
88 @Override
89 protected @Nullable ITmfStateSystemBuilder getStateSystem() {
90 return (ITmfStateSystemBuilder) super.getStateSystem();
91 }
92
93 @Override
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));
99 if (value == null) {
100 throw new IllegalStateException();
101 }
102
103 switch (type) {
104 case TmfXmlStrings.TYPE_INT: {
105 /* Integer value */
106 ITmfStateValue stateValue = TmfStateValue.newValueInt(Integer.parseInt(value));
107 stateValueType = new TmfXmlStateValueTmf(stateValue);
108 break;
109 }
110 case TmfXmlStrings.TYPE_LONG: {
111 /* Long value */
112 ITmfStateValue stateValue = TmfStateValue.newValueLong(Long.parseLong(value));
113 stateValueType = new TmfXmlStateValueTmf(stateValue);
114 break;
115 }
116 case TmfXmlStrings.TYPE_STRING: {
117 /* String value */
118 ITmfStateValue stateValue = TmfStateValue.newValueString(value);
119 stateValueType = new TmfXmlStateValueTmf(stateValue);
120 break;
121 }
122 case TmfXmlStrings.TYPE_NULL: {
123 /* Null value */
124 ITmfStateValue stateValue = TmfStateValue.nullValue();
125 stateValueType = new TmfXmlStateValueTmf(stateValue);
126 break;
127 }
128 case TmfXmlStrings.EVENT_FIELD:
129 /* Event field */
130 stateValueType = new TmfXmlStateValueEventField(value);
131 break;
132 case TmfXmlStrings.TYPE_EVENT_NAME:
133 /* The value is the event name */
134 stateValueType = new TmfXmlStateValueEventName();
135 break;
136 case TmfXmlStrings.TYPE_DELETE:
137 /* Deletes the value of an attribute */
138 stateValueType = new TmfXmlStateValueDelete();
139 break;
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) {
145 if (child == null) {
146 continue;
147 }
148 ITmfXmlStateAttribute queryAttribute = modelFactory.createStateAttribute(child, getSsContainer());
149 childAttributes.add(queryAttribute);
150 }
151 stateValueType = new TmfXmlStateValueQuery(childAttributes);
152 break;
153 default:
154 throw new IllegalArgumentException(String.format("TmfXmlStateValue constructor: unexpected element %s for stateValue type", type)); //$NON-NLS-1$
155 }
156 return stateValueType;
157 }
158
159 // ----------------------------------------------------------
160 // Internal state value classes for the different types
161 // ----------------------------------------------------------
162
163 /**
164 * Base class for all state value. Contain default methods to handle event,
165 * process or increment the value
166 */
167 protected abstract class TmfXmlStateValueTypeReadWrite extends TmfXmlStateValueBase {
168
169 @Override
170 public final void handleEvent(ITmfEvent event, int quark, long timestamp, @Nullable TmfXmlScenarioInfo scenarioInfo) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
171 if (isIncrement()) {
172 incrementValue(event, quark, timestamp, scenarioInfo);
173 } else {
174 ITmfStateValue value = getValue(event, scenarioInfo);
175 processValue(quark, timestamp, value);
176 }
177 }
178
179 @Override
180 protected void processValue(int quark, long timestamp, ITmfStateValue value) throws AttributeNotFoundException, TimeRangeException, StateValueTypeException {
181 ITmfStateSystemBuilder ss = getStateSystem();
182 if (ss == null) {
183 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
184 }
185 switch (getStackType()) {
186 case POP:
187 ss.popAttribute(timestamp, quark);
188 break;
189 case PUSH:
190 ss.pushAttribute(timestamp, value, quark);
191 break;
192 case NULL:
193 case PEEK:
194 default:
195 ss.modifyAttribute(timestamp, value, quark);
196 break;
197 }
198 }
199
200 @Override
201 protected void incrementValue(ITmfEvent event, int quark, long timestamp, @Nullable TmfXmlScenarioInfo scenarioInfo) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
202 ITmfStateSystemBuilder ss = getStateSystem();
203 if (ss == null) {
204 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
205 }
206 StateSystemBuilderUtils.incrementAttributeInt(ss, timestamp, quark, 1);
207 }
208 }
209
210 private static @Nullable ITmfStateValue incrementByType(int quark, ITmfStateSystem ss, ITmfStateValue stateValue) throws AttributeNotFoundException {
211 ITmfStateValue value = null;
212 switch (stateValue.getType()) {
213 case LONG: {
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);
218 return value;
219 }
220 case INTEGER: {
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);
225 return value;
226 }
227 case DOUBLE:
228 case NULL:
229 case STRING:
230 case CUSTOM:
231 default:
232 }
233 return value;
234 }
235
236 /* This state value uses a constant value, defined in the XML */
237 private class TmfXmlStateValueTmf extends TmfXmlStateValueTypeReadWrite {
238
239 private final ITmfStateValue fValue;
240
241 public TmfXmlStateValueTmf(ITmfStateValue value) {
242 fValue = value;
243 }
244
245 @Override
246 public ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) {
247 return fValue;
248 }
249
250 @Override
251 public void incrementValue(ITmfEvent event, int quark, long timestamp, @Nullable TmfXmlScenarioInfo scenarioInfo) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
252 ITmfStateSystem ss = getStateSystem();
253 if (ss == null) {
254 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
255 }
256 ITmfStateValue value = incrementByType(quark, ss, fValue);
257 if (value != null) {
258 processValue(quark, timestamp, value);
259 } else {
260 Activator.logWarning("TmfXmlStateValue: The increment value is not a number type"); //$NON-NLS-1$
261 }
262 }
263
264 @Override
265 public String toString() {
266 return "Value=" + fValue; //$NON-NLS-1$
267 }
268
269 }
270
271 /* The state value uses the value of an event field */
272 private class TmfXmlStateValueEventField extends TmfXmlStateValueTypeReadWrite {
273
274 private final String fFieldName;
275
276 public TmfXmlStateValueEventField(String field) {
277 fFieldName = field;
278 }
279
280 @Override
281 public ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) {
282 if (event == null) {
283 Activator.logWarning("XML State value: requested an event field, but event is null"); //$NON-NLS-1$
284 return TmfStateValue.nullValue();
285 }
286 return getEventFieldValue(event, fFieldName);
287 }
288
289 @Override
290 public void incrementValue(ITmfEvent event, int quark, long timestamp, @Nullable TmfXmlScenarioInfo scenarioInfo) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
291 ITmfStateSystem ss = getSsContainer().getStateSystem();
292 if (ss == null) {
293 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
294 }
295 ITmfStateValue incrementValue = getValue(event, scenarioInfo);
296 ITmfStateValue value = incrementByType(quark, ss, incrementValue);
297 if (value != null) {
298 processValue(quark, timestamp, value);
299 } else {
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$
301 }
302 }
303
304 @Override
305 public String toString() {
306 return "Event Field=" + fFieldName; //$NON-NLS-1$
307 }
308 }
309
310 /* The state value is the event name */
311 private class TmfXmlStateValueEventName extends TmfXmlStateValueTypeReadWrite {
312
313 @Override
314 public @NonNull ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) throws AttributeNotFoundException {
315 if (event == null) {
316 Activator.logWarning("XML State value: request event name, but event is null"); //$NON-NLS-1$
317 return TmfStateValue.nullValue();
318 }
319 return TmfStateValue.newValueString(event.getName());
320 }
321
322 @Override
323 public String toString() {
324 return "Event name"; //$NON-NLS-1$
325 }
326 }
327
328 /* The state value deletes an attribute */
329 private class TmfXmlStateValueDelete extends TmfXmlStateValueTypeReadWrite {
330
331 @Override
332 public @NonNull ITmfStateValue getValue(@Nullable ITmfEvent event, @Nullable TmfXmlScenarioInfo scenarioInfo) throws AttributeNotFoundException {
333 return TmfStateValue.nullValue();
334 }
335
336 @Override
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$
341 }
342 ITmfStateSystemBuilder builder = (ITmfStateSystemBuilder) ss;
343 builder.removeAttribute(timestamp, quark);
344 }
345
346 @Override
347 public String toString() {
348 return "Delete"; //$NON-NLS-1$
349 }
350 }
351
352 /* The state value uses the result of a query */
353 private class TmfXmlStateValueQuery extends TmfXmlStateValueTypeReadWrite {
354
355 private final List<ITmfXmlStateAttribute> fQueryValue;
356
357 public TmfXmlStateValueQuery(List<ITmfXmlStateAttribute> childAttributes) {
358 fQueryValue = childAttributes;
359 }
360
361 @Override
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();
367 if (ss == null) {
368 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
369 }
370
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 */
375 break;
376 }
377 }
378 /*
379 * the query can fail : for example, if a value is requested but has
380 * not been set yet
381 */
382 if (quarkQuery != IXmlStateSystemContainer.ERROR_QUARK) {
383 value = ss.queryOngoingState(quarkQuery);
384 }
385 return value;
386 }
387
388 @Override
389 public void incrementValue(ITmfEvent event, int quark, long timestamp, @Nullable TmfXmlScenarioInfo scenarioInfo) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
390 ITmfStateSystem ss = getStateSystem();
391 if (ss == null) {
392 throw new IllegalStateException(ILLEGAL_STATE_EXCEPTION_MESSAGE);
393 }
394
395 ITmfStateValue incrementValue = getValue(event, scenarioInfo);
396 ITmfStateValue value = incrementByType(quark, ss, incrementValue);
397 if (value != null) {
398 processValue(quark, timestamp, value);
399 } else {
400 Activator.logWarning("TmfXmlStateValue: The query result increment is not a number type"); //$NON-NLS-1$
401 }
402 }
403
404 @Override
405 public String toString() {
406 return "Query=" + fQueryValue; //$NON-NLS-1$
407 }
408 }
409
410 }
This page took 0.040354 seconds and 6 git commands to generate.