我需要使用其使用关联值来执行特定规则,并尝试实现自定义议程过滤器,以便一次只执行一个特定规则,但它似乎执行所有规则。我正在使用我定义的无状态会话:
@Bean
@ConditionalOnMissingBean(KieSession.class)
public StatelessKieSession kieSession() throws IOException {
return kieContainer().newStatelessKieSession();
}
我迭代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
类看起来像这样:
import lombok.extern.slf4j.Slf4j;
import org.kie.api.runtime.rule.AgendaFilter;
import org.kie.api.runtime.rule.Match;
@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;
}
}
}
为什么会对ruleName
中指定的规则执行不同规则的任何想法?
答案 0 :(得分:0)
您在此处向List添加事实:
List<Object> facts = new ArrayList<Object>();
facts.add(entry.getValue());
在这里添加另一个Object,一个FireAllRulesCommand:
facts.add(new FireAllRulesCommand(new RuleNameEqualsAgendaFilter(ruleName)));
然后你打电话
kieSession.execute(facts);
插入所有事实,然后调用fireAllRules。所以AgendaFilter只是WM中的一个事实。
使用statefull会话,插入事实并使用AgendaFilter作为参数调用fireAllRules。