tmf : fix the 'Not' condition in TmfXmlCondition
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.analysis.xml.core / src / org / eclipse / tracecompass / tmf / analysis / xml / core / model / TmfXmlCondition.java
CommitLineData
0f7276b6 1/*******************************************************************************
b39b8c5b 2 * Copyright (c) 2014, 2015 Ecole Polytechnique de Montreal and others
0f7276b6
GB
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
5ddeee68 11 * Naser Ezzati - Add the comparison operators
e13bd4cd 12 * Patrick Tasse - Add message to exceptions
b39b8c5b 13 * Jean-Christian Kouame - Add comparison between two state values
0f7276b6
GB
14 ******************************************************************************/
15
2bdf0193 16package org.eclipse.tracecompass.tmf.analysis.xml.core.model;
0f7276b6
GB
17
18import java.util.ArrayList;
19import java.util.List;
20
12685851 21import org.eclipse.jdt.annotation.Nullable;
b39b8c5b 22import org.eclipse.tracecompass.common.core.NonNullUtils;
e894a508
AM
23import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
24import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
25import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
2bdf0193
AM
26import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer;
27import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils;
28import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings;
29import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
0f7276b6
GB
30import org.w3c.dom.Element;
31
32/**
1d7e62f9 33 * This Class implement a condition tree in the XML-defined state system.
0f7276b6
GB
34 *
35 * <pre>
36 * example:
37 * <and>
38 * <condition>
39 * <stateAttribute type="location" value="CurrentThread" />
40 * <stateAttribute type="constant" value="System_call" />
41 * <stateValue type="null" />
42 * </condition>
43 * <condition>
b39b8c5b
JCK
44 * <stateValue type="long" value="2" />
45 * <stateValue type="long" value="5" />
0f7276b6
GB
46 * </condition>
47 * </and>
48 * </pre>
49 *
50 * @author Florian Wininger
51 */
52public class TmfXmlCondition {
53
54 private final List<TmfXmlCondition> fConditions = new ArrayList<>();
b39b8c5b 55 private final List<ITmfXmlStateValue> fStateValues;
5ddeee68 56 private final LogicalOperator fOperator;
1d7e62f9 57 private final IXmlStateSystemContainer fContainer;
5ddeee68 58 private final ConditionOperator fConditionOperator;
0f7276b6 59
5ddeee68 60 private enum LogicalOperator {
0f7276b6
GB
61 NONE,
62 NOT,
63 AND,
64 OR,
65 }
66
5ddeee68
NE
67 private enum ConditionOperator {
68 NONE,
69 EQ,
70 NE,
71 GE,
72 GT,
73 LE,
74 LT
75 }
76
0f7276b6
GB
77 /**
78 * Constructor
79 *
1d7e62f9
GB
80 * @param modelFactory
81 * The factory used to create XML model elements
0f7276b6
GB
82 * @param node
83 * The XML root of this condition
1d7e62f9
GB
84 * @param container
85 * The state system container this condition belongs to
0f7276b6 86 */
1d7e62f9
GB
87 public TmfXmlCondition(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container) {
88 fContainer = container;
0f7276b6
GB
89
90 Element rootNode = node;
91 /* Process the conditions: in each case, only process Element nodes */
4c4e2816 92 List<@Nullable Element> childElements = XmlUtils.getChildElements(rootNode);
0f7276b6
GB
93
94 /*
95 * If the node is an if, take the child as the root condition
96 *
97 * FIXME: Maybe the caller should do this instead.
98 */
99 if (node.getNodeName().equals(TmfXmlStrings.IF)) {
100 if (childElements.isEmpty()) {
b39b8c5b 101 throw new IllegalArgumentException("TmfXmlCondition constructor: IF node with no child element"); //$NON-NLS-1$
0f7276b6 102 }
f0fd2231 103 rootNode = NonNullUtils.checkNotNull(childElements.get(0));
0f7276b6
GB
104 childElements = XmlUtils.getChildElements(rootNode);
105 }
4c4e2816 106
0f7276b6
GB
107 switch (rootNode.getNodeName()) {
108 case TmfXmlStrings.CONDITION:
1010de5e
JCK
109 int size = rootNode.getElementsByTagName(TmfXmlStrings.STATE_VALUE).getLength();
110 fStateValues = new ArrayList<>(size);
5ddeee68 111 fOperator = LogicalOperator.NONE;
b39b8c5b
JCK
112 if (size == 1) {
113 fConditionOperator = getConditionOperator(rootNode);
114 getStateValuesForXmlCondition(modelFactory, NonNullUtils.checkNotNull(childElements));
0f7276b6 115 } else {
b39b8c5b
JCK
116 fConditionOperator = ConditionOperator.EQ;
117 fStateValues.add(modelFactory.createStateValue(NonNullUtils.checkNotNull(childElements.get(0)), fContainer, new ArrayList<ITmfXmlStateAttribute>()));
118 fStateValues.add(modelFactory.createStateValue(NonNullUtils.checkNotNull(childElements.get(1)), fContainer, new ArrayList<ITmfXmlStateAttribute>()));
0f7276b6
GB
119 }
120 break;
121 case TmfXmlStrings.NOT:
1010de5e 122 fStateValues = new ArrayList<>();
5ddeee68 123 fOperator = LogicalOperator.NOT;
5ddeee68 124 fConditionOperator = ConditionOperator.NONE;
f0fd2231 125 Element element = NonNullUtils.checkNotNull(childElements.get(0));
12685851 126 fConditions.add(modelFactory.createCondition(element, fContainer));
0f7276b6
GB
127 break;
128 case TmfXmlStrings.AND:
1010de5e 129 fStateValues = new ArrayList<>();
5ddeee68 130 fOperator = LogicalOperator.AND;
5ddeee68 131 fConditionOperator = ConditionOperator.NONE;
0f7276b6 132 for (Element condition : childElements) {
12685851
GB
133 if (condition == null) {
134 continue;
135 }
1d7e62f9 136 fConditions.add(modelFactory.createCondition(condition, fContainer));
0f7276b6
GB
137 }
138 break;
139 case TmfXmlStrings.OR:
1010de5e 140 fStateValues = new ArrayList<>();
5ddeee68 141 fOperator = LogicalOperator.OR;
5ddeee68 142 fConditionOperator = ConditionOperator.NONE;
0f7276b6 143 for (Element condition : childElements) {
12685851
GB
144 if (condition == null) {
145 continue;
146 }
1d7e62f9 147 fConditions.add(modelFactory.createCondition(condition, fContainer));
0f7276b6
GB
148 }
149 break;
150 default:
151 throw new IllegalArgumentException("TmfXmlCondition constructor: XML node is of the wrong type"); //$NON-NLS-1$
152 }
153 }
154
b39b8c5b
JCK
155 private void getStateValuesForXmlCondition(ITmfXmlModelFactory modelFactory, List<@Nullable Element> childElements) {
156 Element stateValueElement = NonNullUtils.checkNotNull(childElements.remove(childElements.size() - 1));
157 /*
158 * A state value is either preceded by an eventField or a number of
159 * state attributes
160 */
161 final Element firstElement = NonNullUtils.checkNotNull(childElements.get(0));
162 if (childElements.size() == 1 && firstElement.getNodeName().equals(TmfXmlStrings.ELEMENT_FIELD)) {
163 String attribute = firstElement.getAttribute(TmfXmlStrings.NAME);
164 fStateValues.add(modelFactory.createStateValue(stateValueElement, fContainer, attribute));
165 } else {
166 List<ITmfXmlStateAttribute> attributes = new ArrayList<>();
167 for (Element element : childElements) {
168 if (element == null) {
169 throw new NullPointerException("There should be at list one element"); //$NON-NLS-1$
170 }
171 if (!element.getNodeName().equals(TmfXmlStrings.STATE_ATTRIBUTE)) {
172 throw new IllegalArgumentException("TmfXmlCondition: a condition either has a eventField element or a number of TmfXmlStateAttribute elements before the state value"); //$NON-NLS-1$
173 }
174 ITmfXmlStateAttribute attribute = modelFactory.createStateAttribute(element, fContainer);
175 attributes.add(attribute);
176 }
177 fStateValues.add(modelFactory.createStateValue(stateValueElement, fContainer, attributes));
178 }
179 }
180
181 private static ConditionOperator getConditionOperator(Element rootNode) {
182 String equationType = rootNode.getAttribute(TmfXmlStrings.OPERATOR);
183 switch (equationType) {
184 case TmfXmlStrings.EQ:
185 return ConditionOperator.EQ;
186 case TmfXmlStrings.NE:
187 return ConditionOperator.NE;
188 case TmfXmlStrings.GE:
189 return ConditionOperator.GE;
190 case TmfXmlStrings.GT:
191 return ConditionOperator.GT;
192 case TmfXmlStrings.LE:
193 return ConditionOperator.LE;
194 case TmfXmlStrings.LT:
195 return ConditionOperator.LT;
196 case TmfXmlStrings.NULL:
197 return ConditionOperator.EQ;
198 default:
199 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$
200 }
201 }
202
0f7276b6
GB
203 /**
204 * Test the result of the condition for an event
205 *
206 * @param event
207 * The event on which to test the condition
208 * @return Whether the condition is true or not
209 * @throws AttributeNotFoundException
210 * The state attribute was not found
b39b8c5b 211 * @since 1.0
0f7276b6 212 */
12685851 213 public boolean testForEvent(ITmfEvent event) throws AttributeNotFoundException {
1d7e62f9 214 ITmfStateSystem ss = fContainer.getStateSystem();
b39b8c5b
JCK
215 if (!fStateValues.isEmpty()) {
216 return testForEvent(event, NonNullUtils.checkNotNull(ss));
0f7276b6
GB
217 } else if (!fConditions.isEmpty()) {
218 /* Verify a condition tree */
219 switch (fOperator) {
220 case AND:
221 for (TmfXmlCondition childCondition : fConditions) {
222 if (!childCondition.testForEvent(event)) {
223 return false;
224 }
225 }
226 return true;
227 case NONE:
228 break;
229 case NOT:
230 return !fConditions.get(0).testForEvent(event);
231 case OR:
232 for (TmfXmlCondition childCondition : fConditions) {
233 if (childCondition.testForEvent(event)) {
234 return true;
235 }
236 }
237 return false;
238 default:
239 break;
240
241 }
0f7276b6
GB
242 }
243 return true;
244 }
245
b39b8c5b
JCK
246 private boolean testForEvent(ITmfEvent event, ITmfStateSystem ss) throws AttributeNotFoundException {
247 /*
248 * The condition is either the equality check of a state value or a
249 * boolean operation on other conditions
250 */
251 if (fStateValues.size() == 1) {
252 ITmfXmlStateValue filter = fStateValues.get(0);
253 int quark = IXmlStateSystemContainer.ROOT_QUARK;
254 for (ITmfXmlStateAttribute attribute : filter.getAttributes()) {
255 quark = attribute.getAttributeQuark(event, quark);
256 /*
257 * When verifying a condition, the state attribute must exist,
258 * if it does not, the query is not valid, we stop the condition
259 * check
260 */
261 if (quark == IXmlStateSystemContainer.ERROR_QUARK) {
262 throw new AttributeNotFoundException(ss.getSSID() + " Attribute:" + attribute); //$NON-NLS-1$
263 }
264 }
265
266 /*
267 * The actual value: it can be either queried in the state system or
268 * found in the event
269 */
270 ITmfStateValue valueState = (quark != IXmlStateSystemContainer.ROOT_QUARK) ? ss.queryOngoingState(quark) : filter.getEventFieldValue(event);
271 if (valueState == null) {
272 throw new IllegalStateException("TmfXmlCondition : The state value does not exist in the state system"); //$NON-NLS-1$
273 }
274
275 /* Get the value to compare to from the XML file */
276 ITmfStateValue valueXML;
277 valueXML = filter.getValue(event);
278 return compare(valueState, valueXML, fConditionOperator);
279 }
280 /* Get the two values needed for the comparison */
281 ITmfStateValue valuesXML1 = fStateValues.get(0).getValue(event);
282 ITmfStateValue valuesXML2 = fStateValues.get(1).getValue(event);
283 return valuesXML1.equals(valuesXML2);
284 }
285
446598f9
GB
286 @Override
287 public String toString() {
288 return "TmfXmlCondition: " + fOperator + " on " + fConditions; //$NON-NLS-1$ //$NON-NLS-2$
289 }
290
5ddeee68
NE
291 /**
292 * Compare two ITmfStateValues based on the given comparison operator
293 *
294 * @param source
295 * the state value to compare to
296 * @param dest
297 * the state value to be compared with
298 * @param comparisonOperator
299 * the operator to compare the inputs
300 * @return the boolean result of the comparison
301 */
302 public boolean compare(ITmfStateValue source, ITmfStateValue dest, ConditionOperator comparisonOperator) {
5ddeee68 303 switch (comparisonOperator) {
b39b8c5b 304 //TODO The comparison operator should have a compareHelper that calls compare
5ddeee68
NE
305 case EQ:
306 return (source.compareTo(dest) == 0);
307 case NE:
308 return (source.compareTo(dest) != 0);
309 case GE:
310 return (source.compareTo(dest) >= 0);
311 case GT:
312 return (source.compareTo(dest) > 0);
313 case LE:
314 return (source.compareTo(dest) <= 0);
315 case LT:
316 return (source.compareTo(dest) < 0);
317 case NONE:
318 default:
319 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$
320 }
5ddeee68 321 }
0f7276b6 322}
This page took 0.078201 seconds and 5 git commands to generate.