1

I have implemented builder design pattern and I have conditions on the abstract class and want to update the fact that extends the abstract class. Although the rule is fired but it doesn't update the fact why?

DRL

rule "Rule_1"
    when
        $a : AnotherClas.Builder()
        $b : AnotherClas(definitionName = "test") from $a.build

    then
       $b.setUpdateFact("updated");
   
    end

JAVA CLASS

public abstract class TestClass{
    public final String definitionName;

       protected TestClass(String definitionName) {
            this.definitionName = definitionName;
        }
}

ANOTHER JAVA CLASS

public class AnotherClass extends TestClass{

    private String updateFact;

    private TestClass(String reviewDefinitionName) {
        super(reviewDefinitionName);
    }

    public void setUpdateFact(String updateFact) {
        this.updateFact= updateFact;
    }
   
    public void getUpdateFact() {
        return updateFact;
    }

    public static class Builder {
    private String definitionName;
   
    public TestClass build() {
        return new TestClass(definitionName);
    }

    public Builder definitionName(String definitionName) {
        this.definitionName = definitionName;
        return this;
     } 
   }
 }

Is there anything I am missing? please help me with the correct way to implement this

Harsha
  • 86
  • 5
  • You've not called `modify`, `insert`, or `update`, so the Drools engine won't reevaluate the matches. There's another question about this, let me track it down. – Roddy of the Frozen Peas Sep 28 '22 at 14:06
  • Does this answer your question? [Drool's Rule is holding state and fire rules when it shouldn't](https://stackoverflow.com/questions/64733003/drools-rule-is-holding-state-and-fire-rules-when-it-shouldnt) – Roddy of the Frozen Peas Sep 28 '22 at 14:14
  • Thanks for the reply I tried using modify but still no result – Harsha Sep 29 '22 at 03:48

1 Answers1

1

Your method AnotherClass.Builder.build() is creating a new instance of an object which is not inserted into the Working Memory or a DataSource. That's the reason why call of modify does not sort any effect.

First you bind $a to a static Builder:

$a : AnotherClas.Builder()

then in the from $a.build the method is invoked:

    public ReviewBatchConfig build() {
        return new ReviewBatchConfig(definitionName);
    }

as the above implementation shows this is not inserted in the WM/DS so missing the API requirements.

You could maybe consider using the builder pattern in the code before invoking the rule engine API, once from the builder pattern all objects are known in your code, you insert those in the WM/DS and invoke rule engine rules.

tarilabs
  • 2,178
  • 2
  • 15
  • 23
  • 1
    Thanks for the reply @tarilabs but I am unable to follow what you are trying to say and I think I have done the same thing what you have suggested. it would better if you can provide with an example code. – Harsha Sep 29 '22 at 08:49
  • 1
    They're saying `$b` isn't in working memory. So you're modifying its value, but it doesn't exist in memory. The only thing in memory is the builder. It's equivalent to creating a new variable inside of an if-statement, setting values, and then wondering why you can't see that those changes outside of that if-statement. – Roddy of the Frozen Peas Sep 29 '22 at 14:11
  • Thanks @RoddyoftheFrozenPeas I understood. So how should I can access the fact that is in the working memory and update the same – Harsha Sep 30 '22 at 10:36
  • You need to not put the builder into working memory, but the actual ReviewBatchConfig instance. Or call `insert($b)` and now that instance will be in working memory. – Roddy of the Frozen Peas Sep 30 '22 at 18:03
  • 1
    Thanks you guys I got the mistake I was dong. I was inserting the builder object of AnotherClass.Builder() instead I should have inserted AnotherClass .Builder().definitionName("test").build() and I was calling the the AnotherClass which was not present in the working memory but after inserting the AnotherClass .Builder().definitionName("test").build() it worked as good as it should. – Harsha Oct 06 '22 at 10:23