1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ecole Polytechnique de Montreal
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
10 * Florian Wininger - Initial API and implementation
11 * Naser Ezzati - Add the comparison operators
12 * Patrick Tasse - Add message to exceptions
13 ******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
;
17 import java
.util
.ArrayList
;
18 import java
.util
.List
;
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
;
31 * This Class implement a condition tree in the XML-defined state system.
37 * <stateAttribute type="location" value="CurrentThread" />
38 * <stateAttribute type="constant" value="System_call" />
39 * <stateValue type="null" />
46 * @author Florian Wininger
48 public class TmfXmlCondition
{
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
;
56 private enum LogicalOperator
{
63 private enum ConditionOperator
{
77 * The factory used to create XML model elements
79 * The XML root of this condition
81 * The state system container this condition belongs to
83 public TmfXmlCondition(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
) {
84 fContainer
= container
;
86 Element rootNode
= node
;
87 /* Process the conditions: in each case, only process Element nodes */
88 List
<@Nullable Element
> childElements
= XmlUtils
.getChildElements(rootNode
);
91 * If the node is an if, take the child as the root condition
93 * FIXME: Maybe the caller should do this instead.
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$
99 rootNode
= childElements
.get(0);
100 childElements
= XmlUtils
.getChildElements(rootNode
);
103 if (rootNode
== null) {
104 throw new IllegalArgumentException();
107 switch (rootNode
.getNodeName()) {
108 case TmfXmlStrings
.CONDITION
:
109 fOperator
= LogicalOperator
.NONE
;
110 /* Read comparison type */
111 String equationType
= rootNode
.getAttribute(TmfXmlStrings
.OPERATOR
);
113 switch (equationType
) {
114 case TmfXmlStrings
.EQ
:
115 fConditionOperator
= ConditionOperator
.EQ
;
117 case TmfXmlStrings
.NE
:
118 fConditionOperator
= ConditionOperator
.NE
;
120 case TmfXmlStrings
.GE
:
121 fConditionOperator
= ConditionOperator
.GE
;
123 case TmfXmlStrings
.GT
:
124 fConditionOperator
= ConditionOperator
.GT
;
126 case TmfXmlStrings
.LE
:
127 fConditionOperator
= ConditionOperator
.LE
;
129 case TmfXmlStrings
.LT
:
130 fConditionOperator
= ConditionOperator
.LT
;
132 case TmfXmlStrings
.NULL
:
133 fConditionOperator
= ConditionOperator
.EQ
;
136 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$
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();
145 * A state value is either preceded by an eventField or a number of
148 @Nullable Element firstChild
= childElements
.get(0);
149 if (firstChild
== null) {
150 throw new IllegalStateException();
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
);
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$
162 ITmfXmlStateAttribute attribute
= modelFactory
.createStateAttribute(element
, fContainer
);
163 attributes
.add(attribute
);
165 fStateValue
= modelFactory
.createStateValue(stateValueElement
, fContainer
, attributes
);
168 case TmfXmlStrings
.NOT
:
169 fOperator
= LogicalOperator
.NOT
;
171 fConditionOperator
= ConditionOperator
.NONE
;
172 Element element
= childElements
.get(0);
173 if (element
== null) {
174 throw new IllegalArgumentException();
176 fConditions
.add(modelFactory
.createCondition(element
, fContainer
));
178 case TmfXmlStrings
.AND
:
179 fOperator
= LogicalOperator
.AND
;
181 fConditionOperator
= ConditionOperator
.NONE
;
182 for (Element condition
: childElements
) {
183 if (condition
== null) {
186 fConditions
.add(modelFactory
.createCondition(condition
, fContainer
));
189 case TmfXmlStrings
.OR
:
190 fOperator
= LogicalOperator
.OR
;
192 fConditionOperator
= ConditionOperator
.NONE
;
193 for (Element condition
: childElements
) {
194 if (condition
== null) {
197 fConditions
.add(modelFactory
.createCondition(condition
, fContainer
));
201 throw new IllegalArgumentException("TmfXmlCondition constructor: XML node is of the wrong type"); //$NON-NLS-1$
206 * Test the result of the condition for an 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
214 public boolean testForEvent(ITmfEvent event
) throws AttributeNotFoundException
{
215 ITmfStateSystem ss
= fContainer
.getStateSystem();
217 * The condition is either the equality check of a state value or a
218 * boolean operation on other conditions
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
);
226 * When verifying a condition, the state attribute must exist,
227 * if it does not, the query is not valid, we stop the condition
230 if (quark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
231 throw new AttributeNotFoundException(ss
.getSSID() + " Attribute:" + attribute
); //$NON-NLS-1$
235 /* Get the value to compare to from the XML file */
236 ITmfStateValue valueXML
;
237 valueXML
= filter
.getValue(event
);
240 * The actual value: it can be either queried in the state system or
243 ITmfStateValue valueState
= (quark
!= IXmlStateSystemContainer
.ROOT_QUARK
) ? ss
.queryOngoingState(quark
) :
244 filter
.getEventFieldValue(event
);
245 if (valueState
== null) {
246 throw new IllegalStateException();
249 return compare(valueState
, valueXML
, fConditionOperator
);
251 } else if (!fConditions
.isEmpty()) {
252 /* Verify a condition tree */
255 for (TmfXmlCondition childCondition
: fConditions
) {
256 if (!childCondition
.testForEvent(event
)) {
264 return !fConditions
.get(0).testForEvent(event
);
266 for (TmfXmlCondition childCondition
: fConditions
) {
267 if (childCondition
.testForEvent(event
)) {
277 throw new IllegalStateException("TmfXmlCondition: the condition should be either a state value or be the result of a condition tree"); //$NON-NLS-1$
283 public String
toString() {
284 return "TmfXmlCondition: " + fOperator
+ " on " + fConditions
; //$NON-NLS-1$ //$NON-NLS-2$
288 * Compare two ITmfStateValues based on the given comparison operator
291 * the state value to compare to
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
298 public boolean compare(ITmfStateValue source
, ITmfStateValue dest
, ConditionOperator comparisonOperator
) {
299 switch (comparisonOperator
) {
301 return (source
.compareTo(dest
) == 0);
303 return (source
.compareTo(dest
) != 0);
305 return (source
.compareTo(dest
) >= 0);
307 return (source
.compareTo(dest
) > 0);
309 return (source
.compareTo(dest
) <= 0);
311 return (source
.compareTo(dest
) < 0);
314 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$