JavaCro'14 - Drools Decision tables – form of human-readable rules – Dragan Juričić

Post on 28-Nov-2014

1.007 views 3 download

description

Drools is a business rule management system (BRMS) with an inference based engine, using an enhanced implementation of the Rete algorithm. Business rules implementations are aimed mostly at developers. However, it is sometimes needed that these rules are readable and understandable by the business analysts. Ideally, they should be able to change rules or even write new ones. Decision table as a form of human-readable rules involve business analysts in creation and maintenance of rules. Managing rules in a spreadsheet format is very applicable for business analysts since they already use them. An important aspect of business rules is their readability and user friendliness. Looking at a rule, you should immediately have an idea of what it is about.

Transcript of JavaCro'14 - Drools Decision tables – form of human-readable rules – Dragan Juričić

Dragan Juričić, Privredna banka Zagreb

� Introduction to “business rules”

� High level overview of JBoss Drools

� Decision tables

� Discussion of benefits and downsides

Business Rules

� Basic problem: how to model “business logic”

� try to automate all kinds of business process

� process and decisions are not very wellrepresentded using traditional programinglanguage such as Java or C#

� Drools – makes the process of implementingrules quicker and handles complexity

� Rule engine is a piece of software, which having some knowledge is able to perform conclusions

� Type of Expert System

� Two types of rules:

�“Production (or Inference) Rules”� If x, then y� Often stateless

�“Reaction Rules” (Complex Event Processing)� Wait for set of events� Stateful

� Rules consist of conditions and actions, which are executed when their conditions are true

� Conditions for above rule:� Object type condition - Applicant Object Type

� Field condition - age < 18

� Matching patterns against the inserted data

6

rule "Is of valid age"when

$a : Applicant(age <18) � Conditionthen

$a.setValid( false); � Actionend

public class Applicant {private String name;private int age;private boolean valid;

//getter and setter methods}

7

JBoss Drools

8

“A common platform to modeland govern the business logic of the enterprise.”

� Core Drools product

� Has an enhanced and optimized implementation of the Rete algorithm for object oriented systems called as ReteOO

� Higher memory consumption for increased speed

� Forward chaining rule interpreter : “data driven”

9

� Technical Rule Language (Drools Rule Language –DRL)

� mostly aimed for developers

� Human-readable rules

� Custom Domain Specific Languages (DSL)

� Guided Editor

� DecisionDecisionDecisionDecision TableTableTableTable

� XML

10

Decision Tables

� Excel XLS based

� Tabular representation of decisions

� Compact way to model large sets of related rules

12

13

rule "Interest Calculation_16"

when

$a:Account(type == Account.Type.SAVINGS,

currency == "EUR", balance >= 100 && < 1000,

monthsBetweenStartAndEndDate >= 1 && < 3)

then

$a.setInterestRate(new BigDecimal("3.00"));

end

� Advantages of a decision tables

� It is easy to read and understand

� Refactoring is quicker (change conditions across group of rules)

� Involve “business users” in creation & maintenance

� Many businesses already use spreadsheets for managing data

� Any formatting available in a spreadsheet editor can be applied

� Disadvantages of a decision table

� XLS is a binary format which makes version management more difficult

� It can be awkward to debug these rules

14

15

� 1 DecisionTableConfiguration dtableconfiguration =

� 2 KnowledgeBuilderFactory.newDecisionTableConfiguration();

� 3 dtableconfiguration.setInputType(DecisionTableInputType.XLS);

� 4 dtableconfiguration.setWorksheetName("Tables");

� 5

� 6 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

� 7 kbuilder.add(ResourceFactory.newClassPathResource("SearchCase.xls"),

� 8 ResourceType.DTABLE, dtableconfiguration);

� 9

� 10 if (kbuilder.hasErrors()) {

� 11 for (KnowledgeBuilderError err : kbuilder.getErrors()) {

� 12 System.out.println("Drools error: " + err.getMessage());

� 13 }

� 14 }

� 15 KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

� 16 kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

� 17

� 18 StatelessKnowledgeSession ksession = kbase.newStatelessKnowledgeSession();

� 19 ksession.execute(Iterable p_objects);

16

Discussion

� Large set of rules, with complex branching

� Rules likely to change over time, likely out

of sync with code changes

� Involve “business users” in creation &

maintenance of rules

� Separates “business logic” from applicationcode

� “Hot” changes of rules (no recompile)

� Improved performance for large rule sets

� Communicates rules more clearly than Java code

� New tool� Learning curve

� Extra point of failure

� Overhead� Not practical for small rule sets

� http://www.jboss.org/drools/

� http://en.wikipedia.org/wiki/Drools

� http://docs.jboss.org/drools/release/5.5.0.Final/droolsjbpm-introduction-docs/html_single/index.html

� http://technicalmumbojumbo.wordpress.com/2009/03/28/jboss-drools-decision-tables/

� http://www.packtpub.com/article/human-readable-rules-with-drools-jboss-rules-part1

� http://docs.jboss.org/drools/release/5.5.0.Final/droolsjbpm-introduction-docs/html_single/index.html

21