Existing KieContainer not updating correctly

56 views
Skip to first unread message

Vishesh Mukherjee

unread,
Feb 25, 2025, 12:20:07 PMFeb 25
to Drools Usage

When I'm removing a rule from the KieBase and then adding the same rule again and building the KieFileSystem, the rule is not being added to the KieBase. This happens when I'm updating the existing KieContainer. If I create a new KieContainer, the rule is added correctly.

In the case of updating the existing KieContainer, I have also tried adding new rules, and they are getting added properly. It's just the deleted rule that is not being added.


I'm using org.kie:kie-spring:7.52.0.Final

I have attached the code for both:

  1. Updating the existing KieContainer, and
  2. Creating a new KieContainer (commented).

```
String fooRuleText = "package demo;\n" +
  "import com.example.demo.entity.Cat;\n" +
                  "rule \"foo\"\n" +
                  "when\n" +
                  "m: Cat()\n" +
                  "then \n" +
                  "System.out.println(\"foo\");\n" +
                  "end";

String barRuleText = "package demo;\n" +
  "import com.example.demo.entity.Cat;\n" +
                  "rule \"bar\"\n" +
                  "when\n" +
                  "m: Cat()\n" +
                  "then \n" +
                  "System.out.println(\"bar\");\n" +
                  "end";

KieServices kieServices = KieServices.Factory.get();
KieModuleModel kieModuleModel = kieServices.newKieModuleModel();
KieBaseModel kieBaseModel = kieModuleModel.newKieBaseModel("demo_base");
kieBaseModel.addPackage("demo");
kieBaseModel.newKieSessionModel("demo_session");

// Adding foo rule
KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
kieFileSystem.writeKModuleXML(kieModuleModel.toXML());
kieFileSystem.write("src/main/resources/demo/foo_rule.drl", fooRuleText);
KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem).buildAll();
KieContainer kieContainer = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId());

kieContainer.getKieBase("demo_base").removeRule("demo", "foo");

// Adding foo and bar fule
kieFileSystem.write("src/main/resources/demo/foo_rule.drl", fooRuleText);
kieFileSystem.write("src/main/resources/demo/bar_rule.drl", barRuleText);
kieBuilder = kieServices.newKieBuilder(kieFileSystem).buildAll();


// When creating new kieContainer both foo and bar rule are present in the KieBase
// kieContainer = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId());

// When updating the existing KieContainer only the bar rule is present in the KieBase
kieContainer.updateToVersion(kieBuilder.getKieModule().getReleaseId());

KieSession kieSession = kieContainer.getKieBase("demo_base").newKieSession();
Cat cat = new Cat();
kieSession.insert(cat);
kieSession.fireAllRules();
```

Any reference to the docs will also help.

Thanks

Vishesh Mukherjee

unread,
Feb 25, 2025, 12:25:59 PMFeb 25
to Drools Usage
I'm also attaching the output:
1. While updating the existing KieContainer 
Output:
```
bar
```
2. While creating a new KieContainer
Output
```
bar
foo
```

Vishesh Mukherjee

unread,
Mar 5, 2025, 10:29:25 AMMar 5
to Drools Usage
Hello,

Building on top of this, I have also noticed that if I write the deleted rule in another file and override the deleted rule file with a different rule, both of the rules are being triggered.

```
// Output: foo
// kieFileSystem.write("src/main/resources/demo/foo_rule.drl", fooRuleText);
// kieFileSystem.write("src/main/resources/demo/bar_rule.drl", barRuleText);

// Output: foo bar
kieFileSystem.write("src/main/resources/demo/bar_rule.drl", fooRuleText);
kieFileSystem.write("src/main/resources/demo/foo_rule.drl", barRuleText);

kieBuilder = kieServices.newKieBuilder(kieFileSystem).buildAll();

kieContainer.updateToVersion(kieBuilder.getKieModule().getReleaseId());

KieSession kieSession = kieContainer.getKieBase("demo_base").newKieSession();
Cat cat = new Cat();
kieSession.insert(cat);
kieSession.fireAllRules();
```

Output
```
foo
bar
```

This weird behavior is what I'm trying to resolve.

Thanks
Reply all
Reply to author
Forward
0 new messages