Drools I

Attention Finishing all exercises from this class is necessarily to continue work next week.

Do use Drools system within Eclips, the following configuration of the IDE is required. In the system the Eclipse with Drools plugin is already installed, but the path to Drools runtime must be set:

  1. Go to Window→Preferences.
  2. From the menu on the right choose Drools/Installed Drools Runtime.
  3. Select runtime that is on the list and click Edit.
  4. Point drools-runtime directory that is installed in /usr/local/eclipse/drools-runtime.
  5. Make sure that the runtime is selected on the list and restart Eclipse.

Introduction

Create a Drools project

  1. Choose File→New→Project→Drools→Drools Project
  2. Type the project name and choose Next
  3. Select options as shown on the Figure below and choose Next
  4. Make sure that the project is bound with some Drools runtime and choose Finish.

Drools project structure

Source code

Source code in every Drools project is divided into two parts:

  1. Directory src/main/java where Java source code is located. It is responsible for running an inference process and definition of classes used in knowledge base.
  2. Directory src/main/rules Where rules definition and Drools Flow diagrams are located.

Rules syntax

Rules in Drools are built as follows:

rule "Rule Name"
  // Options
when
  // Condition part
then
  // Decision part - operations that are performed when the condition part is true
end

In „Hello World” example in condition part there appear following line:

m : Message( status == Message.HELLO, myMessage : message )

It should be read as follows: If in the fact base there is an object of Message class which status filed eqals Message.HELLO, then let m be a handle to the object and myMessage a handle to a message field of that object.

In other words th : operator (doubledot) works similarly to an assign operator and a construct that looks like a call of constructor of Message class is an equivalent for if statement.

Running inference in "Hello World"

In the DroolsTest.java file, there is located a source code responsible for creating a knowledge base, adding fats and firing inference process.

Method

private static KnowledgeBase readKnowledgeBase() throws Exception

is responsible for creating a knowledge base based on the file with rules definitions.

Files with rules definitions should be passed to an object of type KnowledgeBuilderFactory. Following line does that work:

...
kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl"), ResourceType.DRL);
...

Facts have to be added to a fact base (that is represented by the object of StatefulKnowledgeSession class)::

...
 
// Create an object-fact
Message message = new Message();
message.setMessage("Hello World");
 
message.setStatus(Message.HELLO);
 
// add a fact to a fact base
ksession.insert(message);
 
...
Exercise 1

Launch the „Hello World” project. Modify Message class by adding another enumerate field of value CONVERSATION. Modify Hello rule so that it will change status to CONVERSATION. Add rule that would be responsible for handling situation when status is set to CONVERSATION. Run project.

Cashpoint

Create another project (call it Cashpoint) the same way as before, selecting that HelloWorld example and DroolsFlow example should be generated.

Follow th instruction modyfing example files.

Description

The system is composed of till which can access a central resource containing the detailed records of customers’ bank accounts. A till is used by inserting a card and typing in a Personal Identification Number (PIN) which is encoded by the till and compared with a code stored on the card. After successfully identifying themselves to the system, customers may either:

  1. make a cash withdrawal or
  2. ask for a balance of their account to be printed.

Withdrawals are subject to a user resources, which means the total amount that user has on account. Another restriction is that a withdrawal amount may not be greater than the value of the till local stock.

Tills may keep illegal cards, i.e. after three failed tests for the PIN.

Classes

Add follwing classes to a src/main/java directory:

Rules

Create following rules. Put them in two files: Authorization and Action

To all new rule file right-clik on project name, and then choose:New→Other…→Drools/Rule.

  1. Sekect project and directory where rule file should be placed (<NAZWAPROJEKTU>/src/main/rules),
  2. Choose name of the file and package name (com.sample). After that choose Finish.

File: Authorization

Create rules that will check if given PIN number is correct and in case it's correct set value of an authorization filed to true Assume that values of fields:correctPIN,balance,moneyAmount,moneyLimit are assigned arbitrary.

Rule nameConditionAction
InvalidPINcorrectPIN != givenPIN && attempts < 3 attempts++
BlockAccountcorrectPIN != givenPIN && attempts == 3 lockCard=true
AuthGrantedcorrectPIN == givenPIN && attempts < 3 authorization=true

File: Action

Creaet rules that would implement Cashpoint actions:

Rule nameConditionAction
PayOut authorization == true && chosenAction == withdrawal && givenAmount ⇐ balance && givenAmount⇐moneyLimit „I'm paying”
CahspointLimit authorization == true && chosenAction == withdrawal && givenAmount ⇐ balance && givenAmount>moneyLimit „Insufficient funds in ATM!”
AccountLimit authorization == true && chosenAction == withdrawal && givenAmount > balance „Insufficient funds in your account.”
Balance authorization == true && chosenAction == balance „Your balance: ”
Unauthorized authorization == false && lockCard == true „Authorization failed”

Inference controll

Modify file from src/main/java/ that is responsible for launching inference so that both files with rules were added to the knowledge base. Create two objects of classes Account and ATM and add them to a fact base.

Run the an inference process. The system will probably not work correctly, or will fall into infinite loop. When calling update the inference engine processes again all rules including the one that called update. Such situation may lead to infinite loop if rules conditions don't prevent it.

To set a rule as the one that should be processed only once during a cycle of inference process a no-loop option should be put in option area of the rule:

rule "Rule name"
  no-loop
when
  // Condition part
then
  // Decision part - operations that are performed when the condition part is true
end

To define an order in which rules should be fired, a salience option is used. It defines a priority of the rule. The higher the salience, the higher the priority. Rules that have higher priority are fired first.

rule "Rule name"
  no-loop
  salience 10
when
  // Condition part
then
  // Decision part - operations that are performed when the condition part is true
end

The same functionality can be achieved with agendas. More about that here

Exercise

Using salience and no-loop attribute make the system work correctly.

pl/dydaktyka/piw/2011/systemy_ekspertowe/drools_i.txt · ostatnio zmienione: 2019/06/27 15:50 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0