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

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

Dragan Juričić, Privredna banka Zagreb

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

� Introduction to “business rules”

� High level overview of JBoss Drools

� Decision tables

� Discussion of benefits and downsides

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

Business Rules

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

� 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

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

� 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

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

� 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}

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

7

JBoss Drools

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

8

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

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

� 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

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

� 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

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

Decision Tables

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

� Excel XLS based

� Tabular representation of decisions

� Compact way to model large sets of related rules

12

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

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

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

� 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

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

15

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

� 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

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

Discussion

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

� 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

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

� Separates “business logic” from applicationcode

� “Hot” changes of rules (no recompile)

� Improved performance for large rule sets

� Communicates rules more clearly than Java code

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

� New tool� Learning curve

� Extra point of failure

� Overhead� Not practical for small rule sets

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

� 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