Drools官网:http://www.jboss.org/drools/documentation.html
1、Myeclipse6.5下安装Drools插件
在线安装地址:http://download.eclipse.org/tools/gef/updates/releases/
安装后重启eclipse,会看到eclipse的工具条上多了一个人头按钮
这就说明插件已经安装成功!
2、规则语言实例
-->新建rule project
-->finish,下面是建好的工程目录树,里面的DroolsTest.java和sample.drl都是插件自己生成的
下面我们自己写的东西:
代码:
UserVO.java:
package com.drools.demo;
/**
* 类:用户
* @author lisanlai
*
*/
public class UserVO {
/** 姓名 */
private String name;
/** 年龄 */
private int age;
/** 性别 */
private String sex;
/** 体重 */
private float weight;
/** 身高 */
private float height;
public UserVO() {
super();
}
public UserVO(String name, int age, String sex, float weight, float height) {
super();
this.name = name;
this.age = age;
this.sex = sex;
this.weight = weight;
this.height = height;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public float getWeight() {
return weight;
}
public void setWeight(float weight) {
this.weight = weight;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
}
UserTest.java:
package com.drools.demo;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
public class UserTest {
public static final void main(String[] args) {
try {
RuleBase ruleBase = readRule();
WorkingMemory workingMemory = ruleBase.newStatefulSession();
UserVO user = new UserVO("张三",10,"男",(float)90.6,(float)140.8);
UserVO user1 = new UserVO("李四",40,"男",(float)130.2,(float)180);
UserVO user2 = new UserVO("西施",8,"女",(float)50,(float)130);
UserVO user3 = new UserVO("貂蝉",24,"女",(float)80,(float)175);
workingMemory.insert( user );
workingMemory.insert( user1 );
workingMemory.insert( user2 );
workingMemory.insert( user3 );
workingMemory.fireAllRules();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static RuleBase readRule() throws Exception {
Reader source = new InputStreamReader( UserTest.class.getResourceAsStream( "/UserTest.drl" ) );
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( source );
Package pkg = builder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
}
UserTest.drl:
#created by lisanlai package com.demo import com.drools.demo.UserVO #自己定义的方法 function void sayHello(UserVO user,String str) { System.out.println(user.getName()+" "+str+",你好!"); } function void printUserInfo(UserVO user) { System.out.println("个人信息:"); System.out.println("姓名: "+user.getName()); System.out.println("年龄: "+user.getAge()); System.out.println("性别: "+user.getSex()); System.out.println("体重: "+user.getWeight()); System.out.println("身高: "+user.getHeight()); System.out.println("============================================"); } rule "say hello to boy" when user : UserVO( sex=="男",age<18 ) then sayHello(user,"小帅哥"); printUserInfo(user); end rule "say hello to man" when user : UserVO( sex=="男",age>18 ) then sayHello(user,"先生"); printUserInfo(user); end rule "say hello to girl" when user : UserVO( sex=="女",age<18) then sayHello(user,"小美女"); printUserInfo(user); end rule "say hello to woman" when user : UserVO( sex=="女",age>18 ) then sayHello(user,"小姐"); printUserInfo(user); end
看看运行UserTest.java的效果:
3、域语言实例
下面我们把上面的实例换成用域语言来写
-->在src/main/rules下面新建UserTest.dsl文件
--->再新建UserTest.dslr文件:
代码:
DSLUserTest.java:
package com.drools.demo;
import java.io.InputStreamReader;
import java.io.Reader;
import org.drools.RuleBase;
import org.drools.RuleBaseFactory;
import org.drools.WorkingMemory;
import org.drools.compiler.PackageBuilder;
import org.drools.rule.Package;
public class DSLUserTest {
public static final void main(String[] args) {
try {
RuleBase ruleBase = readRule();
WorkingMemory workingMemory = ruleBase.newStatefulSession();
UserVO user = new UserVO("张三",10,"男",(float)90.6,(float)140.8);
UserVO user1 = new UserVO("李四",40,"男",(float)130.2,(float)180);
UserVO user2 = new UserVO("西施",8,"女",(float)50,(float)130);
UserVO user3 = new UserVO("貂蝉",24,"女",(float)80,(float)175);
workingMemory.insert( user );
workingMemory.insert( user1 );
workingMemory.insert( user2 );
workingMemory.insert( user3 );
workingMemory.fireAllRules();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static RuleBase readRule() throws Exception {
Reader source = new InputStreamReader( DSLUserTest.class.getResourceAsStream( "/UserTest.dslr" ) );
Reader dsl = new InputStreamReader(DSLUserTest.class.getResourceAsStream("/UserTest.dsl"));
PackageBuilder builder = new PackageBuilder();
builder.addPackageFromDrl( source,dsl );
Package pkg = builder.getPackage();
RuleBase ruleBase = RuleBaseFactory.newRuleBase();
ruleBase.addPackage( pkg );
return ruleBase;
}
}
UserTest.dsl:
[condition][]有一个用户=user:UserVO() [condition][]-性别是"{sex}"=sex=="{sex}" [condition][]-年龄大于{age}=age>{age} [condition][]-年龄小于{age}=age<{age} [consequence][]对"{str}"说你好=sayHello(user,"{str}"); [consequence][]打印个人信息=printUserInfo(user);
UserTest.dslr:
#created on: Sep 2, 2010 package com.drools.demo #list any import classes here. expander UserTest.dsl #自己定义的方法 function void sayHello(UserVO user,String str) { System.out.println(user.getName()+" "+str+",你好!"); } function void printUserInfo(UserVO user) { System.out.println("个人信息:"); System.out.println("姓名: "+user.getName()); System.out.println("年龄: "+user.getAge()); System.out.println("性别: "+user.getSex()); System.out.println("体重: "+user.getWeight()); System.out.println("身高: "+user.getHeight()); System.out.println("============================================"); } #declare any global variables here rule "say hello to boy" when 有一个用户 -性别是"男" -年龄小于18 then 对"小帅哥"说你好 打印个人信息 end rule "say hello to man" when 有一个用户 -性别是"男" -年龄小于18 then 对"小帅哥"说你好 打印个人信息 end rule "say hello to girl" when 有一个用户 -性别是"男" -年龄小于18 then 对"小帅哥"说你好 打印个人信息 end rule "say hello to woman" when 有一个用户 -性别是"男" -年龄小于18 then 对"小帅哥"说你好 打印个人信息 end
运行DSLUserTest.java效果:
跟上面用规则语言的效果一样。