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

Preview:

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

Ing. Martín Cabreramartin.cabrera@moove-it.comMoove-IT

Rule Engine– Java & RubyDrools & Ruleby

Content

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

features Syntax

In ruby ?

Rule def

Motor de Reglas ¿what a #@!$ ?

Rule engine

Application

Rule def.knowledge base

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 !

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 ! ... %&”@ !

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

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)

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

Eclipse & Drools

Syntax or rules definition

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

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

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

Drools Schema

assertmodifyretract

Repository of instances

Rules files

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

Examples in our projects

(demo)

Rule Engine in

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

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

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

That's it !

Recommended