Commit | Line | Data |
---|---|---|
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 | 16 | package org.eclipse.tracecompass.tmf.analysis.xml.core.model; |
0f7276b6 GB |
17 | |
18 | import java.util.ArrayList; | |
19 | import java.util.List; | |
20 | ||
12685851 | 21 | import org.eclipse.jdt.annotation.Nullable; |
b39b8c5b | 22 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
e894a508 AM |
23 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
24 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; | |
25 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; | |
2bdf0193 AM |
26 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.IXmlStateSystemContainer; |
27 | import org.eclipse.tracecompass.tmf.analysis.xml.core.module.XmlUtils; | |
28 | import org.eclipse.tracecompass.tmf.analysis.xml.core.stateprovider.TmfXmlStrings; | |
29 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; | |
0f7276b6 GB |
30 | import 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 | */ | |
52 | public 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 { |
cbac1ac1 JCK |
116 | // No need to test if the childElements size is actually 2. The |
117 | // XSD validation do this check already. | |
b39b8c5b JCK |
118 | fConditionOperator = ConditionOperator.EQ; |
119 | fStateValues.add(modelFactory.createStateValue(NonNullUtils.checkNotNull(childElements.get(0)), fContainer, new ArrayList<ITmfXmlStateAttribute>())); | |
120 | fStateValues.add(modelFactory.createStateValue(NonNullUtils.checkNotNull(childElements.get(1)), fContainer, new ArrayList<ITmfXmlStateAttribute>())); | |
0f7276b6 GB |
121 | } |
122 | break; | |
123 | case TmfXmlStrings.NOT: | |
1010de5e | 124 | fStateValues = new ArrayList<>(); |
5ddeee68 | 125 | fOperator = LogicalOperator.NOT; |
5ddeee68 | 126 | fConditionOperator = ConditionOperator.NONE; |
f0fd2231 | 127 | Element element = NonNullUtils.checkNotNull(childElements.get(0)); |
12685851 | 128 | fConditions.add(modelFactory.createCondition(element, fContainer)); |
0f7276b6 GB |
129 | break; |
130 | case TmfXmlStrings.AND: | |
1010de5e | 131 | fStateValues = new ArrayList<>(); |
5ddeee68 | 132 | fOperator = LogicalOperator.AND; |
5ddeee68 | 133 | fConditionOperator = ConditionOperator.NONE; |
0f7276b6 | 134 | for (Element condition : childElements) { |
12685851 GB |
135 | if (condition == null) { |
136 | continue; | |
137 | } | |
1d7e62f9 | 138 | fConditions.add(modelFactory.createCondition(condition, fContainer)); |
0f7276b6 GB |
139 | } |
140 | break; | |
141 | case TmfXmlStrings.OR: | |
1010de5e | 142 | fStateValues = new ArrayList<>(); |
5ddeee68 | 143 | fOperator = LogicalOperator.OR; |
5ddeee68 | 144 | fConditionOperator = ConditionOperator.NONE; |
0f7276b6 | 145 | for (Element condition : childElements) { |
12685851 GB |
146 | if (condition == null) { |
147 | continue; | |
148 | } | |
1d7e62f9 | 149 | fConditions.add(modelFactory.createCondition(condition, fContainer)); |
0f7276b6 GB |
150 | } |
151 | break; | |
152 | default: | |
153 | throw new IllegalArgumentException("TmfXmlCondition constructor: XML node is of the wrong type"); //$NON-NLS-1$ | |
154 | } | |
155 | } | |
156 | ||
b39b8c5b JCK |
157 | private void getStateValuesForXmlCondition(ITmfXmlModelFactory modelFactory, List<@Nullable Element> childElements) { |
158 | Element stateValueElement = NonNullUtils.checkNotNull(childElements.remove(childElements.size() - 1)); | |
159 | /* | |
160 | * A state value is either preceded by an eventField or a number of | |
161 | * state attributes | |
162 | */ | |
163 | final Element firstElement = NonNullUtils.checkNotNull(childElements.get(0)); | |
164 | if (childElements.size() == 1 && firstElement.getNodeName().equals(TmfXmlStrings.ELEMENT_FIELD)) { | |
165 | String attribute = firstElement.getAttribute(TmfXmlStrings.NAME); | |
166 | fStateValues.add(modelFactory.createStateValue(stateValueElement, fContainer, attribute)); | |
167 | } else { | |
168 | List<ITmfXmlStateAttribute> attributes = new ArrayList<>(); | |
169 | for (Element element : childElements) { | |
170 | if (element == null) { | |
171 | throw new NullPointerException("There should be at list one element"); //$NON-NLS-1$ | |
172 | } | |
173 | if (!element.getNodeName().equals(TmfXmlStrings.STATE_ATTRIBUTE)) { | |
174 | throw new IllegalArgumentException("TmfXmlCondition: a condition either has a eventField element or a number of TmfXmlStateAttribute elements before the state value"); //$NON-NLS-1$ | |
175 | } | |
176 | ITmfXmlStateAttribute attribute = modelFactory.createStateAttribute(element, fContainer); | |
177 | attributes.add(attribute); | |
178 | } | |
179 | fStateValues.add(modelFactory.createStateValue(stateValueElement, fContainer, attributes)); | |
180 | } | |
181 | } | |
182 | ||
183 | private static ConditionOperator getConditionOperator(Element rootNode) { | |
184 | String equationType = rootNode.getAttribute(TmfXmlStrings.OPERATOR); | |
185 | switch (equationType) { | |
186 | case TmfXmlStrings.EQ: | |
187 | return ConditionOperator.EQ; | |
188 | case TmfXmlStrings.NE: | |
189 | return ConditionOperator.NE; | |
190 | case TmfXmlStrings.GE: | |
191 | return ConditionOperator.GE; | |
192 | case TmfXmlStrings.GT: | |
193 | return ConditionOperator.GT; | |
194 | case TmfXmlStrings.LE: | |
195 | return ConditionOperator.LE; | |
196 | case TmfXmlStrings.LT: | |
197 | return ConditionOperator.LT; | |
198 | case TmfXmlStrings.NULL: | |
199 | return ConditionOperator.EQ; | |
200 | default: | |
201 | throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$ | |
202 | } | |
203 | } | |
204 | ||
0f7276b6 GB |
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 | |
b39b8c5b | 213 | * @since 1.0 |
0f7276b6 | 214 | */ |
12685851 | 215 | public boolean testForEvent(ITmfEvent event) throws AttributeNotFoundException { |
1d7e62f9 | 216 | ITmfStateSystem ss = fContainer.getStateSystem(); |
b39b8c5b JCK |
217 | if (!fStateValues.isEmpty()) { |
218 | return testForEvent(event, NonNullUtils.checkNotNull(ss)); | |
0f7276b6 GB |
219 | } else if (!fConditions.isEmpty()) { |
220 | /* Verify a condition tree */ | |
221 | switch (fOperator) { | |
222 | case AND: | |
223 | for (TmfXmlCondition childCondition : fConditions) { | |
224 | if (!childCondition.testForEvent(event)) { | |
225 | return false; | |
226 | } | |
227 | } | |
228 | return true; | |
229 | case NONE: | |
230 | break; | |
231 | case NOT: | |
232 | return !fConditions.get(0).testForEvent(event); | |
233 | case OR: | |
234 | for (TmfXmlCondition childCondition : fConditions) { | |
235 | if (childCondition.testForEvent(event)) { | |
236 | return true; | |
237 | } | |
238 | } | |
239 | return false; | |
240 | default: | |
241 | break; | |
242 | ||
243 | } | |
0f7276b6 GB |
244 | } |
245 | return true; | |
246 | } | |
247 | ||
b39b8c5b JCK |
248 | private boolean testForEvent(ITmfEvent event, ITmfStateSystem ss) throws AttributeNotFoundException { |
249 | /* | |
250 | * The condition is either the equality check of a state value or a | |
251 | * boolean operation on other conditions | |
252 | */ | |
253 | if (fStateValues.size() == 1) { | |
254 | ITmfXmlStateValue filter = fStateValues.get(0); | |
255 | int quark = IXmlStateSystemContainer.ROOT_QUARK; | |
256 | for (ITmfXmlStateAttribute attribute : filter.getAttributes()) { | |
257 | quark = attribute.getAttributeQuark(event, quark); | |
258 | /* | |
259 | * When verifying a condition, the state attribute must exist, | |
260 | * if it does not, the query is not valid, we stop the condition | |
261 | * check | |
262 | */ | |
263 | if (quark == IXmlStateSystemContainer.ERROR_QUARK) { | |
264 | throw new AttributeNotFoundException(ss.getSSID() + " Attribute:" + attribute); //$NON-NLS-1$ | |
265 | } | |
266 | } | |
267 | ||
268 | /* | |
269 | * The actual value: it can be either queried in the state system or | |
270 | * found in the event | |
271 | */ | |
272 | ITmfStateValue valueState = (quark != IXmlStateSystemContainer.ROOT_QUARK) ? ss.queryOngoingState(quark) : filter.getEventFieldValue(event); | |
273 | if (valueState == null) { | |
274 | throw new IllegalStateException("TmfXmlCondition : The state value does not exist in the state system"); //$NON-NLS-1$ | |
275 | } | |
276 | ||
277 | /* Get the value to compare to from the XML file */ | |
278 | ITmfStateValue valueXML; | |
279 | valueXML = filter.getValue(event); | |
280 | return compare(valueState, valueXML, fConditionOperator); | |
281 | } | |
282 | /* Get the two values needed for the comparison */ | |
283 | ITmfStateValue valuesXML1 = fStateValues.get(0).getValue(event); | |
284 | ITmfStateValue valuesXML2 = fStateValues.get(1).getValue(event); | |
285 | return valuesXML1.equals(valuesXML2); | |
286 | } | |
287 | ||
446598f9 GB |
288 | @Override |
289 | public String toString() { | |
cbac1ac1 JCK |
290 | StringBuilder output = new StringBuilder("TmfXmlCondition: "); //$NON-NLS-1$ |
291 | if (fOperator != LogicalOperator.NONE) { | |
292 | output.append(fOperator).append(" on ").append(fConditions); //$NON-NLS-1$ | |
293 | } else { | |
294 | output.append(fConditionOperator).append(" {").append(fStateValues.get(0)); //$NON-NLS-1$ | |
295 | if (fStateValues.size() == 2) { | |
296 | output.append(", ").append(fStateValues.get(1)); //$NON-NLS-1$ | |
297 | } | |
298 | output.append("}"); //$NON-NLS-1$ | |
299 | } | |
300 | return output.toString(); | |
446598f9 GB |
301 | } |
302 | ||
5ddeee68 NE |
303 | /** |
304 | * Compare two ITmfStateValues based on the given comparison operator | |
305 | * | |
306 | * @param source | |
307 | * the state value to compare to | |
308 | * @param dest | |
309 | * the state value to be compared with | |
310 | * @param comparisonOperator | |
311 | * the operator to compare the inputs | |
312 | * @return the boolean result of the comparison | |
313 | */ | |
314 | public boolean compare(ITmfStateValue source, ITmfStateValue dest, ConditionOperator comparisonOperator) { | |
5ddeee68 | 315 | switch (comparisonOperator) { |
b39b8c5b | 316 | //TODO The comparison operator should have a compareHelper that calls compare |
5ddeee68 NE |
317 | case EQ: |
318 | return (source.compareTo(dest) == 0); | |
319 | case NE: | |
320 | return (source.compareTo(dest) != 0); | |
321 | case GE: | |
322 | return (source.compareTo(dest) >= 0); | |
323 | case GT: | |
324 | return (source.compareTo(dest) > 0); | |
325 | case LE: | |
326 | return (source.compareTo(dest) <= 0); | |
327 | case LT: | |
328 | return (source.compareTo(dest) < 0); | |
329 | case NONE: | |
330 | default: | |
331 | throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$ | |
332 | } | |
5ddeee68 | 333 | } |
0f7276b6 | 334 | } |