[英]How to get output result from guvnor rule in java code
I uploaded a patient model jar on guvnor, the class has name and result field. 我在guvnor上上传了一个耐心模型罐,该类具有名称和结果字段。
I created a rule in guvnor to insert result as "pass" whenever the name has particular value: The code of rule is as below: 我在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
Below is the java code to call this rule. 以下是调用此规则的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());
But when I run this code I get same name and result string as null. 但是,当我运行此代码时,我得到的名称和结果字符串都为null。 So what mistake am I doing here.
所以我在这里犯什么错误。
Basically I just need a way through which I can call a simple rule and display back the result using java. 基本上,我只需要一种方法即可调用简单规则并使用java显示结果。 Is there any example demonstrating it.
是否有任何示例证明这一点。
The problem is that, in your rule, you are creating a new instance of Patient instead of modifying the existing one. 问题是,按照您的规则,您正在创建一个新的Patient实例,而不是修改现有实例。 What you need to do is to bind the matching Patient and use it in your RHS:
您需要做的是绑定匹配的患者并在您的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
Hope it helps, 希望能帮助到你,
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.