Rules Engine Overview Denver Java User's Group Sept 2007

Post on 13-May-2015

611 views 0 download

Tags:

description

An overview of rules engines and their development and deployment

Transcript of Rules Engine Overview Denver Java User's Group Sept 2007

Rules Engine Overview

Scott RyanSeptember 2007

Agenda

• Rule Systems Overview• Rule Development Options• Toolsets• Rule Management• Rule Modification• Sample Implementation

Overview

Major Components

• Working Memory/Sessionso Rule Sets

Packages Rules

o Factso Globals and Non Fact Objects

Working Memory

• Stateful• Stateless• Can be dynamically updated• Data can push or pull between Working

Memory

Rule Sets

• Contain groupings of Rule Packages• Are associated with a Session

Packages

• Contain sets of rules• Are serializable

Rules

• Come in various formatso DRL/DSLo XMLo Spreadsheeto BRMSo Spring annotated beans

• Are serializable• Loaded using various techniques

XML Rule

<!-- Check for XYZ Corp--> <rule name="XYZCorp" salience="-1"><!-- Parameters we can pass into the business rule --><parameter identifier="stockOffer"><class>StockOffer</class></parameter><!-- Conditions or 'Left Hand Side' (LHS) that must be met for business rule to fire --><java:condition>stockOffer.getStockName().equals("XYZ")</java:condition> <java:condition>stockOffer.getRecommendPurchase() == null</java:condition><java:condition>stockOffer.getStockPrice() &gt; 10</java:condition><!-- What happens when the business rule is activated --><java:consequence>stockOffer.setRecommendPurchase(StockOffer.NO); printStock(stockOffer);</java:consequence></rule>

DRL Rule

rule "Time Zone One"no-loop truewhen$w : WorkItem(address.state.value memberOf timeZoneOneStates);then$w.setPriority($w.getPriority()+ 128);end

Facts

• Objects that Rules act upon• Follow Java Bean Rules• Can be dynamically updated• No limit to number, type or state of objects

Global and Non Fact Objects

• Used to provide data or functions• Injected during packaging• Can be any Java object• Accessible via any Rule Format

Toolsets

• Eclipse Based Development• Eclipse Based Debugging• Log Debugging

Rule Management

• File and/or classpath loading• JNDI Loading• Database Loading• Pre-Compilation (Rules are serializable)• Pre-Packaging (Packages are

serializable)• Caching Strategies

Rule Modification

• Property or Database Driven• DSL based• BRMS based• XML Dynamic Modification

Sample Implementation

• Simple Replacement of existing Business Rule Interface

• Code

Problem Domain

Pros

• Declarative Programming Modelo Concentrate on the what not the how

• Increased Performanceo Speed and Scalability

• Easily modified which increases coordination between business and IT

• Separation of logic and data (Is this a good thing?)

Cons

• New thought processes• Somewhat black box• Separation of code and logic breaks OO

paradigm.

Spring Support Rule

@Rule( name="Set Pump to Cooling", documentation="If a floors temperature becomes too hot, and the floor's pump is off, set the pump in the cooling state.", salience=10 ) public class PumpOffWhenFloorsWarmEnough {

Spring Support When

@Condition public boolean isPumpOff(@Fact HeatPump pump) { return pump.getState() == OFF; }

Spring Support Then

@Consequence public void consequence(HeatPump pump) { pump.setState(COOLING); }

Spring Configuration

<bean id="pumpRuleSet" class="org.drools.spring.factory.RuleSetFactoryBean"> <property name="rules"> <set> <bean id="pump.OffWhenFloorsCoolEnough" ... /> <bean id="pump.OffWhenFloorsWarmEnough" .../> <bean id="pump.HeatingWhenFloorTooCold" .../> <bean id="pump.CoolingWhenFloorTooHot" .../> </set> </property></bean>