Banana Tech
Banana Tech

Reputation: 337

Drools Object Passing Issue in DRL

I have an issue here where after i set the object to "FAILED" on the first rule but when running the 2nd rule, the value still pointing to original value which is "PASS". Take note the value is passing from the kie client.

    rule "1st rule"
    dialect "java"
    when
    $ruleEngine:RuleEngine()
    then
    OutputObject outputObject = new OutputObject();
    outputObject.setResult("FAIL" );
    $ruleEngine.setOutputObject(outputObject);      
    insert ($ruleEngine);
end

rule "2nd rule"
    dialect "java"
    when
    $ruleEngine:RuleEngine(     
    ( String.valueOf($ruleEngine.getOutputObject.getResult()).equals("PASS") )  //=====> the value is still PASS            
    )

    then
    System.out.println("output object==" + $ruleEngine.getOutputObject().getResult().equals("FAIL"));  // ===> is true      
    System.out.println("output object:" + $ruleEngine.getOutputObject().getResult()); // ==> object is FAIL ?? Why?

end

Output of the Rule

output object==true
output object:FAIL

Question 1) Why the result value being set to "FAIL" but running 2nd rule during "when" checking then the result value is still "PASS"

Question 2) How can i get the result value being set as "FAIL" during 2nd rule "when" checking clause?

Upvotes: 0

Views: 810

Answers (1)

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

There is a specific 'function' to modify a fact in Drools and that is not insert. What you are doing in the first rule is to insert the fact again in the session (causing confusion to Drools). What you are looking for is the modify method:

rule "1st rule"
dialect "java"
when
  $ruleEngine:RuleEngine(outputObject!.result != "FAIL")
then
  OutputObject outputObject = new OutputObject();
  outputObject.setResult("FAIL" );

  modify($ruleEngine){
    setOutputObject(outputObject)
  };
end

rule "2nd rule"
dialect "java"
when
  $ruleEngine:RuleEngine( outputObject!.result == "PASS" )
then
  //...
end

Hope it helps,

Upvotes: 2

Related Questions