当前位置: 首页 > 知识库问答 >
问题:

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

岑和风
2023-03-14

我刚开始流口水,我试图运行你好世界,但它一直给我这个错误

packageBuilder中存在一些错误,无法解析ObjectType“Messagee”:[规则名称='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/消息/Rule_Hello_World720710180.java(1:8):包hellodrools。消息与类型冲突

我的drl文件:

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

消息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个答案

卫飞鹏
2023-03-14

尝试更改包hellodrools。发送给hellodrools包的消息

将message:message(type==“Hello”)修改为message:hellodrools。消息(类型==“Hello”)

 类似资料: