What is the Mvel dialect in Drools?

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

"not displayed.

I tried to check with Jboss examples where they use the "mvel" dialect . It is compiled. I did not know about the dialect. So what is dialect=mvel ?

+6
source share
4 answers

mvel , or MVFLEX expression language has rich syntax , many of which allow you to use more concise and expressive code (and less imperative) than java , for example

  • Abbreviation for get() ters / set() ters (for example, encapsulating private fields), access to which is possible in the alternative property style syntax (similar to VB or C # properties in .Net )

t. instead

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

You can use the syntax (note the thin key of capitalization):

 myObject.someField = "SomeValue" x = myObject.someIntField // Type inferrence 
  • The return is optional (the convention is found in many functional languages ​​such as Scala), as well as the colon, if you do not have multiple statements in the line:

 x // ie return x; 
  • Creating and indexing a string array by sequence numbers

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

 bars = ["a" : "Apple", "b" : "Basket"] // Hashmap, with put bars["a"] bars.a // Similar to dynamically typed object eg in javascript, if key is a string. 
  • Null Safe navigation operator (for example, a null condition operator in Rosyln)

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

Based on

Drools JBoss 5.0 Developer Guide for Developers

A dialect is used to indicate syntax in any code expression that is in a state or consequence. The default value is Java. Drools currently supports another dialect called mvel.

In particular, mvel is an expression language for Java applications. And this is based on Java syntax. more information about mvel

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

The purpose of the "mvel" dialect is to specify Getters and Setters for the variables of your classes in a regular Java object (POJO). Consider the above example, which uses the Holiday class, and uses the month in parentheses (parentheses). Thus, using the dialect "mvel", you can access the recipient and setters of the variable "month".

The java dialect is used to help us write our Java code in our rules. There is one limitation or characteristic to this. We cannot use Java code inside the "when" part of the rule, but we can use Java code in the "next" part.

We can also declare the reference variable $ h1 without the $ character. There are no restrictions on this. The main purpose of putting the $ character in front of a variable is to note the difference between POJO class variables and rules.

Sincerely.

+1
source

I found something in this. I shared with that. Others supported Java or the MVEL scripting language. To get the property values ​​of an object. For example, a Fibonacci has a bean and has several properties, i.e. a sequence

 rule Recurse salience 10 when not ( Fibonacci ( sequence == 1 ) ) f : Fibonacci ( value == -1 ) then insert( new Fibonacci( f.sequence - 1 ) ); System.out.println( "recurse for " + f.sequence ); end 

we need to check the sequence if == 1, then the value is -1. Sequence values ​​are set to a Fibonacci object. We check values ​​based on MVEL or java.MVEL is a superset of Java.

0
source

Source: https://habr.com/ru/post/947930/


All Articles