What is Mvel dialect in Drools?

纵然是瞬间 提交于 2019-11-30 04:09:07

问题


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?


回答1:


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; }



回答2:


Based on

Drools JBoss Rules 5.0 Developer's Guide

Dialect is used to specify the syntax in any code expression that is in a condition or consequence. The default value is Java. Drools currently supports one more dialect called mvel.

Specifically mvel is an expression language for Java-based applications. And it's based on Java syntax. more info about mvel




回答3:


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.

Regards.




回答4:


if you use dialect mvel - will fix your error. Otherwise the scope of that variable is private by default, so use the default getter. getField(). replace "Field" with yoru fieldname.

You can see the source code of the class in Data Objects -> class -> source tab in Business Central.




回答5:


I found some thing on this.I shared with that.Drools was supported Java or MVEL scripting language.To get object properties values.For Example,The Fibonacci has bean and having multiple properties i.e.,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 if sequence ==1 then value is -1 .The sequence values are setting into Fibonacci object.We are checked the values based on MVEL or java.MVEL is a superset of Java.



来源:https://stackoverflow.com/questions/17259341/what-is-mvel-dialect-in-drools

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!