gpt4 book ai didi

java - 如何在java代码中获取guvnor规则的输出结果

转载 作者:行者123 更新时间:2023-12-01 14:17:41 25 4
gpt4 key购买 nike

我在 guvnor 上上传了一个患者模型 jar,该类有名称和结果字段。

我在 guvnor 中创建了一条规则,只要名称具有特定值,就会将结果插入为“pass”:规则代码如下:

rule "IsJohn"
dialect "mvel"
when
Patient( name == "John")
then
Patient fact0 = new Patient();
fact0.setResultString( "Pass" );
fact0.setName( "Patient: John" );
insert( fact0 );
end

下面是调用该规则的java代码。

        KnowledgeBase knowledgeBase = readKnowledgeBase();
StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();

Patient patient = new Patient();

patient.setName("John");

System.out.println("patient.name "+patient.getName());
session.insert(patient);
session.fireAllRules();

System.out.println("************patient.name "+patient.getName());
System.out.println("patient result string is "+patient.getResultString());

但是当我运行此代码时,我得到与 null 相同的名称和结果字符串。那么我在这里犯了什么错误。

基本上,我只需要一种可以调用简单规则并使用 java 显示回结果的方法。有没有例子可以证明这一点。

最佳答案

问题在于,在您的规则中,您正在创建 Patient 的新实例,而不是修改现有实例。您需要做的是绑定(bind)匹配的 Patient 并在您的 RHS 中使用它:

rule "IsJohn"
dialect "mvel"
when
fact0: Patient( name == "John")
then
fact0.setResultString( "Pass" );
fact0.setName( "Patient: John" );
update( fact0 );
// Only do the 'update' if you want other rules to be aware of this change.
// Even better, if you want other rules to notice these changes use 'modify'
// construct insted of 'update'. From the java perspective, you don't need
// to do this step: you are already invoking setResultString() and setName()
// on the real java object.
end

希望对你有帮助

关于java - 如何在java代码中获取guvnor规则的输出结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17962536/

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