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