Rules engine interpretation of the Java Rules Engine

Source: Internet
Author: User
Tags knowledge base

Reprint: http://www.open-open.com/lib/view/open1417528754230.html

In real life, rules are everywhere. Laws, regulations and systems are all; for enterprise applications, rules are applied in many areas of it technology, such as routing tables, firewall policies, and even role permission control (RBAC), or URL matching in a web framework. Regardless of the rules, a set of conditions and the results of this condition are defined.

To give an example:

IF

The car is red
The car is sporty.
The driver is male.
The driver is between 16-25 years old.
Then

20% increase in insurance premiums

As you can see from this example:

Each rule is a series of results determined by a set of conditions
A rule may together with other rules determine the final result. For example, the rules only generate increments, and you need to work with the rules that determine the cardinality to determine the final rate.
There may be rules that intersect the conditions, and it is necessary to prioritize the rules at this time
Rule as a kind of knowledge, its typical use is through the actual situation, according to a given set of rules, draw a conclusion. This conclusion may be a static result or a set of actions that need to be performed. The process of applying this rule is called reasoning. If the process of reasoning is handled by the program, then the program is called the Inference engine/inference machine. The inference engine adopts different control strategies depending on the knowledge representation, and common types include neural networks, case-based and rule-based inference machines. Among them, rule-based inference machine is easy to understand, easy to access, easy to manage, is widely used. This inference engine is called the "rule Engine".


The rule engine originates from a rule-based expert system (Expert clips: A 1984-year-old AI project from NASA, now open source, written by C. , and the rule-based expert system is one of the branches of the expert system. The expert system belongs to the category of artificial intelligence, it imitates the human reasoning way, uses the tentative method to infer, and uses the human understandable terminology to explain and proves its inference conclusion. The rule-based expert system (RBEs) consists of three parts: rule base (Knowledge Base), working Memory (fact base), and inference Engine. Their structure is shown in the following system:

The inference engine (inference engines) consists of three parts: pattern Matcher, Agenda (Agenda), and execution engine (execution engine). The inference engine is added to the agenda by deciding which rules meet the facts or objectives, and giving priority to the rules that meet the facts or objectives.

The pattern match determines which rule is chosen and when the rule is executed;
The order of execution of the rules selected by the agenda management pattern match;
The execution engine is responsible for executing rules and other actions.
As opposed to human thinking, there are two ways of reasoning in the rule engine: Forward inference (forward-chaining) and reverse inference (backward-chaining).

Forward inference is also called deduction, which is driven by fact, and from an initial fact, the rule is applied constantly to draw conclusions. First select a rule in the candidate queue as the enabling rule for inference, and record its conclusion as evidence for the next step of reasoning. This repeats the process until there are no available rules to choose from or to obtain the desired solution.
Reverse inference is also called induction, driven by a goal, first proposing a hypothesis, and then looking for evidence to support the hypothesis, if the necessary evidence can be found, stating that the original hypothesis is correct; if the necessary evidence is not found at all, then the original hypothesis is not established and a new hypothesis needs to be made.
An algorithm that matches facts to rules. The common pattern matching algorithm has rete,lfa,treai,leaps. Rete algorithm is one of the most efficient deductive inference algorithms, and many rule engines are based on the Rete algorithm for reasoning calculation.

The inference engine's reasoning steps are as follows: pattern matching, conflict resolution, execution engine.

Enter the initial data (fact) into working Memory.
Use the Pattern Matcher to compare rules (rule) and data (fact) in the rule base.
If there is a conflict in the execution rule (conflict), which means that more than one rule is activated at the same time, the conflicting rule is placed in the conflicting collection.
Resolve the conflict by placing the activated rules in the order of agenda.
Executes the rules in agenda using the execution engine. Repeat steps 2 through 5 until you have finished executing the rules in all agenda.
The role of the Rule engine:

Rule externality, which facilitates the reuse of rule knowledge, and avoids code change problems caused by changing rules
The rule engine uses an algorithm for the inference process, without the need to write complex and obscure logic judgment code
Developers do not need to focus too much on logical judgment and can focus on logical processing

Rete algorithm

Rete is "net" in Latin and has the meaning of network. The Rete algorithm was developed by Dr Charles L. Forgy, Carnegie Mellon University, and is an efficient pattern matching algorithm for implementing a production-based rule system (production/inference).

The rete algorithm can be divided into two parts: rule compilation and runtime execution (runtime execution).

Rule compilation ===> refers to the process of generating an inference network from a rule set.

Run-time execution ===> refers to the process of feeding data into the inference network for filtering.

Related concepts:

Fact: Relationships between objects and object properties
Rule: An inference statement consisting of conditions and conclusions, generally expressed as an if ... Then. The if part of a rule is called LHS (left-hand-side), then the then part is called RHS (right hand side).
Mode (module): Refers to the condition of the IF statement. Here the IF condition may be a large condition consisting of several smaller conditions. A pattern is the smallest atomic condition that cannot continue to be divided.
Rete Inference Network generation process: from rule set {Rule 1, rule 2 ...} Take out a bar, according to a certain algorithm, into the Rete Inference network node. The Rete inference network is generated when all rules are processed in a continuous loop. Rete network is divided into two parts, Alpha Network and beta network. As shown in.

Alpha Network: Filter the working memory to find the set of each pattern that conforms to the rule, generating an alpha memory (which satisfies the collection of the pattern). There are two types of nodes, which filter the nodes of type and other filters (I think both are set according to need and do not necessarily need two kinds of nodes).
Beta network: There are two types of node beta memory and join node. The former mainly stores the collection after the join IS completed. The latter consists of two input ports, each of which requires a matching two set, which is transferred by the join node to the next node.
In a production system, the main process can be divided into the following steps:

Match: Find the working memory set that fits the LHS section
Confilict Resolution: Choosing a rule that satisfies a condition
ACT: Executing the content of RHS
Returns 1
The rete algorithm mainly improves the match processing process by constructing a network to match.



The process is as follows:

Create the root node (root node) and infer the gateway to the network.
Take rule 1 and remove pattern 1 from Rule 1 (as stated above, the pattern is the minimum atomic condition, so the relationship of the rule pattern is 1:n).
A) Check the parameter type in mode 1 and, if it is a new type, add a type node.

b) Check that the corresponding Alpha node for mode 1 exists, if there is a location for the node under record, and if not, add mode 1 as an Alpha node to the network. The Alpah memory table is also established based on the Alpha node.

c) Repeat B until all patterns are processed.

d) Combined beta node: Beta (2) left input node is Alpha (1), right input node is Alpha (2), beta (i) left input node is beta (i-1), right input node is Alpha (i), and memory table of two parent node is inline into its own memory table

e) Repeat d until all beta nodes have finished processing

f) Wrap the action then part into the last node as beta (n)

Repeat 2 until all rules are processed
Here is an example from the Internet:


Rule P1:

LHS:

C1: (age: R 2)
C2: (Gender: male)
C3: (stature: thinner)
C4: (height: greater than 175cm)
RHS:

Punctuation characters

The Rete algorithm is superior to the traditional pattern matching algorithm

State save. Each change in the fact set, its matched state is saved in the Alpha and beta nodes. The next time the fact set changes, the vast majority of the results do not need to change, rete algorithm by saving the state of the operation process, avoid a large number of repeated calculations. The Rete algorithm is mainly designed for those systems with little change in the fact set, and when the change of fact set is very drastic, the Rete state saving algorithm is not satisfactory.
Node sharing


The rete algorithm used in drools

The compilation algorithm describes how rules produce an effective discriminating network in production memory. In a non-technical word, a discriminating network is used to filter the data. The method is to filter the data through the propagation of the data over the network. There will be a lot of matching data on the top node. As we go down the network, the matching data will be less and fewer. At the bottom of the network is the endpoint (terminal nodes). In Dr Forgy's 1982-year paper, he described 4 basic nodes: root, 1-input, 2-input and terminal.

Is the Rete node type in drools:

The root node (RootNode) is the entry of all objects into the network. Then, immediately from the root node into the Objecttypenode. Objecttypenode's role is to make the engine do only what it needs to do. For example, we have two sets of objects: account and order. If the rule engine needs to perform a periodic evaluation of each object, it will waste a lot of time. To improve efficiency, the engine will only let objects that match object type pass through to the node. In this way, if an application asserts a new account, it does not pass the order object to the node. Many modern Rete implementations have special Objecttypenode. In some cases, Objecttypenode is further optimized using hashing.

Objecttypenode can be spread to alphanodes,leftinputadapternodes and betanodes.

1-input nodes are often referred to as alphanode. Alphanodes is used to evaluate the literal condition (literal conditions). Although the 1982 paper only mentions equal conditions (meaning literal equivalence), many Rete implementations support other operations. For example, Account.name = = "Mr trout" is a literal condition. When a rule has more than one literal condition for an object type, these literal conditions are linked together. This is to say that if an application asserts an account object, it must first satisfy the first literal condition before it can reach the next alphanode. In Dr. In Forgy's paper, he uses intraelement conditions to express. The following illustration shows the alphanode combination of cheese (name = = "Cheddar", strength = = "Strong"):

Drools optimizes the propagation from Objecttypenode to Alphanode by hashing. Each time a alphanode is added to a objecttypenode, the literal value (literal value) is used as the key, and Alphanode is added as value to HashMap. When a new instance enters Objecttypenode, it is not passed to every alphanode, it can get right alphanode directly from HashMap and avoids unnecessary literal checking.



2-input nodes are often referred to as betanode. There are two kinds of betanode:joinnode and Notnode in Drools. The betanodes is used to compare 2 objects. The two objects can be of the same type, or they can be of different types.



We agreed that the 2 inputs of the betanodes are called left and right. The left input of a betanode is usually a list of objects. In Drools, this is an array. The right input is a single object. Two notnode can complete the ' exists ' check. Drools extends the rete algorithm by applying an index to the betanodes. Shows the use of a joinnode:

Notice that the left input in the diagram uses a leftinputadapternode, which is the function of converting a single object into an array of individual objects, which is propagated to the Joinnode node. As we mentioned above, the left input is usually a list of objects.

Terminal nodes is used to indicate that a rule has matched all its conditions (conditions). At this point, we say that this rule has an exact match. In some cases, a rule with a "or" condition can have more than one terminal node.

Drools improves the performance of the rule engine by sharing nodes. Because many of the rules may exist in some of the same patterns, the sharing of nodes allows us to compress the number of nodes in memory to provide the process of traversing the nodes. The following two rules share a subset of nodes:

1 Rule2  when3Cheese ($chedddar: Name = ="Cheddar"  )4$person: Person (Favouritecheese = =$cheddar)5  Then6System.out.println ($person. GetName () +"likes cheddar"  );7 End8 9 RuleTen  when OneCheese ($chedddar: Name = ="Cheddar"  ) A$person: Person (Favouritecheese! =$cheddar) -  Then -System.out.println ($person. GetName () +"does likes cheddar"  ); theEnd
View Code

As you can see from the diagram, Alphanode is shared in the compiled Rete network, and Betanode is not shared. The above-mentioned equality and inequality is reflected in the difference of Betanode. Then these two rules have their own terminal Node.


The second part of the Rete algorithm is the runtime (runtime). When an application asserts an object, the engine passes the data to root
Node From there, it enters the objecttypenode and spreads down the network. When the data matches the condition of a node, the node logs it to the appropriate memory. The reasons for doing so are

The next few things: the main reason is that it can bring faster performance. While remembering that a fully or partially matched object requires memory, it provides the characteristics of speed and scalability. When all the conditions of a rule are fulfilled, this is the end

Full match. Only part of the condition is satisfied, which is a partial match. (I think the engine has its own memory in each node to store the objects that meet that node's condition, which causes if an object is an exact match, that
This object will have its image in the corresponding memory of each node. )

Reference Links:

    • Http://docs.jboss.org/drools/release/6.0.0.Final/drools-docs/html/HybridReasoningChapter.html#ReteOO

Reference Address: http://www.biaodianfu.com/rules-engine.html

Rules engine interpretation of the Java Rules Engine

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.