You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ruleflow-group执行Drools规则时,第2条规则未触发求助

解决方案

问题根源分析

你的规则2未触发主要有两个核心问题:

  1. 规则依赖的对象未插入会话:规则的when部分要求匹配OutputDTO实例,但你只插入了A对象,会话中没有OutputDTO,导致规则条件不满足。
  2. RuleFlowGroup与AgendaGroup混淆使用:规则中声明的是ruleflow-group "status",但代码里操作的是AgendaGroup,两者是Drools中不同的分组机制,无法正确触发规则。

具体修复步骤

1. 向KieSession插入OutputDTO实例

在插入A对象之前,需要创建并插入OutputDTO对象,让规则能匹配到该实例:

List<OutputDTO> outputDTOs = new ArrayList<>();
kieSession.setGlobal("outputDTOs", outputDTOs);
// 新增:创建并插入OutputDTO
OutputDTO outputDTO = new OutputDTO();
kieSession.insert(outputDTO);
kieSession.insert(a); // A对象

2. 正确操作RuleFlowGroup而非AgendaGroup

将代码中操作AgendaGroup的部分改为操作RuleFlowGroup:

// 替换原有的AgendaGroup设置代码
kieSession.getAgenda().getRuleFlowGroup("status").setFocus();
int y = kieSession.fireAllRules();

3. 验证A对象中的B实例是否符合规则条件

确保你创建的A对象中,b属性不为空,且包含符合group == "ABC"/"XYZ"且status == "Approved"的B实例,否则规则的exists条件依然不满足。

修改后的完整执行代码示例

kieSession.getAgenda().getAgendaGroup("scope").setFocus();
int x = kieSession.fireAllRules(); // rule 1执行成功
logger.debug("Number of rules matched for getting dynamic scopes - {} ", x); // x=3

// 处理规则1的数据并创建A对象
List<OutputDTO> outputDTOs = new ArrayList<>();
kieSession.setGlobal("outputDTOs", outputDTOs);
// 插入OutputDTO实例
OutputDTO outputDTO = new OutputDTO();
kieSession.insert(outputDTO);
kieSession.insert(a); // A对象
// 正确设置RuleFlowGroup的焦点
kieSession.getAgenda().getRuleFlowGroup("status").setFocus();
int y = kieSession.fireAllRules(); // 现在y应该大于0
kieSession.dispose();
return outputDTOs;

内容的提问来源于stack exchange,提问作者Sachin Pandey

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 21:55:27