Upgrade to Pro — share decks privately, control downloads, hide ads and more …

KieLive#17: Understanding DRL (Drools Rule Language)

KieLive#17: Understanding DRL (Drools Rule Language)

In this session, we'll see the basic theory behind a rule engine and how to use DRL to create simple and more advanced rules.

Link to the live streaming: http://red.ht/KieLive17

KieLive#17: Understanding DRL (Drools Rule Language)

The Drools Rule Language (DRL) is a programming language used to describe the business rules in Drools.

In this session we'll see the basic theory behind a rule engine and how to use DRL to create simple and more advanced rules.

We'll also see what the executable model is and what are the advantages compared to the classic engine.

About the invited speaker:
Luca Molteni is a Software Engineer from Milan, Italy working on the Drools project since 2017.

He contributed to the creation of the new runtime for Drools called "Executable Model".

He's a functional programming and open source advocate, and he's very active in the Italian programming communities and conferences.

2c8520502587d8827bad79bd2317299b?s=128

KIE Community

January 20, 2021
Tweet

Transcript

  1. Understanding DRL (Drools Rules Language) 1

  2. Before we start Drools community • https://kie.zulipchat.com/ (#drools and #kogito

    channels) • Drools Usage and Drools Setup mailing list • https://groups.google.com/g/drools-usage • https://groups.google.com/g/drools-setup • https://kogito.kie.org/community/ 2
  3. Understanding DRL (Drools Rules Language) https://www.drools.org/learn/documentation.html 3

  4. Declarative vs. Imperative 4

  5. Imperative Programming 5

  6. Declarative Programming 6

  7. Creating a programming language for a rule system 7

  8. Many ways to define business rules • DRL • Executable

    Models • Decision Tables • DMN Models 8
  9. What is a business rule? 9

  10. Anatomy of a rule when: a condition is true then:

    do something 10
  11. Example Validation Java #1 "You need to insert your first

    name to sign up to this service" public void validate(InputData inputData) { if(inputData.getFirstName() == null) { addValidationError(inputData.getUser(), "First name is required"); } } 11
  12. Example Validation Java #2 Multiple Validation public void validate(InputData inputData)

    { if(inputData.getFirstName() == null) { addValidationError(inputData.getUser(), "First name is required"); } else if (inputData.getLastName() == null) { addValidationError(inputData.getUser(), "Last name is required"); } ... and many more } 12
  13. Example: Validation "You need to insert your first name to

    sign up to this service" rule "first name exists" when InputData(firstName == null, $u : user) then validation.error($u, "First name is required"); end 13
  14. Example: Validation Multiple validation rule "first name exists" when InputData(firstName

    == null, $user : user) then validation.error($user, "First name is required"); end rule "address exists" when AddressData(address == null, $address : address) then validation.error($address, "address is required"); end 14
  15. Create a simple Drools project Let's show some code 15

  16. Session • Stateful Session • Stateless Session 16

  17. Stateful Session Customer c = new Customer(); c.setCategory(Category.Silver); Order o1

    = new Order(c, valueOf(100)); session.insert(c); session.insert(o1); session.fireAllRules(); assertEquals(Category.Silver, c.getCategory()); Order o2 = new Order(c, valueOf(200)); session.insert(o2); session.fireAllRules(); assertEquals(Category.Gold, c.getCategory()); 17
  18. Stateful Session Rule updating the customer rule "Will be promoted

    to gold when they make an expensive order" when $o : Order( amount >= 200, $c: customer) then $c.setCategory(Category.Gold); end 18
  19. Update the customer without notifying the engine rule "Will be

    promoted to gold customer when makes an expensive order" when $c : Customer() $o : Order( amount >= 200, customer == $c) then $c.setCategory(Category.Gold) end rule "User will receive an email when becomes a gold customer" when $c : Customer(category == Category.Gold) then System.out.println("Congratulation, you've become a gold customer"); end 19
  20. Modify rule "Will be promoted to gold customer when makes

    an expensive order" when $c : Customer() $o : Order( amount >= 200, customer == $c) then modify($c) { setCategory(Category.Gold); } end rule "User will receive an email when becomes a gold customer" when $c : Customer(category == Category.Gold) then System.out.println("Congratulation, " + $c + "you've become a gold customer"); end 20
  21. Inserting facts in the DRL Defining control types declare IsGoldCustomer

    customer: Customer end rule "Classify Customer - Gold" when $c: Customer( category == Category.Gold ) then insert(new IsGoldCustomer($c)); end 21
  22. Inserting facts in the DRL Defining control types declare IsLowRangeItem

    item: Item end rule "Classify Item - Low price" when $i: Item(cost < 10.00) then insert(new IsLowRangeItem($i)); end rule "Suggest gift" when IsGoldCustomer($c: customer) IsLowRangeItem($i: item) then System.out.println("Suggest giving a gift of item "+$i.getName()+" to customer "+$c.getName()); end 22
  23. Deleting objects into working memory rule "Delete expiring item" when

    $now : Date() $i : Item( expiringDate after $now) then delete($i) end 23
  24. Debugging the evalution of a rule Using the DebugAgendaEventListener The

    order of execution is not obvious, what do we do if we don't understand what's happening? session.addEventListener(new DebugRuleRuntimeEventListener()); 24
  25. Debugging the evalution of a rule An example of debug

    trace 15:04:16,797 INFO ==>[ObjectInsertedEventImpl: getFactHandle()=[fact 0:1:274996233:274996233:1:DEFAULT:NON_TRAIT:org.example.Customer:Customer{category=Silver, name='customer'}], getObject()=Customer{category=Silver, name='customer'}, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:1:274996233:274996233:1:DEFAULT:NON_TRAIT:org.example.Customer:Customer{category=Silver, name='customer'}], originOffset=-1, propagationNumber=2, rule=null, type=INSERTION] ] 15:04:16,819 INFO ==>[ObjectUpdatedEventImpl: getFactHandle()=[fact 0:1:274996233:274996233:5:DEFAULT:NON_TRAIT:org.example.Customer:org.example.Customer{category=Gold, name='customer'}], getObject()=org.example.Customer{category=Gold, name='customer'}, getOldObject()=org.example.Customer{category=Silver, name='customer'}, getKnowledgeRuntime()=KieSession[0], getPropagationContext()=PhreakPropagationContext [entryPoint=EntryPoint::DEFAULT, factHandle=[fact 0:1:274996233:274996233:5:DEFAULT:NON_TRAIT:org.example.Customer:org.example.Customer{category=Gold, name='customer'}], originOffset=-1, propagationNumber=6, rule=[Rule name=Will be promoted to gold customer when makes an expensive order, agendaGroup=MAIN, salience=0, no-loop=false], type=MODIFICATION]] 25
  26. Stateless Session StatelessKieSession statelessKieSession = kContainer.newStatelessKieSession("rules.simple.sl.discount"); Assert.assertNotNull(statelessKieSession); Customer customer =

    new Customer(); customer.setCategory(Customer.Category.SILVER); Order order = new Order(); order.setCustomer(customer); Command newInsertOrder = ks.getCommands().newInsert(order, "orderOut"); Command newInsertCustomer = ks.getCommands().newInsert(customer); Command newFireAllRules = ks.getCommands().newFireAllRules("outFired"); List<Command> cmds = new ArrayList<Command>(); cmds.add(newInsertOrder); cmds.add(newInsertCustomer); cmds.add(newFireAllRules); ExecutionResults execResults = statelessKieSession.execute(ks.getCommands().newBatchExecution(cmds)); 26
  27. Executable Model Another language to declare rules 27

  28. Executable Model Purpose • To have faster startup time •

    To provide an easier way to generate rules. • Enable the cloud native support (Kogito - https://kogito.kie.org) 28
  29. Executable Model Benchmarks BuildKieBaseFromContainerBenchmark.getKieBaseFromContainer numberOfRules useCanonicalModel Units 100 false 9.291

    ± 0.169 ms/op 100 true 0.209 ± 0.004 ms/op 500 false 44.774 ± 0.742 ms/op 500 true 0.926 ± 0.012 ms/op 29
  30. Executable Model Example (PatternTest.java) Result result = new Result(); Variable<Person>

    person = DSL.declarationOf(Person.class); Rule rule = rule("adult") .build( pattern( person ).expr( "exprA", p -> p.getAge() > 18), on(person).execute(adult -> result.setValue( "Person is adult: " + adult.getName())) ); KieBase kieBase = KieBaseBuilder.createKieBaseFromModel( new ModelImpl().addRule( rule ) ); KieSession ksession = kieBase.newKieSession(); ksession.insert(new Person("Leonardo", 3)); ksession.insert(new Person("Luca", 36)); ksession.fireAllRules(); assertEquals("Person is adult: Luca", result.getValue()); 30
  31. Books 31

  32. The end Questions? 32