Drools: Differences between the mvel and java dialects

dshgna picture dshgna · Feb 12, 2015 · Viewed 7k times · Source

As a newbie to Drools, I'm confused about the differences between the mvel and java dialects and the relative pros and cons of using either.

After some initial research, I came across some discussions here, and here.

What other differences are there and what is the added benefit of using mvel over java other than syntactic sugar?

Thanks.

Answer

GauRang Omar picture GauRang Omar · Mar 3, 2017

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.

  • MVEL expression is clean and its syntax is easy to understand.
  • Apache drools use MVEL template for the dynamic code generation.
  • MVEL engine performance is good compared to other EL engine.

Consider an example.

    rule "validate holiday" 
    dialect "mvel"
    dialect "java"
    when
        $h1 : Holiday( month == "july" )
    then
        System.out.println($h1.name + ":" + $h1.month);
    end

The purpose of dialect "mvel" is to point the Getter and Setters of the variables of your Plain Old Java Object (POJO) classes. Consider the above example, in which a Holiday class is used and inside the circular brackets (parentheses) "month" is used. So with the help dialect "mvel" the getter and setters of the variable "month" can be accessed.

Dialect "java" is used to help us write our Java code in our rules. There is one restriction or characteristic on this. We cannot use Java code inside "when" part of the rule but we can use Java code in "then" part.

We can also declare a Reference variable $h1 without the $ symbol. There is no restriction on this. The main purpose of putting the $ symbol before the variable is to mark the difference between variables of POJO classes and Rules.