Este contenido no está disponible en el idioma seleccionado.
Chapter 24. Fibonacci Example
24.1. Fibonacci Example: The Class Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- 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 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
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 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
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
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 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- 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 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- 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 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- 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.