1. Introduction to Drools
2. Environment Setup
3. Programming with Drools
Lets start with a basic but complete program to get a Drools project up and running.
Program
For our first program, we will have the following files
A POJO class with getters and setters
A Java class for executing the application
A Rule file (with.drl extension)
Customer.java
Account.java
BankApp.java
bank.drl
2. Environment Setup
3. Programming with Drools
Lets start with a basic but complete program to get a Drools project up and running.
Program
For our first program, we will have the following files
A POJO class with getters and setters
A Java class for executing the application
A Rule file (with.drl extension)
Customer.java
package com.drools.examples; public class Customer { private Account account; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public Account getAccount() { return account; } public void setAccount(Account account) { this.account = account; } } |
Account.java
package com.drools.examples;
public class Account { private double balance; private int acct_no; public Account(int acct_no) { this.acct_no = acct_no; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getAcct_no() { return acct_no; } } |
BankApp.java
package com.drools.examples; import org.drools.KnowledgeBase; import org.drools.KnowledgeBaseFactory; import org.drools.builder.KnowledgeBuilder; import org.drools.builder.KnowledgeBuilderFactory; import org.drools.builder.ResourceType; import org.drools.io.ResourceFactory; import org.drools.runtime.StatefulKnowledgeSession; public class BankApp { public static final void main(String[] args) { KnowledgeBase knowledgeBase = createKnowledgeBase(); StatefulKnowledgeSession session = knowledgeBase .newStatefulKnowledgeSession(); try { Customer customer1 = new Customer(); Account account1 = new Account(100001); account1.setBalance(100); customer1.setAccount(account1); customer1.setName("George"); Customer customer2 = new Customer(); Account account2 = new Account(100002); account2.setBalance(5000); customer2.setAccount(account1); customer2.setName("Jim"); Customer customer3 = new Customer(); Account account3 = new Account(100003); account3.setBalance(220); customer3.setAccount(account3); customer3.setName("Harry"); session.insert(account1); session.insert(account2); session.insert(account3); session.fireAllRules(); } finally { session.dispose(); } } private static KnowledgeBase createKnowledgeBase() { KnowledgeBuilder builder = KnowledgeBuilderFactory .newKnowledgeBuilder(); builder.add(ResourceFactory .newClassPathResource("bank.drl"), ResourceType.DRL); if (builder.hasErrors()) { throw new RuntimeException(builder.getErrors() .toString()); } KnowledgeBase knowledgeBase = KnowledgeBaseFactory .newKnowledgeBase(); knowledgeBase.addKnowledgePackages(builder .getKnowledgePackages()); return knowledgeBase; } } |
bank.drl
package com.drools.examples //list any import classes here. //declare any global variables here rule "Low Balance Rule" when $account:Account($bal:balance<250) then System.out.println("Rule fired:The following account has low balance(<$250):"+"Account#:"+$account.getAcct_no()+", Available balance:"+$account.getBalance()); end |