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