无法加载drools规则,收到错误:无法解析对象类型

时间:2014-01-15 07:56:18

标签: drools

我是drools的新手,我正试图运行你好的世界,但它一直给我这个错误

  

packageBuilder中存在一些错误   无法解析ObjectType的'Messagee':[Rule name ='Hello World']

Exception in thread "main" java.lang.IllegalArgumentException: Could
not parse knowledge.
at hellodrools.HelloWorldRuleTest.initialiseDrools(HelloWorldRuleTest.java:55)
at hellodrools.HelloWorldRuleTest.main(HelloWorldRuleTest.java:26)
     

规则编译错误:[规则名称='Hello World']     hellodrools / Message / Rule_Hello_World720710180.java(1:8):   package hellodrools.Message与类型碰撞

我的drl文件:

package hellodrools.Message
rule 'Hello World'
when
message:Message (type == 'Hello')
then
message.printMessage();
end

Message.java

package hellodrools;

public class Message{

    private String type;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public void printMessage() {
        System.out.println("Type: " + type);
    }
}

HelloWorldTestRule.java

package hellodrools;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.drools.core.RuleBase;
import org.drools.core.RuleBaseFactory;
import org.drools.core.StatefulSession;
import org.drools.compiler.compiler.DroolsParserException;
import org.drools.compiler.compiler.PackageBuilder;
import org.drools.compiler.compiler.PackageBuilderErrors;
import hellodrools.Message;


public class HelloWorldRuleTest {
    private static RuleBase rbase = RuleBaseFactory.newRuleBase();;
    private static PackageBuilder pbuilder = new PackageBuilder();
    private static StatefulSession sessionObject;
    private static String DRL_FILE = "/helloWorld.drl";



    public static void main(String[] args) {
        initialiseDrools();
        initiliseMessageObject();
        runRules();
    }

    // Method to initialise the package builder and add package to the rule
    // base.
    private static void initialiseDrools() {
        // 1. Read the DRL File and add to package builder
        try {
            Reader reader = new InputStreamReader(
                    HelloWorldRuleTest.class.getResourceAsStream(DRL_FILE));
            pbuilder.addPackageFromDrl(reader);
        } catch (DroolsParserException ex) {
            Logger.getLogger(HelloWorldRuleTest.class.getName()).log(
                    Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(HelloWorldRuleTest.class.getName()).log(
                    Level.SEVERE, null, ex);
        }

        // 2. Check for any errors
        PackageBuilderErrors errors = pbuilder.getErrors();

        if (errors.getErrors().length > 0) {
            System.out.println("Some errors exists in packageBuilder");
            for (int i = 0; i < errors.getErrors().length; i++) {
                System.out.println(errors.getErrors()[i]);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }

        // 3. Add package to rule base
        try {
            rbase.addPackage(pbuilder.getPackage());
        } catch (Exception e) {
            System.out.println("Error: " + e);
        }
    }

    // Method to insert message object in session
    private static void initiliseMessageObject() {
        Message msg = new Message();
        msg.setType("Hello");
        sessionObject = rbase.newStatefulSession();
        sessionObject.insert(msg);
    }

    // Method to fire all rules
    private static void runRules() {
        sessionObject.fireAllRules();
    }
}

1 个答案:

答案 0 :(得分:4)

尝试改变 包hellodrools.Message 至 包装hellodrools

修改 message:消息(type =='Hello') 至 消息:hellodrools.Message(type =='Hello')