Flowable-自定义规则引擎

业务中需要复杂的规则判断,或者flowable无法满足规则需求,或者需要调用自己的规则服务做判断

编写一个规则服务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Component
@Slf4j
public class RuleService {

@Autowired
private RuleEngineService ruleEngineService;

public boolean execute(String ruleExpression, DelegateExecution execution) {
log.info("开始执行规则 ================> {}", ruleExpression);
Map<String, Object> variables = execution.getVariables();//获取全局变量
Map<String, Object> variablesLocal = execution.getVariablesLocal();//获取局部变量
log.info("variables -> {} ", variables.toString());
log.info("variablesLocal -> {} ", variablesLocal.toString());
Map<String, Object> param = new HashMap<>();
param.putAll(variables);
param.putAll(variablesLocal);
Boolean ruleResult = ruleEngineService.executeRule(ruleExpression, param);
log.info("规则[{}]执行结果 -> {}", ruleExpression, ruleResult);
return ruleResult;
}
}

RuleEngineService服务可以使用Groovy或者Drools实现,点击查看Groovy示例

在xml编写规则表达式时,可以直接改为调用RuleService.execute()

1
2
3
<sequenceFlow id="Flow_1kh3iy1" name="审批通过&#38;&#38;f3==a" sourceRef="Gateway_0corauh" targetRef="Activity_1wg31no">
<conditionExpression xsi:type="tFormalExpression">${ruleService.execute('<![CDATA[latestActionButton=="SP"&&formValue.f3=="a"]]>', execution)}</conditionExpression>
</sequenceFlow>