在Java代码中访问Drools返回的事实对象

时间:2014-07-18 23:26:03

标签: java drools guvnor

我有一个通过Guvnor控制台创建的drools规则,如果符合条件,规则会验证并将事实插入到工作内存中。规则是:

    1. | rule "EligibilityCheck001" 
    2. |     dialect "mvel" 
    3. |     when 
    4. |         Eligibility( XXX== "XXX" , YYY== "YYY" , ZZZ== "ZZZ" , BBB == "BBB" ) 
    5. |     then 
    6. |         EligibilityInquiry fact0 = new EligibilityInquiry(); 
    7. |         fact0.setServiceName( "ABCD" ); 
    8. |         fact0.setMemberStatus( true ); 
    9. |         insert(fact0 ); 
   10. |         System.out.println( "Hello from Drools"); 
   11. | end 

执行规则的Java代码如下

RuleAgent ruleAgent = RuleAgent.newRuleAgent("/Guvnor.properties");
RuleBase ruleBase = ruleAgent.getRuleBase();
FactType factType = ruleBase.getFactType("mortgages.Eligibility");

Object obj = factType.newInstance();
factType.set(obj, "XXX", "XXX");
factType.set(obj, "YYY", "YYY");
factType.set(obj, "ZZZ", "XXX");
factType.set(obj, "BBB", "BBB");

WorkingMemory workingMemory = ruleBase.newStatefulSession();
workingMemory.insert(obj);
workingMemory.fireAllRules();
System.out.println("After drools execution");
long count = workingMemory.getFactCount();
System.out.println("count " + count);

输出如下所示:

Hello from Drools
After drools execution
count 2

我似乎找不到在我的Java代码中获取EligibilityInquiry事实对象并获取上述规则(serviceNamestatus)中设置的属性的方法。我使用了StatefulSession方法。

属性文件具有通过用户名和密码进行基本身份验证的快照链接。共有2个事实:EligibilityInquiryEligibility

我对流口水相当陌生,对此有任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:3)

(注意:我修正了声明的顺序,拼写错误(“XX”)并删除了输出中的注释。不那么惊讶。)

此代码段假定在DRL中也声明了EligibilityInquiry

FactType eligInqFactType = ruleBase.getFactType("mortgages", "EligibilityInquiry");
Class<?> eligInqClass = eligInqFactType.getFactClass();
ObjectFilter filter = new FilterByClass( eligInqClass );
Collection<Object> eligInqs = workingMemory.getObjects( filter );

过滤器是

public class FilterByClass implements ObjectFilter {
    private Class<?> theClass;
    public FilterByClass( Class<?> clazz ){
        theClass = clazz;
    }
    public boolean accept(Object object){
        return theClass.isInstance( object );
    } 
}

您也可以使用query,这需要大约相同数量的代码。

// DRL code
query "eligInqs" 
    eligInq : EligibilityInquiry()
end

// after return from fireAllRules
QueryResults results = workingMemory.getQueryResults( "eligInqs" );
for ( QueryResultsRow row : results ) {
    Object eligInqObj = row.get( "eligInq" );
    System.out.println( eligInqClass.cast( eligInqObj ) );
}

或者你可以调用workingMemory.getObjects()并迭代集合并检查每个对象的类。

for( Object obj: workingMemory.getObjects() ){
    if( eligInqClass.isInstance( eligInqClass ) ){
        System.out.println( eligInqClass.cast( eligInqObj ) );
    }
}

或者你可以(有或没有插入创建的EligibilityInquiry对象作为事实)将事实添加到global java.util.List eligInqList并在Java代码中迭代它。请注意,StatefulKnowledgeSession的API是必需的(而不是WorkingMemory)。

   // Java - prior to fireAllRules
   StatefulKnowledgeSession kSession() = ruleBase.newStatefulSession();

   List<?> list = new ArrayList();
   kSession.setGlobal( "eligInqList", list );

   // DRL
   global java.util.List eligInqList;

   // in a rule
   then
       EligibilityInquiry fact0 = new EligibilityInquiry(); 
       fact0.setServiceName( "ABCD" ); 
       fact0.setMemberStatus( true ); 
       insert(fact0 );  
       eligInqList.add( fact0 ); 
   end

   // after return from fireAllRules
   for( Object elem: list ){
    System.out.println( eligInqClass.cast( elem ) );
   }

可能是一个尴尬的尴尬。