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