Drools Object Passing Issue in DRL

759 Views Asked by At

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?

1

There are 1 best solutions below

4
Esteban Aliverti On

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,