Java – cannot run Hello world on drools – kiecontainer does not select DLR files from the classpath

The following documents: 6.1 The basics I created a simple class applicant, which should be checked by using the DRL file loaded from the class path by kiecontainer

From doc:

"At this point, you can create a kiecontainer to read the file to be built from the classpath

KieServices kieServices = KieServices.Factory.get();

KieContainer kContainer = kieServices.getKieClasspathContainer();

The above code snippet compiles all DRL files found in the classpath and puts the compiled result (kiemodule) in kiecontainer If there are no errors, we are now ready to create a session from kiecontainer and execute some data: “

The problem is that the DRL (rule file) is not loaded into the project by kiecontainer, nor is it applied to my test object

Test method:

The first two lines are from the old version just to check whether the file is actually on the classpath It did find the rules file The rule file is located in 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());

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',valid=true}

The applicant object remains unchanged. If the rule file is actually created and loaded, it should become invalid after the rule call The GIT test project provided by the drools community does not display a warning message

My POM uses the same remote JBoss remote repo and 6.2 0 snapshot dependency

What did I miss?

(because I lost my hair here, an additional 50 / 100 will be awarded to the Savior, answer accept)

(HelloWorld is ignored)

Solution

(this roar is out of date. It seems that 6.2.0 is only available as snapshot (you'd better leave it alone) [I couldn't find the compressed tarfile of 6.1.0-final on my first attempt – I found that I didn't like the obscure way that the drools distribution has provided to the "community" since 5.6.0. The last version I got through a simple download was 6.0.0-final. So... It's over.)

From 6.0 0, a simple technique for programmatically compiling one or more DRL files is:

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();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>