Rule engine -- rete algorithm

rete algorithm

The Rete algorithm utilizes the common part of each field among the rules to reduce the storage of the rules, and at the same time saves the temporary results of the matching process to speed up the matching speed. In order to achieve this goal, the algorithm splits the rules, and connects each matching condition in the rules as a basic unit (node) to form a data discrimination network, and then filters and propagates the facts through the network , and finally all the conditions have Fact matching rules are activated.
————————————————
Copyright statement: This article is an original article of CSDN blogger "cloneme01", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting .
Original link: https://blog.csdn.net/goodjava2007/article/details/121989398
insert image description here

  1. Root node: The root node is the entrance for all objects to enter the rete network, which allows all facts to pass through and then spread to the Type node;

  2. Type node: Also called ObjectType node, it is our Fact, that is, the POJO used in our rules, which is used to select the type of fact, that is, to perform type checking on the fact, and the engine only allows objects matching this type to reach subsequent nodes, which will conform to The fact of this node type is propagated to the subsequent Alpha nodes, and a Fact corresponds to a Type node;

  3. Alpha node: used for constraints or constant tests of attributes within the same object type, that is, one literal condition corresponds to one Alpha node, such as: age > 10, when there are multiple literal conditions, these conditions are linked together, that is, multiple Alpha nodes are connected together, and Alpha nodes can be shared;

  4. Beta node: mainly perform connection operations according to the constraints between different objects (pet, cat) in the same rule, such as ("pet.name==cat.name", "pet.age>cat.age"). The Beta node is further divided into Join node , Not node, etc. The Join node includes two types of input. The left part inputs a list of facts, called a tuple (Tuple), and the right part inputs a fact object. Constraints perform a Join operation, add the matching facts to the tuple, and continue to pass to the next beta node. The Beta node has a memory function. The input on the left is called Beta Memory, which will remember all the semantics that have arrived, and the input on the right will be Alpha Memory, which will remember all the objects that have arrived. The Beta node is not shared;

  5. LeftInputAdapterNode node: the function of this node is to convert a fact object into a tuple, and provide the function for the beta node;

  6. Terminal node: This is the end node of this rule, which represents the end of a rule match. When the fact or tuple is passed to the Terminal node, it means that the rule corresponding to the Terminal node has been activated.
    ————————————————
    Copyright statement: This article is an original article of CSDN blogger "cloneme01", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement for reprinting .
    Original link: https://blog.csdn.net/goodjava2007/article/details/121989398

example

Reference document:
https://blogs.sap.com/2022/11/10/introduction-to-the-rete-algorithm/

Example:

If age>60 or age<5 or income<36000 then concession = 50/100.

Assume that age is java variable and income is xml variable. The following rete network can be created to represent this rule.
insert image description here

Advantages and disadvantages of the rete algorithm

advantage

The main advantage of Rete algorithm is speed as it takes the advantage of structural similarity in rules. Many rules often contain similar patterns or group of patterns. Rete algorithm pools the common components so that they need not be computed again.

A large amount of double counting can be avoided when the fact set does not change much.

shortcoming

The main drawback of Rete pattern matching algorithm is that it is memory intensive. Saving the state of the system using pattern matches and partial matches considerable amount of memory. The space complexity of Rete is of the order of O(RFP), where R is the number of rules, F is the number of asserted facts, and P is the average number of patterns per rule.

It takes up a lot of memory.


For each change in the fact set, the matching state is saved in the Alpha and Beta nodes. When the next fact set changes, most of the results do not need to be changed. The Rete algorithm saves the state during the operation , avoiding a lot of double counting. (alpha memory stores the calculated intermediate results, exchanging space for time, thereby speeding up the system. When processing massive data and rules, beta memory grows exponentially according to the conditions of the rules and the number of facts, so when the rules and facts Many times, it will exhaust system resources)

However, when the change of the fact set is very drastic each time, the effect of Rete's state preservation algorithm is not ideal, which will still lead to a large number of calculations. (The rete network changes greatly, various sorts, and optimization fails)

Guess you like

Origin blog.csdn.net/qq_26437925/article/details/131216508