简体   繁体   English

在另一个drl文件中的规则中使用一个drl文件中的Drools类型

[英]Using Drools types from one drl file in rules in another drl file

Using Drools 7.6.0 (although I can upgrade to a newer version). 使用Drools 7.6.0(尽管我可以升级到新版本)。

I have a system composed of a core library that is used by an application. 我有一个由应用程序使用的核心库组成的系统。 The core library defines some common rules, types, and globals in a common.drl file, and then the application defines some additional rules, types, and globals in another app.drl file. 核心库在common.drl文件中定义了一些通用规则,类型和全局变量,然后应用程序在另一个app.drl文件中定义了一些其他规则,类型和全局变量。 The rules in app.drl file may use types and globals defined in common.drl. app.drl文件中的规则可以使用common.drl中定义的类型和全局变量。

I'm trying to follow examples like this , but I get tons of errors like: 我试图按照类似的例子这样 ,但我得到吨喜欢的错误:

Unable to resolve ObjectType 'myType' : [Rule name='My Rule']

Where My Rule is from app.drl and myType is from common.drl. 我的规则来自app.drl,而myType来自common.drl。

Is this pattern not supported, or are there some additional steps I need to take to make it work? 是否不支持此模式,或者我需要采取一些其他步骤使其起作用?

Here is a simplified version demonstrating the problem: 这是演示该问题的简化版本:

common.drl: common.drl:

package mypackage

declare MyType
    myField : String
end

app.drl: app.drl:

package mypackage

rule "My Rule"
    when
        $element : MyType()
    then
        System.out.println("worked");
end

java: Java的:

package mypackage;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieBuilder;
import org.kie.api.builder.KieFileSystem;
import org.kie.api.builder.Message;
import org.kie.api.builder.Results;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

public class MultiLoadTest {

    public static void main(String[] args) throws FileNotFoundException {

        KieServices kieServices = KieServices.Factory.get();
        KieFileSystem kfs = kieServices.newKieFileSystem();
        // repeat
        FileInputStream fisCommon = new FileInputStream( "src/main/resources/rules/common.drl" );
        kfs.write( "src/main/resources/simple.drl",
                        kieServices.getResources().newInputStreamResource( fisCommon ) );

        FileInputStream fisApp = new FileInputStream( "src/main/resources/rules/app.drl" );
        kfs.write( "src/main/resources/simple.drl",
                        kieServices.getResources().newInputStreamResource( fisApp ) );
        // end
        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 kieSession = kieContainer.newKieSession();


    }

}

Output: 输出:

2018-04-23 13:53:00,872 [main] ERROR Unable to build KieBaseModel:defaultKieBase
Unable to resolve ObjectType 'MyType' : [Rule name='My Rule']


Exception in thread "main" [Message [id=1, kieBase=defaultKieBase, level=ERROR, path=simple.drl, line=5, column=0
   text=Unable to resolve ObjectType 'MyType']]
java.lang.IllegalStateException: ### errors ###
    at mypackage.MultiLoadTest.main(MultiLoadTest.java:35)

For me your example worked as expected. 对我来说,您的示例按预期工作。 So I assume that the issue is in configuration/dependencies or part that you didn't provide. 因此,我认为问题出在配置/依赖性或您未提供的部分。

Instead of going through all this details I can provide working example ( spring+maven+junit5) that shows how to insert and retrieve drools declared types from session. 除了提供所有详细信息外,我还可以提供一个工作示例(spring + maven + junit5),该示例演示如何从会话中插入和检索口水声明的类型。

Please follow GitHub link . 请点击GitHub链接

Particular test link . 特定的测试链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM