0

患者モデル jar を guvnor にアップロードしました。クラスには名前と結果フィールドがあります。

名前に特定の値がある場合は常に結果を「合格」として挿入するルールを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());

しかし、このコードを実行すると、同じ名前と結果の文字列が null として取得されます。それで、私はここでどんな間違いをしていますか。

基本的に、単純なルールを呼び出して、Java を使用して結果を表示する方法が必要なだけです。それを示す例はありますか。

4

1 に答える 1

1

問題は、ルールで、既存のインスタンスを変更するのではなく、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 に答える