我有一个包含44个字段的pojo类对象列表。现在我正在迭代java中的每个对象并将其传递给drool进行验证。有没有办法在drools中验证整个列表。示例代码是
rule "RuleSerialNoContent"
when
droolRecord : DroolRecordBO(flowStatus==droolRecord.FLOW_PASS,
serialNo.trim() not matches "^\\d{1,10}$" )
then
droolRecord.setRuleStatus(droolRecord.SERIAL_CONTENT_ERR+
" "+droolRecord.getSerialNo());
droolRecord.setFlowStatus(droolRecord.FLOW_ERROR);
update(droolRecord);
end
rule "RuleInstitutionIdLength"
when
droolRecord : DroolRecordBO(flowStatus==droolRecord.FLOW_PASS,
institutionId.length() != 6 )
then
droolRecord.setRuleStatus(droolRecord.INSTITUTIONID_LENGTH_ERR);
droolRecord.setFlowStatus(droolRecord.FLOW_ERROR);
update(droolRecord);
end
答案 0 :(得分:0)
有可能。插入List<DroolRecordBO>
并添加&#34;来自&#34;所有规则的条款:
rule "RuleSerialNoContent"
when
$list: List()
droolRecord : DroolRecordBO(flowStatus==droolRecord.FLOW_PASS,
serialNo.trim() not matches "^\\d{1,10}$" )
from $list
then...
您可以省略更新,因为列表元素不再是事实。
但是,请注意:您可能无法根据此微妙变化的方案调整某些规则组合。
答案 1 :(得分:0)
可以对对象列表执行操作。优点是你不必为每个对象承受编译时间的痛苦,而只是你只编译一次。
public static KnowledgeBase getKBase(String filePath) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newFileResource(filePath), ResourceType.DRL);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
}
public static void main(String args[]) {
Collection<DroolRecordBO> droolRecordBOs = // Collection of DroolRecordBO
KnowledgeBase kbase = getKBase(".drl File Path");
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
for(DroolRecordBO droolRecordBO : droolRecordBOs) {
ksession.insert(droolRecordBO);
}
ksession.fireAllRules();
ksession.dispose();
}
如果您希望按特定顺序执行规则,则可能需要考虑使用salience