This is a creation in Article, where the information may have evolved or changed.
In the previous blog, we introduced several options in the process of strategic business logic, one of which is to use the rule engine to process logic. In fact, hidden in the rules of the engine is still a lot of algorithms, one of the algorithms is the rete algorithm, recently just pay attention to the algorithm, the simple learning, because the algorithm is very clear understanding, so if there are familiar with the algorithm of the friend, you can give correct.
The RETE algorithm is an efficient pattern-matching algorithm for the production system. In a production system, the processed data is called working memory, and the rules for judging are divided into two parts LHS(left-hand-side ) and RHS(righthand side), respectively, to indicate the premise and the conclusion. The main process can be divided into the following steps:
- Match : Find out which matches LHS part of working Memory Collection
- confilict Resolution : Choose a rule that satisfies a condition
- Act : Perform RHS of the content
- return 1
The RETE algorithm mainly improves the match processing process by constructing a network to match.
Algorithm detailed description
RETE Network is divided into two parts,Alpha network and beta Network. As shown (images refer to other sites).
() alpha network: filtering working memory alpha memory (satisfies the collection of this pattern). There are two types of nodes, filtering type Nodes and other conditions that are filtered by the node.
(2)beta Network: There are two types of node beta Memory and Joinnode. 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.
Matching Process Description
- Import the facts that need to be handled to Facts the collection.
- if Facts is not empty, select a fact be processed. Otherwise, the matching process is stopped.
- Select Alpha The first node of the network runs (set when the network is established), through which the node enters Alpha the next node of the network until it enters Alpha Memory . Otherwise jump to the next judgment path
- will beAlpha Memorythe results are added to theBeta Memory, if you do notTerminalnode, a fact that satisfies the condition is detected in another input collection, and the executionJoin, go to the nextBeta MemoryRepeat 3. If another input set does not satisfy the fact that the condition is met, return to 2. If the node isTerminalnode, executingACTand add toFactsthe.
describe the process with an example
(1) The following facts are present in the WME.
W1: (B1 ^on B2)
W2: (B1 ^on B3)
W3: (B1 ^color Red)
W4: (B2 ^on table)
W5: (B2 ^left-of B3)
W6: (B2 ^color Blue)
W7: (B3 ^left-of B4)
W8: (B3 ^on table)
W9: (B3 ^color Red)
(2) The following describes the rules
Here are three matching criteria
C1: (<x> ^on <y>)
C2: (<y> ^left-of <z>)
C3: (<z> ^color Red)
Here are all the conditions that the rule satisfies, i.e. all LHS
P1 has conditions c1^c2^c3
P2 has conditions c1^c2^c4^c5
P3 has conditions c1^c2^c4^c3
(3) Inference Description
According to Ci, the blue Alpha node in the figure should have three, respectively, to determine on,left-of and Color . The yellow Alpha memory contains three sets, each of which is a
Meet C1 : {W1 W2 W4 W8}
Meet C2:{W5 W7}
Meet C3:{w2 W6 W9}
with p1 c1 c2 dummy input beta momory w1^w5,w2^w7 ). Then take this set and c3 for input, operation derived w1^w5^w9 At this point, no more patterns are found to match, reaching terminal node, match end. This gives the collection of rules that satisfy the rule.
Through these theories and an example should be cleared for the rete algorithm, the following blog introduces the drools Rule Engine tool, it is an implementation Rete A good rule engine framework for the algorithm.