TMF: Refactor XML model code, using factories to re-use element parsers
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.analysis.xml.core / src / org / eclipse / linuxtools / tmf / analysis / xml / core / model / TmfXmlStateValue.java
1 /*******************************************************************************
2 * Copyright (c) 2014 Ecole Polytechnique de Montreal
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 * Florian Wininger - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.analysis.xml.core.model;
14
15 import java.util.List;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.linuxtools.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
20 import org.eclipse.linuxtools.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
21 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
23 import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
24 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
25 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
26 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
27 import org.eclipse.linuxtools.tmf.core.statevalue.ITmfStateValue;
28 import org.eclipse.linuxtools.tmf.core.statevalue.TmfStateValue;
29 import org.w3c.dom.Element;
30
31 /**
32 * This Class implements a State Value in the XML-defined state system, along
33 * with the path to get to the value (either a list of state attributes or an
34 * event field)
35 *
36 * <pre>
37 * Example:
38 * <stateAttribute type="location" value="CurrentThread" />
39 * <stateAttribute type="constant" value="System_call" />
40 * <stateValue type="null" />
41 * </pre>
42 *
43 * @author Florian Wininger
44 */
45 public abstract class TmfXmlStateValue implements ITmfXmlStateValue {
46
47 private final TmfXmlStateValueBase fStateValue;
48
49 /* Path in the State System */
50 private final List<ITmfXmlStateAttribute> fPath;
51 /* Event field to match with this state value */
52 private final String fEventField;
53
54 /* Whether this state value is an increment of the previous value */
55 private final boolean fIncrement;
56 /* Stack value */
57 private final ValueTypeStack fStackType;
58 /* Forced value type */
59 private final ITmfStateValue.Type fForcedType;
60
61 private final IXmlStateSystemContainer fContainer;
62
63 /**
64 * Different behaviors of an attribute that is to be stacked
65 */
66 protected enum ValueTypeStack {
67 /** Not stacked */
68 NULL,
69 /** Peek at the value at the top of the stack */
70 PEEK,
71 /** Take the value at the top of the stack */
72 POP,
73 /** Push the value on the stack */
74 PUSH;
75
76 /**
77 * Get the type stack value corresponding to a string
78 *
79 * @param input
80 * The string to match to a value
81 * @return The ValueTypeStack value
82 */
83 public static ValueTypeStack getTypeFromString(String input) {
84 switch (input) {
85 case TmfXmlStrings.STACK_PUSH:
86 return PUSH;
87 case TmfXmlStrings.STACK_POP:
88 return POP;
89 case TmfXmlStrings.STACK_PEEK:
90 return PEEK;
91 default:
92 return NULL;
93 }
94 }
95 }
96
97 /**
98 * Constructor
99 *
100 * @param modelFactory
101 * The factory used to create XML model elements
102 * @param node
103 * The state value XML element
104 * @param container
105 * The state system container this state value belongs to
106 * @param eventField
107 * The event field where to get the value
108 * @param attributes
109 * The attributes representing the path to this value
110 */
111 protected TmfXmlStateValue(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container, List<ITmfXmlStateAttribute> attributes, String eventField) {
112 fPath = attributes;
113 fContainer = container;
114 fEventField = eventField;
115 if (!node.getNodeName().equals(TmfXmlStrings.STATE_VALUE)) {
116 throw new IllegalArgumentException("TmfXmlStateValue constructor: Element is not a stateValue"); //$NON-NLS-1$
117 }
118
119 /* Check if there is an increment for the value */
120 fIncrement = Boolean.parseBoolean(node.getAttribute(TmfXmlStrings.INCREMENT));
121
122 /* Process the XML Element state value */
123 fStateValue = initializeStateValue(modelFactory, node);
124
125 /*
126 * Forced type allows to convert the value to a certain type : For
127 * example, a process's TID in an event field may arrive with a LONG
128 * format but we want to store the data in an INT
129 */
130 switch (node.getAttribute(TmfXmlStrings.FORCED_TYPE)) {
131 case TmfXmlStrings.TYPE_STRING:
132 fForcedType = ITmfStateValue.Type.STRING;
133 break;
134 case TmfXmlStrings.TYPE_INT:
135 fForcedType = ITmfStateValue.Type.INTEGER;
136 break;
137 case TmfXmlStrings.TYPE_LONG:
138 fForcedType = ITmfStateValue.Type.LONG;
139 break;
140 default:
141 fForcedType = ITmfStateValue.Type.NULL;
142 }
143
144 /*
145 * Stack Actions : allow to define a stack with PUSH/POP/PEEK methods
146 */
147 String stack = node.getAttribute(TmfXmlStrings.ATTRIBUTE_STACK);
148 fStackType = ValueTypeStack.getTypeFromString(stack);
149 }
150
151 /**
152 * Initialize a {@link TmfXmlStateValueBase} object for the type and value
153 *
154 * @param modelFactory
155 * The factory used to create XML model elements
156 * @param node
157 * The state value XML element
158 * @return The internal state value type corresponding to this state value
159 */
160 protected TmfXmlStateValueBase initializeStateValue(ITmfXmlModelFactory modelFactory, Element node) {
161 return new TmfXmlStateValueNull();
162 }
163
164 /**
165 * Return the state system container this class is attached to
166 *
167 * @return The state system container
168 */
169 protected IXmlStateSystemContainer getSsContainer() {
170 return fContainer;
171 }
172
173 /**
174 * Get the state system associated with this value's container
175 *
176 * @return The state system associated with the state system container
177 */
178 protected ITmfStateSystem getStateSystem() {
179 return fContainer.getStateSystem();
180 }
181
182 /**
183 * Return whether this value is an increment of the previous value
184 *
185 * @return <code>true</code> if the value is an increment
186 */
187 protected boolean isIncrement() {
188 return fIncrement;
189 }
190
191 /**
192 * Get the stack type of this attribute. If the attribute is to be pushed or
193 * popped to a stack. The behavior of the stack attribute will depend on the
194 * implementation of the model.
195 *
196 * @return The stack type of the attribute
197 */
198 protected ValueTypeStack getStackType() {
199 return fStackType;
200 }
201
202 /**
203 * Get the forced type of the value. For example, if the value obtained from
204 * the attributes is not in this forced type, it will be converted to this.
205 *
206 * @return The desired type of the value
207 */
208 protected ITmfStateValue.Type getForcedType() {
209 return fForcedType;
210 }
211
212 /**
213 * Get the current {@link ITmfStateValue} of this state value for an event.
214 * It does not increment the value and does not any other processing of the
215 * value.
216 *
217 * @param event
218 * The current event, or <code>null</code> if no event available.
219 * @return the {@link ITmfStateValue}
220 * @throws AttributeNotFoundException
221 * May be thrown by the state system during the query
222 */
223 @Override
224 public ITmfStateValue getValue(@Nullable ITmfEvent event) throws AttributeNotFoundException {
225 return fStateValue.getValue(event);
226 }
227
228 /**
229 * Get the value of the event field that is the path of this state value
230 *
231 * @param event
232 * The current event
233 * @return the value of the event field
234 */
235 @Override
236 public ITmfStateValue getEventFieldValue(@NonNull ITmfEvent event) {
237 return getEventFieldValue(event, fEventField);
238 }
239
240 /**
241 * Get the value of an event field
242 *
243 * @param event
244 * The current event
245 * @param fieldName
246 * The name of the field of which to get the value
247 * @return The value of the event field
248 */
249 protected ITmfStateValue getEventFieldValue(@NonNull ITmfEvent event, String fieldName) {
250
251 ITmfStateValue value = TmfStateValue.nullValue();
252
253 final ITmfEventField content = event.getContent();
254
255 /* Exception for "CPU", returns the source of this event */
256 /* FIXME : Nameclash if a eventfield have "cpu" for name. */
257 if (fieldName.equals(TmfXmlStrings.CPU)) {
258 return TmfStateValue.newValueInt(Integer.valueOf(event.getSource()));
259 }
260 if (content.getField(fieldName) == null) {
261 return value;
262 }
263
264 Object field = content.getField(fieldName).getValue();
265
266 /*
267 * Try to find the right type. The type can be forced by
268 * "forcedType" argument.
269 */
270
271 if (field instanceof String) {
272 String fieldString = (String) field;
273
274 switch (fForcedType) {
275 case INTEGER:
276 value = TmfStateValue.newValueInt(Integer.parseInt(fieldString));
277 break;
278 case LONG:
279 value = TmfStateValue.newValueLong(Long.parseLong(fieldString));
280 break;
281 case DOUBLE:
282 value = TmfStateValue.newValueDouble(Double.parseDouble(fieldString));
283 break;
284 case NULL:
285 case STRING:
286 default:
287 value = TmfStateValue.newValueString(fieldString);
288 break;
289 }
290 } else if (field instanceof Long) {
291 Long fieldLong = (Long) field;
292
293 switch (fForcedType) {
294 case INTEGER:
295 value = TmfStateValue.newValueInt(fieldLong.intValue());
296 break;
297 case STRING:
298 value = TmfStateValue.newValueString(fieldLong.toString());
299 break;
300 case DOUBLE:
301 value = TmfStateValue.newValueDouble(fieldLong.doubleValue());
302 break;
303 case LONG:
304 case NULL:
305 default:
306 value = TmfStateValue.newValueLong(fieldLong);
307 break;
308 }
309 } else if (field instanceof Integer) {
310 Integer fieldInteger = (Integer) field;
311
312 switch (fForcedType) {
313 case LONG:
314 value = TmfStateValue.newValueLong(fieldInteger.longValue());
315 break;
316 case STRING:
317 value = TmfStateValue.newValueString(fieldInteger.toString());
318 break;
319 case DOUBLE:
320 value = TmfStateValue.newValueDouble(fieldInteger.doubleValue());
321 break;
322 case INTEGER:
323 case NULL:
324 default:
325 value = TmfStateValue.newValueInt(fieldInteger);
326 break;
327 }
328 } else if (field instanceof Double) {
329 Double fieldDouble = (Double) field;
330
331 switch (fForcedType) {
332 case LONG:
333 value = TmfStateValue.newValueLong(fieldDouble.longValue());
334 break;
335 case STRING:
336 value = TmfStateValue.newValueString(fieldDouble.toString());
337 break;
338 case INTEGER:
339 value = TmfStateValue.newValueInt(fieldDouble.intValue());
340 break;
341 case DOUBLE:
342 case NULL:
343 default:
344 value = TmfStateValue.newValueDouble(fieldDouble);
345 break;
346 }
347 }
348 return value;
349 }
350
351 /**
352 * Get the list of state attributes, the path to the state value
353 *
354 * @return the list of Attribute to have the path in the State System
355 */
356 @Override
357 public List<ITmfXmlStateAttribute> getAttributes() {
358 return fPath;
359 }
360
361 /**
362 * Handles an event, by setting the value of the attribute described by the
363 * state attribute path in the state system.
364 *
365 * @param event
366 * The event to process
367 * @throws AttributeNotFoundException
368 * Pass through the exception it received
369 * @throws TimeRangeException
370 * Pass through the exception it received
371 * @throws StateValueTypeException
372 * Pass through the exception it received
373 */
374 @Override
375 public void handleEvent(@NonNull ITmfEvent event) throws AttributeNotFoundException, StateValueTypeException, TimeRangeException {
376 int quark = IXmlStateSystemContainer.ROOT_QUARK;
377
378 for (ITmfXmlStateAttribute attribute : fPath) {
379 quark = attribute.getAttributeQuark(event, quark);
380 /* the query is not valid, we stop the state change */
381 if (quark == IXmlStateSystemContainer.ERROR_QUARK) {
382 throw new AttributeNotFoundException();
383 }
384 }
385
386 long ts = event.getTimestamp().getValue();
387 fStateValue.handleEvent(event, quark, ts);
388 }
389
390 /**
391 * Base class for all state values. Contain default methods to handle event,
392 * process or increment the value
393 */
394 protected abstract class TmfXmlStateValueBase {
395
396 /**
397 * Get the value associated with this state value.
398 *
399 * @param event
400 * The event which can be used to retrieve the value if
401 * necessary. The event can be <code>null</code> if no event
402 * is required.
403 * @return The state value corresponding to this XML state value
404 * @throws AttributeNotFoundException
405 * Pass through the exception it received
406 */
407 public abstract ITmfStateValue getValue(@Nullable ITmfEvent event) throws AttributeNotFoundException;
408
409 /**
410 * Do something with the state value, possibly using an event
411 *
412 * @param event
413 * The event being handled. If there is no event is
414 * available, use <code>null</code>.
415 * @param quark
416 * The quark for this value
417 * @param timestamp
418 * The timestamp of the event
419 * @throws StateValueTypeException
420 * Pass through the exception it received
421 * @throws TimeRangeException
422 * Pass through the exception it received
423 * @throws AttributeNotFoundException
424 * Pass through the exception it received
425 */
426 public void handleEvent(@NonNull ITmfEvent event, int quark, long timestamp) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
427 if (fIncrement) {
428 incrementValue(event, quark, timestamp);
429 } else {
430 ITmfStateValue value = getValue(event);
431 processValue(quark, timestamp, value);
432 }
433 }
434
435 /**
436 * Set the value of a quark at a given timestamp.
437 *
438 * @param quark
439 * The quark for this value
440 * @param timestamp
441 * The timestamp
442 * @param value
443 * The value of this state value
444 * @throws TimeRangeException
445 * Pass through the exception it received
446 * @throws StateValueTypeException
447 * Pass through the exception it received
448 * @throws AttributeNotFoundException
449 * Pass through the exception it received
450 */
451 @SuppressWarnings("unused")
452 protected void processValue(int quark, long timestamp, ITmfStateValue value) throws TimeRangeException, StateValueTypeException, AttributeNotFoundException {
453 }
454
455 /**
456 * Increments the value of the parameter
457 *
458 * @param event
459 * The event being handled
460 * @param quark
461 * The quark for this value
462 * @param timestamp
463 * The timestamp of the event
464 * @throws StateValueTypeException
465 * Pass through the exception it received
466 * @throws TimeRangeException
467 * Pass through the exception it received
468 * @throws AttributeNotFoundException
469 * Pass through the exception it received
470 */
471 @SuppressWarnings("unused")
472 protected void incrementValue(ITmfEvent event, int quark, long timestamp) throws StateValueTypeException, TimeRangeException, AttributeNotFoundException {
473 }
474 }
475
476 /* This state value uses a constant value, defined in the XML */
477 private class TmfXmlStateValueNull extends TmfXmlStateValueBase {
478
479 @Override
480 public ITmfStateValue getValue(@Nullable ITmfEvent event) throws AttributeNotFoundException {
481 return TmfStateValue.nullValue();
482 }
483
484 }
485
486 }
This page took 0.0428 seconds and 6 git commands to generate.