如何在使用dsl和dslr文件时构建Kie会话?

时间:2015-08-18 03:37:02

标签: java drools rule-engine

我是流口水的新手。我已经从我的drl文件中执行了规则。 这是我的drl文件。

rule "Person is 21"
salience 1
    when
        $person : Person(age>=21,car.equalsIgnoreCase("duster"))               
    then
        System.out.println($person.name);
end

这是我的模型文件

public class Person
{
    private String name;
    private int age;
    private String car;




    public Person()
    {

    }

    public Person(String name, int age,String car)
    {
        this.name = name;
        this.age = age;
        this.car=car;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }


    public String getCar() {
        return car;
    }

    public void setCar(String car) {
        this.car = car;
    }

}

这是我的规则运行器方法代码,它将规则和事实作为参数并构建kie会话并触发规则

 public void runRules(String[] rules, Object[] facts)
    {

        KieServices kieServices = KieServices.Factory.get();
        KieResources kieResources = kieServices.getResources();
        KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
        KieRepository kieRepository = kieServices.getRepository();

        for(String ruleFile : rules)
        {
            Resource resource = kieResources.newClassPathResource(ruleFile);

            // path has to start with src/main/resources
            // append it with the package from the rule
            kieFileSystem.write( resource);
        }

        KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);

        kb.buildAll();

        if (kb.getResults().hasMessages(Level.ERROR))
        {
            throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
        }

        KieContainer kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());

        KieSession kSession = kContainer.newKieSession();

        for (Object fact : facts)
        {
            kSession.insert(fact);
        }

        kSession.fireAllRules();
    }

当我使用dslr文件时,我应该做哪些更改(下面是dslr文件)

rule "My First Rule"

    when
        Person is at least 21 and car is "duster"
    then
        Log : "hello world"

end

在eclipse中,从dslr文件生成的drl文件是正确的。 下面是dsl扩展器生成的drl文件中的规则(我可以在eclipse中的drl查看器中看到上面的dslr)

rule "My First Rule"

    when
        i: Person(age > 21, car=="duster")
    then
        System.out.println("hello world");

end

我尝试从我的drl文件运行相同的规则并成功执行

我面临的问题是我尝试将DSL和DSLR文件作为参数传递给规则资源

Resource resource = kieResources.newClassPathResource("MyDslr.dslr","dslExample.dsl");

我收到以下异常

Exception in thread "main" java.lang.RuntimeException: Build Errors:
Error Messages:
Message [id=1, level=ERROR, path=MyDslr.dslr, line=17, column=0
   text=[17] No mapping entries for expanding:         Person is at least 21 and
 car is "duster"]
Message [id=2, level=ERROR, path=MyDslr.dslr, line=17, column=0
   text=[17] Unable to expand: Person is at least 21 and car is "duster"]
Message [id=3, level=ERROR, path=MyDslr.dslr, line=19, column=0
   text=[19] No mapping entries for expanding:         Log : "hello world"]
Message [id=4, level=ERROR, path=MyDslr.dslr, line=19, column=0
   text=[19] Unable to expand:         Log : "hello world"]
Message [id=5, level=ERROR, path=MyDslr.dslr, line=17, column=0
   text=[ERR 102] Line 17:15 mismatched input 'is' in rule "My First Rule"]
---
Warning Messages:
---
Info Messages:

    at controller.RuleRunner.runRules(RuleRunner.java:43)
    at mainPackage.Main.main(Main.java:21)

以下是我的dsl文件在eclipse中的样子(并且它正在转换drl中的dslr,转换后的规则在直接从drl文件中获取时不会出现异常) enter image description here

请帮帮我。如果有使用kie api和dslr以及dsl文件触发规则的示例,那么这也会有所帮助。

由于

1 个答案:

答案 0 :(得分:2)

电话

... = kieResources.newClassPathResource("MyDslr.dslr","dslExample.dsl")

不能正确 - 比较此方法的Javadoc:第二个论证应指明编码。

然而,这一系列陈述有效:

FileInputStream fis1 = new FileInputStream( "simple/simple.dsl" );
kfs.write( "src/main/resources/simple.dsl",
           kieServices.getResources().newInputStreamResource( fis1 ) );
FileInputStream fis2 = new FileInputStream( "simple/simple.dslr" );
kfs.write( "src/main/resources/simple.dslr",
           kieServices.getResources().newInputStreamResource( fis2 ) );
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();

您应该使用一系列路径名称DSL和DSLR来调用方法runRules(String[] rules, Object[] facts)