我有大量具有类似 when
部分的 Drools 规则。例如
rule "Rule 1"
when
trn: TransactionEvent()
// some `trn` related statements
not ConfirmEvent (
processMessageId == trn.groupId )
then
// some actions
end
rule "Rule 2"
when
trn: TransactionEvent()
// some other `trn` related statements
not ConfirmEvent (
processMessageId == trn.groupId )
then
// some other actions
end
是否可以定义一次此语句
not ConfirmEvent (
processMessageId == trn.groupId )
并在需要的地方以某种方式重用?
最佳答案
两种方法的想法:
在每个规则中使用规则“extends”关键字来扩展包含共享when语句的基本规则。
使用推断事实的共享when 语句创建规则(“提取规则”)。在需要共享条件的规则的 when 条件中使用该事实。此选项通常是我的首选方法,因为它为这些条件定义了一个“概念”(命名事实),并且对每个规则仅评估一次。
#2 的规则示例:
rule "Transaction situation exists" when trn: TransactionEvent() // some `trn` related statements $optionalData : // bind as wanted not ConfirmEvent ( processMessageId == trn.groupId ) then InferredFact $inferredFact = new InferredFact($optionalData); insertLogical($inferredFact); end rule "Rule 1" when InferredFact() AdditionalCondition() then // some actions end rule "Rule 2" when InferredFact() OtherCondition() then // some other actions end
关于java - 重用部分 Drools `when` 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39779232/