Drools 7 - How to load new rules to a kieSession at runtime

5,899 views
Skip to first unread message

José Alexandre D'Abruzzo Pereira

unread,
Apr 23, 2018, 4:47:59 AM4/23/18
to drools...@googlegroups.com
Hi,

I'm trying to load new rules at runtime. Is there a way to do that without creating a new KieSession?
Also, is there a suggested approach to keep polling rules from a database?

Regards,
José Alexandre D'Abruzzo Pereira

José Alexandre D'Abruzzo Pereira

unread,
Apr 26, 2018, 11:45:37 AM4/26/18
to drools...@googlegroups.com
It seems it is not supposed to change the rules at runtime in previous versions of Drools: https://stackoverflow.com/questions/22205945/drools-6-add-rules-to-a-running-kiesession

I'm using Drools 7. Is it still the same? I created a new sessions with the new rules, but I'm not sure if this is the right approach.

Do you have any suggestions?

Atenciosamente,

José Alexandre D'Abruzzo Pereira

Mark Proctor

unread,
Apr 26, 2018, 5:17:22 PM4/26/18
to drools...@googlegroups.com
It is possible to change the rules, both in version 6 and version 7.  It can even work for existing running stateful sessions.

Mark

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.
To post to this group, send email to drools...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/drools-usage/CANfAs_EStdURD1DgwvwJA99azP%2BbaCaZbn0ETX6V1A3VdfMahg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

José Alexandre D'Abruzzo Pereira

unread,
Apr 27, 2018, 9:04:46 AM4/27/18
to drools...@googlegroups.com
Thank you, Mark!

How can I do that without destroying the session? In other words, I want to add new rules without changing the objects that I have in the working memory.

On Thu, Apr 26, 2018 at 10:17 PM Mark Proctor <mdpr...@gmail.com> wrote:
It is possible to change the rules, both in version 6 and version 7.  It can even work for existing running stateful sessions.

Mark

On Thu, Apr 26, 2018 at 4:45 PM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
It seems it is not supposed to change the rules at runtime in previous versions of Drools: https://stackoverflow.com/questions/22205945/drools-6-add-rules-to-a-running-kiesession

I'm using Drools 7. Is it still the same? I created a new sessions with the new rules, but I'm not sure if this is the right approach.

Do you have any suggestions?

Atenciosamente,
José Alexandre D'Abruzzo Pereira

On Mon, Apr 23, 2018 at 9:47 AM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
Hi,

I'm trying to load new rules at runtime. Is there a way to do that without creating a new KieSession?
Also, is there a suggested approach to keep polling rules from a database?

Regards,
José Alexandre D'Abruzzo Pereira

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

Mark Proctor

unread,
Apr 27, 2018, 9:28:52 AM4/27/18
to drools...@googlegroups.com
Just call updateToVersion on the kiecontianer, the rest is magic ;)

Mark

On Fri, Apr 27, 2018 at 2:04 PM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
Thank you, Mark!

How can I do that without destroying the session? In other words, I want to add new rules without changing the objects that I have in the working memory.

On Thu, Apr 26, 2018 at 10:17 PM Mark Proctor <mdpr...@gmail.com> wrote:
It is possible to change the rules, both in version 6 and version 7.  It can even work for existing running stateful sessions.

Mark

On Thu, Apr 26, 2018 at 4:45 PM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
It seems it is not supposed to change the rules at runtime in previous versions of Drools: https://stackoverflow.com/questions/22205945/drools-6-add-rules-to-a-running-kiesession

I'm using Drools 7. Is it still the same? I created a new sessions with the new rules, but I'm not sure if this is the right approach.

Do you have any suggestions?

Atenciosamente,
José Alexandre D'Abruzzo Pereira

On Mon, Apr 23, 2018 at 9:47 AM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
Hi,

I'm trying to load new rules at runtime. Is there a way to do that without creating a new KieSession?
Also, is there a suggested approach to keep polling rules from a database?

Regards,
José Alexandre D'Abruzzo Pereira

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

José Alexandre D'Abruzzo Pereira

unread,
May 1, 2018, 5:51:33 PM5/1/18
to drools...@googlegroups.com
Mark,

Thank you for replying!
I've been looking for some examples of updateToVersion, but I'm not being successful.
The error that I'm getting is:
Line 1:0 mismatched input 'PK '  expecting one of the following tokens: '[package, unit, import, global, declare, function, rule, query]'.

Right now, I have a simple rule just to make sure that the process works, but it is not working.

        String drl = "package org.foo.bar\n" + 
                "dialect \"mvel\"" +
                "rule X when\n" + 
                "    $s : String()\n" + 
                "then\n" + 
                "    System.out.println( $s );" +
                "end\n";
        rules.add(drl); //rules is a List<String>

        ReleaseId releaseId = new ReleaseIdImpl("foo", "bar", "1.0.0-" + UUID.randomUUID().toString().replaceAll("-", ""));

        Resource resource = ks.getResources().newByteArrayResource(createKJar(releaseId, rules));
        KieModule module = ks.getRepository().addKieModule(resource);
        kc = ks.newKieContainer(module.getReleaseId()); // kc is KieContainer

        kbuilder.add(resource, ResourceType.DRL); // kbuilder is a KnowledgeBuilder

         if (kbuilder.hasErrors())
            // do something

I'm getting an error when I check if there is any error. Am I doing something wrong?

Thank you,
José

On Fri, Apr 27, 2018 at 2:28 PM Mark Proctor <mdpr...@gmail.com> wrote:
Just call updateToVersion on the kiecontianer, the rest is magic ;)

Mark
On Fri, Apr 27, 2018 at 2:04 PM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
Thank you, Mark!

How can I do that without destroying the session? In other words, I want to add new rules without changing the objects that I have in the working memory.

On Thu, Apr 26, 2018 at 10:17 PM Mark Proctor <mdpr...@gmail.com> wrote:
It is possible to change the rules, both in version 6 and version 7.  It can even work for existing running stateful sessions.

Mark

On Thu, Apr 26, 2018 at 4:45 PM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
It seems it is not supposed to change the rules at runtime in previous versions of Drools: https://stackoverflow.com/questions/22205945/drools-6-add-rules-to-a-running-kiesession

I'm using Drools 7. Is it still the same? I created a new sessions with the new rules, but I'm not sure if this is the right approach.

Do you have any suggestions?

Atenciosamente,
José Alexandre D'Abruzzo Pereira

On Mon, Apr 23, 2018 at 9:47 AM, José Alexandre D'Abruzzo Pereira <jos...@gmail.com> wrote:
Hi,

I'm trying to load new rules at runtime. Is there a way to do that without creating a new KieSession?
Also, is there a suggested approach to keep polling rules from a database?

Regards,
José Alexandre D'Abruzzo Pereira

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

Mark Proctor

unread,
May 1, 2018, 10:17:21 PM5/1/18
to drools...@googlegroups.com
looks like something is wrong with your createKJar, I can't see where PK would be coming from.

Mark

Mark

To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage+unsubscribe@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

José Alexandre D'Abruzzo Pereira

unread,
May 2, 2018, 4:38:43 AM5/2/18
to drools...@googlegroups.com
Mark,

The createKJar method is listed below. I found from it on the following page: https://www.programcreek.com/java-api-examples/?api=org.kie.api.builder.ReleaseId

    public static byte[] createKJar(ReleaseId releaseId, List<String> drls) {
        KieFileSystem kfs = ks.newKieFileSystem();
        kfs.generateAndWritePomXML(releaseId);
        drls.forEach(drl -> kfs.write("src/main/resources/" + drl.hashCode() + ".drl", drl));
        KieBuilder kb = ks.newKieBuilder(kfs).buildAll();
        if (kb.getResults().hasMessages(Message.Level.ERROR)) {
            for (Message result : kb.getResults().getMessages()) {
                System.out.println(result.getText());
            }
            return null;
        }
        InternalKieModule kieModule = (InternalKieModule) ks.getRepository().getKieModule(releaseId);
        byte[] jar = kieModule.getBytes();
        return jar;
    }
    
The PK error that you are seeing comes from the errors of the kbuilder. The drl in it is roughly the same I sent you before. The only difference is that I added a few line breaks that were missing. Anyway, the error remains the same.

Regards,
José Alexandre D'Abruzzo Pereira
Mark

Mark

To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Drools Usage" group.
To unsubscribe from this group and stop receiving emails from it, send an email to drools-usage...@googlegroups.com.

To post to this group, send email to drools...@googlegroups.com.

ree...@gmail.com

unread,
May 4, 2018, 1:54:43 AM5/4/18
to Drools Usage
Hi,
I am joining in this discussion, as i have similar requirement.

Drools version:  7.5
Requirement :     I want to edit the rules placed in a location dynamically. 


I have tried the sample  from the link below:

It works as. First time the demo does not hit the rule, a rule file is dynamically added in the middle, and the second hit is matched again. But is it possible to pass the drl file itself not the string as pasted below:
   
Private static final String rules = "package com.sample; import com.sample.Message; rule \"Hello World \" when message:Message (status == 1) then System.out.println(\"hello, Drools!\"); end";

Is the alternative for all this code in the link is KieScanner() below is sample i have tried but when i edit drl in running session still the changes are not taken

KieServices kieServices = KieServices.Factory.get();
ReleaseId releaseId = kieServices.newReleaseId("org.acme", "myartifact", "1.0-SNAPSHOT");
KieContainer kContainer = kieServices.newKieContainer( releaseId );
// Start the KieScanner polling the Maven repository every 10 seconds
kScanner.start( 10000L );


Please help. 

Thanks in advance
Reply all
Reply to author
Forward
0 new messages