0% found this document useful (0 votes)
195 views46 pages

Rete Pattern Matching Algorithm Overview

The Rete algorithm improves the efficiency of rule-based expert systems by storing and reusing the results of pattern matching between rules and facts. It constructs a pattern network to determine which facts match which patterns, and a join network to check consistency of variable bindings across patterns. By remembering previous matching results and only recomputing changes, Rete avoids unnecessary re-evaluation of rules during each cycle as facts are added or removed.

Uploaded by

v4vix
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
195 views46 pages

Rete Pattern Matching Algorithm Overview

The Rete algorithm improves the efficiency of rule-based expert systems by storing and reusing the results of pattern matching between rules and facts. It constructs a pattern network to determine which facts match which patterns, and a join network to check consistency of variable bindings across patterns. By remembering previous matching results and only recomputing changes, Rete avoids unnecessary re-evaluation of rules during each cycle as facts are added or removed.

Uploaded by

v4vix
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

The Rete Pattern-Matching Algorithm

Rete Pattern Matching Algorithm


• R
Rule-based
l b d llanguages lik
like CLIPS,
CLIPS JJess,
Eclipse, and OPS5 use the Rete Pattern-
M t hi Algorithm
Matching Al ith forf matching
t hi facts
f t
against the patterns in rules to determine
which
hi h rules
l h have hhad
d th
theiri conditions
diti
satisfied.

• An understanding of this algorithm makes


it easier to conceptualize why writing rules
one way is more efficient than writing them
another way.

CS 470: Expert Systems Rete Pattern Matching Algorithm 2


• If the matching process occurs only once
once, the
inference engine simply examines each rule and
then searches the set of facts to see if the rule’s
rule s
patterns have been satisfied; if so, the rule is
placed
p aced o
on tthe
e age
agenda.
da
• In rule-based languages, the matching process
t k place
takes l repeatedly
t dl and the fact
d th f t list
li t will
ill b
be
modified during each cycle of execution.
• Such changes may cause previously unsatisfied
patterns to be satisfied or vice versa. During
each cycle, as facts are added and removed, the
set of rules must be updated / maintained.

CS 470: Expert Systems Rete Pattern Matching Algorithm 3


• Ch
Checking
ki rules
l against
i ffacts on each
h cycle
l iis a
slow process. Most rule-based systems exhibit
temporal redundancy  facts in an expert
system change slowly over time 

• having rules drive the search for needed facts


requires unnecessary computation
computation, which can
be avoided by remembering what has already
been matched from cycle to cycle and
computing only the changes necessary for the
newlyy added or newlyy removed facts  rules
remain static and facts change.

CS 470: Expert Systems Rete Pattern Matching Algorithm 4


• The Rete Algorithm takes advantage of
redundancy by saving the state of the matching
process from cycle to cycle and re-computing
re computing
the changes in this state only for the change that
occur in the fact list
list.

• Th
The state
t t off the
th matching
t hi process is
i updated
d t d
only as facts are added and removed.

• Appartial match refers to state information


indicating that facts which have matched some
of the p
previous ppatterns in a rule.

CS 470: Expert Systems Rete Pattern Matching Algorithm 5


• Appattern match occurs when a fact has
satisfied a single pattern in any rule
without regard to variables in other
patterns that may restrict the matching
process.

• Th
The RRete
t algorithm
l ith also l ttakes
k advantage
d t
of structural similarity in rule  the fact that
many rules
l often
ft containt i similar
i il patterns
tt
or groups of patterns  by pooling
common components so they h need d not b
be
computed more than once.

CS 470: Expert Systems Rete Pattern Matching Algorithm 6


Pattern Network -1
• The problems related to matching facts to rules
can be
b di
divided
id d iinto
t ttwo steps:
t
1. When facts are added and removed it must be
determined which patterns have been matched.
2 Comparison of variable bindings across
2.
patterns must be checked to determine the
partial matches for a group of patterns
patterns.
• The p process of determining
g which facts have
matched which patterns is performed in the
ppattern network.
CS 470: Expert Systems Rete Pattern Matching Algorithm 7
Pattern Network -2
• pattern network is structured like a tree
where the first slot constraint of all the
p
patterns are the nodes connected to the root
of the tree,
• the second slot constraints of the all of the
patterns are the nodes connected to those
nodes, etc.
• The
h last
l slot
l constraints
i are the
h leaves
l off the
h
tree.

CS 470: Expert Systems Rete Pattern Matching Algorithm 8


Pattern Network -3
• The nodes are called one-input nodes  each
receives information from the node above it.
• Leaff nodes d to as terminal
d are referred
f i l nodes
d .
• E
Eachh pattern
tt node
d contains
t i a specification
ifi ti usedd
for determining whether the slot value of a fact
h matched
has h d the
h slot
l constraint
i off a pattern.
• Wh
When a fact
f t is
i asserted,
t d the
th pattern
tt nodes
d for
f
the first slot constraints in the pattern network
are checked.
h k d
CS 470: Expert Systems Rete Pattern Matching Algorithm 9
Pattern Network -4
4
• Any pattern node whose matching specification
is satisfied will activate the pattern nodes directly
below it.

• The process continues until a terminal node has


been reached.

• Each terminal node has an alpha or right


memory associated with it. The alpha memory
holds the set of all facts that have matched the
pattern associated with the terminal node.

CS 470: Expert Systems Rete Pattern Matching Algorithm 10


Join Network -1
• Once it is determined which patterns
have been matched by facts, comparison
off variable
i bl binding
bi di across patterns must
be checked to ensure that variables used
in more than one pattern have consistent
values.
values

• This is done in the join network.

CS 470: Expert Systems Rete Pattern Matching Algorithm 11


Join Network -2
• Each terminal node in the pattern network
acts as an input to a join, or two-input
node
d , in
i th
the jjoin
i network.
t k

• Each join contains a matching


specification for the matches of the alpha
memory associated with its terminal node
and
d ffor th
the sett off partial
ti l matches
t h that
th t have
h
matched previous patterns.
CS 470: Expert Systems Rete Pattern Matching Algorithm 12
Join Network -3
3
• Partial matches for previous patterns are
stored in the beta or left memory of the
join.
join
• The join network takes advantage of
structural similarity by sharing joins
between rules. Joins in join networks can
be shared by two rules if they have
identical patterns and join comparisons for
two or more patterns, starting with the first
pattern
pattern.
CS 470: Expert Systems Rete Pattern Matching Algorithm 13
Rete Network Example
p 1
(deftemplate x ?
?= x ?
?= y
(slot a))
?v1 ?v1
(d f
(deftemplate
l y
Left.0.a ?= Right.b
(slot b)) ?v1 = ?v1

(defrule example-1
p
(x (a ?v1))
(y (b ?v1))
==> ) example-1

CS 470: Expert Systems Rete Pattern Matching Algorithm 14


Rete Left and Right
g Memories
• left (alpha) memory
– contains the left input
p of a jjoin node
• right (beta) memory ?= x ?= y
– contains the right input of a join
node ?v1 ?v1
• notation:
Left.p.q ?= Right.r Left.0.a ?= Right.b
– comparep the contents of slot q in ?v1 = ?v1
fact p from the left memory with slot
r in the fact from the right memory
(deftemplate x (slot a))
(deftemplate y (slot b))

(defrule example-1
(x (a ?v1))
(y (b ?v1)) example-1
==> )

CS 470: Expert Systems Rete Pattern Matching Algorithm 15


Running the Network
• onlyy facts x and y are considered
• all facts where x.a == y.b pass ?= x ?= y
the join network
– all {x
{x, y} tuples are fowarded to ?v1 ?v1
the next node
– compare the contents of slot q in Left.0.a ?= Right.b
fact p from the left memory y with ?v1 = ?v1
slot r in the fact from the right
memory
(deftemplate x (slot a))
(deftemplate y (slot b))

(defrule example-1
( p
(x (a ?v1))
(y (b ?v1))
example-1
==> )

CS 470: Expert Systems Rete Pattern Matching Algorithm 16


Rete Network Example
p 2
• shares some facts with ?= x ?= y ?= z
Example 1
(deftemplate x (slot ?v2 ?v2
a))
Left.0.a ?= Right.b
(deftemplate p y (slot ?v2 = ?v2
b))
?v2
(deftemplate z (slot
c))
(defrule example-2
(x (a ?v2))
(y (b ?v2)) example-2

(z)
CS 470:
==> Expert
)Systems Rete Pattern Matching Algorithm 17
Rete Network Example 2 with Assert

• additional fact asserted


?= x ?= y ?= z
(deftemplate x (slot
a)) ? 2
?v2 ?v2 17
(deftemplate y (slot
Left.0.a ?= Right.b
b)) ? 2 = ?v2
?v2 ? 2
(deftemplate z (slot
?v2
c))
))
(defrule example-2
?v2 = 17
(x (a ?
( ?v2))))
(y (b ?v2))
(z) example-2

==> )
CS 470: Expert Systems Rete Pattern Matching Algorithm 18
(assert (z (c 17))
Rete Network Optimization
p
• networks with shared
?= x ?= y ?= z
facts can be combined
(deftemplate
p x (slot a))
(deftemplate y (slot b))
(deftemplate z (slot c)) Left.0.a ?= Right.b
(d f l example-1
(defrule l 1
(x (a ?v1))
(y (b ?v1))
==> )
(defrule example-2
(x (a ?v2))
(y (b ?v2))
(z) example-1 example-2
==> )
CS 470: Expert Systems Rete Pattern Matching Algorithm 19
Further Optimizations
p

• sophisticated data structures to optimize


the network
– hash table to presort the tokens before
running the join node tests
• fine-tuning via parameters
– frequently trade-off between memory usage
and time

CS 470: Expert Systems Rete Pattern Matching Algorithm 20


Special
p Cases for Pattern Matching
g

• additional
dditi l enhancements
h t off th
the R
Rete
t network
t k
can be used to implement specific methods
– backward
b k d chaining
h i i
• requires a signal indicating to the network that a particular fact
is needed
– not conditional element
• indicates the absence of a fact
• requires
i special
i l jjoin
i nodes
d andd special
i l fifields
ld iin th
the ttokens
k
passing through the network
– test conditional element
• uses a special join node that ignores its right input
• the result of the function is passed on

CS 470: Expert Systems Rete Pattern Matching Algorithm 21


Assert and Retract with Rete

• asserting
ti additional
dditi l ffacts
t iimposes some more
constraints on the network
• retracting facts indicates that some previously computed
activation records are not valid anymore, and should be
discarded
• in addition to the actual facts, tags are sent through the
networks
– ADD to add facts (i
(i.e.
e for assert)
– REMOVE to remove facts (i.e. for retract)
– CLEAR to flush the network memories (i.e. for reset)
– UPDATE to populate the join nodes of newly added rules
• already existing join nodes can neglect these tokens

CS 470: Expert Systems Rete Pattern Matching Algorithm 22


Rule Formulation

Pattern Order
General vs. Specific Rules
Simple vs
vs. Complex Rules
Loading and Saving Facts

CS 470: Expert Systems Rete Pattern Matching Algorithm 23


Pattern Order
• since Rete saves information about rules
and facts,, it can be critical to order
patterns in the right way

– otherwise a potentially huge number of partial


matches can be generated

CS 470: Expert Systems Rete Pattern Matching Algorithm 24


Example
p Pattern Order
(deffacts information (deffacts information
(find-match a c e g) (find-match a c e g)
f1 (it
(item a)
)
(item a) f2 (item b)
(item b) f3 (item c)
(it
(item c)
) f4 (item d)
(item d) f5 (item e)
(item e) f6 (item f)
(item f) f7 (item g))
(item g)) f8 (defrule match-1
(defrule match-1 (item ?x)
(find-match ?x ?y ?z ?w) (item ?y)
P1 (item ?z)
(item ?x) P2 (item ?w)
(item ?y) P3 (find-match ?x ?y ?z
(item ?z) P4 ?w)
(item ?w) P5 ==>>
==> (assert (found-match ?x
(assert (found-match ?x ?y ?z ?w))
CS 470: Expert Systems Rete Pattern Matching Algorithm 25
?y ?z ?w))
Pattern Matches
• full matches • full matches
P1: f1 P1: f1
P2: f2,f3,f4,f5,f6,f7,f8 P2: f2,f3,f4,f5,f6,f7,f8
P3: f2,f3,f4,f5,f6,f7,f8 P3: f2,f3,f4,f5,f6,f7,f8
P4: f2,f3,f4,f5,f6,f7,f8
P4: f2,f3,f4,f5,f6,f7,f8
P5: f2,f3,f4,f5,f6,f7,f8
P5: f2,f3,f4,f5,f6,f7,f8
• partial matches
• partial matches P1: [f2,f3,f4,f5,f6,f7,f8]
P1: [f1] P1-2:
P1-2: [f1,f2] [f2,f2],[f2,f3],[f2,f4
P1-3: [f1,f2,f4] ],[f2,f5],
[f2,f6],[f2,f7],[f2,f8],
P1-4: [f1,f2,f4,f6]
P1-5: [f1,f2,f4,f6,f8]
[ ] [f3,f2],[f3,f3],[f3,f4
Total: 29 full, 5 partial matches ],[f3,f5],
[f3,f6],[f3,f7],[f3,f8],
...
CS 470: Expert Systems
P1-3, P1-4: ...
Rete Pattern Matching Algorithm 26
P1-5: [f2 f4 f6 f8 f1]
Guidelines for Pattern Ordering
• mostt specific
ifi patterns
tt first
fi t
– smallest number of matching facts
– largest
l t number
b off variable
i bl bi
bindings
di tto constrain
t i other
th
facts
• patterns matching volatile facts go last
– facts that are changing frequently should be used by
patterns late in the LHS
– smallest number of changes in partial matches
– mayy cause a dilemma with the above g guideline
• patterns matching the fewest facts first
– reduces the number of p
partial matches

CS 470: Expert Systems Rete Pattern Matching Algorithm 27


Importance
p of Pattern Order
• Because the Rete algorithm saves the state from one cycle to
the
h next, it
i is
i important
i to make
k sure that
h rulesl do
d not
generate large numbers of partial matches.

The Matches Command


• The matches command (debugging command) will display
the pattern matches, partial matches, and activations of a rule
 useful for finding rules that generate large numbers of
partial matches and for debugging cases in which a rule
appears to have all of its patterns satisfied but is still not
activated.
ti t d

CS 470: Expert Systems Rete Pattern Matching Algorithm 28


Importance
p of Pattern Order

• The syntax of the matches command is:


(matches <rule-name>)\

Watching the Changing State

• The matches command provides a way of examining


partial matches for a rule; another way is to consider
them partial activations of a rule.

CS 470: Expert Systems Rete Pattern Matching Algorithm 29


Ordering
g Patterns for Efficiency
y
• To limit the number of partial matches, it
is best to follow certain guidelines for
ordering patterns.
patterns
• The most specific pattern should be
placed toward the front of the LHS of a
p
rule. The most specific p
pattern has the
smallest number of matching facts in the
fact list and the largest number of variable
bindings that constrain other patterns.

CS 470: Expert Systems Rete Pattern Matching Algorithm 30


Ordering Patterns for Efficiency

• Patterns matching against facts that are


frequently added and removed from the fact list
should be placed toward the end of the LHS of a
rule  thereby causing the smallest number of
changes in the partial matches for the rule.

• Patterns that will match very few facts in the fact


list should be placed near the front of the rule.

CS 470: Expert Systems Rete Pattern Matching Algorithm 31


General Rules vs. Specific
p Rules

1. Specific rules tend to isolate much of the


pattern-matching process in the pattern
network, reducing the amount of work in the
join network.
2. General rules often provide more opportunity
to sharing in the pattern and join networks.
3. A single rule can also be more maintained than
a group.
4. General rules need to be written carefully.

CS 470: Expert Systems Rete Pattern Matching Algorithm 32


Simple Rules vs. Complex Rules

1. The easiest way to code a problem in a rule-


based language is not necessarily the best.

2 The number of comparisons performed can


2.
often be reduced by using temporary facts to
store data
data.

CS 470: Expert Systems Rete Pattern Matching Algorithm 33


CS 470: Expert Systems Rete Pattern Matching Algorithm 34
Rete Example
p 3

• Ruleblock
((rule rb1
(r1 if ((class C1)(a1 =x)(a2 4))
((class C2)(a1 =x)(a2 6))
then (deposit (C4 (a1 8))))
(r2 if ((class C1)(a1 =x)(a2 4))
((
((class C
C2)(a1
)( =x)(a2
)( 7)) ))
((class C3)(a1 6))
th (deposit
then (d it (C4 (a1
( 1 5)))) )

CS 470: Expert Systems Rete Pattern Matching Algorithm 35


Rete Example
p 3

• Working memory
(working-memory
(working memory wm1
{((class C1)(a1 1)(a2 4)),
((class C2)(a1 1)(a2 6)),
((class C2)(a1 1)(a2 7))
7)),
((class C3)(a1 7)) })
• Rete network sample

CS 470: Expert Systems Rete Pattern Matching Algorithm 36


CS 470: Expert Systems Rete Pattern Matching Algorithm 37
Rule: if P1 P2 … Pn then...
then

CS 470: Expert Systems Rete Pattern Matching Algorithm 38


Rete Example
p 4
• FACTS RULES
• (has-goal e1 simplify) (R1 (has-goal ?x simplify)
• (expression e1 0 + 3) (expression ?x 0 + ?y)
• (has-goal e2 simplify)
==>....)
• (expression e2 0 + 5)
(R2 (has-goal ?x simplify)
• (has-goal e3 simplify)
(expression ?x 0 * ?y)
• (expression e3 0 * 2)
==> )
==>....)
• 3 one-input nodes
• 2 two-input nodes
• – has-goal,
– R1
• – expression *,
– R2
• – expression
p +

CS 470: Expert Systems Rete Pattern Matching Algorithm 39


Rete Example
p 4

CS 470: Expert Systems Rete Pattern Matching Algorithm 40


Rete Example
p 5
(deftemplate
p animal
(slot type) (slot color) (slot size) (slot hair))
(defrule rule_1
rule 1
(animal (type cat) (color ?c) (size sm) (hair ?h)
(animal (type dog) (color ?c) (size ?q) (hair med)
(animal (type cat) (color ?c) (size lrg) (hair ?h)
( i l (type
(animal (t dog)
d ) (color
( l ?c)? ) (size
( i ?q)
? ) (hair
(h i long)
l )
=>
(assert ..)
)

CS 470: Expert Systems Rete Pattern Matching Algorithm 41


Rete Example
p 5
f-1 ((animal ((type
ype cat)
c ) (color
(co o yellow)
ye ow) (size
(s e lrg)
g) (hair
( short)
s o )
f-2 (animal (type cat) (color calico) (size lrg) (hair short)
ff-33 (animal (type cat) (color calico) (size sm) (hair short)
f-4 (animal (type dog) (color brown) (size med) (hair long)
ff-5
5 (animal (type cat) (color brown) (size sm) (hair med)
f-6 (animal (type dog) (color brown) (size sm) (hair short)
f-7 (animal (type dog) (color calico) (size med) (hair medium)
f-8 (animal (type dog) (color brown) (size lrg) (hair medium)
f-9 (animal (type dog) (color calico) (size med) (hair long)

CS 470: Expert Systems Rete Pattern Matching Algorithm 42


CS 470: Expert Systems Rete Pattern Matching Algorithm 43
CS 470: Expert Systems Rete Pattern Matching Algorithm 44
CS 470: Expert Systems Rete Pattern Matching Algorithm 45
CS 470: Expert Systems Rete Pattern Matching Algorithm 46

You might also like