Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to load drl file from file system in drools 6.3.0

Tags:

java

drools

Hi I am trying to load drl file from file system in drools 6.3.0. I got the code from drools project. Code that is used to load drl file is as follows.

package org.drools.examples.banking;

import org.kie.internal.KnowledgeBase;
import org.kie.internal.KnowledgeBaseFactory;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.KnowledgeBuilderFactory;
import org.kie.internal.definition.KnowledgePackage;
import org.kie.internal.io.ResourceFactory;
import org.kie.internal.runtime.StatefulKnowledgeSession;
import org.kie.api.io.ResourceType;

import java.util.Collection;

public class RuleRunner {

    public RuleRunner() {
    }

    public void runRules(String[] rules,
                         Object[] facts) {

        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

        for ( int i = 0; i < rules.length; i++ ) {
            String ruleFile = rules[i];
            System.out.println( "Loading file: " + ruleFile );
            kbuilder.add( ResourceFactory.newClassPathResource( ruleFile,
                                                                        RuleRunner.class ),
                                  ResourceType.DRL );
        }

        Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
        kbase.addKnowledgePackages( pkgs );
        /*ResourceFactory.getResourceChangeNotifierService().start();
        ResourceFactory.getResourceChangeScannerService().start();
        */
        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

        for ( int i = 0; i < facts.length; i++ ) {
            Object fact = facts[i];
            System.out.println( "Inserting fact: " + fact );
            ksession.insert( fact );
        }

        ksession.fireAllRules();
    }
}

Here KnowledgeBase is deprecated. Is there any alternative for KnowledgeBase. If any alternative exist kindly provide me sample code for that. I don't want to copy drl file in resources folder of the application. Any help is appreciated. At the end i did it as follows:

KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kfs = kieServices.newKieFileSystem();
        kfs.write(ResourceFactory.newFileResource(new File("C:/Users/ab/Desktop/fly.drl")));
        KieBuilder kieBuilder = kieServices.newKieBuilder(kfs).buildAll();
        KieContainer kieContainer = kieServices.newKieContainer(kieServices.getRepository().getDefaultReleaseId());
        KieSessionConfiguration conf=  SessionConfiguration.getDefaultInstance();
        KieSession ksession = kieContainer.newKieSession(conf);
        if (kieBuilder.getResults().hasMessages(Level.ERROR)) {
            List<Message> errors = kieBuilder.getResults().getMessages(Level.ERROR);
            StringBuilder sb = new StringBuilder("Errors:");
            for (Message msg : errors) {
                sb.append("\n  " + msg);
            }
            throw new Exception(sb.toString());
        }

        System.out.println("KieServices built: ");
like image 814
chandra Avatar asked Feb 16 '16 02:02

chandra


People also ask

What is DRL file in drools?

DRL (Drools Rule Language) rules are business rules that you define directly in . drl text files. These DRL files are the source in which all other rule assets in Business Central are ultimately rendered.


1 Answers

I'm using Drools 6.3.0 and I could load rules from a .drl file using the following code.

KieServices ks = KieServices.Factory.get();
KieRepository kr = ks.getRepository();
KieFileSystem kfs = ks.newKieFileSystem();

kfs.write(ResourceFactory.newClassPathResource("rules/ruleset.drl", this.getClass()));

KieBuilder kb = ks.newKieBuilder(kfs);

kb.buildAll(); // kieModule is automatically deployed to KieRepository if successfully built.
if (kb.getResults().hasMessages(Message.Level.ERROR)) {
    throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
}

KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());

KieSession kSession = kContainer.newKieSession();

for ( int i = 0; i < facts.length; i++ ) {
    Object fact = facts[i];
    System.out.println( "Inserting fact: " + fact );
    kSession.insert( fact );
}

System.out.println("Fire All Rules...");
kSession.fireAllRules();
kSession.dispose();

I have my rule file inside the resources/rules directory.

like image 194
CharithaMadu Avatar answered Oct 03 '22 20:10

CharithaMadu