Zeng xijin
Zeng xijin

Reputation: 23

Drools rules insert new fact and fired other rules dynamic

I am new to drools and the kie-server. I got problem about how to insert new facts in working memory by fired rule RHS(then action) then activate other rules by these dynamic-insered facts.

What I expect is when BaseFeature insert into Working memory from outside and activate RuleOne, inside the RuleOne "then" it will insert the new fact RuleResult, and expected to activate the RuleTwo, but it doesn't activate RuleTwo, just RuleOne was activated, is that something I doing it wrong?

Here I have two rules and a set of facts:

FACTS definition:

public class AppResult implements java.io.Serializable
{
   static final long serialVersionUID = 1L;

   private java.lang.String key;
   private java.lang.String value;

   //getters and setters ...
}


public class BaseFeature implements java.io.Serializable
{

   static final long serialVersionUID = 1L;

   private int age;

   //getters and setters ...
}

public class RuleResult implements java.io.Serializable
{

   static final long serialVersionUID = 1L;

   private java.lang.String ruleName;
   private java.lang.Long score;
   private boolean state;

   //getters and setters ...
}

Rule definition:

rule "RuleOne"
    dialect "mvel"
    salience 100
    no-loop true
    lock-on-active true
    when
        $b : BaseFeature( age < 22 || age > 40 )
    then
        RuleResult $r = new RuleResult();
        $r.setRuleName( "RuleOne" );
        $r.setState( false );
        insertLogical( $r );
end

rule "RuleTwo"
    dialect "mvel"
    no-loop false
    lock-on-active true
    salience 10
    when
        $r : RuleResult( ruleName == "RuleOne" , state == false )
        $a : AppResult( )
    then
        $a.setKey( "PASS" );
        $a.setValue( "false" );
end

how to achieve fired-rules insert new fact and fired other rules dynamic? thank in advance!

I am using kie-server(6.5 Final) to test, and the POST and Reponse as below:

POST:

{
    "lookup": "RuleChainTestStateless",
    "commands": [
    {
        "insert": {
            "return-object": false,
            "object": {
                "com.qf.rulechaintest.BaseFeature": {
                    "age": "10"
                }
            }
        }
    },

    {
        "insert": {
            "return-object": true,
            "out-identifier": "AppResult",
            "object": {
                "com.qf.rulechaintest.AppResult": {

                }
            }
        }
    },
    {
        "fire-all-rules": ""
    },
    {
            "get-objects":{
                "out-identifier":"allFactsInWrokingMemory"
            }
    }

    ]
}

REPONSE:

{
  "type": "SUCCESS",
  "msg": "Container RuleChainTest1.1 successfully called.",
  "result": {
    "execution-results": {
      "results": [
        {
          "key": "",
          "value": 1
        },
        {
          "key": "AppResult",
          "value": {
            "com.qf.rulechaintest.AppResult": {
              "key": null,
              "value": null
            }
          }
        },
        {
          "key": "allFactsInWrokingMemory",
          "value": [
            {
              "com.qf.rulechaintest.BaseFeature": {
                "age": 10
              }
            },
            {
              "com.qf.rulechaintest.AppResult": {
                "key": null,
                "value": null
              }
            },
            {
              "com.qf.rulechaintest.RuleResult": {
                "ruleName": "RuleOne",
                "score": null,
                "state": false
              }
            }
          ]
        }
      ],
      "facts": [
        {
          "key": "AppResult",
          "value": {
            "org.drools.core.common.DefaultFactHandle": {
              "external-form": "0:2:1542374590:1542374590:2:DEFAULT:NON_TRAIT:com.qf.rulechaintest.AppResult"
            }
          }
        }
      ]
    }
  }
}

Notice that the reponse:

"com.qf.rulechaintest.AppResult": {
              "key": null,
              "value": null
            }

what it expected to be:

"com.qf.rulechaintest.AppResult": {
                  "key": "PASS",
                  "value": "false"
                }

Upvotes: 1

Views: 3112

Answers (3)

Joydip Kumar
Joydip Kumar

Reputation: 37

Try using update function "

then

    RuleResult $r = new RuleResult();
    $r.setRuleName( "RuleOne" );
    $r.setState( false );
    update ( $r );

"

Upvotes: 0

Tibor Zim&#225;nyi
Tibor Zim&#225;nyi

Reputation: 317

I agree with laune that you should read what the rule attributes mean (see ere in the docs [1]). I think it is because you have lock-on-active on the rules. Please see the docs that I linked.

[1] https://docs.jboss.org/drools/release/6.5.0.Final/drools-docs/html/ch08.html#d0e9196

Upvotes: 0

laune
laune

Reputation: 31290

If you don't know the purpose of these rule attribute you should read the documentation. Then, I think, you'll come to realize that you can remove them.

salience 100 or 10
no-loop true or false
lock-on-active true

Upvotes: 0

Related Questions