Este contenido no está disponible en el idioma seleccionado.
Chapter 25. Banking Example
25.1. Banking Example: RuleRunner Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The class
RuleRunneris used to execute one or more DRL files against a set of data. It compiles the Packages and creates the Knowledge Base for each execution, allowing us to easily execute each scenario and inspect the outputs.
25.2. Banking Example: Rule in Example1.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Output:
Loading file: Example1.drl Rule 01 Works
Loading file: Example1.drl
Rule 01 Works
- This rule has a single
evalcondition that will always be true, so that this rule will match and fire after it has been started. - The output shows the rule matches and executes the single print statement.
25.3. Banking Example: Java Example 2 Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- This example asserts basic facts and prints them out.
25.4. Banking Example: Rule in Example2.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Output:
- This is a basic rule for printing out the specified numbers. It identifies any facts that are
Numberobjects and prints out the values. Notice the use of the abstract classNumber. - The pattern matching engine is able to match interfaces and superclasses of asserted objects.
- The output shows the DRL being loaded, the facts inserted and then the matched and fired rules. You can see that each inserted number is matched and fired and thus printed.
25.5. Banking Example: Example3.java Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- This is a basic rule-based sorting technique.
25.6. Banking Example: Rule in Example3.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Output:
- The first line of the rule identifies a
Numberand extracts the value. - The second line ensures that there does not exist a smaller number than the one found by the first pattern. The retraction of the number after it has been printed means that the smallest number has been removed, revealing the next smallest number, and so on.
25.7. Banking Example: Class Cashflow Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- Class
Cashflowhas two simple attributes, a date and an amount. (Using the typedoublefor monetary units is generally bad practice because floating point numbers cannot represent most numbers accurately.) - There is an overloaded constructor to set the values and a method
toStringto print a cashflow.
25.8. Banking Example: Example4.java Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The Java code in this example inserts five Cashflow objects, with varying dates and amounts.
25.9. Banking Example: Class SimpleDate Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- The convenience class
SimpleDateextendsjava.util.Date, providing a constructor taking a String as input and defining a date format.
25.10. Banking Example: Rule in Example4.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Output:
- A
Cashflowis identified and the date and amount are extracted. - In the second line of the rule in it is determined that there is no Cashflow with an earlier date than the one found.
- In the consequence, the
Cashflowis printed. This satisfies the rule and then retracts it, making way for the next earliestCashflow.
25.11. Banking Example: Class TypedCashflow Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- When the
Cashflowis extended it results in aTypedCashflow, which can be a credit or a debit operation.
25.12. Banking Example: Example5.java Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Class:
Rule:
Output:
- Both the class and the .drl are supplied to the rule engine.
- In the class, a set of
Cashflowobjects are created which are either credit or debit operations. - A
Cashflowfact is identified with a type ofCREDITand extract the date and the amount. In the second line of the rule we ensure that there is noCashflowof the same type with an earlier date than the one found. In the consequence, we print the cashflow satisfying the patterns and then retract it, making way for the next earliest cashflow of type
25.13. Banking Example: Class Account Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- Two separate
Accountobjects are created and injected into theCashflowsobjects before being passed to the Rule Engine.
25.14. Banking Example: Class AllocatedCashflow Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- Extending the
TypedCashflow, results inAllocatedCashflow, which includes anAccountreference.
25.15. Banking Example: Extending Example5.java Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
- This Java code creates two
Accountobjects and passes one of them into each cashflow in the constructor call.
25.16. Banking Example: Rule in Example6.drl Copiar enlaceEnlace copiado en el portapapeles!
Copiar enlaceEnlace copiado en el portapapeles!
Output:
- In this example, each cashflow in date order is applied and calculated, resulting in a print of the balance.
- Although there are separate rules for credits and debits, there is no type specified when checking for earlier cashflows. This is so that all cashflows are applied in date order, regardless of the cashflow type.
- In the conditions the account has identified and in the consequences, the cashflow amount is updated.