Dieser Inhalt ist in der von Ihnen ausgewählten Sprache nicht verfügbar.
Chapter 24. Fibonacci Example
24.1. Fibonacci Example: The Class Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
- 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 Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
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):
24.3. Fibonacci Example: Execution Details Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
ksession.insert( new Fibonacci( 50 ) ); ksession.fireAllRules();
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
Fibonacci
objects. - This example uses the MVEL dialect. This means you can use the
modify
keyword, which allows a block setter action which also notifies the engine of changes.
24.4. Fibonacci Example: Recurse Rule Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
- The Recurse rule matches each asserted
Fibonacci
object with a value of -1, creating and asserting a newFibonacci
object 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
not
conditional 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
Fibonacci
objects are asserted before the Bootstrap rule is executed. - You can switch to the Audit view to show the original assertion of the
Fibonacci
object 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 assertedFibonacci
object causes the Recurse rule to become activated and to fire again.
24.5. Fibonacci Example: Bootstrap Rule Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
- When a
Fibonacci
object 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
Fibonacci
object 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 thenot
conditional element stops the rule's matching as soon as aFibonacci
object with a sequence of 1 exists.
24.6. Fibonacci Example: Calculate Rule Link kopierenLink in die Zwischenablage kopiert!
Link kopierenLink in die Zwischenablage kopiert!
- When there are two
Fibonacci
objects 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 thatf1
references sequence 1 andf2
references sequence 2. The final pattern finds the Fibonacci with a value equal to -1 and with a sequence one greater thanf2
. - There are three
Fibonacci
objects correctly selected from the available cross products. You can calculate the value for the thirdFibonacci
object that's bound tof3
. - The
modify
statement updates the value of theFibonacci
object 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
Fibonacci
object, 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 allFibonacci
objects.