Drools
- Drools is a Business rule management system
- It is used to separate the data from logic.
- It allow us to write logic (rules) in more layman readable form. This allow business people to create their own rules instead of depending on programmers and developers.
Rules
Rules are pieces of knowledge often expressed as, "When some conditions occur, then do some tasks."The most important part of a Rule is it’s when part. If the when part is satisfied, the then part is triggered
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rule <rule_name> | |
<attribute> <value> | |
when | |
<conditions> | |
then | |
<actions> | |
end |
Installing Drools in Eclipse
Drools comes with plugin for eclipse to create and manage the project from eclipse itself.Prerequisites to install Drools Plugin:
- Java 1.5 (or higher)
- Eclipse 4.2
Installation Step
- Download the latest binaries from http://download.jboss.org/drools/release/
- Launch Eclipse -> help -> install new software -> Add.
- Name – Drools
- Location - http://download.jboss.org/drools/release/X.X.X.Final/org.drools.updatesite/
where X.X.X is version of drool downloaded. - Click Ok
- Checkmark “Drools and jBPM”. Click next. Accept the terms and wait for installation to finish.
Drools Runtime
Drool runtime is needed to create and execute drool rules.- Click on Windows -> Preference -> Drools -> Installed Drools Runtime -> Add -> Browse.
- Browse to the downloaded binaries folder and Add.
Example Project
- File -> New -> Drool Project -> Create an empty project.
- Enter project name -> finish.
- The project follow maven folder structure. Two primary folder are
- src/main/java – Java code would go here.
- src/main/resources – Drool files would go here.
- Create a new java class file in src/main/java. Say ‘Hello.java’ in package ‘com.example’.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
package com.example; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; public class Hello { private String hellostr = "Hello World"; public String getHellostr() { return hellostr; } public void setHellostr(String hellostr) { this.hellostr = hellostr; } public static void main(String[] args) { Hello hello = new Hello(); System.out.println("String before firing rule: "+hello.getHellostr()); // Create knowledge base from default drool file. // The default folder from where knowledge base will read // drool file is defined /src/main/resources/META-INF/kmodule.xml // The default value in this generated file is /src/main/resources/rules KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieSession kSession = kContainer.newKieSession("ksession-rules"); kSession.insert(hello); kSession.fireAllRules(); System.out.println("String after firing rule: "+hello.getHellostr()); } } - Create a new drool file in src/main/resources/rules. Say ‘ReplaceHello.drl’.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
import com.example.Hello; rule "SayGoodBye" when h : Hello(hellostr.contains("Hello")) then h.setHellostr(h.getHellostr().replace("Hello","Goodbye")); end - Result.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
String before firing rule: Hello World String after firing rule: Goodbye World
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
KieServices ks = KieServices.Factory.get(); | |
KieFileSystem kieFileSystem = ks.newKieFileSystem(); | |
kieFileSystem.write(ResourceFactory.newClassPathResource("myrule/HelloRule.drl")); | |
KieBuilder builder = ks.newKieBuilder(kieFileSystem).buildAll(); | |
KieContainer kContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()); | |
KieSession kSession = kContainer.newKieSession(); | |
if(builder.getResults().hasMessages(Level.ERROR)) { | |
throw new RuntimeException(builder.getResults().toString()); | |
} |
No comments:
Post a Comment