drools entry notes

Brief introduction

Drools is a Java-based open source rules engine to achieve a business decision to separate from the application.

advantage:

  • Simplify system architecture, application optimization
  • Improve maintainability and maintenance cost of the system
  • To facilitate system integration
  • Reduce write "hard-coded" the costs and risks of business rules

Fact objects:

Refers to the object drools script is a common javabean, javaBean refer to the original object, can read and write the object, call the object's method and
when inserted into a java bean working Memory (memory storage), the rule using a reference to the original object, the rules by reading and writing fact object,
read and write data on the application of these properties, it is necessary to provide get and set methods, rules of access to working memory can be dynamically insert a new deleted the fact objects

Drools of basic grammar:

Packet routing, reference body rule (rule package path and wherein the body is necessary)

package rules.testword
rule "test001"
  when 
    //这里如果为空,则表示eval(true)
  then
    System.out.println("hello word");
end
  • package: package path, the path is a logical path (can easily write, but can not write, the best and the file directory of the same name, separated by () way.), the rule is always the first line of the file
  • rule: Rule body, beginning rule, ending with End, each file can contain multiple rule, the rule body divided into three parts: LHS, RHS, three major properties
  • LHS: (Left Hand Side), the condition portion, a rule which part is LHS portion "when" and "then" intermediate, the LHS which may comprise 0 ~ N condition, if the LHS is empty, then the engine automatically add a condition eval (true), since this condition always returns true, so the LHS is empty rule always returns true.
  • RHS: (Right Hand Side), "then" part is behind the RHS, only if all conditions are met LHS, RHS section will be performed in a single rule. RHS section is part of the rules actually do things, to meet the conditions to trigger the operation part of the action, you can use the bind variable names among the LHS part of the definition of the RHS, global variable, or direct write java code, you can use the import class. Not recommended for conditional judgment. Macro function can be used and a fast-working Memory operations, such as insert / insertLogical, update / modify retract and can be achieved in the current Working Memory objects Fact add, edit, or delete, can drool Objects, also the Drools providing macro objects kcontext of the object can access the current Working Memory of KnowledgeRuntime directly.
  • import: import rules files require external variables to be introduced into the class, the class may be a static method
    , for example:
          Import com.dinpay.dpp.rcp.service.util.RuleLogUtil; import classes
          import com.dinpay.dpp .rcp.service.util.RuleLogUtil.getLog; // import static methods

Drools API calls

API can be divided into three categories: rule compilation, collection rules, rule execution

  1. The compilation Kmodule.xml
    stored in src / main / resources / META- INF / folder
<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="rules" packages="rules.testword">
        <ksession name="session"/>
    </kbase>
</kmodule>
  • May comprise a plurality of kbase, but not any of the same name
  • There packages, is src / main / resources following the folder name, you can define multiple packages, separated by commas
  • ksession has a name, any string but not the same name, there may be a plurality of
  • At runtime, KieContainer will create KieModule, KieBase, KieSession objects based * Model objects, which KieModule and KieBase only created once, and may create multiple KieSession
  1. KieSession
    for the session to interact with the rules engine
    into two categories:
  • There are state KieSession: KieSession method will be many times with a rules engine interaction, maintain session state, the value of the type attribute is stateful, and finally need to clean up state KieSession maintenance, call dispose () in

  • Stateless StatelessKieSession: StatelessKieSession isolated interact with each rule engine does not maintain session state, no side effects, the value of the type attribute is stateless

    Application Scenario: data validation, calculation, data filtering, message routing, any rule can be described as a function or formula

Structure of the code of the standard rules file

package package-name (package name must be the only limit to manage the logic, if a custom query or function belong to the same package name, regardless of the physical location, you can call)
Imports (need to import the class name)
Globals ( global variables)
functions (function)
queries (queries)
rules (rules, more)

coding

maven dependence

<dependency>
    <groupId>org.kie</groupId>
    <artifactId>kie-api</artifactId>
    <version>6.5.0.Final</version>
</dependency>
<dependency>
    <groupId>org.drools</groupId>
    <artifactId>drools-compiler</artifactId>
    <version>6.5.0.Final</version>
</dependency>
<!-- Drools规则引擎包 end -->

Drl write rules file

Write kmodule.xml

Configuration file tells the code where the rules drl file in, placed under resources / META-INF directory

Write DroolsUtil

通过该类加载kmodule.xml文件,并获得KieSession

Implementation of the rules

Based on the way the database

Table Structure

Get KieSession

Implementation of the rules

https://www.jianshu.com/p/e713860b128e

发布了202 篇原创文章 · 获赞 6 · 访问量 3万+

Guess you like

Origin blog.csdn.net/fall_hat/article/details/104692176