Commit | Line | Data |
---|---|---|
0f7276b6 | 1 | /******************************************************************************* |
e13bd4cd | 2 | * Copyright (c) 2014, 2015 Ecole Polytechnique de Montreal |
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 |
0f7276b6 GB |
13 | ******************************************************************************/ |
14 | ||
2bdf0193 | 15 | package org.eclipse.tracecompass.tmf.analysis.xml.core.model; |
0f7276b6 GB |
16 | |
17 | import java.util.ArrayList; | |
18 | import java.util.List; | |
19 | ||
12685851 | 20 | import org.eclipse.jdt.annotation.Nullable; |
e894a508 AM |
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; | |
2bdf0193 AM |
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; | |
0f7276b6 GB |
28 | import org.w3c.dom.Element; |
29 | ||
30 | /** | |
1d7e62f9 | 31 | * This Class implement a condition tree in the XML-defined state system. |
0f7276b6 GB |
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<>(); | |
12685851 | 51 | private final @Nullable ITmfXmlStateValue fStateValue; |
5ddeee68 | 52 | private final LogicalOperator fOperator; |
1d7e62f9 | 53 | private final IXmlStateSystemContainer fContainer; |
5ddeee68 | 54 | private final ConditionOperator fConditionOperator; |
0f7276b6 | 55 | |
5ddeee68 | 56 | private enum LogicalOperator { |
0f7276b6 GB |
57 | NONE, |
58 | NOT, | |
59 | AND, | |
60 | OR, | |
61 | } | |
62 | ||
5ddeee68 NE |
63 | private enum ConditionOperator { |
64 | NONE, | |
65 | EQ, | |
66 | NE, | |
67 | GE, | |
68 | GT, | |
69 | LE, | |
70 | LT | |
71 | } | |
72 | ||
0f7276b6 GB |
73 | /** |
74 | * Constructor | |
75 | * | |
1d7e62f9 GB |
76 | * @param modelFactory |
77 | * The factory used to create XML model elements | |
0f7276b6 GB |
78 | * @param node |
79 | * The XML root of this condition | |
1d7e62f9 GB |
80 | * @param container |
81 | * The state system container this condition belongs to | |
0f7276b6 | 82 | */ |
1d7e62f9 GB |
83 | public TmfXmlCondition(ITmfXmlModelFactory modelFactory, Element node, IXmlStateSystemContainer container) { |
84 | fContainer = container; | |
0f7276b6 GB |
85 | |
86 | Element rootNode = node; | |
87 | /* Process the conditions: in each case, only process Element nodes */ | |
4c4e2816 | 88 | List<@Nullable Element> childElements = XmlUtils.getChildElements(rootNode); |
0f7276b6 GB |
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 | ||
4c4e2816 AM |
103 | if (rootNode == null) { |
104 | throw new IllegalArgumentException(); | |
105 | } | |
106 | ||
0f7276b6 GB |
107 | switch (rootNode.getNodeName()) { |
108 | case TmfXmlStrings.CONDITION: | |
5ddeee68 NE |
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 | } | |
0f7276b6 GB |
138 | /* The last element is a state value node */ |
139 | Element stateValueElement = childElements.remove(childElements.size() - 1); | |
12685851 GB |
140 | if (stateValueElement == null) { |
141 | throw new IllegalStateException(); | |
142 | } | |
0f7276b6 GB |
143 | |
144 | /* | |
145 | * A state value is either preceded by an eventField or a number of | |
146 | * state attributes | |
147 | */ | |
4c4e2816 AM |
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); | |
12685851 | 155 | fStateValue = modelFactory.createStateValue(stateValueElement, fContainer, attribute); |
0f7276b6 | 156 | } else { |
1d7e62f9 | 157 | List<ITmfXmlStateAttribute> attributes = new ArrayList<>(); |
0f7276b6 | 158 | for (Element element : childElements) { |
4c4e2816 | 159 | if (element == null || !element.getNodeName().equals(TmfXmlStrings.STATE_ATTRIBUTE)) { |
0f7276b6 GB |
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 | } | |
1d7e62f9 | 162 | ITmfXmlStateAttribute attribute = modelFactory.createStateAttribute(element, fContainer); |
0f7276b6 GB |
163 | attributes.add(attribute); |
164 | } | |
1d7e62f9 | 165 | fStateValue = modelFactory.createStateValue(stateValueElement, fContainer, attributes); |
0f7276b6 GB |
166 | } |
167 | break; | |
168 | case TmfXmlStrings.NOT: | |
5ddeee68 | 169 | fOperator = LogicalOperator.NOT; |
0f7276b6 | 170 | fStateValue = null; |
5ddeee68 | 171 | fConditionOperator = ConditionOperator.NONE; |
12685851 GB |
172 | Element element = childElements.get(0); |
173 | if (element == null) { | |
174 | throw new IllegalArgumentException(); | |
175 | } | |
176 | fConditions.add(modelFactory.createCondition(element, fContainer)); | |
0f7276b6 GB |
177 | break; |
178 | case TmfXmlStrings.AND: | |
5ddeee68 | 179 | fOperator = LogicalOperator.AND; |
0f7276b6 | 180 | fStateValue = null; |
5ddeee68 | 181 | fConditionOperator = ConditionOperator.NONE; |
0f7276b6 | 182 | for (Element condition : childElements) { |
12685851 GB |
183 | if (condition == null) { |
184 | continue; | |
185 | } | |
1d7e62f9 | 186 | fConditions.add(modelFactory.createCondition(condition, fContainer)); |
0f7276b6 GB |
187 | } |
188 | break; | |
189 | case TmfXmlStrings.OR: | |
5ddeee68 | 190 | fOperator = LogicalOperator.OR; |
0f7276b6 | 191 | fStateValue = null; |
5ddeee68 | 192 | fConditionOperator = ConditionOperator.NONE; |
0f7276b6 | 193 | for (Element condition : childElements) { |
12685851 GB |
194 | if (condition == null) { |
195 | continue; | |
196 | } | |
1d7e62f9 | 197 | fConditions.add(modelFactory.createCondition(condition, fContainer)); |
0f7276b6 GB |
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 | */ | |
12685851 | 214 | public boolean testForEvent(ITmfEvent event) throws AttributeNotFoundException { |
1d7e62f9 | 215 | ITmfStateSystem ss = fContainer.getStateSystem(); |
0f7276b6 GB |
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) { | |
1d7e62f9 GB |
221 | ITmfXmlStateValue filter = fStateValue; |
222 | int quark = IXmlStateSystemContainer.ROOT_QUARK; | |
223 | for (ITmfXmlStateAttribute attribute : filter.getAttributes()) { | |
0f7276b6 GB |
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 | */ | |
1d7e62f9 | 230 | if (quark == IXmlStateSystemContainer.ERROR_QUARK) { |
e13bd4cd | 231 | throw new AttributeNotFoundException(ss.getSSID() + " Attribute:" + attribute); //$NON-NLS-1$ |
0f7276b6 GB |
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 | */ | |
1d7e62f9 GB |
243 | ITmfStateValue valueState = (quark != IXmlStateSystemContainer.ROOT_QUARK) ? ss.queryOngoingState(quark) : |
244 | filter.getEventFieldValue(event); | |
12685851 GB |
245 | if (valueState == null) { |
246 | throw new IllegalStateException(); | |
247 | } | |
0f7276b6 | 248 | |
5ddeee68 | 249 | return compare(valueState, valueXML, fConditionOperator); |
0f7276b6 GB |
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 | ||
446598f9 GB |
282 | @Override |
283 | public String toString() { | |
284 | return "TmfXmlCondition: " + fOperator + " on " + fConditions; //$NON-NLS-1$ //$NON-NLS-2$ | |
285 | } | |
286 | ||
5ddeee68 NE |
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) { | |
5ddeee68 NE |
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 | ||
0f7276b6 | 319 | } |