Drools Dynamic rule file generation from database (drl file) using Rule Template

梦想的初衷 提交于 2019-12-08 09:38:55

问题


See below I have tried the example from the page below. Every things works fine but I am not getting the rule applied, and generated status applicable from this rule.

Location of sample code: http://dilipsarangi.blogspot.co.uk/2015/09/drools-610-dynamic-rules-in-database.html

I am able to load data from table in to the test case. but when System.out.println(aa.getName() + "," + aa.getStatus());

The aa.getstatus is null.

The dynamic generated rule .

package org.drools.template.jdbc;
dialect "mvel"

rule "ageRule_7"
    when
        $person : Person(age>=81 && age<100)
    then
     $person.status=":" + "Old Aged";
end

rule "ageRule_6"
    when
        $person : Person(age>=61 && age<81)
    then
     $person.status=":" + "Senior Citizen";
end

rule "ageRule_5"
    when
        $person : Person(age>=41 && age<61)
    then
     $person.status=":" + "Middle Aged";
end

rule "ageRule_4"
    when
        $person : Person(age>=18 && age<41)
    then
     $person.status=":" + "Youth";
end

rule "ageRule_3"
    when
        $person : Person(age>=13 && age<18)
    then
     $person.status=":" + "Juvenile";
end

rule "ageRule_2"
    when
        $person : Person(age>=6 && age<13)
    then
     $person.status=":" + "Young Age";
end

rule "ageRule_1"
    when
        $person : Person(age>=2 && age<6)
    then
     $person.status=":" + "Baby";
end

rule "ageRule_0"
    when
        $person : Person(age>=0 && age<2)
    then
     $person.status=":" + "Infant";
end

回答1:


You need to update the $person object in each rule, after amending the status property:

update($person);

Without doing this, any change to the object is not set in the working memory.

Although the above works, you are better using the modify keyword instead:

modify($person) {$person.setStatus=":" + "Infant"};

And similar for each of the rules. In this case the update keyword is not required.

There is some functionality such as property reactive beans where update cannot be used, so it is best to use modify as best-practice.



来源:https://stackoverflow.com/questions/44633476/drools-dynamic-rule-file-generation-from-database-drl-file-using-rule-template

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