自定义AgendaFilter执行指定规则
自定义AgendaFilter来执行指定的规则。
创建KIESession部分代码:
@Bean @ConditionalOnMissingBean(KieSession.class) public KieSession kieSession() throws IOException { return kieContainer().newKieSession(); }
遍历Map,执行每一个规则:
for (Map.Entry<Long, String> entry : ruleIdsAndData.entrySet()){ List<Object> facts = new ArrayList<Object>(); facts.add(entry.getValue()); Map<String, String> ruleResponse = new HashMap<String, String>(); kieSession.setGlobal("ruleResponse", ruleResponse); TrackingAgendaEventListener agendaEventListener = new TrackingAgendaEventListener(); kieSession.addEventListener(agendaEventListener); String ruleName = myRuleService.retrieveRuleNameById(entry.getKey()); log.debug("Attempting to fire the following rule : " + ruleName); facts.add(new FireAllRulesCommand(new RuleNameEqualsAgendaFilter(ruleName))); kieSession.execute(facts); log.debug("RuleResponse after rule has fired is : " + ruleResponse); }
自定义RuleNameEqualsAgendaFilter代码如下:
@Slf4j public class RuleNameEqualsAgendaFilter implements AgendaFilter { private final String ruleName; private final boolean accept; public RuleNameEqualsAgendaFilter(final String name) { this(name, true); } public RuleNameEqualsAgendaFilter(final String name, final boolean accept) { this.ruleName = name; this.accept = accept; } public String getName() { return ruleName; } public boolean isAccept() { return accept; } public boolean accept( Match match) { log.debug("Comparing : " + match.getRule().getName() + " to ruleName : " + this.ruleName); if (match.getRule().getName().equals(this.ruleName)) { return this.accept; } else { return !this.accept; } } }
关注公众号:程序新视界,一个让你软实力、硬技术同步提升的平台
除非注明,否则均为程序新视界原创文章,转载必须以链接形式标明本文链接