Need clear example on how to compile to KieContainer or KieBase and deserialize to external file

508 views
Skip to first unread message

arnie....@gmail.com

unread,
Sep 21, 2016, 5:34:23 PM9/21/16
to Drools Usage
I'm involved in a project that uses Drools 4. It reads in each .drl or .xls file separately, got a RuleBase instance and deserialized it to a .ser file. These files were individually deserialized at runtime as needed.

Now, with 6.3.0, I cannot figure out the various Kie* classes and technique to use to do the same thing. Based on lots of still fruitless googling, I find that I am not alone. I think I am close though:

COMPILER LOGIC

KieBase result = null;
KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();

Resource resource = kieServices.getResources().newInputStreamResource(getFileAsStream(ruleName));
resource.setResourceType(ResourceType.DRL);
kfs.write(file.getName(), resource);
KieBuilder kieBuilder = kieServices.newKieBuilder(kfs);
kieBuilder.buildAll();
if (kieBuilder.getResults().hasMessages(org.kie.api.builder.Message.Level.ERROR)) {
    throw new RuntimeException("Build time Errors: " + kieBuilder.getResults().toString());
}
KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
// get the KIE related configuration container and set the EventProcessing (from default cloud) to Stream
KieBaseConfiguration config = kieServices.newKieBaseConfiguration();
config.setOption(EventProcessingOption.STREAM);
result = kieContainer.newKieBase(config);

SERIALIZATION LOGIC

FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream out = new ObjectOutputStream(fos);

// export the KieBase
out.writeObject(kieBase);

out.flush();
out.close();

THE PROBLEM is that every .ser file generated is 23kb and some of those .drl or .xls files are large. I find it difficult to believe this is working.

I suspect that the problem is that the KieContainer that is created for stream mode is empty, even though the builder worked just fine.

Perhaps I have a step out of order, but I cannot see how.

Can someone PLEASE clarify this?

arnie....@gmail.com

unread,
Sep 22, 2016, 2:34:56 PM9/22/16
to Drools Usage
Added a reporting method, that always shows no packages or processes.

So clearly, the parsed .drl or .xls file data isn't making it from the KFS->builder to the container->base.

private static void explainKieBase(KieBase kieBase) {
        if ( kieBase == null ) return;
        Collection<Process> processes = kieBase.getProcesses();
        if ( processes == null || processes.isEmpty() ) {
            LOGGER.error("kieBase contained NO processes.");
        } else {
            LOGGER.info(MessageFormat.format("Found {0,number} processes:", processes.size()));
            for ( Process process : processes ) {
                LOGGER.info(MessageFormat.format("{0}:{1} - {2} - {3}",
                    process.getPackageName(), process.getName(), process.getType(), process.getVersion()));
            }
        }
       
        Collection<KiePackage> packages = kieBase.getKiePackages();
        if ( packages == null || packages.isEmpty() ) {
            LOGGER.error("kieBase contained NO packages.");
        } else {
            LOGGER.info(MessageFormat.format("Found {0,number} packages:", packages.size()));
            for ( KiePackage p : packages ) {
                LOGGER.info(MessageFormat.format("{0}", p.getName()));
                Collection<Rule> rules = p.getRules();
                if ( rules == null || rules.isEmpty() ) {
                    LOGGER.error("kieBase:package contained NO rules.");
                } else {
                    if ( rules == null || rules.isEmpty() ) {
                        for ( Rule rule : rules ) {
                            LOGGER.info(MessageFormat.format("{0}:{1}",
                                rule.getPackageName(), rule.getName()));
                        }
                    }
                }
            }
        }
    }

Michael Anstis

unread,
Sep 22, 2016, 2:42:04 PM9/22/16
to Drools Usage

There's a smorgasbord of examples in drools/drools-examples. I assume you've pawed through those?

--
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/b9dab9b7-b031-4636-af4c-bad821de3c8a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages