Drools rule engine practice

Drools rule engine practice

Part 1 Rules Engine

Rule engine: The full name is business rule management system, or BRMS in English. The main idea of ​​the rule engine is to separate the business decision part of the application and use predefined semantic modules to write business decisions (business rules), which are configured and managed by users or developers when needed. It should be noted that the rule engine is not a specific technical framework, but refers to a type of system, namely the business rule management system.

Java open source rule engines include: Drools, Easy Rules, Mandarax, IBM ILOG . The most widely used and open source one is Drools .

Main application scenarios: For some systems with complex business rules and frequently changing business rules, it is more suitable to use rule engines, as follows:

Risk control decision system-------risk loan, risk assessment

Anti-fraud projects ----- bank loans, credit verification, anti-money laundering

Real-time anti-fraud platform - mobile payment, credit card consumption

Drools rule engine composition

The drools rule engine consists of the following parts:

  • Working Memory
  • Rules
  • Inference Engine

◆Pattern Match (matcher) matches a specific rule, which is completed by it

◆Agenda

◆Execution Engine

Drools rule engine concepts

Working Memory: Working memory. The drools rule engine will obtain data from the Working Memory and perform pattern matching with the rules defined in the rule file, so the application we develop only needs to insert our data into the Working Memory. For example, in this case, we call kieSession.insert(order) to insert the order object into the working memory.

Fact: In the drools rule application, the object after inserting a common JavaBean into the Working Memory is a Fact object. For example, the Order object in this case belongs to a Fact object. The Fact object is the bridge or channel for data interaction between our application and the rule engine.

Rules: rule base. All the rules defined in the rule file will be loaded into the rule base.

Pattern Matcher: Matcher that performs pattern matching on all rules in Rule Base and Fact objects in Working Memory. Successfully matched rules will be activated and placed in Agenda.

Agenda: Agenda is used to store rules that are activated after pattern matching by the matcher.

Drools basic syntax

Rules file composition

Keywords

describe

package

It is limited to logical management. Queries or functions under the same package name can be called directly.

import

Used to import classes or static methods

global

Global variables

Function

Custom Functions

query

Query

rule end

Rule Body

A rule usually consists of three parts: attribute, condition (LHS) and result (RHS)

Syntax structure:

 rule "ruleName" attributes when LHS then RHS end

rule: keyword, indicating the start of a rule. The parameter is the unique name of the rule.

attributes: rule attributes, which are the parameters between rule and when and are optional.

when: Keyword followed by the conditional part of the rule.

LHS(Left Hand Side): is the generic name for the conditional part of a rule. It consists of zero or more conditional elements. If LHS is empty, it will be treated as a conditional element that is always true.

then: keyword followed by the result part of the rule.

RHS (Right Hand Side): is the general name for the consequence or action part of a rule.

end: keyword, indicating the end of a rule.

Part 2: Rule comparison operators

symbol

illustrate

>

Greater than

<

Less than

>=

Greater than or equal to

<=

Less than or equal to

==

equal

!=

Not equal to

contains

Checks whether a property value of a Fact object contains a specified object value

does not contain

Checks whether a property value of a Fact object does not contain a specified object value

memberOf

Determine whether a property of a Fact object is in one or more collections

not memberOf

Determines whether a property of a Fact object is not in one or more collections

matches

Determines whether a property of a Fact object matches a provided standard Java regular expression

not matches

Determines whether a property of a Fact object does not match the provided standard Java regular expression

Part 3 Drools rule attributes

The properties provided in Drools are as follows (partial properties)

Property name

illustrate

salience

Specify rule execution priority

dialect

Specifies the language type used by the rule. The value can be java or mvel.

enabled

Specifies whether the rule is enabled.

date-effective

Specify the effective time of the rule

date-expires

Specify the rule expiration time

activation-group

Activate grouping. Only one rule with the same group name can be triggered.

agenda-group

Agenda grouping, only the rules in the focused group can be triggered

timer

Timer, specify the time when the rule is triggered

auto-focus

Automatically acquire focus, usually used in conjunction with agenda-group

no-loop

Prevent infinite loops and prevent self-updated rules from being triggered again

lock-on-active

No-loop enhanced version. It can prevent others from updating the rules and starting again

Part 4 Project Application

In order to achieve real-time monitoring of the video usage quality of set-top box users, the provincial set-top box soft probe platform uses real-time big data to perform quasi-real-time indicator statistics, and pushes the indicator results to Kafka. The platform's newly added alarm engine consumes Kafka data in real time and realizes real-time alarm judgment through pre-customized expert rules. The final alarm result is notified and fed back to the alarm platform in real time, realizing the life cycle from alarm to alarm recovery.

Part 5 Rule Examples

 rule "播放成功率告警" no-loop true when $fact:Fact() eval($fact.getPlay() > 0.0) eval($fact.getPlay() < MapUtils.getDoubleValue($fact, "playThreshold", 0.96)) then RuleResult fr = new RuleResult("播放成功率告警"); fr.setRuleCode(900001); fr.setActualTime(MapUtils.getString($fact, "actualTime")); fr.setAlarmTime(RuleUtil.getAlarmTime(MapUtils.getString($fact, "actualTime"), MapUtils.getString($fact, "900001"))); //说明当前是要告警的fr.setAlarmStatus("1"); fr.setAlarmTitle("指标播放成功率异常"); fr.setLocateInfo("指标播放成功率异常"); fr.setAlarmText(RuleUtil.playAlarmText($fact, MapUtils.getDoubleValue($fact, "playThreshold", 0.96))); insert(fr); end rule "播放成功率告警结束" no-loop true when $fact:Fact() eval($fact.getPlay() >= MapUtils.getDoubleValue($fact, "playThreshold", 0.96)) eval(MapUtils.getLongValue($fact, "900001") > 0) eval(RuleUtil.checkAlarmTime($fact.getActualTime(), MapUtils.getLongValue($fact, "900001_time"), MapUtils.getLongValue($fact, "reThreshold", 900000))) then RuleResult fr = new RuleResult("播放成功率告警恢复"); fr.setRuleCode(900001); fr.setActualTime(MapUtils.getString($fact, "actualTime")); fr.setAlarmTime(MapUtils.getString($fact, "900001")); fr.setAlarmStatus("0"); fr.setMsg("告警恢复"); fr.setAlarmTitle("指标播放成功率异常"); fr.setLocateInfo("指标播放成功率异常"); logger.info("播放告警恢复了{}", $fact); insert(fr);


<<:  Ubuntu 23.10 release episode: ISO image urgently "recalled" due to hate speech

>>:  Android 14 system causes trouble, Google Pixel 6 phone storage space cannot be accessed after upgrade

Recommend

"Flying Giant Eye", made in China!

Original title: Planned to be launched this year,...

How to use data to analyze whether an operation project is going well or not?

Recently, my friends often ask me the following q...

3 questions you must know about APP promotion and operation

3 questions you must know about APP promotion and...

Why do public toilets have a gap under the door? It’s out of caring…

A survey by the Chinese Center for Disease Contro...

Nokia N1 released: Another lost moment for the Finnish giant

Nokia has made too many wrong choices over the ye...

Feng Gong's 12-part film collection

Feng Gong has created a comedy method of communic...

The bathroom is so small, why is the WiFi signal always so bad?

The bathroom is so small, why is the WiFi signal ...

Will the new iPhone be made in India?

Perhaps in the near future, the words “Made in In...

How to conduct data analysis for information flow advertising?

During the Internet advertising process, the oper...