当前位置:网站首页>Drools (3): drools basic syntax (1)
Drools (3): drools basic syntax (1)
2022-07-26 07:02:00 【Undead bird. Alexander. Wolf cub】
1 Composition of rule file
drl yes Drools Rule Language Abbreviation . In the rule ⽂ Write specific rules in the document .
⼀ A complete set of rules ⽂ The contents of this document are as follows :
package: Package name ,package Corresponding no ⼀ It must be real ⽬ record , You can write whatever you want com.abc, Same as ⼀ Under a bag drl⽂ Pieces can visit each other
import:⽤ Yu Dao ⼊ Class or static ⽅ Law
global: Global variables
function:⾃ Defined function
query: Inquire about
rule end: Regular form
2 Regular syntax structure
⼀ A rule usually consists of three parts : Properties section (attribute) 、 Condition part (LHS) And results section (RHS)
rule "ruleName" //rule keyword , Indicates the beginning of the rule , The parameter is the rule only ⼀ name
attributes // Rule properties , yes rule And when Parameters between , For optional
when // keyword , after ⾯ It's the conditional part of the rule
LHS //Left Hand Side, It's the conditional part of the rule
then // after ⾯ Follow the result part of the rule
RHS // Is the result of rules or ⾏ by
end // Express ⼀ The end of the rule 3 The rule syntax of the condition part
LHS(Left Hand Side): It is the pass of the conditional part of the rule ⽤ name . It consists of zero or more conditional elements . If LHS It's empty , Then it will be regarded as always true Conditional element of .( Left ⼿ edge )
LHS Partly from ⼀ One or more conditions make up , Conditions ⼜ be called pattern.
pattern The grammatical structure of is : Bind variable name :Object(Field constraint )The bound variable name can be omitted , Usually, the name of the binding variable name ⼀ It is generally recommended to $ Start . If the binding variable name is defined , Can In the regular body RHS Partially make ⽤ Bind the variable name to operate the corresponding Fact object .Field The constraint part is to return true perhaps false Of 0 One or more expressions .
// The rules 1:100 Yuan of the following , No bonus points
rule "score_1"
When
//⼯ Must exist in the memory Order This type of Fact object ----- Type constraint
//Fact Object's amout Attribute value must be ⼩ Is equal to 100------ Attribute constraints
$s : Order(amout <= 100)
Then
$s.setScore(0);
System.out.println(" Successfully matched to rule 1:100 Yuan of the following , No bonus points ");
endIf LHS If the part is empty , Then the engine will ⾃ Dynamic add ⼀ individual eval(true) Conditions , Because the condition always returns true, the With LHS Empty rules always return true
3.1 Constraint connection
stay LHS among , Can contain 0~n Conditions , Multiple pattern You can take ⽤“&&” (and) 、 “||”(or) and “,”(and) To achieve , Or not , The default connection is and.
// The rules 2:100 element -500 element Add 100 branch
rule "score_2"
when
$s : Order(amout > 100 && amout <= 500)
then
$s.setScore(100);
System.out.println(" Successfully matched to rule 2:100 element -500 element Add 100 branch ");
end3.2 Comparison operator
stay Drools When the Communist Party of China provided ⼗⼆ Types of ⽐ Better operator , Namely : >、 >=、 <、 <=、 = =、 !=、 contains、 not contains、memberof、not memberof、matches、not matches; Here ⼗⼆ Among the three types of comparison operators , The first six are more commonly used comparison operators
| Symbol | explain |
| > | Greater than |
| < | Less than |
| >= | Greater than or equal to |
| <= | Less than or equal to |
| == | be equal to |
| != | It's not equal to |
| contains | Check ⼀ individual Fact Whether a property value of an object contains ⼀ Specified object values |
| not contains | Check ⼀ individual Fact Whether a property value of an object does not contain ⼀ Specified object values |
| memberOf | Judge ⼀ individual Fact Whether a property of the object is ⼀ In one or more sets |
| not memberOf | Judge ⼀ individual Fact Whether a property of the object is not ⼀ In one or more sets |
| matches | Judge ⼀ individual Fact Whether the properties of the object are consistent with the provided standard Java Regular expressions go into ⾏ matching |
| not matches | Judge ⼀ individual Fact Whether the properties of the object are different from the provided standard Java Regular expressions go into ⾏ matching |
Example :
The first ⼀ Step : Create entity class ,⽤ For testing ⽐ Better operator
package com.xiongpeng.entity;
import java.util.List;
public class Customer {
// Name of customer
private String name;
private List<Order> orderList;
// Order set
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Order> getOrderList() {
return orderList;
}
public void setOrderList(List<Order> orderList) {
this.orderList = orderList;
}
}The first ⼆ Step : stay /resources/rules Create rules under ⽂ Pieces of customer-rules.drl
package rules
import com.xiongpeng.entity.*;
// test contains The rules
rule "rule1"
when
$order:Order();
$customer:Customer(orderList contains $order);
then
System.out.println(" test contains Rules trigger :"+$customer.getName());
end
// test not contains The rules
rule "rule2"
when
$order:Order();
$customer:Customer(orderList not contains $order);
then
System.out.println(" test not contains Rules trigger :"+$customer.getName());
end
// test ⽐ Better operator matches
rule "rule3"
when
Customer(name matches " Zhang .*")
then
System.out.println(" test ⽐ Better operator matches Trigger ...");
end
// test ⽐ Better operator not matches
rule "rule4"
when
Customer(name not matches " Zhang .*")
then
System.out.println(" test ⽐ Better operator not matches Trigger ...");
endThe third step : Write unit tests
@Test
public void test2() {
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// Conversation object ,⽤ Interact with the rule engine
KieSession kieSession = kieContainer.newKieSession();
// Construct order object , Set up order ⾦ forehead , The integral calculated by the rule engine
Order order = new Order();
// Matching rules :$order:Order();
kieSession.insert(order);
Customer customer = new Customer();
List<Order> orderList = new ArrayList<Order>();
// Matching rules : $customer:Customer(orderList contains $order);
// orderList.add(order);
customer.setOrderList(orderList);
customer.setName("Jack");
// Give the data to the rule engine , The rule engine will enter ⾏ Rule matching
kieSession.insert(customer);
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules();
// Close session
kieSession.dispose();
}The results are as follows :

The above results call all rules when calling , Connect the score-rules.drl Rules are called , In this case , We can call the specified rules .
Be careful :
Execute specified rules
We're adjusting ⽤ Rule code , All rules that meet the conditions will be executed . So if we just want to cling ⾏ How to implement one of the rules ?
Drools For us ⽅ Formula is implemented by rule filter ⾏ Specify the rules . For the rules ⽂ There is no need to make any modification , It just needs to be modified Java The code can be ,
as follows :
@Test
public void test3() {
KieServices kieServices = KieServices.Factory.get();
KieContainer kieContainer = kieServices.getKieClasspathContainer();
// Conversation object ,⽤ Interact with the rule engine
KieSession kieSession = kieContainer.newKieSession();
// Construct order object , Set up order ⾦ forehead , The integral calculated by the rule engine
Order order = new Order();
// Matching rules :$order:Order();
kieSession.insert(order);
Customer customer = new Customer();
List<Order> orderList = new ArrayList<Order>();
// Matching rules : $customer:Customer(orderList contains $order);
// orderList.add(order);
customer.setOrderList(orderList);
customer.setName("Jack");
// Give the data to the rule engine , The rule engine will enter ⾏ Rule matching
kieSession.insert(customer);
// Activate rule engine , If the match is successful, execute ⾏ The rules
kieSession.fireAllRules(new RuleNameEqualsAgendaFilter("rule4"));
// Close session
kieSession.dispose();
}Key statement :
// Implement execution only through rule filters ⾏ Specify the rules
kieSession.fireAllRules(new RuleNameEqualsAgendaFilter("rule4"));The results are as follows :

边栏推荐
- 替换license是否要重启数据库?
- Delete ^m from VIM
- Getting started with kernel PWN (5)
- Binary tree knowledge summary
- C#使用log4net插件,输出日志到文件
- On stock price prediction model (3): are you falling into the trap of machine learning
- 【QT】怎样获得QTableView和QTableWidget的行数和列数
- On the difference between Eval and assert
- 7. Reverse Integer整数反转
- 123123123
猜你喜欢

"Niuke | daily question" template stack
![Rust语言- Slice(切片)类型(&[u8])](/img/d1/68c73c8b34b848212083c08df3137f.png)
Rust语言- Slice(切片)类型(&[u8])
![[database] CTE (common table expression)](/img/36/812026995f5d0b64d26f1667638010.png)
[database] CTE (common table expression)

Drools(3):Drools基础语法(1)

20000 words will take you from 0 to 1 to build an enterprise level microservice security framework

CS5801_ HDMI to EDP advantage replaces lt6711a solution

Question: can't download sh shellcheck Please install it manually and some commands of shell script

MySQL Foundation (II) -- MySQL Foundation

你了解MySQL都包含哪些“零件“吗?

针对前面文章的整改思路
随机推荐
Binary tree knowledge summary
Depth cloning and reflection of typescript class objects
Acwing- daily question
On the difference between Eval and assert
How does the national standard gb28181 protocol easygbs platform realize device video recording and set streaming IP?
SQL shell (PSQL) tool under PostgreSQL
File server fastdfs
从Architecture带你认识JVM
如何删除语句审计日志?
[749. Isolate virus]
MySQL table write lock
Do you want to restart the database to replace the license?
Realize the full link grayscale based on Apache APIs IX through MSE
浅谈eval与assert一句话木马执行区别
数据库性能测试(mysql)
Development stage of source code encryption technology
npm 命令
The results of the soft test can be checked, and the entry to query the results of the soft test has been opened in the first half of 2022
one hundred and twenty-three million one hundred and twenty-three thousand one hundred and twenty-three
XSS labs (1-10) break through details