drools - 无法加载 drools 规则,出现错误 : unable to resolve object type

标签 drools

我是流口水的新手,我正在尝试运行 hello world,但它一直给我这个错误

Some errors exists in packageBuilder Unable to resolve 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)

Rule Compilation error : [Rule name='Hello World'] hellodrools/Message/Rule_Hello_World720710180.java (1:8) : The package hellodrools.Message collides with a type

我的 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();
    }
}

最佳答案

尝试改变 包 hellodrools.Message 到 包hellodrools

或者

修改 消息:消息(类型=='你好') 到 消息:hellodrools.Message(类型=='Hello')

关于drools - 无法加载 drools 规则,出现错误 : unable to resolve object type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21131880/

相关文章:

java - 从 Drools 规则执行 bpmn 文件

java - Drools 从 session 中获取相同类型的对象

java - 为什么 drools 规则引擎即使在循环内给出也只执行一次?

drools - Drools Fusion CEP 中的测试事件到期

java - 在 drools 的 statelessKieSession 中插入列表作为事实

rest - Apache Camel 和 Drools 融合集成

heroku - 玩!在 Heroku 上使用 Drools 的框架 [1.2.4]

java - 如何实例化 .drl 文件中的对象

java - 升级到 Java 7 时出现 Drools 规则编译错误

java - 如何使用accumulate进行多次运算和集合