Project

General

Profile

<xweld/>-expression

The xweld-expression module provides a small but powerful standalone module for representing Boolean or mathematical expressions, and parsing them from Strings that can be evaluated into either a Boolean or Numeric result. While there are several other Java expression languages available – SpEL, OGNL, MVEL, and JBoss EL, to name a few – the xweld-expression module is extremely lightweight and provides only those primitives typically required by the <xweld/> suite of tools. Because of its simplicity, it is extremely easy to set up and use.

One primary motivation for the expression class library is to support Permissions in xweld-security that use a context when they are evaluated. For example, expressions support the notion of context-sensitive permissions, to handle things like GUI controls being enabled or not, depending on the state of a workflow task and a particular user. That said, xweld-expression is based on a technology-agnostic API allowing other expression language implementations to be integrated should the need arise.

Primary Features

The xweld-expression language supports the following functionality:

  • Double or Boolean evaluation results
  • Expressions containing quoted String values and numeric or boolean literals and lists
  • Expressions containing Boolean relational operators, such as >, < ,=, & and |
  • Expressions referencing fields or methods of an object context passed at evaluation time using reflection
  • Expressions supporting parameters supplied at evaluation time
  • Expressions checking whether a field, parameter, or literal is contained in another list of values

Key Concepts

Building Expressions

Expressions are built using an implementation of the IExpressionBuilder interface, and an instance of an expression builder can be obtained by calling the ExpressionBuilderFactory ‘create’ method.
Here is an example of how to create an expression builder factory:

IExpressionBuilderFactory factory = new ExpressionBuilderFactory();
IExpressionBuilder b = factory.create();

Reserved Characters/Words

The following characters and words are considered tokens and hence reserved:
  • +,-,*,/,^,% are the binary Numeric operators
  • !, &, | are the Boolean operators
  • =,<,> ,<=,>= are relational operators
  • [] are the array indexer delimiters
  • (){} used to delimit expressions and lists respectively
  • ‘’ single quotes are used to delimit string literals
  • false, true
  • $ refers to the context object
  • Other reserved characters include ‘;’

Operator Precedence

The precedence of operators is:
1. * /^
2. + -
3. in & | =

List Literals

Lists can be expressed directly in an expression using {} notation as:
"'b' in {'a', 'b', 50}"

Boolean Operators

Examples include:
  • "false = true", “5 < 6”, “5 < (6 – 2)”

Packages and Classes

The xweld-expression classes are contained in the following package:

  • com.xweld.expression - the expression classes

expression Class Diagram

Examples

Calculator

String expressionString = "5 + 20 + (1 + 2 + 3)";
Expression expression = ExpressionBuilder.build(expressionString);
Double result = expression.evaluate();

Simple Boolean Expression using Literal Values

String expressionString = "true & (true | !false)";
Expression expression = ExpressionBuilder.build(expressionString);
Boolean result = expression.evaluate();

Boolean Expression Comparing Two String Literal Values

String expressionString = "true & (true | !false)";
Expression expression = ExpressionBuilder.build(expressionString);
Boolean result = expression.evaluate();

Boolean Expression Using a Context Object

@class Task
{
String assignedTo = "jsmith";
String status = "RESOLVED";
}
Task context = new Task ();
// inspect the assignedTo  field and the state field of the context 
// object at run time, ‘$’ character represents the context object
String expressionString = "($assignedTo = 'jsmith') &  ($status = 'RESOLVED')";
Expression expression = ExpressionBuilder.build(expressionString, context); 
Boolean result = expression.evaluate();@

Boolean Expression Using a Parameter to Represent the User Name Value

The same example as above, but using a parameter to represent the user name value:

@String expressionString = "($assignedTo   = @user) & ($status = 'RESOLVED')";
Expression expression = ExpressionBuilder.build(expressionString, context);
expression.setParamater("user", AuthorizationManager.getInstance().getUser().getName());
Boolean result = expression.evaluate();@

Contact Us

For more information, please contact us at

Copyright

Copyright © 1999-2012 Free North Alliance Inc., All Rights Reserved.

The contents of this web site, including all images, text, graphics and software, are the sole property of the <xweld/> Development Community and its agents. Unauthorized use or transmission is strictly forbidden by international copyright law.

xweld-expression.JPG View - expression Class Diagram (59.1 KB) John Free, 13 August 2011 07:55 AM