0

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

我在 guvnor 中创建了一个规则,以便在名称具有特定值时将结果插入为“通过”: 规则代码如下:

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());

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

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

4

1 回答 1

1

问题是,在您的规则中,您正在创建一个新的 Patient 实例,而不是修改现有的实例。您需要做的是绑定匹配的 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

希望能帮助到你,

于 2013-07-31T09:48:38.077 回答