Drools: NullPointerException when addPackageFromDr

2019-08-02 03:48发布

I'm trying to execute a simple HelloWorld rule within an OSGi application. During parsing and compiling however, the following exception occurs:

java.lang.NullPointerException
at org.drools.rule.builder.RuleBuilder.build(RuleBuilder.java:47)
at org.drools.compiler.PackageBuilder.addRule(PackageBuilder.java:446)
at org.drools.compiler.PackageBuilder.addPackage(PackageBuilder.java:304)
at org.drools.compiler.PackageBuilder.addPackageFromDrl(PackageBuilder.java:167)

The DRL file is found by the application, since introducing syntax errors results in a failed compilation warning. I guess I'm overlooking something trivial, but haven't found it yet...

I'm using Drools 4.0.7, since this one was available on the Springsource Enterprise Bundle Repository. Here are my application code and drl:

            //read in the source
        Reader source = new InputStreamReader( getClass().getResourceAsStream( "hello.drl" ) );

        PackageBuilder builder = new PackageBuilder();


        //this wil parse and compile in one step
        builder.addPackageFromDrl( source );

        // Check the builder for errors
        if ( builder.hasErrors() ) {
            System.out.println( builder.getErrors().toString() );
            throw new RuntimeException( "Unable to compile \"hello.drl\".");
        }

        //get the compiled package (which is serializable)
        org.drools.rule.Package pkg = builder.getPackage();

        //add the package to a rulebase (deploy the rule package).
        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
        ruleBase.addPackage( pkg );

        StatefulSession session = ruleBase.newStatefulSession();

        session.fireAllRules();


#created on: May 1, 2011

package test

rule "A stand alone rule"

when
    eval(true)
then 
    System.out.println("hello world");

end

As always, help is highly appreciated. KR,

Niels

EDIT: During debugging I noticed that the internal builder object within the PackageBuilder was null, as were the package and packagedescription. I got around the original problem by adding this description manually:

    PackageBuilder builder = new PackageBuilder();
        PackageDescr packageDescr = new PackageDescr("be.ugent.intec.doctr.processor.job.fever");
        builder.addPackage(packageDescr);

        //this will parse and compile in one step
        builder.addPackageFromDrl( source );

My rule was edited to the following form:

package be.ugent.intec.doctr.processor.job.fever
rule "hello"
when
    eval( true )
then 
    System.out.println("hello there");      
end

This however results in a compile failure:

BR.recoverFromMismatchedToken
[1,0]: unknown:1:0 mismatched token: [@0,0:6='println',<7>,1:0];
java.lang.RuntimeException: Unable to compile "hello.drl".
at be.ugent.intec.doctr.processor.job.fever.FeverJob.execute(FeverJob.java:45)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:525)

When removing the package line from the rule, then my example goes all the way through, without printing anything however. Am I again overlooking anything? I guess this is related to an issue within the drl itself, considering everything stands or falls with the declaration of the package. Just to be clear, the drl is loaded in a class contained in the package be.ugent.intec.doctr.processor.job.fever.

Thx!

1条回答
成全新的幸福
2楼-- · 2019-08-02 04:39

A NullPointerException shouldn't happen during compilation: either you get a clear compilation error during parsing (which includes line number) or it works. Drools 4.0.7 is old. This is probably already fixed in a newer version of drools. If it isn't, raise a JIRA issue.

Try a more up-to-date version of drools, preferably even a 5.2 version (5.2.0.CR1 will be out later today or tomorrow), which uses the new, better parser.

查看更多
登录 后发表回答