What is Mvel dialect in Drools?

tech2504 picture tech2504 · Jun 23, 2013 · Viewed 23k times · Source

I am new to Drools. I am creating a rule but I get a compile time error

"field is not visible'.

I've tried to check with Jboss examples, where they use dialect "mvel". It compiled. I didn't understand about dialect. So what is dialect=mvel?

Answer

StuartLC picture StuartLC · Nov 22, 2013

mvel, or the MVFLEX Expression Language has a rich syntax, many of which allow for more concise and expressive code (and less imperative) than java, e.g.

  • Shorthand for get()ters / set()ters (e.g. encapsulating private fields) to be accessed in an alternative property style syntax (similar to VB or C# properties in .Net)

ie. instead of

myObject.setSomeField("SomeValue");
int x = myObject.getSomeIntField();

You can use the syntax (note the subtle capitalization switch as well):

myObject.someField = "SomeValue"
x = myObject.someIntField // Type inferrence
  • The return statement is optional (a convention found in many functional languages like Scala), as is the semi-colon, unless you have multiple statements per line:

x  // i.e. return x;
  • Shorthand array creation and indexing by ordinal

foos = {2, 4, 6, 8, 10}
foos[3] // foos.get(3)
  • Similarly for Maps (Dictionaries)

bars = ["a" : "Apple", "b" : "Basket"] // Hashmap, with put
bars["a"]
bars.a // Similar to dynamically typed object e.g. in javascript, if key is a string.
  • Null Safe navigation operator (like the null-conditional operator in Rosyln)

foo.?bar.baz // if (foo.bar != null) { return foo.bar.baz; } else { return null; }