Show Table of Contents
Chapter 24. Fibonacci Example
24.1. Fibonacci Example: The Class
public static class Fibonacci {
private int sequence;
private long value;
public Fibonacci( final int sequence ) {
this.sequence = sequence;
this.value = -1;
}
... setters and getters go here...
}- The sequence field is used to indicate the position of the object in the Fibonacci number sequence.
- The value field shows the value of that Fibonacci object for that sequence position, using -1 to indicate a value that still needs to be computed.
24.2. Fibonacci Example: Execution
Procedure 24.1. Task
- Launch the Eclipse IED.
- Open the class
org.drools.examples.fibonacci.FibonacciExample. - Right-click the class and select
Run as...and thenJava application.
Result
Eclipse shows the following output in its console window (with "...snip..." indicating lines that were removed to save space):
recurse for 50 recurse for 49 recurse for 48 recurse for 47 ...snip... recurse for 5 recurse for 4 recurse for 3 recurse for 2 1 == 1 2 == 1 3 == 2 4 == 3 5 == 5 6 == 8 ...snip... 47 == 2971215073 48 == 4807526976 49 == 7778742049 50 == 12586269025
24.3. Fibonacci Example: Execution Details
ksession.insert( new Fibonacci( 50 ) ); ksession.fireAllRules();
- To use this with Java, a single Fibonacci object is inserted with a sequence field of 50.
- A recursive rule is used to insert the other 49
Fibonacciobjects. - This example uses the MVEL dialect. This means you can use the
modifykeyword, which allows a block setter action which also notifies the engine of changes.
24.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.
24.5. Fibonacci Example: Bootstrap Rule
rule Bootstrap
when
f : Fibonacci( sequence == 1 || == 2, value == -1 ) // multi-restriction
then
modify ( f ){ value = 1 };
System.out.println( f.sequence + " == " + f.value );
end- When a
Fibonacciobject with a sequence field of 2 is asserted the Bootstrap rule is matched and activated along with the Recurse rule. - Note the multi-restriction on field
sequence, testing for equality with 1 or 2. - When a
Fibonacciobject with a sequence of 1 is asserted the Bootstrap rule is matched again, causing two activations for this rule. The Recurse rule does not match and activate because thenotconditional element stops the rule's matching as soon as aFibonacciobject with a sequence of 1 exists.
24.6. Fibonacci Example: Calculate Rule
rule Calculate
when
// Bind f1 and s1
f1 : Fibonacci( s1 : sequence, value != -1 )
// Bind f2 and v2; refer to bound variable s1
f2 : Fibonacci( sequence == (s1 + 1), v2 : value != -1 )
// Bind f3 and s3; alternative reference of f2.sequence
f3 : Fibonacci( s3 : sequence == (f2.sequence + 1 ), value == -1 )
then
// Note the various referencing techniques.
modify ( f3 ) { value = f1.value + v2 };
System.out.println( s3 + " == " + f3.value );
end
- When there are two
Fibonacciobjects with values not equal to -1, the Calculate rule is able to match them. - There are 50 Fibonacci objects in the Working Memory. A suitable triple should be selected to calculate each of value in turn.
- Using three Fibonacci patterns in a rule without field constraints to confine the possible cross products would result in many incorrect rule firings. The Calculate rule uses field constraints to correctly constraint the Fibonacci patterns in the correct order. This technique is called cross product matching.
- The first pattern finds any Fibonacci with a value != -1 and binds both the pattern and the field. The second Fibonacci does this too, but it adds an additional field constraint to ensure that its sequence is greater by one than the Fibonacci bound to
f1. When this rule fires for the first time, the two constraints ensure thatf1references sequence 1 andf2references sequence 2. The final pattern finds the Fibonacci with a value equal to -1 and with a sequence one greater thanf2. - There are three
Fibonacciobjects correctly selected from the available cross products. You can calculate the value for the thirdFibonacciobject that's bound tof3. - The
modifystatement updates the value of theFibonacciobject bound tof3. This means there is now another new Fibonacci object with a value not equal to -1, which allows the Calculate rule to rematch and calculate the next Fibonacci number. - Switching to the Audit view will show how the firing of the last Bootstrap modifies the
Fibonacciobject, enabling the "Calculate" rule to match. This then modifies another Fibonacci object allowing the Calculate rule to match again. This continues till the value is set for allFibonacciobjects.

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.