20
Ing. Martín Cabrera [email protected] Moove-IT Rule Engine– Java & Ruby Drools & Ruleby

Rules Engine - java(Drools) & ruby(ruleby)

Embed Size (px)

DESCRIPTION

Rules Engine ? What is this ? When to use ? RETE algorithm Rules Engine In Java - JBoss Drools - features - Syntax Rules Engine In ruby - ruleby

Citation preview

Page 1: Rules Engine - java(Drools) & ruby(ruleby)

Ing. Martín [email protected]

Rule Engine– Java & RubyDrools & Ruleby

Page 2: Rules Engine - java(Drools) & ruby(ruleby)

Content

Rules Engine ? What is this ? When to use ? RETE algorithm In Java - JBoss Drools

features Syntax

In ruby ?

Page 3: Rules Engine - java(Drools) & ruby(ruleby)

Rule def

Motor de Reglas ¿what a #@!$ ?

Rule engine

Application

Rule def.knowledge base

Page 4: Rules Engine - java(Drools) & ruby(ruleby)

When to use?

Infrastructure decoupled from the source System to allow the final user to define their rules

and change them dynamically Developers are not the primary responsibility for changing

rules

Moderate or high complexity of business rules Performance !

Conclusion: down the rules of your system, so that they can be reused, decouple the code and allow the user to know the rules ... to modify !

Page 5: Rules Engine - java(Drools) & ruby(ruleby)

Tipical Code

01 if ((user.isMemberOf(AdministratorGroup) && user.isMemberOf(teleworkerGroup)) ||

02 user.isSuperUser() {

03 // more checks for specific cases

04 if ((expenseRequest.code().equals("B203") ||

05 (expenseRequest.code().equals("A903") && (totalExpenses < 200) &&

06 (bossSignOff > totalExpenses)) && (deptBudget.notExceeded)) {

07 // issue payments

08 }

09 else if {

10 // check lots of other conditions

11 }

12 }

13 else {

14 // even more business logic

15 }

16 }

Change this rules ! ... %&”@ !

Page 6: Rules Engine - java(Drools) & ruby(ruleby)

Rete Algorithm – the Solution ! Pattern matching algorithm for implementing

production rule systems (wikipedia) http://en.wikipedia.org/wiki/Rete_algorithm

replaces if ... Then with an optimized logic network

Page 7: Rules Engine - java(Drools) & ruby(ruleby)

Rete 2

RETE – characteristics: It reduces or eliminates certain types of redundancy through the use of

node sharing. It stores partial matches when performing joins between different fact

types. It allows for efficient removal of memory elements when facts are

retracted from working memory.

Widely used to implement matching functionality within pattern-matching engines that exploit a match-resolve-act cycle to support forward and backward chaining and inferencing. Forward-chaining (facts -> goals) Backguard chaining (goals -> facts)

Page 8: Rules Engine - java(Drools) & ruby(ruleby)

Drools –

http://jboss.org/drools

Introduction video - http://blog.athico.com/2010/03/fosdem-50-minute-introduction-into.html

Introduction Rules engine framework for java It is used in other jboss projects like JBPM, SEAM, Jboss ESB and

others The rules can be written in Java, Python, Groovy Ruby and your own

rule language Jboss tools – plugin Eclipse to use this framework

Page 9: Rules Engine - java(Drools) & ruby(ruleby)

Eclipse & Drools

Page 10: Rules Engine - java(Drools) & ruby(ruleby)

Syntax or rules definition

rule “<name>” <attribute> <value> when <LHS> then <RHS>end

Page 11: Rules Engine - java(Drools) & ruby(ruleby)

Example – rule definition

package com.mooveit.rule

import com.mooveit.rule.Global

global Global global;

rule "r1 - first rule"

wheninstance : Entity( field1 > 2010)

then...

end

rule.drl

Page 12: Rules Engine - java(Drools) & ruby(ruleby)

Example

StatefulSession session = getRulesBase().newStatefulSession();session.setGlobal("rulesImpl", this);

session.insert(instance);session.fireAllRules();

Business Action

knowledge base+instance

Execute the “then” part for the instance that match the facts

1

2

3

Page 13: Rules Engine - java(Drools) & ruby(ruleby)

Drools Schema

assertmodifyretract

Repository of instances

Rules files

Page 14: Rules Engine - java(Drools) & ruby(ruleby)

Agenda

JBoss Rules Overview

RuleRule

RuleRule

Rule

FactFact

FactFact

Fact

WorkingMemory3. Assert Facts

RuleBase

FactFact

FactFact

Fact

RuleRule

RuleRule

Rule

2. Create

4. F

ire A

ll Rule

s

(5) activation-> consequence

1.Parse DRLActivation

Rule Fact

Activation

RuleFact

Fact

Page 15: Rules Engine - java(Drools) & ruby(ruleby)

Examples in our projects

(demo)

Page 16: Rules Engine - java(Drools) & ruby(ruleby)

Rule Engine in

Page 17: Rules Engine - java(Drools) & ruby(ruleby)

Rules Engine In ruby

Ruleby - https://github.com/codeaspects/ruleby Rule engine for Ruby Active project but with few people dedicated October 11 of 2010 last push in the master branch Install: gem install ruleby

Others Ruby Rools - http://rools.rubyforge.org/

Last activity (ago 2009 in rools users) Status: Beta 2 members

A few projects without activity: rein, ruby-rules Treetop

language for describing languages It's not a pure rule engine ... but can serve

Page 18: Rules Engine - java(Drools) & ruby(ruleby)

Ruleby Description

Provide a pure Ruby Domain Specific Language (DSL)

(LHS) Left hand side of the production (the ‘when’) is represented by the Array parameter

(RHS) The right hand side (the ‘then’) is represented by the block.

Result:Hello World

rule [m.status == :HELLO] do |v| puts v[:m].messageend

engine :hello_engine do |e| HelloWorldRulebook.new(e).rules assert e, Message.new(:HELLO, ‘Hello World’) e.match end

Page 19: Rules Engine - java(Drools) & ruby(ruleby)

Ruleby links

The Ruleby DSL for more information. https://github.com/codeaspects/ruleby/wiki/The-Ruleby-DSL

Download http://gemcutter.org/gems/ruleby

User Group http://groups-beta.google.com/group/ruleby

Page 20: Rules Engine - java(Drools) & ruby(ruleby)

That's it !