Document toolboxDocument toolbox

Using the Expression <Expr> Element

You use an <Expr> element to define expressions when performing logical tests or assignments. If you use no attribute for the <Expr> element, the text string contents of the element are used as the value. Thus, the two following expressions are equivalent:

<Expr value="foo"/>
<Expr>foo</Expr>

An <Expr> element may contain other expressions and may represent its value as a constant, field lookup, or another operation. If no attributes are defined, the <Expr> contents (the element text) will be used as a String. An example appears in the section Expression Attributes and Matching.
The <Expr> tag may have the following attributes.

<Expr> Attribute

Description

<Expr> Attribute

Description

expression=

Used for shorthand boolean expressions, has always been available for SetFilter and PolicyRuleLogic elements. The functionality also extends to the Expr element:

<Expr op="or">
   <Expr op="and">
     <Expr variable="foo"/>
     <Expr variable="bar"/>
   </Expr>
   <Expr op="and">
     <Expr variable="x"/>
     <Expr variable="y"/>
   </Expr>
</Expr>
can now be written more succinctly as:
<Expr expression="(1 and 2) or (3 and 4)">
   <Expr label="1" variable="foo"/>
   <Expr label="2" variable="bar"/>
   <Expr label="3" variable="x"/>
   <Expr label="4" variable="y"/>
</Expr>

type=

Explicitly sets the return type of the expression. Possible values can be stated as the following:

bool, boolean (as in type="bool")

int, integer, number (as in type="int", type="integer")

float, double, decimal (as in type="double")

datetime (as in type="datetime")

ip (as in type="ip")

nil, null

To create a nil expression, use type="nil" or type="null" (which are equivalent).

value=

Used to set the <Expr> value to a constant.
When using this, it may be necessary to specify the type= attribute as well. For example, if the value is 'true', by default the value matches the String 'true'. In this example, to represent the actual Boolean true, you must specify the type as 'bool'.

object=

The name of a variable holding an object to treat as the current object during evaluation of this expression. Generally this may be omitted as the current object is known as part of the context.

field=

The name of an object attribute or instance method of the current object, such as a data field (device="Switch-Router") in NetMRI. You may chain these calls arbitrarily. For this example, the call would be field="device".

method=

The name of a method to call on the current object. The arguments to the method will include the values of any attribute types other than 'object', 'method', 'output' (these three types are omitted) in their defined order, followed by the values of any sub-expressions, in their listed order. Note that there are very few methods currently available that require parameters. Example:

<PolicyRuleLogic editor='raw-xml'>
   <Assign variable='parent'><Expr method='parent_device'/>
    </Assign>
   <If>
    <Expr op='=='>
     <Expr object='parent' field='DeviceModel'/>
     <Expr value='N7Kc7010'/>
    </Expr>
    <Then><PolicyRulePass/></Then>
    <Else><PolicyRuleFail/></Else>
   </If>
</PolicyRuleLogic>

No parameters are returned to the <parent_device> method, which is used to match if and only if the device checked is a Nexus 7K switch-router.

variable=

Allows the value of a regular expression to be the value of a named variable.

output=

In previous releases, the output attribute was available in the SetFilter element to define the variable in which to store the resulting array; it is now available for all ScriptXML elements.

<Assign variable="foo"><Expr value="bar"/></Assign>

may now be written as

<Expr value="bar" output="foo"/>

op=

Defines an Operator, that converts sub-expressions into a value. When comparing to objects of dissimilar type, the second object will be converted to the same type as the first object before comparison. Operator types include:

  • Logical Ands (and, &&),

  • Logical ORs (or and ||), and not;

  • Math operators (+, -, * and /);

  • Comparison operators: equality (=, ==, eq), inequality of two sub-expressions (!=, <>, ne), numeric or case-sensitive Greater Than (>), numeric or case-sensitive Less Than (<), numeric or case-sensitive Greater Than or Equal to (>=), numeric or case-sensitive Less Than or Equal to(<).

  • String operators (concat, contains, does-not-contain, matches, does-not-match): respectively, concatenation of strings; string containment (true if the second expression is contained within the first) or non-containment (true if the second expression is not contained within the first); a match or non-match between two sub-expressions. For the final four, the second expression of the comparison should be the regular expression.

For any operators using < or >, you may have to write them as &lt and &gt in the Raw XML Editor.
Additional operators include the following:

  • ~ (tilde) or bnot – Bitwise NOT (i.e. complement)

  • & or band – Bitwise AND

  •  | or bor – Bitwise OR

  • ^ or bxor – Bitwise XOR

  • << or lshift – Left shift

  • >> or rshift – Right shift

  • ** – Modulus

op= (continued)

Other operators include the following:

  • defined= Positive match if the current object is not nil, and all of the listed attributes or instance methods are not nil;

  • in= An example: should the first expression be a field that returns an IP address, or is explicitly given type 'ip', then the second operator will be treated as a CIDR range, and this operator will return true if the IP is in the CIDR;

  • not-in= The logical NOT of an operator;

  • in-group= Positive match if the current object matches the group criteria of at least one group (device group, interface group) whose name is returned by any sub-expression;

  • not-in-group= Positive match if the current object matches no group listed as a sub-expression.

  • All applicable array operators may also be used. For more information, see Policy Rule XML Capabilities.

Divisions by zero will result in an exception and an invalid Rule state. Previous releases returned a value of zero, which could lead to unintended consequences in rules.

Â