Drools: Rule Engine Notes

Drools Rule Engine

The following is a description of the important libraries that make up JBoss Drools

  • Knowledge-api.jar
    • this provides the interfaces and factories.
  • Knowledge-internal-api.jar
    • this provides internal interfaces and factories.
  • Drools-core.jar
    • This is the core engine, runtime component.
    • Contains both the RETE engine and the LEAPS engine.
    • This is the only runtime dependency.
  • Drools-compiler.jar
    • This contains the compiler/builder components to take rule source, and build executable rule bases. This is often a runtime dependency of your application, but it need not be if you are pre-compiling your rules. This depends on drools-core.
  • Drools-jsr94.jar
    • This is the JSR-94 compliant implementation, this is essentially a layer over the drools-compiler component.
  • Drools-decisiontables.jar
    • this is the decision tables ‘compiler’ component, which uses the drools-compiler component. This supports both excel and CSV input formats.

Basics:

Stateless Knowledge Session:

  • Don not use inference, forms the simplest use case.
  • A stateless session can be called like a function passing it some data and then receiving some results back.
  • Some common use cases for stateless sessions are, but not limited to:
    • Validation : Is this person eligible for a mortgage?
    • Calculation: Compute a mortgage premium.
    • Routing and Filtering
      • Filter incoming messages, such as emails, into folders.
      • Send incoming messages to a destination.

Stateful Knowledge Session:

  • Stateful Sessions are long lived and allow iterative changes over time.
  • Some common use cases for Stateful Sessions are, but not limited to:
    • Monitoring: Stock market monitoring and analysis for semi-automatic buying.
    • Diagnostics: Fault finding, medical diagnostics
    • Logistics: Parcel tracking and delivery provisioning
    • Compliance User Guide
    • Validation of legality for market trades.

Method versus Rule:

Methods :

  • Methods are called directly.
  • Specific instances are passed.
  • One call results in a single execution.

Rule:

  • Rules execute by matching against any data as long it is inserted into the engine.
  • Rules can never be called directly.
  • Specific instances cannot be passed to a rule.
  • Depending on the matches, a rule may fire once or several times, or not at all.

Execution Control

Agenda:

  • The Agenda is a Rete feature.
  • It maintains set of rules that are able to execute, its job is to schedule that execution in a deterministic order.
  • During actions on the RuleRuntime, rules may become fully matched and eligible for execution; a single Rule Runtime Action can result in multiple eligible rules.
  • The Agenda controls the execution order of these Matches using a Conflict Resolution strategy.

The engine cycles repeatedly through two phases:

  1. Rule Runtime Actions.
    1. This is where most of the work takes place, either in the Consequence (the RHS itself) or the main Java application process.
    2. Once the Consequence has finished or the main Java application process calls fireAllRules() the engine switches to the Agenda Evaluation phase.
  2. Agenda Evaluation.
    1. This attempts to select a rule to fire.If no rule is found it exits,
    2. otherwise it fires the found rule, switching the phase back to Rule Runtime Actions.

Salience:

  • When we want to prioritise rule then this value to rule.
  • Max value have max priority.

Agenda Group:

  • Place rules under groups and place them on stack
  • setFocus put following group onto stack.

Rule language:

  • Rule are defined in rule file with extension as .drl.
  • Structure of rule file
    • package package-name imports
    • globals
    • functions
    • Queries
    • rules
  • Package should be first element, other can be declared in any order
  • Structure of rule
    • rule “name” attributes
      • When
        • LHS
      • Then
        • RHS
      • End

Keywords:

  • Hard keywords : reserved for use in .drl file (true,false and null)
  • Soft keywords : can be used but, but recommended to avoid use (confusion)
    • Global , function, no-loop etc

Package:

drools_package.png

  • Import

drools_import.png

  • Global

drools_global.png

Function:

drools_function.png

Type Declaration:

drools_type_decelaration.png

Meta_data :

drools_type_meta_data.png

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: