gpt4 book ai didi

java - 使用 dsl 和 dslr 文件时如何构建 Kie session ?

转载 作者:行者123 更新时间:2023-12-02 04:26:40 26 4
gpt4 key购买 nike

我是流口水的新手。我已经执行了 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 session 并触发规则

 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 文件 rul 时没有给出异常(exception)) enter image description here

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

谢谢

最佳答案

来电

... = 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)

关于java - 使用 dsl 和 dslr 文件时如何构建 Kie session ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32063120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com