Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't run hello world on drools - dlr files are not picked from classpath by KieContainer

Tags:

java

drools

Following documentation: 6.1. The Basics I created a simple class Applicant which should be checked with drl file loaded from the class path by KieContainer.

From the doc:

"At this point it is possible to create a KieContainer that reads the files to be built, from the classpath.

KieServices kieServices = KieServices.Factory.get();

KieContainer kContainer = kieServices.getKieClasspathContainer();

The above code snippet compiles all the DRL files found on the classpath and put the result of this compilation, a KieModule, in the KieContainer. If there are no errors, we are now ready to create our session from the KieContainer and execute against some data:.."

The problem is that the drl (rules files) are not loaded into the project by the KieContainer, and not applied to my test object.

Test method:

first two lines are from the older version just to check that the file is actually on the class path. And it does find the rules file. The rules files is located under: src/main/resources/bla/checkLicense.drl - correctly under resources.

        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); 

        kbuilder.add(ResourceFactory.newClassPathResource("bla/checkLicense.drl"), ResourceType.DRL);

        KieServices kieServices = KieServices.Factory.get();

        KieContainer kContainer = kieServices.getKieClasspathContainer();

        KieSession kSession = kContainer.newKieSession();

        Applicant applicant = new Applicant("Mr John Smith",16);

        System.out.println(applicant.toString());

        assertTrue(applicant.isValid());

        kSession.insert(applicant);

        kSession.fireAllRules();

        System.out.printf(applicant.toString());
        assertFalse(applicant.isValid());

The output:

[main] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Found kmodule: file:/Users/<MyUserName>/Drools/target/classes/META-INF/kmodule.xml
[main] WARN org.drools.compiler.kie.builder.impl.ClasspathKieProject - Unable to find pom.properties in /Users/<MyUserName>/Drools/target/classes
[main] INFO org.drools.compiler.kie.builder.impl.ClasspathKieProject - Recursed up folders,  found and used pom.xml /Users/<MyUserName>/Drools/pom.xml
[main] INFO org.drools.compiler.kie.builder.impl.KieRepositoryImpl - KieModule was added:FileKieModule[ ReleaseId=drools:drools-test:6.2.0-SNAPSHOTfile=/Users/<MyUserName>/Drools/target/classes]

[main] WARN org.drools.compiler.kie.builder.impl.AbstractKieModule - No files found for KieBase HelloWorldKB, searching folder /Users/<MyUserName>/Drools/target/classes

Applicant{name='Mr John Smith', age=16, valid=true}
Applicant{name='Mr John Smith', age=16, valid=true}

The applicant object stayed the same, while should've become invalid after rules invocation if the rule file was actually founded and loaded. The warning message does not appear for the git test projects provided by drools community...

My pom uses the same remote jboss remote repo and 6.2.0 SNAPSHOT dependencies...

What am I missing?

(since I am loosing my hair here, the additional +50/+100 will be awarded to the saviour, post answer acceptance)

(ignore HelloWorld in the picture)

enter image description here

like image 320
Aubergine Avatar asked Nov 29 '22 07:11

Aubergine


1 Answers

(This rant is obsolete. It seems 6.2.0 is only available as a SNAPSHOT (which you'd better leave alone). [And I couldn't find a zipped tarfile for the 6.1.0-Final on first try - found this later.] I dislike the obscure ways Drools distributions since 5.6.0 are offered to the "community". The last version I managed to get with a simple download was 6.0.0-Final. And therefore... End of rant.)

A simple technique for compiling one or more drl files programmatically that works since 6.0.0 is this:

private KieSession kieSession;

public void build() throws Exception {
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kfs = kieServices.newKieFileSystem();

    // for each DRL file, referenced by a plain old path name:
    FileInputStream fis = new FileInputStream( "simple/simple.drl" );
    kfs.write( "src/main/resources/simple.drl",
                kieServices.getResources().newInputStreamResource( fis ) );

    KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();
    Results results = kieBuilder.getResults();
    if( results.hasMessages( Message.Level.ERROR ) ){
        System.out.println( results.getMessages() );
        throw new IllegalStateException( "### errors ###" );
    }

    KieContainer kieContainer =
    kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );

    KieBase kieBase = kieContainer.getKieBase();
    kieSession = kieContainer.newKieSession();
}
like image 75
laune Avatar answered Dec 15 '22 17:12

laune