JAVA and MVEL

JAVA and MVEL

Introduction

MVEL is an Apache licensed powerful Expression Language (EL), written in Java for java based applications and hence its syntax is mostly similar to Java. In general, the regular expression language is used to match, find and replace the strings and numerals from large content using search patterns.

MVEL is similar to OGNL and it provides variety of expressions as given below:

  • Property Expressions
  • Boolean Expressions
  • Method invocations
  • Variable assignments
  • Functions definition

Why MVEL

  • When used as an Expression Language the operators directly support collection, array and string matching as well as regular expressions.
  • When used as a Templating Language, can be used for String construction and configuration.
  • MVEL performance is very faster compared to OGNL.

Use case

  • Usage of property expressions
  • Usage of Boolean expressions
  • Usage of functions definition
  • Collections usage in MVEL
  • Templating with MVEL

Pre-requisite

  • Setup JDK1.6+
  • MVEL jars

Basic Syntax

  • MVEL expression may be a simple statement or a complicated script.
  • The script will contain multiple statements separated by semicolon, denoting the statement termination and newline is not a substitute for the semicolon.
  • The expression execution is based on the operator precedence.
  • By default, the expression will return the value out of the last statement in the expression or script based on the last out value principle.

Usage of Property Expressions

Property expression is used to extract the property value out of the variable or from the model objects. Extracting from the property is always straight forward and known to the programmers.

The following example illustrates accessing the property using the MVEL expression:

Example

“Employee” is the java bean containing the firstName, lastName and age as properties.  The example code is used to extract the bean property using the MVEL expression:

Usually developers access the object using its reference, actually this is not the case. In this example, the “Employee” object is passed with the employee key to the MVEL expression.  If we try to access the value using its reference “e”, then the following exception will be thrown.

Here, “input“ is the object context to the expression and MVEL maps the variable, finds the lastName property and extracts the value.

identifier

Usage of Boolean Expressions

Boolean expressions are used to validate a particular condition using the operators. MVEL has numerous operators to check the criteria. The following are the operators that are available in the MVEL:

  • Unary operators (>=, <=, ==, >, <, ! =)
  • Logical operators (&&, ||)
  • Bitwise operator (&, |, ^, -, /, *,  %)
  • contains, instance of, string concatenation (+), projections (in)

Example

Usage of Functions Definition

Functions definition is a simple method calling. In MVEL, the function is defined using the keyword “def” or “function”. We can pass the parameters to this function and it returns the output value. It always returns a single output by the last statement.

Example

concat.mvel

In the above example, the function is defined to concatenate the “firstName” and “lastName” of the employee bean and finally return the list of concatenated strings as output.

The following is the complete program:

MVELController.java

Employee.java

Output

output

Note: Method invocations are possible using the MVEL expression language.

Collections usage in MVEL

Comparing the collection

By using the MVEL expression we can compare two collections and can apply the operators (>, < and =) on the collection to find output.

Example

To find the numeric greater than constant from the numeric list:

Templating with MVEL

Like other templating language MVEL also supports the following:

  • Binding dynamic values with the static HTML content.
  • Possible to call the static java method from the MVEL template to return the output.
  • Ability to use the java code in the MVEL template.
  • Binding dynamic values with static content is a general use case possible with other templating language. We have used MVEL templating for different use cases to generate the dynamic queries, to form the API calls, R script calls and many more.

Example

Here is the complete example explaining the different use cases for MVEL templating:

Output

final_output

Conclusion

  • MVEL expression is clean and its syntax is easy to understand.
  • There will be a number of use cases where the MVEL will be used efficiently and effectively.
  • MVEL engine performance is good compared to other EL engine.
  • Apache drools use MVEL template for the dynamic code generation and Apache Camel use the MVEL template for the message generation.

References

18019 Views 5 Views Today
  • shanks

    where we have to put the concat.mvel ??

  • Saravanan

    Hi, I see the below exception.
    I couldn’t find why. This is happening on random basis

    org.mvel.CompileException: null returned for foreach in expression: null
    at org.mvel.TemplateInterpreter.execute(TemplateInterpreter.java:465)
    at org.mvel.TemplateInterpreter.parse(TemplateInterpreter.java:338)
    at org.drools.rule.builder.dialect.java.AbstractJavaBuilder.generatTemplates(AbstractJavaBuilder.java:113)
    at org.drools.rule.builder.dialect.java.JavaConsequenceBuilder.build(JavaConsequenceBuilder.java:118)