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