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
<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 switch (rootNode
.getNodeName()) {
104 case TmfXmlStrings
.CONDITION
:
105 fOperator
= LogicalOperator
.NONE
;
106 /* Read comparison type */
107 String equationType
= rootNode
.getAttribute(TmfXmlStrings
.OPERATOR
);
109 switch (equationType
) {
110 case TmfXmlStrings
.EQ
:
111 fConditionOperator
= ConditionOperator
.EQ
;
113 case TmfXmlStrings
.NE
:
114 fConditionOperator
= ConditionOperator
.NE
;
116 case TmfXmlStrings
.GE
:
117 fConditionOperator
= ConditionOperator
.GE
;
119 case TmfXmlStrings
.GT
:
120 fConditionOperator
= ConditionOperator
.GT
;
122 case TmfXmlStrings
.LE
:
123 fConditionOperator
= ConditionOperator
.LE
;
125 case TmfXmlStrings
.LT
:
126 fConditionOperator
= ConditionOperator
.LT
;
128 case TmfXmlStrings
.NULL
:
129 fConditionOperator
= ConditionOperator
.EQ
;
132 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$
134 /* The last element is a state value node */
135 Element stateValueElement
= childElements
.remove(childElements
.size() - 1);
136 if (stateValueElement
== null) {
137 throw new IllegalStateException();
141 * A state value is either preceded by an eventField or a number of
144 if (childElements
.size() == 1 && childElements
.get(0).getNodeName().equals(TmfXmlStrings
.ELEMENT_FIELD
)) {
145 String attribute
= childElements
.get(0).getAttribute(TmfXmlStrings
.NAME
);
146 if (attribute
== null) {
147 throw new IllegalArgumentException();
149 fStateValue
= modelFactory
.createStateValue(stateValueElement
, fContainer
, attribute
);
151 List
<ITmfXmlStateAttribute
> attributes
= new ArrayList
<>();
152 for (Element element
: childElements
) {
153 if (!element
.getNodeName().equals(TmfXmlStrings
.STATE_ATTRIBUTE
)) {
154 throw new IllegalArgumentException("TmfXmlCondition: a condition either has a eventField element or a number of TmfXmlStateAttribute elements before the state value"); //$NON-NLS-1$
156 ITmfXmlStateAttribute attribute
= modelFactory
.createStateAttribute(element
, fContainer
);
157 attributes
.add(attribute
);
159 fStateValue
= modelFactory
.createStateValue(stateValueElement
, fContainer
, attributes
);
162 case TmfXmlStrings
.NOT
:
163 fOperator
= LogicalOperator
.NOT
;
165 fConditionOperator
= ConditionOperator
.NONE
;
166 Element element
= childElements
.get(0);
167 if (element
== null) {
168 throw new IllegalArgumentException();
170 fConditions
.add(modelFactory
.createCondition(element
, fContainer
));
172 case TmfXmlStrings
.AND
:
173 fOperator
= LogicalOperator
.AND
;
175 fConditionOperator
= ConditionOperator
.NONE
;
176 for (Element condition
: childElements
) {
177 if (condition
== null) {
180 fConditions
.add(modelFactory
.createCondition(condition
, fContainer
));
183 case TmfXmlStrings
.OR
:
184 fOperator
= LogicalOperator
.OR
;
186 fConditionOperator
= ConditionOperator
.NONE
;
187 for (Element condition
: childElements
) {
188 if (condition
== null) {
191 fConditions
.add(modelFactory
.createCondition(condition
, fContainer
));
195 throw new IllegalArgumentException("TmfXmlCondition constructor: XML node is of the wrong type"); //$NON-NLS-1$
200 * Test the result of the condition for an event
203 * The event on which to test the condition
204 * @return Whether the condition is true or not
205 * @throws AttributeNotFoundException
206 * The state attribute was not found
208 public boolean testForEvent(ITmfEvent event
) throws AttributeNotFoundException
{
209 ITmfStateSystem ss
= fContainer
.getStateSystem();
211 * The condition is either the equality check of a state value or a
212 * boolean operation on other conditions
214 if (fStateValue
!= null) {
215 ITmfXmlStateValue filter
= fStateValue
;
216 int quark
= IXmlStateSystemContainer
.ROOT_QUARK
;
217 for (ITmfXmlStateAttribute attribute
: filter
.getAttributes()) {
218 quark
= attribute
.getAttributeQuark(event
, quark
);
220 * When verifying a condition, the state attribute must exist,
221 * if it does not, the query is not valid, we stop the condition
224 if (quark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
225 throw new AttributeNotFoundException(ss
.getSSID() + " Attribute:" + attribute
); //$NON-NLS-1$
229 /* Get the value to compare to from the XML file */
230 ITmfStateValue valueXML
;
231 valueXML
= filter
.getValue(event
);
234 * The actual value: it can be either queried in the state system or
237 ITmfStateValue valueState
= (quark
!= IXmlStateSystemContainer
.ROOT_QUARK
) ? ss
.queryOngoingState(quark
) :
238 filter
.getEventFieldValue(event
);
239 if (valueState
== null) {
240 throw new IllegalStateException();
243 return compare(valueState
, valueXML
, fConditionOperator
);
245 } else if (!fConditions
.isEmpty()) {
246 /* Verify a condition tree */
249 for (TmfXmlCondition childCondition
: fConditions
) {
250 if (!childCondition
.testForEvent(event
)) {
258 return !fConditions
.get(0).testForEvent(event
);
260 for (TmfXmlCondition childCondition
: fConditions
) {
261 if (childCondition
.testForEvent(event
)) {
271 throw new IllegalStateException("TmfXmlCondition: the condition should be either a state value or be the result of a condition tree"); //$NON-NLS-1$
277 public String
toString() {
278 return "TmfXmlCondition: " + fOperator
+ " on " + fConditions
; //$NON-NLS-1$ //$NON-NLS-2$
282 * Compare two ITmfStateValues based on the given comparison operator
285 * the state value to compare to
287 * the state value to be compared with
288 * @param comparisonOperator
289 * the operator to compare the inputs
290 * @return the boolean result of the comparison
292 public boolean compare(ITmfStateValue source
, ITmfStateValue dest
, ConditionOperator comparisonOperator
) {
293 switch (comparisonOperator
) {
295 return (source
.compareTo(dest
) == 0);
297 return (source
.compareTo(dest
) != 0);
299 return (source
.compareTo(dest
) >= 0);
301 return (source
.compareTo(dest
) > 0);
303 return (source
.compareTo(dest
) <= 0);
305 return (source
.compareTo(dest
) < 0);
308 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$