1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 Ecole Polytechnique de Montreal and others
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 * Jean-Christian Kouame - Add comparison between two state values
14 ******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.analysis
.xml
.core
.model
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import org
.eclipse
.jdt
.annotation
.Nullable
;
22 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
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
;
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
;
30 import org
.w3c
.dom
.Element
;
33 * This Class implement a condition tree in the XML-defined state system.
39 * <stateAttribute type="location" value="CurrentThread" />
40 * <stateAttribute type="constant" value="System_call" />
41 * <stateValue type="null" />
44 * <stateValue type="long" value="2" />
45 * <stateValue type="long" value="5" />
50 * @author Florian Wininger
52 public class TmfXmlCondition
{
54 private final List
<TmfXmlCondition
> fConditions
= new ArrayList
<>();
55 private final List
<ITmfXmlStateValue
> fStateValues
;
56 private final LogicalOperator fOperator
;
57 private final IXmlStateSystemContainer fContainer
;
58 private final ConditionOperator fConditionOperator
;
60 private enum LogicalOperator
{
67 private enum ConditionOperator
{
81 * The factory used to create XML model elements
83 * The XML root of this condition
85 * The state system container this condition belongs to
87 public TmfXmlCondition(ITmfXmlModelFactory modelFactory
, Element node
, IXmlStateSystemContainer container
) {
88 fContainer
= container
;
90 Element rootNode
= node
;
91 /* Process the conditions: in each case, only process Element nodes */
92 List
<@Nullable Element
> childElements
= XmlUtils
.getChildElements(rootNode
);
93 final Element firstElement
= NonNullUtils
.checkNotNull(childElements
.get(0));
96 * If the node is an if, take the child as the root condition
98 * FIXME: Maybe the caller should do this instead.
100 if (node
.getNodeName().equals(TmfXmlStrings
.IF
)) {
101 if (childElements
.isEmpty()) {
102 throw new IllegalArgumentException("TmfXmlCondition constructor: IF node with no child element"); //$NON-NLS-1$
104 rootNode
= firstElement
;
105 childElements
= XmlUtils
.getChildElements(rootNode
);
107 int size
= rootNode
.getElementsByTagName(TmfXmlStrings
.STATE_VALUE
).getLength();
108 fStateValues
= new ArrayList
<>(size
);
109 if (size
> 2 || size
== 0) {
110 throw new IllegalArgumentException("TmfXmlCondition: a condition should have 1 or 2 state values at most"); //$NON-NLS-1$
113 switch (rootNode
.getNodeName()) {
114 case TmfXmlStrings
.CONDITION
:
115 fOperator
= LogicalOperator
.NONE
;
117 fConditionOperator
= getConditionOperator(rootNode
);
118 getStateValuesForXmlCondition(modelFactory
, NonNullUtils
.checkNotNull(childElements
));
120 fConditionOperator
= ConditionOperator
.EQ
;
121 fStateValues
.add(modelFactory
.createStateValue(NonNullUtils
.checkNotNull(childElements
.get(0)), fContainer
, new ArrayList
<ITmfXmlStateAttribute
>()));
122 fStateValues
.add(modelFactory
.createStateValue(NonNullUtils
.checkNotNull(childElements
.get(1)), fContainer
, new ArrayList
<ITmfXmlStateAttribute
>()));
125 case TmfXmlStrings
.NOT
:
126 fOperator
= LogicalOperator
.NOT
;
127 fConditionOperator
= ConditionOperator
.NONE
;
128 Element element
= firstElement
;
129 fConditions
.add(modelFactory
.createCondition(element
, fContainer
));
131 case TmfXmlStrings
.AND
:
132 fOperator
= LogicalOperator
.AND
;
133 fConditionOperator
= ConditionOperator
.NONE
;
134 for (Element condition
: childElements
) {
135 if (condition
== null) {
138 fConditions
.add(modelFactory
.createCondition(condition
, fContainer
));
141 case TmfXmlStrings
.OR
:
142 fOperator
= LogicalOperator
.OR
;
143 fConditionOperator
= ConditionOperator
.NONE
;
144 for (Element condition
: childElements
) {
145 if (condition
== null) {
148 fConditions
.add(modelFactory
.createCondition(condition
, fContainer
));
152 throw new IllegalArgumentException("TmfXmlCondition constructor: XML node is of the wrong type"); //$NON-NLS-1$
156 private void getStateValuesForXmlCondition(ITmfXmlModelFactory modelFactory
, List
<@Nullable Element
> childElements
) {
157 Element stateValueElement
= NonNullUtils
.checkNotNull(childElements
.remove(childElements
.size() - 1));
159 * A state value is either preceded by an eventField or a number of
162 final Element firstElement
= NonNullUtils
.checkNotNull(childElements
.get(0));
163 if (childElements
.size() == 1 && firstElement
.getNodeName().equals(TmfXmlStrings
.ELEMENT_FIELD
)) {
164 String attribute
= firstElement
.getAttribute(TmfXmlStrings
.NAME
);
165 fStateValues
.add(modelFactory
.createStateValue(stateValueElement
, fContainer
, attribute
));
167 List
<ITmfXmlStateAttribute
> attributes
= new ArrayList
<>();
168 for (Element element
: childElements
) {
169 if (element
== null) {
170 throw new NullPointerException("There should be at list one element"); //$NON-NLS-1$
172 if (!element
.getNodeName().equals(TmfXmlStrings
.STATE_ATTRIBUTE
)) {
173 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 ITmfXmlStateAttribute attribute
= modelFactory
.createStateAttribute(element
, fContainer
);
176 attributes
.add(attribute
);
178 fStateValues
.add(modelFactory
.createStateValue(stateValueElement
, fContainer
, attributes
));
182 private static ConditionOperator
getConditionOperator(Element rootNode
) {
183 String equationType
= rootNode
.getAttribute(TmfXmlStrings
.OPERATOR
);
184 switch (equationType
) {
185 case TmfXmlStrings
.EQ
:
186 return ConditionOperator
.EQ
;
187 case TmfXmlStrings
.NE
:
188 return ConditionOperator
.NE
;
189 case TmfXmlStrings
.GE
:
190 return ConditionOperator
.GE
;
191 case TmfXmlStrings
.GT
:
192 return ConditionOperator
.GT
;
193 case TmfXmlStrings
.LE
:
194 return ConditionOperator
.LE
;
195 case TmfXmlStrings
.LT
:
196 return ConditionOperator
.LT
;
197 case TmfXmlStrings
.NULL
:
198 return ConditionOperator
.EQ
;
200 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$
205 * Test the result of the condition for an event
208 * The event on which to test the condition
209 * @return Whether the condition is true or not
210 * @throws AttributeNotFoundException
211 * The state attribute was not found
214 public boolean testForEvent(ITmfEvent event
) throws AttributeNotFoundException
{
215 ITmfStateSystem ss
= fContainer
.getStateSystem();
216 if (!fStateValues
.isEmpty()) {
217 return testForEvent(event
, NonNullUtils
.checkNotNull(ss
));
218 } else if (!fConditions
.isEmpty()) {
219 /* Verify a condition tree */
222 for (TmfXmlCondition childCondition
: fConditions
) {
223 if (!childCondition
.testForEvent(event
)) {
231 return !fConditions
.get(0).testForEvent(event
);
233 for (TmfXmlCondition childCondition
: fConditions
) {
234 if (childCondition
.testForEvent(event
)) {
247 private boolean testForEvent(ITmfEvent event
, ITmfStateSystem ss
) throws AttributeNotFoundException
{
249 * The condition is either the equality check of a state value or a
250 * boolean operation on other conditions
252 if (fStateValues
.size() == 1) {
253 ITmfXmlStateValue filter
= fStateValues
.get(0);
254 int quark
= IXmlStateSystemContainer
.ROOT_QUARK
;
255 for (ITmfXmlStateAttribute attribute
: filter
.getAttributes()) {
256 quark
= attribute
.getAttributeQuark(event
, quark
);
258 * When verifying a condition, the state attribute must exist,
259 * if it does not, the query is not valid, we stop the condition
262 if (quark
== IXmlStateSystemContainer
.ERROR_QUARK
) {
263 throw new AttributeNotFoundException(ss
.getSSID() + " Attribute:" + attribute
); //$NON-NLS-1$
268 * The actual value: it can be either queried in the state system or
271 ITmfStateValue valueState
= (quark
!= IXmlStateSystemContainer
.ROOT_QUARK
) ? ss
.queryOngoingState(quark
) : filter
.getEventFieldValue(event
);
272 if (valueState
== null) {
273 throw new IllegalStateException("TmfXmlCondition : The state value does not exist in the state system"); //$NON-NLS-1$
276 /* Get the value to compare to from the XML file */
277 ITmfStateValue valueXML
;
278 valueXML
= filter
.getValue(event
);
279 return compare(valueState
, valueXML
, fConditionOperator
);
281 /* Get the two values needed for the comparison */
282 ITmfStateValue valuesXML1
= fStateValues
.get(0).getValue(event
);
283 ITmfStateValue valuesXML2
= fStateValues
.get(1).getValue(event
);
284 return valuesXML1
.equals(valuesXML2
);
288 public String
toString() {
289 return "TmfXmlCondition: " + fOperator
+ " on " + fConditions
; //$NON-NLS-1$ //$NON-NLS-2$
293 * Compare two ITmfStateValues based on the given comparison operator
296 * the state value to compare to
298 * the state value to be compared with
299 * @param comparisonOperator
300 * the operator to compare the inputs
301 * @return the boolean result of the comparison
303 public boolean compare(ITmfStateValue source
, ITmfStateValue dest
, ConditionOperator comparisonOperator
) {
304 switch (comparisonOperator
) {
305 //TODO The comparison operator should have a compareHelper that calls compare
307 return (source
.compareTo(dest
) == 0);
309 return (source
.compareTo(dest
) != 0);
311 return (source
.compareTo(dest
) >= 0);
313 return (source
.compareTo(dest
) > 0);
315 return (source
.compareTo(dest
) <= 0);
317 return (source
.compareTo(dest
) < 0);
320 throw new IllegalArgumentException("TmfXmlCondition: invalid comparison operator."); //$NON-NLS-1$