Show Table of Contents
21.4. Fibonacci Example: Recurse Rule
rule Recurse
salience 10
when
f : Fibonacci ( value == -1 )
not ( Fibonacci ( sequence == 1 ) )
then
insert( new Fibonacci( f.sequence - 1 ) );
System.out.println( "recurse for " + f.sequence );
end- The Recurse rule matches each asserted
Fibonacciobject with a value of -1, creating and asserting a newFibonacciobject with a sequence of one less than the currently matched object. - Each time a Fibonacci object is added while the one with a sequence field equal to 1 does not exist, the rule re-matches and fires again.
- The
notconditional element is used to stop the rule's matching once we have all 50 Fibonacci objects in memory. - The Recurse rule has a salience value so all 50
Fibonacciobjects are asserted before the Bootstrap rule is executed. - You can switch to the Audit view to show the original assertion of the
Fibonacciobject with a sequence field of 50, done with Java code. From there on, the Audit view shows the continual recursion of the rule, where each assertedFibonacciobject causes the Recurse rule to become activated and to fire again.

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.