| = Pricing Rule Decision Table Example |
| |
| |
| The Pricing Rule decision table demonstrates the use of a decision table in a spreadsheet, in Excel's XLS format, in calculating the retail cost of an insurance policy. |
| The purpose of the provide set of rules is to calculate a base price and a discount for a car driver applying for a specific policy. |
| The driver's age, history and the policy type all contribute to what the basic premium is, and an additional chunk of rules deals with refining this with a discount percentage. |
| |
| [source] |
| ---- |
| Name: Example Policy Pricing |
| Main class: org.drools.examples.decisiontable.PricingRuleDTExample |
| Module: drools-examples |
| Type: Java application |
| Rules file: ExamplePolicyPricing.xls |
| Objective: demonstrate spreadsheet-based decision tables. |
| ---- |
| |
| == Executing the example |
| |
| |
| Open the file [path]_PricingRuleDTExample.java_ |
| and execute it as a Java application. |
| It should produce the following output in the Console window: |
| |
| [source] |
| ---- |
| Cheapest possible |
| BASE PRICE IS: 120 |
| DISCOUNT IS: 20 |
| ---- |
| |
| |
| The code to execute the example follows the usual pattern. |
| The rules are loaded, the facts inserted and a Stateless Session is created. |
| What is different is how the rules are added. |
| |
| [source,java] |
| ---- |
| DecisionTableConfiguration dtableconfiguration = |
| KnowledgeBuilderFactory.newDecisionTableConfiguration(); |
| dtableconfiguration.setInputType( DecisionTableInputType.XLS ); |
| |
| KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(); |
| |
| Resource xlsRes = ResourceFactory.newClassPathResource( "ExamplePolicyPricing.xls", |
| getClass() ); |
| kbuilder.add( xlsRes, |
| ResourceType.DTABLE, |
| dtableconfiguration ); |
| ---- |
| |
| |
| Note the use of the `DecisionTableConfiguration` object. |
| Its input type is set to ``DecisionTableInputType.XLS``. |
| If you use the BRMS, all this is of course taken care of for you. |
| |
| There are two fact types used in this example, `Driver` and ``Policy``. |
| Both are used with their default values. |
| The `Driver` is 30 years old, has had no prior claims and currently has a risk profile of ``LOW``. |
| The `Policy` being applied for is ``COMPREHENSIVE``, and it has not yet been approved. |
| |
| == The decision table |
| |
| |
| In this decision table, each row is a rule, and each column is a condition or an action. |
| |
| .Decision table configuration |
| image::Examples/PricingExample/DT_Config.png[align="center"] |
| |
| |
| Referring to the spreadsheet show above, we have the `RuleSet` declaration, which provides the package name. |
| There are also other optional items you can have here, such as `Variables` for global variables, and `Imports` for importing classes. |
| In this case, the namespace of the rules is the same as the fact classes we are using, so we can omit it. |
| |
| Moving further down, we can see the `RuleTable` declaration. |
| The name after this (Pricing bracket) is used as the prefix for all the generated rules. |
| Below that, we have "CONDITION or ACTION", indicating the purpose of the column, i.e., whether it forms part of the condition or the consequence of the rule that will be generated. |
| |
| You can see that there is a driver, his data spanned across three cells, which means that the template expressions below it apply to that fact. |
| We observe the driver's age range (which uses `$1` and `$2` with comma-separated values), ``locationRiskProfile``, and `priorClaims` in the respective columns. |
| In the action columns, we are set the policy base price and log a message. |
| |
| .Base price calculation |
| image::Examples/PricingExample/DT_Table1.png[align="center"] |
| |
| |
| In the preceding spreadsheet section, there are broad category brackets, indicated by the comment in the leftmost column. |
| As we know the details of our drivers and their policies, we can tell (with a bit of thought) that they should match row number 18, as they have no prior accidents, and are 30 years old. |
| This gives us a base price of 120. |
| |
| .Discount calculation |
| image::Examples/PricingExample/DT_Table2.png[align="center"] |
| |
| |
| The above section contains the conditions for the discount we might grant our driver. |
| The discount results from the `Age` bracket, the number of prior claims, and the policy type. |
| In our case, the driver is 30, with no prior claims, and is applying for a `COMPREHENSIVE` policy, which means we can give a discount of 20%. Note that this is actually a separate table, but in the same worksheet, so that different templates apply. |
| |
| It is important to note that decision tables generate rules. |
| This means they aren't simply top-down logic, but more a means to capture data resulting in rules. |
| This is a subtle difference that confuses some people. |
| The evaluation of the rules is not necessarily in the given order, since all the normal mechanics of the rule engine still apply. |