作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 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/
我是一名优秀的程序员,十分优秀!