1.1.rete - a fast algorithm for the many pattern, many object pattern match problem

Upload: cfscholl

Post on 03-Jun-2018

226 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    1/21

    ARTIFICIAL INTELLIGENCE 17

    R e t e : A F a s t A l g o r i t h m f o r t h eM a n y P a t t e r n / M a n y O b j e c tP a t t e r n M a t c h P r o b l e mC h a r l e s L . F o r g yDepa rtment of Com puter Science Carnegie-Mellon UniversityPittsburgh PA 15213 U.S.A.

    Recommended by Harry Barrow

    ABSTRACTThe R ete M atc h Alg ori th m is an ef fic ient m etho d for com paring a large collection o f patterns to a largecollection o f objects. It inds al l the objects that ma tch each pattern. The algorithm wa s developed fo r use inproduction system interpreters an d i t has been used for systems containing fr om a fe w hun dred to moretha n a tho usa nd patterns a nd objects. This article presents the algorithm in detail. I t explains the basicconcepts o f the algorithm it describes pattern a nd object representations t ha t are appropriate for thealgorithm an d it describes the operations performed by the pattern ma tcher.

    1. IntroductionIn many pa t tern/many o bjec t pat tern matching a co l l ec t ion o f pat terns iscom pared to a co l l ec t ion o f objec ts and a ll the matches are de term ined. Thati s the pat tern m atcher f inds every objec t that matches each pat tern . This k indof pat tern matching i s used extens ive ly in Art i f i c ia l Inte l l igence programstoday . Fo r ins tance i t i s a bas ic comp one nt o f product ion sys tem interpre ters .Th e interpre ters use i t to de term ine ~vhich produ ct ions have sa t is f ied cond i t ionparts. U nfor tun ate ly i t can be s low wh en large num bers o f pat terns or objec tsa r e i n v o l v e d . S o m e s y s t e m s h a v e b e e n o b s e r v e d t o s p e n d m o r e t h a n n i n e -tenths o f the ir to ta l run t ime p er forming th i s k ind of pat tern m atching [5 ]. This

    *This research was sponsored by the Defense Advanced Research Projects Agency DOD),ARPA Order No. 3597, monitored by the Air Force Avionics Laboratory under ContractF33615-78-C-1551.

    The views and conclusions contained in this document are those of the author and should not beinterpret ed as representing the ottieial policies, either expressed or implied, of the DefenseAdvanced Research Projects Agency or the US Government.Artificial Intelligence 19 1982) 17-37IX 10 4- 37 02 /8 2/ ~/ 02 .7 5 O 1982 North-Holland

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    2/21

    18 C.L. FORG Ya r t i c l e d e s c r i b e s a n a l g o r i t h m t h a t w a s d e s i g n e d t o m a k e m a n y p a t t e r n / m a n yo b j e c t p a t t e r n m a t c h i n g l es s e x p e n s i v e . T h e a l g o r i t h m w a s d e v e l o p e d f o r u s e inp r o d u c t i o n s y s t e m i n t e r p r e t e r s , b u t s i n c e i t s h o u l d b e u s e f u l f o r o t h e r l a n -g u a g e s a n d s y s t e m s a s w e l l , i t i s p r e s e n t e d i n d e t a i l .T h i s a r t ic l e a t t e n d s t o t w o c o m p l e m e n t a r y a s p e c t s o f e f f ic i en c y : (1 ) d e s i g n i n ga n a l g o r i t h m f o r t h e t a s k a n d (2) i m p l e m e n t i n g t h e a l g o r i t h m o n t h e c o m p u t e r .T h e r e st o f S e c ti o n 1 p r o v i d es s o m e b a c k g r o u n d i n f o r m a t i o n . S e c ti o n 2p r e s e n t s t h e b a s ic c o n c e p t s o f t h e a l g o r i t h m . S e c t i o n 3 e x p l a in s h o w t h e o b j e c t sa n d p a t t e r n s s h o u l d b e r e p r e s e n t e d t o a l l o w th e m o s t e ff i ci e n t i m p l e m e n t a t i o n s .S e c t i o n 4 d e s c r i b e s i n d e t a i l a v e r y f a s t i m p l e m e n t a t i o n o f t h e a l g o r i t h m .F i n a l l y , S e c t i o n 5 p r e s e n t s s o m e o f t h e r e s u l t s o f t h e a n a l y s e s o f th e a l g o r i t h m .1.1. oPs5T h e m e t h o d s d e s c r i b e d i n t h i s a r t i c l e w e r e d e v e l o p e d f o r p r o d u c t i o n s y s t e mi n t e r p r e t e r s , a n d t h e y w i l l b e i l l u s t r a t e d w i t h e x a m p l e s d r a w n f r o m p r o d u c t i o ns y s t e m s . T h i s s e c t i o n p r o v i d e s a b r i e f i n t r o d u c t i o n t o t h e l a n g u a g e u s e d i n t h ee x a m p l e s , o P s 5 . F o r a m o r e c o m p l e t e d e s c r i p t i o n o f o PS 5 , s e e [ 6] .

    A p r o d u c t i o n s y s t e m p r o g r a m c o n s is ts o f an u n o r d e r e d c o ll e ct io n o f If - T h e ns t a t e m e n t s c a l l e d p r o d u c t i o n s T h e d a t a o p e r a t e d o n b y t h e p r o d u c t i o n s is h e l di n a g l o b a l d a t a b a s e c a l l e d w o r k i n g m e m o r y B y c o n v e n t i o n , t h e I f p a r t o f ap r o d u c t i o n i s c a l l ed i t s L H S ( l e f t -h a n d s i d e) , a n d i ts T h e n p a r t i s ca l l e d it s R H S( r i g h t - h a n d s i d e ) . T h e i n t e r p r e t e r e x e c u t e s a p r o d u c t i o n s y s t e m b y p e r f o r m i n gt h e f o l l o w i n g o p e r a t i o n s .

    (1) M a t c h E v a l u a t e t h e L H S s o f t h e p r o d u c t i o n s t o d e t e r m i n e w h i c h a r es a ti sf ie d g iv e n t h e c u r r e n t c o n t e n t s o f w o r k i n g m e m o r y .

    (2) Confl ict resolut ion S e l e ct o n e p r o d u c t i o n w i t h a s a ti s fi e d L H S ; i f n op r o d u c t i o n s h a v e s a t i s f i e d L H S s , h a l t t h e i n t e r p r e t e r .

    (3) A c t P e r f o r m t h e a c t io n s in t h e R H S o f t h e s e le c t ed p r o d u c t i o n .(4 ) Go to 1 .o P s s w o r k i n g m e m o r i e s t y p i c al ly c o n t a i n s e v e ra l h u n d r e d o b j e c ts , a n d e a c h

    o b j e c t t y p ic a l ly h a s b e t w e e n t e n a n d o n e h u n d r e d a s s o c ia t e d a t tr i b u t e - v a lu ep a i r s . A n o b j e c t t o g e t h e r w i t h i t s a t t r i b u t e - v a l u e p a i r s i s c a l l e d a w o r k i n gm e m o r y e le m e n t T h e f o l l o w i n g is a t y p i c a l , t h o u g h v e r y s m a l l , o P s 5 w o r k i n gm e m o r y e l e m e n t ; i t i n d i c a t e s t h a t t h e o b j e c t o f c l a s s E x p r e s s i o n w h i c h i sn a m e d E x p r l 7 h a s 2 a s i ts f ir s t a r g u m e n t , * a s i ts o p e r a t o r , a n d X a s i ts s e c o n da r g u m e n t .

    ( E x p r e s s i o n 1 N a m e E x p r l 7 1 A r g l 2 1 O p * ~ A r g 2 X )T h e 1 i s t h e o P s5 o p e r a t o r t h a t d i s t in g u i s h e s a t t r i b u t e s f r o m v a l u es .

    T h e L H S o f a p r o d u c t i o n c o n s i s t s o f a s e q u e n c e o f p a t t e r n s ; t h a t i s , as e q u e n c e o f p ar t ia l d e s c ri p ti o n s o f w o r k i n g m e m o r y e l e m e n t s . W h e n a p a t t e r nP d e s c r i b e s a n e l e m e n t E , P i s s a i d to m a t c h E . I n s o m e p r o d u c t i o n s , s o m e o f

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    3/21

    THE RET E MATCH ALGORITHM 19t h e p a t t e r n s a r e p r e c e d e d b y t h e n e g a t i o n s y m b o l , - . A n L H S i s s a ti sf ie d w h e n(1 ) E v e r y p a t t e rn t h at i s n o t p r e c e d e d b y - m a t c h e s a w o rk i ng m e m o r ye l e m e n t , a n d

    (2 ) N o p a t t e rn t h at is p r e c e d e d b y - m a t c h e s a w o r k in g m e m o r y e le m e n t .T h e s i m p l e s t p a tt e r n s c o n t a in o n l y c o n s t a n t s y m b o l s a n d n u m b e r s . A p a t t e r n

    c o n t a in i n g o n l y c o n s t a n t s m a t c h e s a w o r k in g m e m o r y e l e m e n t i f e v e r y c o n s t a n ti n t h e p a t t e r n o c c u r s i n t h e c o r r e s p o n d i n g p o s i t i o n i n t h e w o r k i n g m e m o r ye l emen t . ( S i n ce p a t t e r n s a r e p a r t i a l d e s c r i p t i o n s , i t i s n o t n eces s a r y f o r ev e r yc o n s t a n t i n t h e w o r k i n g m e m o r y e l e m e n t t o o c c u r in t h e p a t t e rn . ) T h u s t h ep a t t e r n

    ( Ex p r e s s i o n 1 O p * 1 A r g 2 0 )w o u l d m a t c h t h e e l e m e n t

    ( Ex p r e s s i o n 1 N am e Ex p r 8 6 1 A r g l X 1 O p * 1` A r g 2 0 )M an y n o n - co n s t an t s y m b o l s a r e av a i l ab l e in o PS 5 f o r d ef in f n g p a t t e r n s , b u t t h e

    t w o mo s t i mp o r t an t a r e v a r i ab l e s an d p r ed i ca t e s . A v a r i ab l e i s a s y mb o l t h a tb e g i n s w i th t h e c h a r a c t e r ( a n d e n d s w i th t h e c h a r a c t e r ) - - f o r e x a m p l e (X ) . Av a r i ab l e i n a p a t t e r n w i ll m a t ch an y v a l u e in a w o r k i n g m em o r y e l eme n t , b u t if av a r i a b le o c c u r s m o r e t h a n o n c e i n a p r o d u c t i o n s L H S , a l l o c c u r r e n c e s m u s t m a t c ht h e s a m e v a l u e . T h u s t h e p a t t e r n

    ( E x p re s s io n T A r g l ( V A L ) T A r g 2 ( V A L ) )w o u l d ma t ch e i t h e r o f t h e f o l l o w i n g

    ( E x p r e s s io n 1 N a m e E x p r 9 1 ` A r g l E x p r 2 3 1 O p * T A r g 2Ex p r 2 3 )( E x p r e s s io n 1` N a m e E x p r 5 1 ` A r g l 0 1 ` O p - l A r g 2 0 )

    b u t i t w o u l d n o t m a t c h( E x p r e s s io n 1 N a m e E x p r 8 1 A r g l 0 1 O p * 1 A r g 2 E x p r 2 3 )

    Th e p r ed i ca t e s in o P s 5 i n c l u d e = ( eq u a l ), < > ( n o t eq u a l ) , < ( le s s t h an ), >( g r ea t e r th an ) , < = ( le s s t h an o r eq u a l ) , an d > = ( g r ea t e r t h an o r eq u a l ) . Ap r ed i ca t e i s p l aced b e t w een an a t t r i b u t e an d a v a l u e t o i n d i ca t e t h a t t h e v a l u em a t c h e d m u s t b e r e l a t e d i n th a t w a y t o th e v a l u e i n t h e p a t t e rn . F o r i n s ta n c e ,

    ( E x p re s s io n 1 O p < > * )w i l l ma t ch an y ex p r e s s i o n w h o s e o p e r an d i s n o t * . P r ed i ca t e s can b e u s ed w i t hv a r i ab l e s a s w e l l a s w i t h co n s t an t v a l u es . F o r ex amp l e , t h e f o l l o w i n g p a t t e r n

    ( E x p re s s io n 1 A r g l ( L E F T ) 1 A r g 2 < > ( L E F T ) )w i l l ma t ch an y ex p r e s s i o n i n w h i ch t h e f i r s t a r g u men t d i f f e r s f r o m t h e s eco n da r g u m e n t .

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    4/21

    20 C.L. FORGYT h e R H S o f a p r o d u c t i o n c o n s is ts o f an u n c o n d i t i o n a l s e q u e n c e o f a c ti o n s.

    T h e o n l y a c t i o n s t h a t n e e d t o b e d e s c r i b e d h e r e a r e t h e o n e s t h a t c h a n g ew o r k i n g m e m o r y . M A K E b u il d s a n e w e l e m e n t a n d a d d s it t o w o r k i n gm e m o r y . T h e a r g u m e n t t o M A K E is a p a t t e r n l ik e t h e p a t te r n s in L H S s . F o re x a m p l e ,

    ( M A K E E x p r e s s io n ~ N a m e E x p r l T A r g l 1)w i ll b u i ld a n e x p r e s s i o n w h o s e n a m e i s E x p r l , w h o s e f ir st a r g u m e n t is I , a n dw h o s e o t h e r a t t r i b u t e s a l l h a v e t h e v a l u e N I L ( t h e d e f a u l t v a l u e i n o e s s ) .M O D I F Y c h a n g e s o n e o r m o r e v a l u e s o f a n e x i s t i n g e l e m e n t . T h i s a c t i o n t a k e sa s a r g u m e n t s a p a t t e r n d e s i g n a t o r a n d a lis t o f a t t r i b u t e - v a l u e p a i rs . T h ef o l l o w i n g a c t i o n , f o r e x a m p l e

    ( M O D I F Y 2 ~ O p N I L T A r g 2 N I L )w o u l d t a k e t h e e x p r e s s i o n m a t c h i n g t h e s e c o n d p a t t e r n a n d c h a n g e i ts o p e r a t o ra n d s e co n d a r g u m e n t t o N I L . T h e a c ti on R E M O V E d e l et e s e l e m e n t s f r o mw o r k i n g m e m o r y . I t t a k e s p a t te r n d e s i g n a t o rs a s a r g u m e n t s . F o r e x a m p l e

    ( R E M O V E 1 2 3 )w o u l d d e l e t e t h e e l e m e n t s m a t c h i n g t h e f i r s t t h r e e p a t t e r n s i n a p r o d u c t i o n .

    A n o r s 5 p r o d u c t i o n c o n s i s ts o f ( 1 ) t h e s y m b o l P , ( 2) th e n a m e o f t h ep r o d u c t i o n , ( 3 ) t h e L H S , ( 4 ) t h e sy m b o l - - > , a n d ( 5 ) t h e R H S , w i th e v e r y t h i n ge n c l o s e d i n p a r e n t h e s e s . T h e f o l l o w i n g i s a t y p i c a l p r o d u c t i o n .

    ( P T i m e 0 x( G o a l 1 T y p e S i m p l i f y 1 O b j e c t ( X ) )( E x p r e s s i o n I N a m e ( X ) 1 A r g l 0 1 O p * )( M O D I F Y 2 I O P N I L ~ A r g2 N I L ))

    1 2 W ork on produ ction system efficiencyS i n c e e x e c u t i o n s p e e d h a s a lw a y s b e e n a m a j o r i ss u e fo r p r o d u c t i o n s y s te m s ,s e v e r al r e s e a r c h e r s h a v e w o r k e d o n t h e p r o b l e m o f e f fi ci en c y . T h e m o s tc o m m o n a p p r o a c h h a s b e e n t o c o m b i n e a p r o c e s s c a ll e d in exing w i t h d i r e c ti n t e r p r e t a t i o n o f th e L H S s . I n t h e s i m p l e st f o r m o f i n d e x in g , t h e i n t e r p r e t e rb e g i n s th e m a t c h p r o c e s s b y e x t ra c t i n g o n e o r m o r e f e a t u r e s f r o m e a c h w o r k i n gm e m o r y e l e m e n t , a n d u s e s t h o s e f e a t u r e s t o h a sh i n t o t h e c o l l e ct io n o fp r o d u c t i o n s . T h i s p r o d u c e s a s e t o f p r o d u c t i o n s t h a t m i g h t h a v e s a ti sf ie d L H S s .T h e i n t e r p r e t e r e x a m i n e s e a c h L H S i n t h i s s e t i n d i v i d u a l l y t o d e t e r m i n ew h e t h e r i t is i n f a c t s a ti s fi e d . A m o r e e f f i c ie n t f o r m o f in d e x i n g a d d s m e m o r y t ot h e p r o c e s s . A t y p i c a l s c h e m e i n v o l v e s s t o r i n g a c o u n t w i th e a c h p a t t e r n . T h ec o u n t s a r e a ll z e r o w h e n e x e c u t i o n o f th e s y s t e m b e g in s . W h e n a n e l e m e n te n t e r s w o r k i n g m e m o r y , t h e i n d e x i n g f u n c t io n i s e x e c u t e d w i th t h e n e w

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    5/21

    THE RETE MATCH ALGORrrHM 21element as its only input, and all the patterns that are reached have theircounts increased by one. When an element leaves working memory, the indexis again executed, and the patterns that are reached have their countsdecreased by one. The interpreter performs the direct interpretation step onlyon those LHSs that have non-zero counts for all their patterns. Interpretersusing this scheme--in some cases combined with other efficiency measures--have been described by McCracken [8], McDermott, Newell, and Moore [9],and Rychener [10].

    The algorithm that will be presented here, the Rete Match Algorithm, can bedescribed as an indexing scheme that does not require the interpretive step.The indexing function is represented as a network of simple feature recog-nizers. This representation is related to the graph representations for so-calledstructured patterns. See for example [2] and [7]). The Rete algorithm was firstdescribed in 1974 [3] . A 1977 paper [4] described some rather complexinterpreters for the networks of feature recognizers, including parallel inter-preters and interpreters which delayed evaluation of patterns as long aspossible. Delaying evaluation is useful because it makes it less likely thatpatterns will be evaluated unnecessarily.) A 1979 paper [5] discussed simple butvery fast interpreters for the networks. This article is based in large part on the1979 paper.

    2 . The Rete Match Algor i thm Bas ic ConceptsIn a production system interpreter, the output of the match process and theinput to conflict resolution is a set called the con f l i c t s et T h e conflict set is acollection of ordered pairs of the form

    Production, List of elements matched by its LHS)The ordered pairs are called i n s t an t i a t i ons The Rete Match Algorithm is analgorithm for computing the conflict set. That is, it is an algorithm to compare aset of LHSs to a set of elements fn order to discover all the instantiations. Thealgorithm can efficiently process large sets because it does not iterate over thesets.2 .1 . How to avo id i terat ing over working memoryA pattern matcher can avoid iterating over the elements in working memory bystoring information between cycles. The step that can require iteration isdetermining whether a given pattern matches any of the working memoryelements. The simplest interpreters determine this by comparing the pattern tothe elements one by one. The iteration can be avoided by storing, with eachpattern, a list of the elements that it matches. The lists are updated whenworking memory changes. When an element enters working memory, theinterpreter finds all the patterns that match it and adds it to their lists. When an

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    6/21

    22 C L FORG Yelement leaves working memory, the interpreter again finds all the patternsthat match it and deletes it from their lists.

    Since pattern matchers using the Rete algorithm save this kind of in-formation, they never have to examine working memory. The pattern matchercan be viewed as a black box with one input and one output.

    (Changes to Working Memory)

    Black Box

    (Changes to the Conflict Set)The box receives information about the changes that are made to workingmemory, and it determines the changes that must be made in the conflict set tokeep it consistent. For example, the black box might be told that the element

    (Goal T Type Simplify 1' Object Exprl9)has been added to working memory, and it might respond that productionTimexN has just become instantiated.2.1.1. TokensThe descriptions of working memory changes that are passed into the blackbox are called tokens A token is an ordered pair of a tag and a list of dataelements. In the simplest implementations of the Rete Match Algorithm, onlytwo tags are needed, + and -. The tag + indicates that something has beenadded to working memory. The tag - indicates that something has beendeleted from working memory. When an element is modified, two tokens aresent to the black box; one token indicates that the old form of the element hasbeen deleted from working memory, and the other that the new form of theelement has been added. For example, if

    (Expression 1' Name Expr41 1' Argl Ywas changed to

    (Expression 1' Name Expr41 I' Ar gl 2the following two tokens would be processed.

    (-(Expression(+(Expression

    1' Op + 1' Arg2 Y)TOp * I' Arg2 Y)

    1' Name Expr41 T Ar gl Y 1 Op + 1' Arg2 Y)>TNameExpr41 l 'Argl2 I 'Op* l 'Arg2Y)>2 2 H o w t o a v o i d i te r a ti n g o v e r p r o d u c t io n m e m o r yThe Rete algorithm avoids iterating over the set of productions by using atree-structured sorting network or index for the productions. The network,

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    7/21

    THE RETE MATCH ALGORITHM 23which is compiled from the patterns, is the principal component of the blackbox. The following sections explain how patterns are compiled into networksand how the networks perform the functions of the black box.2.2.1. ompiling the patternsWhen a pattern matcher processes a working memory element, it tests manyfeatures of the element. The features can be divided into two classes. The firstclass, which could be called the intra-element features, are the ones thatinvolve only one working memory element. For an example of these features,consider the following pattern.

    (Expression 1 Name (N) l Ar gl 0 l Op + l Arg2 (X))When the pattern matcher processes this pattern, it tries to find workingmemory elements having the following intra-element features.-The class of the element must be Expression.-The value of the Argl attribute must be the number 0.

    The value of the Op attribute must be the atom +.The other class of features, the inter-element features, results from having avariable occur in more than one pattern. Consider Plus0x s LHS.

    (P Plus0x(Goal 1 Type Simplify 1 Object (N))(Expression 1 Name (N) l Ar gl 0 l Op +

    _ - ~ . . .)1 Arg2 (X))

    The intra-element features for the second pattern are listed above. A similarlist can be constructed for the first pattern. But in addition to those two lists,the following inter-element feature is necessary because the variable (N) occurstwice.-The value of the Object attribute of the goal must be equal to the value ofthe Name attribute of the expression.

    The pattern compiler builds a network by linking together nodes which testelements for these features. When the compiler processes an LHS, it beginswith the intra-element features. It d~termines the intra-element features thateach pattern requires and builds a linear sequence of nodes for the pattern.Each node tests for the presence of one feature. After the compiler finisheswith the intra-element features, it builds nodes to test for the inter-elementfeatures. Each of the nodes has two inputs so that it can join two paths in thenetwork into one. The first of the two-input nodes joins th e linear sequencesfor the first two patterns, the second two-input nodes joins the output of thefirst with the sequence for the third pattern, and so on. The two-input nodestest every inter-element feature that applies to the elements they process.Finally, after the two-input nodes, the compiler builds a special terminal nodeto represent the production. This node is attached to the last of the two-input

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    8/21

    24 C L FORGY

    n o d es . F i g . 1 s h o w s t h e n e t w o r k f o r P lu s 0 x an d t h e s i mi la r p r o d u c t i o n T i me0 x .N o t e t h a t w h e n t w o L H S s r e q u i r e i d e n t i c a l n o d e s , t h e c o m p i l e r s h a r e s p a r t s o ft h e n e t w o r k r a t h e r t h an b u i l d i n g d u p l i ca t e n o d es .2.2.2. rocessing in the networkTh e r o o t n o d e o f t h e n e t w o r k ( a t t h e t o p i n F ig . 1 ) i s t h e i n p u t t o t h e b l ack b o x .T h i s n o d e r e c e i v e s t h e t o k e n s t h a t a r e s e n t t o t h e b l a c k b o x a n d p a s s e s c o p i e so f t h e t o k en s t o a l l i t s s u cces s o r s . Th e s u cces s o r s o f t h e t o p n o d e , t h e n o d es t op e r f o r m t h e i n tr a - e le m e n t t e st s, h a v e o n e i n p u t a n d o n e o r m o r e o u t p u t s . E a c hn o d e t e s t s o n e f ea t u r e an d s en d s t h e t o k en s t h a t p a s s t h e t e s t t o i t s s u cces s o r s .T h e t w o - i n p u t n o d e s c o m p a r e t o k e n s f r o m d i f f e r e n t p a t h s a n d j o i n t h e m i n t ob i g g e r t o k e n s i f t h e y s a t is fy th e i n te r - e l e m e n t c o n s t ra i n ts o f t h e L H S . B e c a u s eo f t h e t e s ts p e r f o r m e d b y t h e o t h e r n o d e s , a t e rm i n a l n o d e w i ll r e c e i v e o n lyt o k e n s t h a t i n s ta n t ia t e t h e L H S . T h e t e rm i n a l n o d e s e n d s o u t o f th e b l a c k b o xt h e i n f o r ma t i o n t h a t t h e co n f l i c t s e t mu s t b e ch an g ed .

    F o r a n e x a m p l e o f th e o p e r a t i o n o f t h e n o d e s , c o n s i d e r w h a t h a p p e n s i n th en e t w o r k in F ig . 1 w h e n t h e f o ll o w in g tw o e l e m e n t s a r e p u t i n to a n e m p t yw o r k in g m e m o r y .

    ( G o a l 1 T y p e S i m p li fy 1 O b j e c t E x p r l 7 )( E x p r e s s io n 1 N a m e E x p r 1 7 1 A r g l 0 1 O p * 1 A r g 2 X )

    F i r s t t h e t o k en( + ( G o a l 1 T y p e S i m p li fy 1 O b j e c t E x p r l 7 ) )

    i s c r e a t e d a n d s e n t t o t h e r o o t o f t h e n e t w o r k . T h i s n o d e s e n d s t h e t o k e n t o it ssuccesso r s . One o f t he successo r s (on t he r i gh t i n F ig . 1 ) t es t s i t and r e j ec t s i tb ecau s e i t s c l a s s i s n o t Ex p r e s s i o n . Th i s n o d e d o es n o t p a s s t h e t o k en t o i t ss u c c e ss o r . T h e o t h e r s u c c e s s o r o f t h e t o p n o d e a c c e p t s th e t o k e n ( b e c a u s e i tsc l a s s i s G o a l ) an d s o s en d s i t t o i t s s u cces s o r . Th a t n o d e a l s o accep t s t h e t o k en( s ince i t s t ype i s S impl i fy ) , and i t sends t he t oken to i t s successo r s , t het w o - i n p u t n o d e s . S i n c e n o o t h e r t o k e n s h a v e a r r i v e d a t t h e t w o - i n p u t n o d e s ,t h ey can p e r f o r m n o t e s t s ; t h ey mu s t j u s t s t o r e t h e t o k en an d w a i t .W h e n t h e t o k e n

    ( + ( E x p r e s s i o n 1 N a m e E x p r l 7 1 A r g l 0 1` O p * 1 A r g 2 X ) )i s p r o ces s ed , i t i s t e s t ed b y t h e o n e - i n p u t n o d es an d p as s ed d o w n t o t h e r i g h ti n p u t o f T i m e 0 x s t w o - i n p u t n o d e . T h i s n o d e c o m p a r e s t h e n e w t o k e n t o t h eea r l i e r o n e , an d f i n d i n g t h a t t h ey a l l o w t h e v a r i ab l e t o b e b o u n d co n s i s t en t l y , i tc r e a t e s a n d s e n d s o u t t h e t o k e n

    ( + ( G o a l 1 T y p e S i m p li fy 1 O b j e c t E x p r 1 7 )( E x p r e s s io n 1 N a m e E x p r 1 7 1 A r g l 0 1 O p * 1 A r g 2 X ) )

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    9/21

    T H E R E T E M A T C H A L G O R I T H M 2 5

    (P Plus0x(Go a l 1 Ty pe S impl i fy(Expre s s ion T Na m e (N)

    _ _ > . . .

    ( P T i m e 0 x(Go a l 1 Ty pe S impl i fy(Expre s s i on 1 Na m e (N)

    _ _ : > . . .

    1 Objec t (N))1 A r g l 0 1 O p +

    1 Objec t (N))1 A r g l 0 1 O p *

    1 Arg2 (X))

    1 Arg2 (X))

    Di s t r i bu t e t he t oke ns .j lI s the e l e m e n t c l as s Go a l ?I s t he va l ue o f t heType S i mp l i fy? ~

    I s t he va l ue o f t heO p ? [J o i n t h e e l e m e n t s i n w h i c ht h e v a l u e o f t h e O b j e c ta t t r i bu t e f rom t he l e f ti s e q u a l t ot h e v a l u e o f t h e N a m ea t t r i bu t e f rom t he r i gh t .R e por t t ha t p roduc t i onP l u s 0 x i s s a t i s f ie d

    I s t he e l e m e n t c l a ss Expre s s i on?I s t h e v a l u e o f t h e A r g l 0 ?

    I s t he va l ue o f t he

    Jo i n t he e l e me n t s i n w h i c ht h e v a l u e o f t h e O b j e c ta t t r ibu t e f ro m t h e l e f ti s e qua l t ot h e v a l u e o f t h e N a m ea t t r ibu t e f rom t he r i gh t .R e por t t ha t p roduc t i onT i m e 0 x i s s a t i s f i e d

    FIG. 1 . The ne twork for P lus0x and Time0x.W hen i t s succes sor the terminal no de for T im e0x rece ives th i s token i t addsthe ins tant ia t ion o f T ime0x to the conf l i c t s e t .2 . 2 . 3 . aving in format ion in the networkA s e x p l a i n e d a b o v e t h e b la c k b o x m u s t m a i n t ai n s ta t e in f o r m a t i o n b e c a u s e i tm u s t k n o w w h a t i s i n w o r k i n g m e m o r y . I n s i m p l e R e t e n e t w o r k s a ll su c h s t a tei s s t o r ed b y t h e t w o - i n p u t n o d e s . E a c h t w o - i n p u t n o d e c o n t a i n s t w o l i st s c a l l edi t s l e f t a n d r i g h t m e m o r i e s . T h e l e f t m e m o r y h o l d s c o p i e s o f t h e t o k e n s t h a tarr ived a t i t s l e f t input and the r ight m em ory h o lds copie s o f the token s thatarr ived a t i t s r ight input . The tokens are s tored as long as they are use ful . Then e x t s e c t i o n e x p l a i n s h o w t h e n o d e s d e t e r m i n e w h e n t h e t o k e n s a r e n o l o n g e ruse ful .

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    10/21

    26 C L FORGY2.2 .4 . sing the tagsT h e t a g in a t o k e n i n d i c a t e s h o w t h e s t a t e i n f o r m a t i o n i s t o b e c h a n g e d w h e nt h e t o k e n i s p r o c e s s e d . T h e + a n d - t o k e n s a r e p r o c e s s e d i d e n t ic a l ly e x c e p t :- T h e t e r m i n a l n o d e s u s e th e t a g s t o d e t e r m i n e w h e t h e r t o a d d a n i n s t a n ti a t io n t o

    t h e c o n f l ic t s e t o r t o r e m o v e a n e x i s ti n g in s t a n t i a t i o n . W h e n a + t o k e n i sp r o c e s s e d , a n i n s t a n t i a t i o n i s a d d e d ; w h e n a - t o k e n i s p r o c e s s e d , a ni n s t a n t ia t i o n is r e m o v e d .

    - T h e t w o - i n p u t n o d e s u s e t h e t a g s t o d e t e r m i n e h o w t o m o d i f y th e i r in t e rn a lm e m o r i e s . W h e n a + t o k e n i s p r o c e s s e d , i t i s s t o r e d i n t h e i n te r n a l m e m o r y ;w h e n a - t o k e n i s p r o c e s s e d , a t o k e n w i t h a n i d e n t i c a l d a t a p a r t is d e l e t e d .

    - T h e t w o - i n p u t n o d e s u s e th e t a g s t o d e t e r m i n e t h e a p p r o p r i a t e t a g s f o r t h et o k e n s t h e y b u i ld . W h e n a n e w o u t p u t is c r e a t e d , it is g i v e n th e t a g o f th e t o k e nt h a t j u s t a r r i v e d a t t h e t w o - i n p u t n o d e .

    2 3 C o m p l e t in g t h e s e t o f n o d e t y p e sT h e n e t w o r k i n F i g. 1 c o n t a i n e d f o u r k i n d s o f n o d e s : t h e r o o t n o d e , t h et e r m i n a l n o d e s , t h e o n e - i n p u t n o d e s , a n d t h e t w o - i n p u t n o d e s . C e r t a i n l y o n ec o u l d d e f i n e m a n y m o r e k i n d s o f n o d e s , b u t o n l y a f e w m o r e a r e n e c e s s a r y t oh a v e a c o m p l e t e a n d u s e f u l s e t. I n fa c t , o n l y t w o m o r e k i n d s o f n o d e s a r en e c e s s a r y t o i n t e r p r e t o P s 5 .

    A s e c o n d k i n d o f tw o - i n p u t n o d e is n e e d e d f o r n e g a t e d p a t t e r n s ( t h a t is ,p a t t e r n s p r e c e d e d b y - ) . T h e n e w t w o - i n p u t n o d e s t o re s a c o u n t w i t h e a c ht o k e n i n it s l e ft m e m o r y . T h e c o u n t i n d i c a t e s t h e n u m b e r o f t o k e n s i n t h e r i gh tm e m o r y t h a t a ll o w c o n s i s t e n t v a r i a b l e b i n d in g s . T h e t o k e n s in it s r i g h t m e m o r yc o n t a i n t h e e l e m e n t s t h a t m a t c h t h e n e g a t e d p a t t e r n - - - o r , m o r e p r e c i s e l y , t h et o k e n s c o n t a i n t h e e l e m e n t s t h a t h a v e t h e i n t r a - e l e m e n t f e a t u r e s t h a t t h en e g a t e d p a t t e r n r e q u i r e s . T h e n o d e a l l o w s t h e t o k e n s w i th a c o u n t o f z e r o t op a s s .

    T h e l a st n o d e t y p e t h a t n e e d s t o b e d e f i n e d is a v a r i a n t o f t h e o n e - i n p u tn o d e s d e s c r i b e d e a r l ie r . T h o s e n o d e s t e s t e d W o r k in g m e m o r y e l e m e n t s fo rc o n s t a n t f e a t u r e s ( t es ti n g , f o r e x a m p l e , w h e t h e r a v a l u e w a s e q u a l t o a g iv e na t o m i c s y m b o l ) . T h e n e w o n e - i n p u t n o d e s c o m p a r e t w o v a l u e s f r o m a w o r k i n gm e m o r y e l e m e n t . T h e s e n o d e s a r e u s e d t o p r o c e s s p a t t e r n s t h a t co n t a i n t w o o rm o r e o c c u r r e n c e s o f a v a r i ab l e . T h e f o ll o w in g , f o r e x a m p l e , w o u l d r e q u i r e o n eo f th e s e n o d e s b e c a u s e ( X ) o c c u r s t w ic e .

    ( E x p r e s s i o n 1 A r g l ( X ) 1 O p + 1 A r g 2 ( X ) )3 R e p r e s e n ti n g t h e N e t w o r k a n d t h e T o k e n s

    T h i s s e c t io n d e s c r i b e s r e p r e s e n t a t i o n s f o r t o k e n s a n d n o d e s ,, th a t a l l o w v e r y fa s ti n t e r p r e t e r s t o b e w r i t te n .

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    11/21

    T H E R E T E M A T C H A L G O R IT H M 2 73 1 W o r k i n g m e m o r y e l em e n t sT h e r e p r e s e n t a t i o n c h o s e n f o r t h e w o r k i n g m e m o r y e l e m e n t s s h o u l d h a v e t w op r o p e r t i e s .- T h e r e p r e s e n t a t i o n s h o u l d m a k e i t e a s y to e x t r a c t v a l u e s f r o m e l e m e n t s b e c a u s ee v e r y t e s t in v o l v e s e x tr a c t in g o n e o r m o r e v a l u es .- T h e r e p r e s e n t a t i o n s h o u l d m a k e i t e a s y to p e r f o r m t h e t e s t s o n c e t h e v a l u e s a r e

    a v a i l a b l e .T o m a k e e x t r a c t i n g t h e v a l u e s e a s y , e a c h e l e m e n t s h o u l d b e s t o r e d i n ac o n t i g u o u s b l o c k in m e m o r y , a n d e a c h a t t r i b u t e s h o u l d h a v e a d e s i g n a t e d i n d e xi n th e b l o c k . F o r e x a m p l e , i f e l e m e n t s o f cl as s C k h a d s e v e n t e e n a t t r i b u te s , A 1t h r o u g h A 1 7 , t h e y s h o u l d b e s t o r e d a s b l o c k s o f e i g h t e e n v a l u e s. T h e f ir st v a l u ew o u l d b e t h e c la ss n a m e C k ) . T h e s e c o n d v a l u e w o u l d b e t h e v a l u e o f a t t r ib u t eA 1 . T h e t h i rd w o u l d b e t h e v a l u e o f a t t r ib u t e A 2 , a n d s o o n . T h e p a r t i c u la ra s s i g n m e n t o f i n d i ce s t o a t t r ib u t e s i s u n i m p o r t a n t ; i t is i m p o r t a n t o n l y t h a t e a c ha t t r i b u t e h a v e a f ix e d i n d e x , a n d t h a t t h e i n d i c e s b e a s s i g n e d a t c o m p i l e t im e .T h i s a l l o w s t h e c o m p i l e r t o b u i l d t h e i n d i c e s i n t o t h e n o d e s . T h u s i n s t e a d o f an o d e l i k e t h e f o l l o w i n g :

    I s t h e v a l u e o f t h e S t a t u s a t t r i b u t e P e n d i n g ?t h e c o m p i l e r c o u l d b u i l d t h e n o d e

    I s t h e v a l u e a t l o c a t i o n 8 P e n d i n g ?W i t h t h i s r e p r e s e n t a t i o n , e a c h v a l u e c a n b e a c c e s s e d i n o n e m e m o r y r e f e r e n c e ,r e g a r d l es s o f th e n u m b e r o f a tt r ib u t e s p o s s e s se d b y a n e l e m e n t .

    T o m a k e t h e t e s ts i n e x p e n s i v e , t h e r e p r e s e n t a t i o n s h o u l d h a v e e x p l i ci t t y p eb it s. O n e o b v i o u s w a y t o r e p r e s e n t a v a l u e is t o u s e o n e w o r d f o r t h e t y p e a n do n e o r m o r e w o r d s f o r th e v a l u e p r o p e r . B u t m o r e s p a c e- e ff ic i en t r e p r e s e n -t a t io n s a r e a ls o p o s si b le . F o r e x a m p l e , c o n s i d e r a p r o d u c t i o n s y s t e m l a n g u a g et h a t s u p p o r t s t h r e e d a t a t y p e s , i n te g e r s , fl o a ti n g p o i n t n u m b e r s , a n d a t o m s . Ar e p r e s e n t a t i o n l ik e th e f o l lo w i n g m i g h t b e u s e d : O n e w o r d w o u l d b e a l l o c a te dt o e a c h v a l u e . F o r i n t e g e rs a n d a t o m s , t h e l o w o r d e r s i x te e n s a y) b i ts w o u l dh o l d t h e d a t u m a n d t h e s e v e n t e e n t h b i t w o u l d b e a t y p e b it . F o r f lo a ti n g p o i n tn u m b e r s , t h e e n t i r e w o r d w o u l d b e u s e d t o s t o r e a n o r m a l i z e d f l o a t i n g p o i n tn u m b e r . A f lo a t in g p o i n t n u m b e r w o u l d b e r e c o g n i z e d b y h a v in g a t l e as t o n en o n - z e r o i n t h e h i g h o r d e r b i t s .3 2 T h e n e t w o r kT h i s s e ct io n e x p l a in s h o w t o r e p r e s e n t n o d e s i n a f o r m s i m i la r to v o n N e u m a n nm a c h i n e i n s tr u c ti o n s . T h i s r e p r e s e n t a t i o n w a s c h o s e n b e c a u s e i t a ll o w s t h en e t w o r k i n t e r p r e t e r t o b e o r g a n i z e d l ik e th e i n t e r p r e t e r s f o r c o n v e n t i o n a l v o nN e u m a n n a r c h i t e c t u r e s .

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    12/21

    28 C.L. FORGY3.2.1 n assembly language no ta t ionT o m a k e i t e a s i e r t o d is cu s s t h e r e p r e s e n t a t i o n f o r t h e n o d e s , a n a s s e m b l yl a n g u a g e n o t a t i o n i s u s e d b e lo w A o n e - i n p u t n o d e l ik e

    I s t h e v a l u e o f l o c a t i n g 8 P e n d i n g ?b e c o m e s

    T E Q A 8, P e n d in gT h e T , w h i c h s t a n d s f o r t e s t, i n d i c a t e s th a t t h i s is a o n e - i n p u t n o d e T h e E Qi n d i c a t e s t h a t i t i s a t e st f o r e q u a l it y I t i s a l so n e c e s s a r y t o h a v e N E f o r n o te q u a l s , L T f o r l es s t h a n , e t c . ). T h e A i n d i c a t e s t h e n o d e t e s t s d a t a o f t y p ea t o m . T h e r e is a l s o a t y p e N f o r i n t e g e r v a l u e s , a t y p e F f o r f l o a ti n g p o i n t , a n da ty p e S fo r c o m p a r i n g t w o v a l u e s i n th e s a m e w o r k i n g m e m o r y e l e m e n t ) .T w o - i n p u t n o d e s a r e i n d i c a t e d b y l i n e s l i k e t h e f o l l o w i n g

    L 00 1 A N D 2 ) = 1 )L 0 0 1 i s a l a b e l . A N D i n d i c a t e s t h a t t h i s i s a t w o - i n p u t n o d e f o r n o n - n e g a t e dp a t t e r n s . T h e s e q u e n c e 2 ) = 1 ) i n d ic a t e s t h a t t h e n o d e c o m p a r e s t h e s e c o n dv a l u e o f e l e m e n t s f r o m t h e l e f t a n d t h e f i r s t v a l u e o f e l e m e n t s f r o m t h e r i g h t ;t h e -- i n d i c a te s t h a t i t p e r f o r m s a t e s t f o r e q u a l i t y . T h e t e r m i n a l n o d e s c o n t a i nt h e ty p e T E R M a n d t h e n a m e o f t h e p ro d u c t io n F o r e x a m p l e

    T E R M P l u s 0 xA s w i ll b e e x p l a i n e d b e l o w , t h e R O O T n o d e i s n o t n e e d e d in th i s r e p r e s e n -t a t i o n 322 Lineariz ing the networkT o m a k e t h e n o d e s l ik e th e i n st ru c t io n s fo r a v o n N e u m a n n m a c h i n e , it isn e c e s s a r y t o e l i m i n a t e t h e e x p l i c it l in k s b e t w e e n n o d e s . M a n y o f t h e e x p l i c itl i n k s c a n b e e l i m i n a t e d s i m p l y b y l i n e a r i z i n g t h e n e t w o r k , p l a c i n g a n o d e a n di t s s u c c e s s o r i n c o n t i g u o u s m e m o r y l o c a t i o n s H o w e v e r , s i n c e s o m e n o d e s h a v em o r e t h a n o n e s u c c e ss o r , a n d o t h e r s t h e t w o - i n p u t n o d e s ) h a v e m o r e th a n o n ep r e d e c e s s o r , l i n e a r iz i n g is n o t s u f fi c ie n t i n it se l f: t w o n e w n o d e t y p e s m u s t b ed e f i n e d t o r e p l a c e s o m e o f t h e l in k s. T h e f ir st o f th e n e w n o d e s , t h e F O R K , isu s e d t o i n d i c a t e t h a t a n o d e h a s m o r e t h a n o n e s u c c e s s o r . T h e F O R K n o d ec o n t a i n s t h e a d d r e s s o f o n e o f t h e s u c c e s s o r s . T h e o t h e r s u c c e s s o r i s p l a c e di m m e d i a t e l y a f t e r t h e F O R K F o r e x a m p l e , t h e F O R K i n t h e f o l l o w i n gi n d i c a t e s t h a t t h e n o d e L 0 0 3 h a s t w o s u c c e s s o r s

    L 0 0 3 T E Q A 0, E x p r e ss io nF O R K L 00 4T E Q A 3, +

    L 0 0 4 T E Q A 3, *

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    13/21

    T H E R E T E M A T C H A L G O R I T H M 2 9T h e o t h e r n e w n o d e t yp e , t h e M E R G E , is u s ed w h e r e t h e n e t w o r k h as t o g r owb a c k t o g e t h e r - - t h a t is , b e f o r e t w o - in p u t n o d e s . T h e t w o - i n p u t n o d e is p la c e da f t e r o n e o f it s p r e d e c e s s o r s ( sa y i ts le f t p r e d e c e s s o r ) a n d t h e M E R G E isp l a c ed a f t e r t h e o t h e r. T h e M E R G E , w h i c h co n t a in s t h e a d d re s s o f t h et w o - i n p u t n o d e , f u n c t i o n s m u c h l ik e a n u n c o n d i t i o n a l j u m p . F ig . 2 s h o w s t h ee f f e c t o f t h e l i n e a r i z a t i o n p r o c e s s ; i t c o n t a i n s t h e p r o d u c t i o n s f r o m F i g . 1 a n dt h e l i n e a r iz e d n e t w o r k f o r t h e i r L H S s .3 . 2 . 3 . epresenting the nodes in memoryT h i s s e c t i o n s h o w s h o w t h e n o d e s c o u l d b e r e p r e s e n t e d o n a c o m p u t e r w h i c hh a s a t h i r ty - t w o b i t w o r d l e n g t h . T h e t h i r t y - tw o b i t w o r d l e n g t h w a s c h o s e nb e c a u s e i t is ty p i c a l o f t o d a y s c o m p u t e r s ; t h e p r e c i s e w o r d l e n g th is n o tc r it ic a l, h o w e v e r . S i n c e t h e n e t w o r k c a n b e r o o t e d a t a F O R K ( s ee th e e x a m p l ei n F ig . 2 ) i t i s n o t n e c e s s a r y t o h a v e a n e x p l i c it r o o t n o d e f o r t h e n e t w o r k .H e n c e o n l y s e v e n c la ss es o f n o d e s a re n e e d e d ; F O R K s , M E R G E s , t h e tw ok i n d s o f o n e - i n p u t n o d e s , t h e t w o k i n d s o f tw o - i n p u t n o d e s , a n d t h e t e r m i n a ln o d e s .

    F O R K s a n d M E R G E s c o u l d b e r e p r e s e n t e d a s si ng le w o rd s . S ix b it s c o u ldb e u s e d f o r a t y p e f i e l d ( t h a t i s , a f i e l d t o i n d i c a t e w h a t t h e w o r d r e p r e s e n t s )a n d t h e r e m a i n i n g t w e n t y - s ix b it s c o u ld b e u s e d f o r t h e a d d r e s s o f t h e n o d ep o i n t ed to . F O R K s a n d M E R G E s w o u l d t hu s b e r ep r e s en t e d :

    I TYPE [ ADDRESS II_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_1_16 b i t s ) 2 6 b i t s )

    B o t h k i n d s o f o n e - i n p u t n o d e s c o u l d b e r e p r e s e n t e d a s s in g le w o r d s t h a t a r ed i v i d e d i n t o t h r e e f i e l d s . T h e f i r s t f i e l d w o u l d h o l d t h e t y p e o f t h e n o d e . T h es e c o n d f ie ld w o u l d h o l d t h e i n d e x o f t h e v a l u e t o t es t. T h e t h i r d f ie ld w o u l dh o l d e i t h e r a c o n s t a n t o r a s e c o n d i n d e x . T h e b i t s in a w o r d c o u l d b e a l l o c a t e da s f o l l o w s .

    I TYPE I INDEX I CONSTANT r INDEX Iil _ l _ l _ _ _ _ l _ l _ l _ l _ l _ l_ l _ l _ l _ l _ i _ l_ l _ i _ l _ l _ l _ l_ l _ l _ l _ l _ l _ l_ i _ i _ l6 bit s) 10 bit s) 16 bit s)

    A s i x t e e n - b i t f i el d is r e q u i r e d t o r e p r e s e n t a n i n t e g e r o r a n a t o m u s i n g t h ef o r m a t o f S e c t i o n 3 . 1 . S i n c e a f l o a t i n g p o i n t n u m b e r c a n n o t b e r e p r e s e n t e d i ns i x t e e n b i ts , in n o d e s t h a t t e s t f l o a ti n g p o i n t n u m b e r s , t h i s f ie l d w o u l d h o l d n o tt h e n u m b e r , b u t t h e a d d r e ss o f t h e n u m b e r .

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    14/21

    30 C .L . F O R G Y( P P l u s 0 x

    ( G o a l 1' T y p e S i m p l i f y( E x p r e s s i o n ]' N a m e ( N )_ _ ~ . .)

    ( P T i m e 0 x( G o a l 1 T y p e S i m p l i f y( E x p r e s s i o n ~ N a m e ( N )

    T O b j e c t ( N ) )~ ' A r gl 0 q ' O p +

    1' O b j e c t ( N ) )q 'A r g l 0 l ' O p *

    1' A r g 2 ( X ) )

    ~' Arg 2 (X) )

    R O O T F O R K L 00 3T E Q A 0 , G o a lT E Q A 1, S i m p l if yF O R K L 00 2

    LO01 AND 2) = 1)T E R M P lu s0 xL 0 0 2 A N D ( 2 ) = ( 1 )

    T E R M T im e 0 xL 0 03 T E Q A 0 , E x p r e s s i o n

    T E Q N 2 , 0F o r k L 0 0 4T E Q A 3, +M E R G E L 0 0 1

    L 0 0 4 T E Q A 3 , *M E R G E L 00 2

    ; R o o t n o d e o f th e n e t w o r k; I s t h e e l e m e n t c l a s s G o a l ?; I s t h e T y p e S i m p l i f y ?; T w o - i n p u t n o d e f o r P l u s 0 x; R e p o r t P l u s 0 x i s sa t i s f ie d; T w o - i n p u t n o d e f o r T i m e 0 x; R e p o r t T i m e 0 x i s sa t is f ie d; I s t h e e l e m e n t c l as s E x p r e s s i o n ?; I s t h e A r g l 0 ?; I s t h e O p + ?; I s t h e O p * ?

    F IG . 2 . A c o m p i l e d n e t w o r k .

    T h e t e r m i n a l n o d e s c o u l d a l s o b e s t o r e d i n s i n g l e w o r d s . T h e s e n o d e s c o n t a i nt w o f i e ld s t h e u s u a l t y p e f i e l d p l u s a l o n g e r f i e l d f o r t h e i n d e x o r a d d r e s s o f t h ep r o d u c t i o n t h a t t h e n o d e r e p r e s e n t s .

    I I II TYPE [ PRODUCTION [I I II_1_1_1_1_1_1 _1_1_1_1_1_ 1_1_I_]_1_ I_1_1_1_1_1_ I_1_1_1_1_1_1_1_1_16 bit s) 26 bit s)T h e l e n g th o f a t w o - i n p u t n o d e w o u l d d e p e n d o n t h e n u m b e r o f v a l u e pa i rs

    t e s t e d b y t h e n o d e . E a c h n o d e c o u l d h a v e o n e w o r d o f b as ic i n f o r m a t i o n p l u so n e w o r d f o r e a c h v a l u e p a i r . T h e f ir st w o r d w o u l d c o n t a i n a t y p e fi el d ap o i n t e r t o th e m e m o r y f o r th e l e ft i n p u t a p o i n t e r t o th e m e m o r y f o r th e r ig h ti n p ut a n d a fi el d i n d ic a ti n g h o w m a n y t e s ts a r e p e r f o r m e d b y t h e n o d e . T h eb i t s i n t h e w o r d c o u l d b e a l l o c a t e d a s fo l l o w s .

    I I I I I] TYPE I COUNT I MEMORY POINTER I MEMORY POINTER II _ I _ I _ I _ I_ I _ I _ I _ I _ I _ I _ I _ I_ I _ I _ I _ I _ I _ I _ I_ I _ I _ I _ I _ I _ I _ I _ I_ I _ I _ I _ I I6 bit s) 4 bit s) 11 bit s) 11 bi ts )

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    15/21

    T H E R E T E M A T C H A L G O R I T H M 3

    T h e w o r d f o r e a c h t e s t w o u l d c o n t a i n t h r e e f ie ld s. T w o f i el ds w o u l d h o l d t h ei n d ic e s o f t h e t w o e l e m e n t s t o t e st . T h e r e m a i n i n g f ie ld w o u l d i n d i c a te t h e t e stt o p e r f o r m ; t h a t i s, it w o u l d i n d i c a t e w h e t h e r t h e n o d e is t o t e st f o r e q u a l it y o ft h e t w o e l e m e n t s , f o r i n e q u a l i t y , o r f o r s o m e t h i n g e l se . T h e b i ts in t h e W o r dm i g h t b e a l l o c a t e d a s f o l l o w s .

    I I I ITEST [ INDEX INDEX [_ l _ _ _ _ l _ _ l _ _ _ l _ l _ _ _ l _ _ _ _ _ l _ _ _ l _ l _ l _ _ _ _ _ _ _ _6 bit s) 13 bi ts ) 13 bi ts)

    N o t e t h a t t h e i n d e x f ie l ds h e r e a r e l o n g e r t h a n t h e i n d e x f ie l d s i n t h e o n e - i n p u tn o d e s . T h i s i s n e c e s s a r y b e c a u s e t h e i n d i c e s i n t h e t w o - i n p u t n o d e s m u s td e s i g n a t e e l e m e n t s i n t h e t o k e n s a s w e l l a s v a l u e s i n t h e e l e m e n t s .3 . 3 . T h e t o k e n sT h i s se c t i o n d e s c r i b e s a s p a c e - e f fi c i e n t r e p r e s e n t a t i o n f o r t o k e n s . T h i sr e p r e s e n t a t i o n i s n o t s u i t a b l e f o r a l l i n t e r p r e t e r s ; i t r e q u i r e s t h e i n t e r p r e t e r t op r o c e s s o n l y o n e w o r k i n g m e m o r y c h a n g e a t a t im e , a n d i t r e q u i r e s t h a t c e r ta i np a r t s o f t h e n e t w o r k b e t r a v e r s e d d e p t h f ir st . F o r t u n a t e l y , t h e s e a r e n o t s e r i o u sr e s tr i ct io n s . T h e s im p l e s t w a y t o p e r f o r m t h e m a t c h is p r o c e s s o n e t o k e n a t at i m e , t r a v e r s i n g t h e e n t i r e n e t w o r k d e p t h f i r s t . S e c t i o n 4 d e s c r i b e s a n i n t e r -p r e t e r t h a t o p e r a t e s i n t h i s m a n n e r .

    I f t h e i n t e r p r e t e r o p e r a t e s t h is w a y , t h e n i t c a n u s e a s ta c k t o r e p r e s e n t i tst o k e n s . W h e n a t o k e n h a s t o b e b u i lt , f ir st t h e t a g f o r t h e t o k e n is p u s h e d o n t ot h e s t a c k , a n d t h e n t h e w o r k i n g m e m o r y e l e m e n t s a r e p u s h e d o n t o t h e s t a c k i no r d e r . W h e n t o k e n s h a v e to b e e x te n d e d a v e r y c o m m o n o p e r a t i o n - - s e e t h ec o d e in S e c t io n 4 ) t h e a d d i t i o n a l w o r k i n g m e m o r y e l e m e n t s a r e ju s t p u s h e do n t o t h e s t a c k .

    T h e o n e - i n p u t n o d e s w i ll b e m o r e e f fi c ie n t i f t h e y d o n o t u s e t h is s ta c k . S i n c ea l l t h e o n e - i n p u t n o d e s w i l l p r o c e s s t h e s a m e w o r k i n g m e m o r y e l e m e n t - - t h ee l e m e n t th a t w a s j u st a d d e d t o o r d e le t e d f ro m w o r k i n g m e m o r y - - t h e e l e m e n ts h o u l d b e m a d e e a s i l y a v a i l a b l e . T h e e l e m e n t c o u l d b e c o p i e d i n t o a d e d i c a t e dl o c a t io n i n m e m o r y , o r t h e a d d r e s s o f t h e e l e m e n t c o u l d b e l o a d e d i n t o ad e d i c a t e d b a s e r e g is t er . E i t h e r o f t h e s e w o u l d m a k e it p o ss i bl e f o r t h eo n e - i n p u t n o d e s t o a c c e s s t h e e l e m e n t w i t h o u t g o i n g t h r o u g h t h e s t a c k .3 . 4 . T h e i n t e r p r e t e r s s t a t eI n a d d i t io n t o t h e s ta c k f o r t o k e n s , t h e i n t e r p r e t e r m u s t m a i n t a i n a n o t h e r s ta c kf o r i t s s t a te i n f o r m a t i o n . O n e r e a s o n f o r t h e s t ac k i s t o a l l o w t h e i n t e r p r e t e r t of in d i ts w a y a b o u t i n th e n e t w o r k . W h e n t h e i n t e r p r e t e r p a s s e s a F O R K , i tp u s h e s t h e p o i n t e r i t d o e s n o t f o l l o w o n t o t h e s ta c k . T h e n w h e n i t r e a c h e s t h e

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    16/21

    32 C L FORGY

    e n d o f a p a th i t p o p s a p o i n t e r f r o m t h e s t ac k a n d f o l lo w s it . A n o t h e r r e a s o nf o r t h e s t a c k i s t o p r o v i d e a p l a c e f o r t h e t w o - i n p u t n o d e s t o k e e p t h e i r l o c a li n f o r m a t i o n . A s w ill b e s e e n in t h e n e x t s e c t io n , t h e t w o - i n p u t n o d e s s o m e t i m e sh a v e t o s u s p e n d t h e m s e l v e s w h i l e t h e i r s u c c e s s o r s a r e p r o c e s s e d . T h e s t a c kh o l d s t h e i n f o r m a t i o n t h a t i s n e e d e d t o r e s u m e p r o c e s s i n g th e t w o - i n p u t n o d e s .

    4 T h e N e t w o r k I n t er p r e te rT h i s s e c t io n p r o v i d e s a c o n c r e t e d e s c r i p t io n o f t h e o p e r a t i o n s p e r f o r m e d b y t h en e t w o r k i n t e r p r e t e r . O n e n o d e f r o m e a c h c l a s s h a s b e e n s e l e c t e d , a n d t h e c o d et o i n t e r p r e t t h e n o d e s h a s b e e n w r i t t e n . I t m i g h t b e n o t e d t h a t s i n c e t h e c o d es e q u e n c e s a r e s h o r t a n d s i m p l e , t h e y c o u l d e a s il y b e w r i t t e n in m i c r o c o d e .

    T h e c o d e i s w r i t t e n i n a P A SC A L -lik e l a n g u a g e w h i c h h a s l i t e r a l la b e l s a n df i e l d e x t r a c t i o n . F i e l d e x t r a c t i o n i s i n d i c a t e d b y p u t t i n g t w o n u m b e r s w i t h i na n g l e b r a c k e t s ; t h e f ir st n u m b e r i s t h e i n d e x o f t h e h i g h o r d e r b i t in t h e f i el d ,a n d t h e s e c o n d n u m b e r is t h e i n d e x o f th e l o w o r d e r b it . T h e a s s u m p t i o n w i llb e m a d e t h a t t h e b i ts a r e n u m b e r e d f r o m r i g h t t o l e ft , w i t h t h e l o w o r d e r b i tb e i n g b i t z e r o . T h u s t h e e x p r e s s i o n S E L F ( 3 1 : 2 6 ) i n d i c a t e s t h a t t h e h i g h o r d e rs ix b it s o f t h e v a l u e o f t h e v a r i a b l e S E L F a r e t o b e e x t r a c t e d a n d r i g h t j u s ti f ie d .

    T h e m a i n l o o p o f t h e i n t e r p r e t e r is v e r y s im p l e : t h e i n t e r p r e t e r f e t c h e s t h en e x t n o d e f r o m m e m o r y a n d d i s p a t c h e s o n i t s t y p e f i e l d . L e t t h e s e g m e n t o fm e m o r y th a t h ol ds th e n o d e s b e ca ll e d N O D E _ M E M O R Y a n d le t t h e p o i n te rt o t h e c u r r e n t n o d e b e c a l le d N C . T h e m a i n l o o p i s t h e n :

    M A I N : S E L F :--- N O D E _ M E M O R Y [ N C ] ;C A S E S E L F ( 3 1 : 2 6 ) O F T y p e f ie ld is h i gh o r d e r 6 b i ts

    0 : G O T O F O R K ;1: G O T O M E R G E ;2: G O T O T E R M ;3: G O T O T E Q A ;E N D ;

    T h e n o d e i s c o p i e d i n t o t h e v a r i a b le S E L F s o t h a t t h e n o d e p r o g r a m s c a ne x a m i n e i t. T h e a s s ig n m e n t o f n u m b e r s t o t h e v a r io u s n o d e t y p e s is a rb i t ra r y .G o t o ' s a r e u s e d i n s t e a d o f p r o c e d u r e c a ll s b e c a u s e t h e s e e x a m p l e s m a k e a ll t h es t a t e o f t h e i n t e r p r e t e r e x p l i c i t, a n d n o t h i d d e n i n P A SC A L'S s t a ck .

    T E Q A is ty p i c a l o f t h e o n e - i n p u t n o d e s f o r te s t in g c o n s t a n t s. I f th e s e g m e n to f m e m o r y t h a t h o ld s t h e w o r k i n g m e m o r y e l e m e n t b e i n g p r o c e s s e d is c a ll edC U R R E N T , t h e n T E Q A is a s f ol lo w s.T E Q A : T E M P :-- C U R R E N T [ S E L F ( 2 5 : 1 6 ) ] : G e t t he w o r d p o i n t ed t o

    b y t h e i n d e x f ie l d

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    17/21

    T H E R E T E M A T C H A L G O R IT H M 3 3I F ( T E M P ( 3 1 : 1 6) = 0 ) A N D

    ( T E M P ( 1 5 : 0 ) = S E L F ( 1 5 : 0 ) )T H E N G O T O S U C CE L S E G O T O F A I L :

    T e s t t y p e b i t sT e s t v a l u e

    E i t h e r S U C C o r F A I L i s e x e c u t e d a f te r e a c h o n e - in p u t n o d e . S U C C ise x e c u t e d w h e n t h e t e s t su c c e e d s , a n d F A I L is e x e c u t e d w h e n t h e t e st fa ils .S U C C i n c r e m e n t s t h e n o d e c o u n t e r t o p o i n t to t h e n e x t n o d e .

    S U C C : N C : = N C + 1;G O T O M A I N ;

    F A I L t ri e s t o g e t a n o d e f r o m t h e s t a c k o f u n p r o c e s s e d n o d e s ; i f i t c a n n o t , i th a l t s t h e m a t c h . A s s u m i n g t h e s t a c k i s n a m e d N S a n d t h e p o i n t e r t o t h e t o p o ft h e s t a c k is c a l le d N S T O P , t h e c o d e i s:

    F A I L : I F N S T O P < 0 T H E N G O T O E X I T _ M A T C H ;N C : = N S [ N S T O P ] ;N S T O P := N S T O P - 1;G O T O M A I N ;

    T h e o n e - i n p u t n o d e s f o r c o m p a r i n g p a i rs o f v a l u e s a r e s im i l ar t o t h e o t h e ro n e - i n p u t n o d e s . T E Q S is t y p ic a l o f t h e s e n o d e s .

    T E Q S : I F C U R R E N T [ S E L F ( 2 5 : 16)] = C U R R E N T [ S E L F ( 9 : 0 ) ]T H E N G O T O S U C CE L S E G O T O F A I L ;a d d r e s s o n t o N S a n d t h e n p a s s e s c o n t r o l t o t h e f o l l o w i n gO R K p u s h e s a n

    n o d e .F O R K : N S T O P : = N S T O P + 1;

    N S [ N S T O P ] : = S E L F ( 2 5 : 0 ) ;G O T O S U C C ;

    A t w o - i n p u t n o d e m u s t b e a b l e to d e t e r m i n e w h e t h e r i t w a s r e a c h e d o v e r it sl e f t i n p u t o r i t s r i g h t i n p u t . T h i s c a n b e i n d i c a t e d t o t h e n o d e b y a g l o b a lv a r i a b l e w h i c h u s u a l l y h a s t h e v a l u e L E F T , b u t w h i c h i s t e m p o r a r i l y s e t t oR I G H T w h e n a M E R G E p a ss e s c o n t r o l t o a t w o - i n p u t n o d e . I f t hi s g lo b a lv a ri ab le is c a l le d D I R E C T I O N , t h e co d e f o r t h e M E R G E is

    M E R G E : D I R E C T I O N : = R I G H T ;N C : = S E L F ( 2 5 : 0 ) ;G O T O M A I N ;

    T h e t w o k i n d s o f tw o - i n p u t n o d e s a r e v e r y si m il ar , so o n l y A N D i s s h o w nh e r e . I n o r d e r n o t t o o b s c u r e t h e m o r e i m p o r t a n t i n f o r m a t i o n , s o m e d e t a i l s o f

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    18/21

    34 C L FORGY

    t h e p r o g r a m a r e o m i t t e d . T h e c o d e d o e s n o t s h o w h o w t h e v a r ia b l e s a re t e s te d ,n o r d o e s i t s h o w h o w t o k e n s a r e a d d e d t o a n d r e m o v e d f r o m t h e n o d e ' sm e m o r i e s . A s s u m i n g t h e t o k e n s ta c k is c a l le d T S a n d t h e p o i n t e r t o t h e to pe l e m e n t i s c a l l e d T S T O P , t h e p r o g r a m i s a s f o l l o w s .

    i C o n t r o l c a n r e a c h th i s p o i n t m a n y t i m e s d u r in g t h e p r o c e s s in go f a t o k e n . T h e n o d e n e e d s t o u p d a t e i ts s t a te a n d p u ti n f o r m a t i o n o n N S o n l y o n c e , h o w e v e r .

    A N D : I F N S [ N S T O P ] < > N CT H E N

    B E G I NN S T O P :- - N S T O P + 4 ;N S [ N S T O P ] : = N C ;N S [ N S T O P - 1] :- - D I R E C T I O N ;N S [ N S T O P - 2] : = M E M O R Y C O N T E N T S

    ( O P P O S I T E ( D I R E C I ' I O N ) ) ;N S [ N S T O P - 3 ] : = T S T O P ;M O D I F Y _ M E M O R Y ( D I R E C T I O N ) ; S to re t h e t o k e nD I R E C T I O N : = L E F T ; R e s e t t o t h e d e fa u ltE N D ;

    G o p r o c e s s t h e t o k e n s

    L L O O P :

    I F N S [ N S T O P ] - 1] = R I G H T T H E N G O T O R L O O PE L S E G O T O L L O O P ;

    C o m p a r e t h e t o k e n t o t h e e l e m e n t s i n t h e r i g h t m e m o r y

    I f t h e s t a t e i s n o t i n N ST h e n p u t i t t h e r e

    F a ll o u t o f th e l o o p w h e n t h e t e s t s u c c e e d s s o th a tt h e s u c c e s s o r s o f t h i s n o d e c a n b e a c t i v a t e d

    R E P E A TT E M P : = N E X T _ P O S I T I O N ( N S [ N S T O P - 2]) ;I F T E M P = N I L I f r ig h t m e m o r y is e m p t yT H E N T h e n c le an u p a n d e x it

    B E G I NT S T O P : = N S [ N S T O P - 3 ];N S T O P : = N S T O P - 4 ;G O T O F A I L ;E N DU N T I L P E R F O R M A N D _ T E S T ( T E M P , L E F T ) ;

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    19/21

    T H E R E T E M A T C H A L G O R r l' /- IM 3 5E x t e n d t h e t o k e n

    T S T O P : = N S [ N S T O P - 3 ] + 1 ; T S [ N S T O P ] : = T E M P ;P r e p a r e N S s o th a t c o n t r o l w il l r e t u r n t o t hi s n o d e

    N S T O P : = N S T O P + 1; N S [ N S T O P ] : = N C ;P a s s c o n t r o l t o t h e s u c c e s s o r s o f th i s n o d eN C : = N C + S E L F ( 2 5 : 2 2) + 1 ; G O T O M A I N ;

    C o m p a r e t h e t o k e n t h e e l e m e n t s i n t h e le ft m e m o r yR L O O P :

    T h i s is si m il a r to L L O O P .T h e o n l y r e m a i n i n g n o d e t y p e i s t h e T E R M n o d e . S i n c e u p d a t i n g t h e c o nf l i c ts et is a l a n g u a g e - d e p e n d e n t o p e r a t i o n , t h a t d et a i l o f t h e T E R M n o d e c a n n o t

    b e s h o w n . T h e r e s t o f th e p r o c e s s i n g o f t h e n o d e is a s fo l lo w s .T E R M : U P D A T E _ C O N F L I C T _ S E T ( S E L F < 2 5 : 0 ) ) ;

    G O T O F A I L ;5 P e r f o r m a n c e o f t h e A l g o r i t h m

    E x t e n s i v e s t u d i e s h a v e b e e n m a d e o f t h e e ff ic i en c y o f t h e R e t e M a t c h A l -g o r i t h m . B o t h a n a l y t i ca l s t u d ie s ( w h i c h d e t e r m i n e d t h e t im e a n d s p a c e c o m -p l e x it y o f t h e a l g o r i t h m ) a n d e m p i r i c a l s tu d i e s h a v e b e e n m a d e . T h i s s e c t i o np r e s e n t s s o m e o f t h e r e s u l t s o f t h e a n a l y t i c a l st u d i e s. B e c a u s e o f s p a c ec o n s t r a i n t s , i t w a s n o t p o s s i b l e t o p r e s e n t t h e e m p i r i c a l r e s u l t s o r t h e p r o o f s o f

    T A B L E 1. S p a c e a n d t i m e c o m p l e x i t yCom plexity measure Best case Worst caseEffect of working m emorysize on num ber of tokens O(1) O (W cEffect of production m em orysize on num ber of nodes O (P) O( P)Effect of production memorysize on num ber of tokens O(1) O(P )Effect of working m emorysize on time for one firing O(1) O (W 2c-1)Effect of production memorysize on time for one firing O(log2 P) O (P )

    C is the number of patterns in a production.P is the number of productions in production memory.W is the number of elements in working memory.

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    20/21

    36 C.L. FORGYt h e a n a l y t i c a l r e s u l t s . T h e p r o o f s a n d d e t a i l e d r e s u l t s o f s o m e e m p i r i c a l s t u d i e sc a n b e f o u n d i n [ 5 ] .

    T a b l e 1 s u m m a r i z e s t h e r e s u l t s o f t h e a n a l y t i c a l s t u d i e s o f t h e a l g o r i t h m . T h eu s u a l n o t a t i o n f o r a s y m p t o t i c c o m p l e x i t y is u s e d i n t h is t a b l e [ 1 ] .W r i t in g t h a t a c o s tis O f x ) ) i n d i c a t e s t h a t t h e c o s t v a r ie s a s f x ) p l u s p e r h a p s s o m e s m a l l e r t e r m s i nx , T h e s m a l l e r te r m s a r e i g n o r e d b e c a u s e t h e f x ) t e r m w i ll d o m i n a t e w h e n x isl a rg e . W r i t in g t h a t a c o s t is O 1 ) i n d i c a t e s t h a t t h e c o s t is u n a f f e c t e d b y t h e f a c t o rb e i n g c o n s i d e r e d . I t s h o u l d b e n o t e d t h a t a ll t h e c o m p l e x i t y r e s u l ts in T a b l e 1s h a r p ; p r o d u c t i o n s y s t e m s a c h i e v i n g t h e b o u n d s a r e d e s c r i b e d i n [5 ].

    6 C o n c l u s i o n sT h e R e t e M a t c h A l g o r i t h m i s a m e t h o d f o r c o m p a r i n g a se t o f p a t t e r n s t o a se to f o b j e c t s in o r d e r t o d e t e r m i n e a l l t h e p o s s i b l e m a t c h e s . I t w a s d e s c r i b e d i nd e t a i l i n t h i s a r t i c l e b e c a u s e e n o u g h e v i d e n c e h a s b e e n a c c u m u l a t e d s i n c e i t sd e v e l o p m e n t i n 1 9 7 4 t o m a k e i t c l e a r t h a t i t is a n e f f i c ie n t a l g o r i t h m w h i c h h a sm a n y p o s s i b l e a p p l i c a t i o n s .

    T h e a l g o r i t h m is e f fi c ie n t e v e n w h e n i t p r o c e s s e s l a r g e s e ts o f p a t t e r n s a n do b j e c t s , b e c a u s e i t d o e s n o t i t e r a t e o v e r th e s e t s. I n th i s a lg o r i t h m , t h e p a t t e r n sa r e c o m p i l e d in t o a p ro g r a m t o p e r f o r m t h e m a t c h p r o c e ss . T h e p r o g r a m d o e sn o t h a v e t o i t e r a t e o v e r t h e p a t t e r n s b e c a u s e i t c o n t a i n s a t r e e - s tr u c t u r e ds o r ti n g n e t w o r k o r i n d e x f o r th e p a t t e r n s . I t d o e s n o t h a v e t o i t e r a t e o v e r t h ed a t a b e c a u s e i t m a i n t a i n s s t a t e i n f o r m a t i o n : t h e p r o g r a m c o m p u t e s t h e m a t c h e sa n d p a r t i a l m a t c h e s f o r e a c h o b j e c t w h e n i t e n t e r s t h e d a t a m e m o r y , a n d i ts t o r e s t h e i n f o r m a t i o n a s l o n g a s t h e o b j e c t r e m a i n s i n t h e m e m o r y .

    A l t h o u g h t h e R e t e a l g o ri th m w a s d e v e l o p e d f o r u se in p r o d u c t i o n s y s te mi n t e r p r e t e r s , i t c a n b e u s e d f o r o t h e r p u r p o s e s a s w e l l . I f t h e r e i s a n y t h i n gu n u s u a l a b o u t t h e p a t t e r n m a t c h i n g o f p r o d u c t i o n s y s te m s , it is o n ly t h a t t h ep a t t e r n m a t c h i n g t a k e s p l a c e o n a n u n u s u a l l y la r g e s c a le . P r o d u c t i o n s y s t e m sc o n t a i n r a t h e r o r d i n a r y p a t t e r n s a n d d a t a o b j e c t s , b u t t h e y c o n t a i n l a r g en u m b e r s o f th e m , a n d i n v o c a t io n s o f th e p a t t e r n m a t c h e r o c c u r v e r y f r e q u e n t lyd u r i n g e x e c u t i o n . If p r o g r a m s o f o t h e r k i n d s b e g i n t o u s e p a t t e r n m a t c h i n gm o r e h e a v i ly , t h e y c o u l d h a v e t h e s a m e e f fi c ie n c y p r o b l e m s a s p r o d u c t i o ns y s te m s , a n d i t c o u l d b e n e c e s s a r y t o u se m e t h o d s l i k e t h e R e t e M a t c hA l g o r i t h m in t h e i r i n t e r p r e t e r s a s w e ll . C e r t a in l y t h e a l g o r i th m s h o u l d n o t b eu s e d f o r a ll m a t c h p r o b l e m s ; i t s u s e i s i n d i c a t e d o n l y i f t h e f o l l o w i n g t h r e ec o n d i t i o n s a r e s a t i s f i e d .- T h e p a t t e r n s m u s t b e c o m p i l a b l e . I t m u s t b e p o s s i b l e t o e x a m i n e t h e m a n d

    d e t e r m i n e a l is t o f f e a t u r e s l i k e t h e l i st s i n S e c t i o n 2 . 2 .1 .- T h e o b j e c t s m u s t b e c o n s t a n t. T h e y c a n n o t c o n t a i n v a ri a b le s o r o t h e r

    n o n - c o n s t a n t s a s p a t t e r n s c a n .- T h e s e t o f o b j e c t s m u s t c h a n g e r e l a t i v e l y s l ow l y . S i n c e t h e a l g o r i t h m m a i n t a i n ss t a t e b e t w e e n c y c le s , i t i s i n e f fi c ie n t i n s i tu a t i o n s w h e r e m o s t o f t h e d a t a c h a n g e so n e a c h c y c l e .

  • 8/12/2019 1.1.Rete - A Fast Algorithm for the Many Pattern, Many Object Pattern Match Problem

    21/21

    T H E R E T E M A T C H A L G O R I T H M 7

    A C K N O W L E D G M E N TThe a u t ho r wou l d l i ke t o t ha nk Al l e n Ne we l l a nd R obe r t S p rou l l fo r ma ny use fu l d i s c us s i onsc onc e rn i ng t h is work , a nd Al l e n Ne w e l l , John M c De rm ot t , a nd M i c ha e l R yc he ne r fo r t he i r va l ua b l ec om me n t s on e a r l i e r ve r s i ons o f t h is a r t ic l e .

    R E F E R E N C E S1 . A ho , A .V. , Hopc ro f t , J .E . , a nd UUm a n , J .D . , T he Design and Analysis o f Computer Algorithms

    (Add i son -W e s l e y , R e a d i ng , MA, 1974) .2 . Coh en, B .L. , A p ow erful and e ff ic ient s t ruc tura l pa t te rn recogn i t ion sys tem, Artificial Intelligence 9 (1977) 223-255.3 . F o rgy , C .L . , A ne t work ma t c h rou t i ne fo r p roduc t i on sys t e ms , W ork i ng P a pe r , 1974 .4 . F o rgy , C .L . , A p roduc t i on sys t e m mon i t o r fo r pa ra l l e l c ompu t e r s , De pa r t me n t o f C ompu t e r

    Sc ience , Carnegie -Mel lon Univers i ty , 1977.5 . F o rgy , C .L . , On t he e f f ic i en t i mp l e m e n t a t i on o f p roduc t i on sys te ms , P h .D. The s i s, C a rne g i e -Mel lon Univers i ty , 1979.6 . F o rgy , C .L . , o~ 5 use r s ma nua l , De p a r t me n t o f C om pu t e r S c i e nc e, C a rne g i e -Me l l on

    Univers i ty , 1981.7 . Ha ye s -R o t h , F . a nd Mos t ow, D . J . , An a u t oma t i c a l l y c ompi l a b l e r e c ogn i t i on ne t work fo rs t ruc tured pa t te rns , Proc . Fourth Internat. Joint Conference on Artificial Intelligence (1975)246-251.8 . Mc C ra c ke n , D . , A p roduc t i on sys t e m ve r s i on o f t he He a r sa y - I I spe e c h unde r s t a nd i ng sys t e m,P h .D. The s i s , C a rne g i e -M e l l on Un i ve r s i t y , 1978 .9 . M c D e rm ot t , J . , Ne w e l i , A . , a nd M oore , J . , Th e e f f ic i enc y o f c e r ta i n p rodu c t i on sys t e m

    i mpl e me n t a t i ons , i n : W a t e rma n , D .A. a nd Ha ye s -R o t h , F . (Ee l s . ) , Pattern Directed InferenceSystems (Ac a de m i c P re ss , N e w York , 1978) 155-176 .10. R yc he ne r , M.D . , P rodu c t i on sys te ms a s a p rog ra m mi ng l a ngua ge fo r Ar t i f i ci a l In t e l l ige nc eappl ica t ions . Ph .D. Thes is , Carnegie -Mel lon Univers i ty , 1976.

    R e c e i v e d M a y 1 9 80 ; r e v i s e d v e r si o n r e c ei v e d A p r i l 1 9 8 1