What is Mvel dialect in Drools?

前端 未结 5 1942
无人及你
无人及你 2021-01-02 16:21

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 Jbo

相关标签:
5条回答
  • 2021-01-02 16:56

    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

    0 讨论(0)
  • 2021-01-02 17:02

    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.

    0 讨论(0)
  • 2021-01-02 17:03
    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.

    0 讨论(0)
  • 2021-01-02 17:05

    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; }
    
    0 讨论(0)
  • 2021-01-02 17:16

    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.

    0 讨论(0)
提交回复
热议问题