【底层服务/编程功底系列】「手把手教学系列」带你打造一个属于自己的规则引擎服务,打破任何业务难题(逻辑模型和API设计)(二)https://developer.aliyun.com/article/1471069
规则引擎RuleEngine
RuleEngine属于整个规则引擎的总体门面接口类,它主要用于操作整体的规则引擎的总体操作入口。
首先,针对于RuleEngine属于一个接口,他主要定义不同种类的规则引擎的抽象定义结构。它主要分为三个部分:RuleEngineListener-规则执行监听器,它主要作为做维护和规则引擎层面的控制监听器,获取和处理对应的RuleEngineParameter-规则引擎参数,它主要用于处理规则引擎参数将引擎级别参数进行控制对应的规则执行流程,从而进行控制对应的规则处理模式。
RuleEngineListener
该监听器与RuleListener不同,主要用于规则引擎级别的操作处理机制。它的主要作用是处理所有规则的操作,而对应的RuleListener负责处理规则内部的条件和动作操作的审计。
代码案例
java
复制代码
public interface RulesEngineListener { /** * Triggered before evaluating the rule set. * <strong>When this listener is used with a {@link InferenceRulesEngine}, * this method will be triggered before the evaluation of each candidate rule * set in each iteration.</strong> * * @param rules to fire * @param facts present before firing rules */ default void beforeEvaluate(Rules rules, Facts facts) { } /** * Triggered after executing the rule set * <strong>When this listener is used with a {@link InferenceRulesEngine}, * this method will be triggered after the execution of each candidate rule * set in each iteration.</strong> * * @param rules fired * @param facts present after firing rules */ default void afterExecute(Rules rules, Facts facts) { } }
- beforeEvaluate:在评估规则集之前触发。当与默认的 RulesEngine 一起使用时,该方法将在每次迭代中在评估每个候选规则集之前触发。
- afterExecute:在执行规则集后触发。当此监听器与默认规则引擎一起使用时,在每次迭代中执行完每个候选规则集后都会触发此方法。
代码案例
java
复制代码
public interface RulesEngine { /** * Return the rules engine parameters. * * @return The rules engine parameters */ RulesEngineParameters getParameters(); /** * Return the list of registered rule listeners. * * @return the list of registered rule listeners */ default List<RuleListener> getRuleListeners() { return Collections.emptyList(); } /** * Return the list of registered rules engine listeners. * * @return the list of registered rules engine listeners */ default List<RulesEngineListener> getRulesEngineListeners() { return Collections.emptyList(); } /** * Fire all registered rules on given facts. */ void fire(Rules rules, Facts facts); /** * Check rules without firing them. * @return a map with the result of evaluation of each rule */ default Map<Rule, Boolean> check(Rules rules, Facts facts) { return Collections.emptyMap(); } }
RulesEngineParameters
RulesEngineParameters的参数模型主要用于以下的几种类型:
- skipOnFirstAppliedRule:应用规则时跳过下一条适用规则的参数。
- skipOnFirstNonTriggeredRule:当某条规则未触发时,跳过下一条适用规则的参数
- skipOnFirstFailedRule:当一条规则失效时,跳过下一条适用规则的参数。
- priorityThreshold: 如果优先级超过用户定义的阈值,则跳过下一条规则的参数。
java
复制代码
public class RulesEngineParameters { /** * Default rule priority threshold. */ public static final int DEFAULT_RULE_PRIORITY_THRESHOLD = Integer.MAX_VALUE; /** * Parameter to skip next applicable rules when a rule is applied. */ private boolean skipOnFirstAppliedRule; /** * Parameter to skip next applicable rules when a rule is non triggered */ private boolean skipOnFirstNonTriggeredRule; /** * Parameter to skip next applicable rules when a rule has failed. */ private boolean skipOnFirstFailedRule; /** * Parameter to skip next rules if priority exceeds a user defined threshold. */ private int priorityThreshold; /** * Create a new {@link RulesEngineParameters} with default values. */ public RulesEngineParameters() { this.priorityThreshold = RulesEngineParameters.DEFAULT_RULE_PRIORITY_THRESHOLD; } /** * Create a new {@link RulesEngineParameters}. * * @param skipOnFirstAppliedRule parameter to skip next applicable rules on first applied rule. * @param skipOnFirstFailedRule parameter to skip next applicable rules on first failed rule. * @param skipOnFirstNonTriggeredRule parameter to skip next applicable rules on first non triggered rule. * @param priorityThreshold threshold after which rules should be skipped. */ public RulesEngineParameters(final boolean skipOnFirstAppliedRule, final boolean skipOnFirstFailedRule, final boolean skipOnFirstNonTriggeredRule, final int priorityThreshold) { this.skipOnFirstAppliedRule = skipOnFirstAppliedRule; this.skipOnFirstFailedRule = skipOnFirstFailedRule; this.skipOnFirstNonTriggeredRule = skipOnFirstNonTriggeredRule; this.priorityThreshold = priorityThreshold; } public int getPriorityThreshold() { return priorityThreshold; } public void setPriorityThreshold(final int priorityThreshold) { this.priorityThreshold = priorityThreshold; } public RulesEngineParameters priorityThreshold(final int priorityThreshold) { setPriorityThreshold(priorityThreshold); return this; } public boolean isSkipOnFirstAppliedRule() { return skipOnFirstAppliedRule; } public void setSkipOnFirstAppliedRule(final boolean skipOnFirstAppliedRule) { this.skipOnFirstAppliedRule = skipOnFirstAppliedRule; } public RulesEngineParameters skipOnFirstAppliedRule(final boolean skipOnFirstAppliedRule) { setSkipOnFirstAppliedRule(skipOnFirstAppliedRule); return this; } public boolean isSkipOnFirstNonTriggeredRule() { return skipOnFirstNonTriggeredRule; } public void setSkipOnFirstNonTriggeredRule(final boolean skipOnFirstNonTriggeredRule) { this.skipOnFirstNonTriggeredRule = skipOnFirstNonTriggeredRule; } public RulesEngineParameters skipOnFirstNonTriggeredRule(final boolean skipOnFirstNonTriggeredRule) { setSkipOnFirstNonTriggeredRule(skipOnFirstNonTriggeredRule); return this; } public boolean isSkipOnFirstFailedRule() { return skipOnFirstFailedRule; } public void setSkipOnFirstFailedRule(final boolean skipOnFirstFailedRule) { this.skipOnFirstFailedRule = skipOnFirstFailedRule; } public RulesEngineParameters skipOnFirstFailedRule(final boolean skipOnFirstFailedRule) { setSkipOnFirstFailedRule(skipOnFirstFailedRule); return this; } @Override public String toString() { return "Engine parameters { " + "skipOnFirstAppliedRule = " + skipOnFirstAppliedRule + ", skipOnFirstNonTriggeredRule = " + skipOnFirstNonTriggeredRule + ", skipOnFirstFailedRule = " + skipOnFirstFailedRule + ", priorityThreshold = " + priorityThreshold + " }"; } }
fire触发操作入口
EasyRule是一个规则引擎库,它提供了一种方便的方法来处理规则和事实的关联。在EasyRule中,fire(Rules rules, Facts facts)
是一个方法,用于触发并执行一组规则。
java
复制代码
void fire(Rules rules, Facts facts);
这个方法需要两个参数:
rules
:规则集合,它包含了需要执行的规则。facts
:事实集合,它包含了给定的事实,这些事实将被用于匹配规则的条件。
当调用fire(Rules rules, Facts facts)
方法时,规则引擎会对每个规则进行逐个匹配。如果某个规则的条件能够与给定的事实相匹配,那么该规则的操作将被执行。
换句话说,fire(Rules rules, Facts facts)
方法将根据给定的事实启动所有符合条件的规则,以便执行规则中定义的操作。
下节预告
接下来,我们会进行介绍和说明对应的“【底层服务/编程功底系列】「手把手教学系列」带你打造一个属于自己的规则引擎服务,打破任何业务难题(核心层实现设计)”,敬请期待。