A complete BMRS system (business rule management system) generally includes at least three parts: rule designer, rule engine and rule storage management.

I. Drools terminology

Q1: What are the facts?

As FAR as I’m concerned, we can think of it as a data object, a User object, a Student object… Any data object that needs to be brought into the rules for matching processing is called a fact.

Q2: What is a rule file and what is a KJAR?

Rules are entities that pattern match and process data objects (facts) thrown at them. Such as User. Java:

public class User{
	private int age;
	// Is an adult
	private boolean isAdult; 
	// The Setter and Getter are ignored
}
Copy the code

UserHandle. DRL:

import xx.User;
rule "handleUser01" 
	when
		$user: User(age > 18)
    then
 		$student.setIsAdult(true);
end
Copy the code

The WHEN part (also known as LHS, or rule condition) performs pattern matching, and the THEN part (also known as RHS, or subsequent processing part).

For syntax, see Drools introduction to LHS and RHS syntax

It can also be seen from above:

  • If the rules were simple, it might just be this oneDRL rule file+ aJava Bean;
  • If the rules are complicated, it could be oneRule project in the form of Maven. Because DRL supports writing Java code, rule projects can be as rich as they want: write more Java classes, and even introduce tripartite dependencies! .

Since the rule project is a Maven project, to use this “rule”, you need to type the project into a JAR, called kJAR, which is packaged by maven-kjar-plugin. Compared to our ordinary JAR, its package:

<package>kjar</package>
Copy the code

At the same time, its Resource, also contains configuration files such as kmodule. XML, these are the key files needed for Kie-Server parsing!

Q3: What are the components of drools?

The drools bucket includes WorkBench(Rule Designer + Rule Store Management)+ Kie-Server(rule engine container)+ Rule engine core(Drools core)+ rules (either rule file or KJAR)!

Q4: What is Workbench (hereinafter referred to as WB)?

In fact, with version 7.44 (the newer version of 2020), we can think of WB as a GitLab!

  1. Quick creationRule project, the project is hosted in the WB built-in GIT repository;
  2. The rule item is visibleThe directory structure, as well asModify theA file in any folder;
  3. Each rule item is visibleThe submitterWhich ones, as wellRecently submitted statistics;
  4. Each rule item is visibleGIT remote clone address(Support GIT, SSH and HTTP to pull the project to the local);
  5. Each rule item is visibleVisual dependency list(similar to visual pom.xml), and support for adding individual dependencies toWhite list(we’ll talk about whitelisting later);
  6. Of a rule item that can be setRemote Maven RepositoryandLocal repository(Similar to manipulating POM.xml<repository>);
  7. clickablebuildorbuild&install, will rule the itemPackaging/InstallationTo the local Maven repository (see GitLab integrating Jenkins);
  8. clickabledepooyThrow the finished package to the Kie-Server to generate the rule service;

And it has Maven private:

  1. inbuildorbuild&installAfter execution, kjar is installed into a local Maven repository and exposed via HTTP via Maven private server.

It also serves as a designer:

  1. Support for the creation and online editing of decision tables, decision trees, scorecards, and rule flows (more on these terms later);
  2. Support to create test scenarios, written rule projects, functional testing;
  3. Support to resolve all fact objects according to the dependencies of rule items, easy to import when writing rules;

Finally, it has the monitoring capability of Kie-Server (not to mention) :

  1. You can monitor the execution of rules and rule flows in Kie-Server.
Q5: What is Kie-Server?

Kie-server is the equivalent of Tomcat, JBoss, etc. All KJars (rule items) are instantiated as KieContainer objects.

At the same time, Kie-Server provides a restful interface externally, accepts the fact object, throws it to the KieContainer object for rule processing, and then returns it to the caller.

Currently WB and Kie-Server are seamlessly integrated!

Second, Drools operation mode

1. Embedded

The business system simply imports the core part of the rules engine (that is, related dependent jars such as Drools-Core) and creates the rules file manually. This mode abandons WB, as well as Kie-Server.

  • Advantages: Simple and easy to debug
  • Disadvantages: Core only, light weight at the same time, but also lost the separation of business logic complete isolation, and separation of high availability, designer and other advantages.

2, separated type (WB)

The illustration above explains:

  • Local GIT REPO: WB internal GIT repository, mentioned above;

    Access the address: http://localhost:8080/kie-wb/rest/spaces/ your name space;

  • MAVEN REPO: WB internal MAVEN repository, described above;

    Access the address: http://localhost:8080/kie-wb/rest/maven2/ package path;

  • REST API(WB): WB provides a REST interface for developers to do some general (rarely used) operations on rule projects on WB;
  • Controller REST API: Only used to interact with Kie-Server.
  • REST API(Kie-Server): Provides a unified API interfaceRule callerorWBComplete the creation of KieContainer and letRule callerMake rule calls;

    Swagger address: http://localhost:8080/kie-server/docs;

Execution steps:

  1. WB interface to create rules project (or local Git upload WB git repository);
  2. WB performbuild&installPackage the rules project as kJAR and install it intoBuilt-in Maven repository;
  3. After the Kie-Server is started, the user requests WB authentication and registers with WB. At this time, the successfully registered Kie-Server instance can be seen on the Server page of WB.
  4. WB clickdeployAfter that, the relevant Rest API of kie-Server will be called and specifiedGAV(group, artifactId, version), request the Kie-Server pair to be specifiedGAVthekjarLoad;
  5. Kie-server uses HTTP to access the WB Maven private Server, downloads the KJAR, instantiates it as KieContainer, and generates a KieContainerId (which is generated based on GAV).
  6. The caller accesses the specified rule item based on the specified KieContainerId;
  7. WB can also set periodic scanning time, that is, let Kie-Server periodically update the latest KJAR on WB private Server at specified intervals.

3, separated type (without WB)

Because WB is heavy, contains Git and Maven services, and is a huge project like designer, it is possible to use WB in a development environment, but you may need to think about it when you deploy it online.

The above is the deployment mode using Kie-Server alone, there is no such mode officially.

This pattern may address the following issues:

  • The operating environment of rules is closed, and the details of external shielding rules need to be designed;
  • Due to limited online resources, lightweight deployment is required

The CPU and memory usage of WB is N times that of Kie-Server. I measured version 7.44 here, and it takes at least 10 minutes for Wildfly to start WB.

  • Online is a pure Linux environment, WB deployment can not use;

The above are some common terms and basic architecture of Drools. Later, we will talk about KieService, KieBase, KieSession and other concepts in Drools, as well as decision table, decision tree and decision card.

If you like this article, please pay attention to the public account: Open ape notes, there will be continuous updates!