如何在Drools 5中匹配多个相同类型的对象? [英] How can i match on multiple objects of the same kind in Drools 5?

查看:730
本文介绍了如何在Drools 5中匹配多个相同类型的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定两个或多个内存中的BMSContract对象的集合,我需要使用BMSContract.status字段来匹配特定的模式。如果在这样的集合中恰好一个且只有一个BMSContract的状态为ACTIVE,则我的规则应解析为Success。对象和状态代码的任何其他组合应解析为失败。同样,此规则仅适用于2个或更多对象的集合,并且可以有任意数量的对象:2、5、10、15或更多。管理单个BMSContract记录的规则集略有不同。这些规则仅适用于多个记录方案,例如:

Given a collection of BMSContract objects in memory of two or more, I need to match specific patterns using the BMSContract.status field. My rule should resolve to Success if in such collection exactly ONE and ONLY ONE BMSContract has a status of ACTIVE. Any other combination of objects and status codes should resolve to Fail. Again this rule is for a collection of 2 or more objects only and there could be any number of them: 2, 5, 10, 15 or more. There is a slightly different set of rules governing single BMSContract records. These rules are specific just to the multiple records scenarios such as these:

Case1 - Success
BMSContract(status=ACTIVE)
BMSContract(status=PENDING)
Reason:  only one Active in the collection

Case2 - Success
BMSContract(status=ACTIVE)
BMSContract(status=PENDING)
BMSContract(status=HOLD)
Reason: only one Active in the coll

Case3 - Success
BMSContract(status=ACTIVE)
BMSContract(status=PENDING)
BMSContract(status=HOLD)
BMSContract(status=CANCEL)
Reason:  only one Active in the coll

Case4 - Failure
BMSContract(status=ACTIVE)
BMSContract(status=ACTIVE)
Reason:  too many Active records in coll

Case5 - Failure
BMSContract(status=ACTIVE)
BMSContract(status=ACTIVE)
BMSContract(status=ACTIVE)
Reason:  too many Active records in coll

Case6 - Failure
BMSContract(status=ACTIVE)
BMSContract(status=ACTIVE)
BMSContract(status=ACTIVE)
BMSContract(status=ACTIVE)
BMSContract(status=ACTIVE)
Reason:  too many Active records in coll

Case7 - Failure
BMSContract(status=PENDING)
BMSContract(status=HOLD)
BMSContract(status=OTHER)
Reason:  No Active records in coll


推荐答案

鉴于您有许多 BMSContract 事实上,以下规则就足够了:

Given that you have a number of BMSContract facts, the following rules would be enough:

rule OK
when
  $ac: BMSContract( status == "ACTIVE" )
  not BMSContract( this != $ac, status == "ACTIVE" )
then
  // there is exactly one ACTIVE
end

rule not_OK_1
when
  not BMSContract( status == "ACTIVE" )
then
  // there is no ACTIVE
end

rule not_OK_2
when
  $ac: BMSContract( status == "ACTIVE" )
  exists BMSContract( this != $ac, status == "ACTIVE" )
then
  // there is more than one ACTIVE
end

如果您需要确定引起麻烦的事实,可能会出现变量,并且可能需要变量:

Variants are possible and may be required if you need to identify the facts that cause trouble:

rule not_OK_2a
when
  $ac1: BMSContract( status == "ACTIVE" )
  $ac2: BMSContract( this != $ac, status == "ACTIVE" )
then
  // log and retract $ac2
end

这篇关于如何在Drools 5中匹配多个相同类型的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆