[英]Access to Drools returned fact object in Java Code
I have a drools rule created via the Guvnor console and the rule validates and inserts a fact into the working memory if conditions were met. 我有一个通过Guvnor控制台创建的流口水规则,如果满足条件,该规则将验证事实并将事实插入工作内存中。 The rule is:
规则是:
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 code that executes the rule is as follows 执行规则的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);
Everything looks great with the output as below: 一切看起来都很不错,输出如下:
Hello from Drools
After drools execution
count 2
I cannot seem to find a way to get the EligibilityInquiry
fact object back in my Java code and get the attributes set in the rule above ( serviceName
and status
). 我似乎找不到找到在我的Java代码中获取
EligibilityInquiry
事实对象并获取上述规则中设置的属性的方法( serviceName
和status
)。 I have used the StatefulSession
approach. 我使用了
StatefulSession
方法。
The properties file has the link to the snapshot with basic authentication via username and password. 属性文件具有通过用户名和密码进行基本身份验证的快照链接。 There are 2 total facts:
EligibilityInquiry
and Eligibility
. 总共有2个事实:
EligibilityInquiry
和Eligibility
。
I am fairly new to drools and any help with the above is appreciated. 我对流口水是相当陌生的,对上述任何帮助都将受到赞赏。
(Note: I fixed the order of statement, a typo ("XX") and removed the comments from the output. Less surprise.) (注意:我固定了语句的顺序,即错字(“ XX”),并从输出中删除了注释。不足为奇。)
This snippet assumes that EligibilityInquiry
is also declared in DRL. 此摘要假定
EligibilityInquiry
中也声明了EligibilityInquiry
。
FactType eligInqFactType = ruleBase.getFactType("mortgages", "EligibilityInquiry");
Class<?> eligInqClass = eligInqFactType.getFactClass();
ObjectFilter filter = new FilterByClass( eligInqClass );
Collection<Object> eligInqs = workingMemory.getObjects( filter );
And the filter is 过滤器是
public class FilterByClass implements ObjectFilter {
private Class<?> theClass;
public FilterByClass( Class<?> clazz ){
theClass = clazz;
}
public boolean accept(Object object){
return theClass.isInstance( object );
}
}
You might also use a query
, which takes about the same amount of code. 您可能还会使用
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 ) );
}
Or you can call workingMemory.getObjects()
and iterate the collection and check for the class of each object. 或者,您可以调用
workingMemory.getObjects()
并迭代该集合并检查每个对象的类。
for( Object obj: workingMemory.getObjects() ){
if( obj.isInstance( eligInqClass ) ){
System.out.println( eligInqClass.cast( eligInqObj ) );
}
}
Or you can (with or without inserting the created EligibilityInquiry object as a fact) add the fact to a global java.util.List eligInqList
and iterate that in your Java code. 或者,您可以(有或没有插入创建的EligibilityInquiry对象作为事实)将事实添加到
global java.util.List eligInqList
并在Java代码中进行迭代。 Note that the API of StatefulKnowledgeSession
is required (instead of WorkingMemory
). 请注意,需要
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 ) );
}
Probably an embarras de richesses. 可能是一个尴尬的富裕阶层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.