Rule Engine Research (i)--rete algorithm (1)

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Rete Algorithm Introduction
I. Overview of Rete
Rete algorithm is a forward rule fast matching algorithm, and its matching speed is independent of the number of rules. Rete is Latin, which corresponds to the net in English, that is, the Internet. The Rete algorithm improves the system pattern matching efficiency by forming a rete network for pattern matching, using two characteristics of the rule-based system, i.e. time redundancy (temporalredundancy) and structural similarity (structuralsimilarity).
second, related concepts
2.1 Facts (FACT):
Fact: The multivariate relationship between objects and object properties. For simplicity's sake, the fact is represented by a ternary group: (identifier^attributeValue), such as the following facts:
W1: (B1^ onB2)W6: (B2^color Blue)
W2: (B1^ onB3)W7: (B3^left-of B4)
W3: (B1^ colorred)W8: (B3^on table)
W4: (B2^ontable)W9: (B3^color Red)
W5: (B2^left-of B3)
2.2 (rule):
The inference statement consisting of the condition and the conclusion is activated when the fact satisfies the condition. The general form of a rule is as follows:
(name-of-this-production
LHS/*one or more conditions*/
-
RHS/*one or more actions*/
)
Where LHS is the conditional part, RHS is the conclusion part.
Here is an example of a rule:
(Find-stack-of-two-blocks-to-the-left-of-a-red-block
(^on)
(^left-of)
(^color Red)
-
... RHS ...
)
2.3 Mode (Patten):
Pattern: The If part of the rule, the generalized form of the known fact, and the multivariate relationship that is not instantiated.
(^on)
(^left-of)
(^color Red)
three, the general algorithm of pattern matching
The rule consists of two parts: the condition and the conclusion, the condition part is also called the Left End (recorded as Lhs,left-hand side), the conclusion part is also called the right side (recorded as RHS, Right-handside). For analysis convenience, assume that there are N rules in the system, the conditional part of each rule has a P-mode, the working memory has m facts, the facts can be understood as the data object to be processed.
Rule matching, that is, for each rule R, judge the current fact o whether to make LHS (R) =true, if so, the rule R's instance R (O) is added to the conflict set. The so-called rule R example is to use the value of the data object o instead of the corresponding parameters of the rule r, that is, the rule r that binds the data object o.
General algorithm for rule matching:
1) Remove an R from the n rule;
2) Remove a combination of p facts from M facts C;
3) with C test LHS (R), if LHS (R (c)) =true, RHS (R (c)) is added to the conflict concentration;
4) Remove the next combination C,goto 3;
5) Remove the next rule R,goto 2;
four, Rete algorithm
The compilation result of the Rete algorithm is the corresponding Rete network for the ruleset, such as. The Rete network is a figure in which a fact can flow. The nodes of the Rete network can be divided into four categories: root node (root), type node (typenode), alpha node, beta node. Among them, the root node is a virtual nodal point, which is the gateway to build Rete network. The types node stores the various types of facts, each of which enters the Rete network from the corresponding type node.

4.1 Establish Rete network
The compilation algorithm for the Rete network is as follows:
1) create root;
2) add rule 1 (Alpha node starting from 1, beta node starting from 2);
A. Remove the mode 1, check the mode parameter type, if it is a new type, then join a type node;
B. Check if the corresponding Alpha node in mode 1 already exists, record the location of the node if it exists, and if not, add mode 1 as an Alpha node to the network, and establish an Alpha memory table based on the pattern of the Alpha node;
C. Repeat b until all the patterns have been processed;
D. Combine the beta nodes as follows:
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) i>2
and the memory table of two parent nodes is built into its own memory table;
E. Repeat d until all beta nodes have been processed;
F. Encapsulate the action (then part) into a leaf node (action node) as the output node of beta (n);
3) repeat 2) until all rules have been processed;
The rete algorithm can be likened to relational database operations.
Consider the fact set as a relationship, each rule as a query, the operation that binds each fact to each pattern as a select operation, a rule of p, a pattern in the rule c1,c2,..., the result of the Ci,select operation as R (CI), and the match of Rule P is R (C1 ) ◇r (C2) ◇ ... ◇ (RCI). Where ◇ represents the connection (join) operation of the relationship.

4.2 using Rete network for matching
To use a rete process:
1) for each fact, filter through the Select operation to enable the fact to reach the appropriate alpha node along the Rete network.
2) for the alpha node of each fact received, separate the appropriate variable bindings with project (projection operation). Enable each new variable binding set to reach the appropriate bete node along the Rete network.
3) for beta nodes that receive new variable bindings, use the project action to generate a new binding set that binds the new variables along the Rete network to the next beta node and to the final project.
4) for each rule, use the project action to isolate the required bindings for the conclusion instantiation.

The following illustration shows the execution of connection (join) Operations and projection (project).

Characteristics of 4.3 rete algorithm
The rete algorithm has two advantages over the traditional pattern matching algorithm.
1. State saving
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.
2. Node sharing
Another feature is that different rules have the same pattern and can share the same node. The various parts of the Rete network contain a variety of different node shares.


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.