码迷,mamicode.com
首页 > 其他好文 > 详细

drools6入门例子

时间:2015-08-18 19:36:24      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:drools

项目结构如下:

技术分享


pom.xml依赖:

<dependency>
	<groupId>org.drools</groupId>
	<artifactId>drools-core</artifactId>
	<version>6.2.0.Final</version>
</dependency>
<dependency>
	<groupId>org.drools</groupId>
	<artifactId>drools-compiler</artifactId>
	<version>6.2.0.Final</version>
</dependency>


person.drl规则文件为:

package com.person;
 
import com.lala.bean.Person;
 
rule "boy"
    salience 1
    when
        $p : Person(age > 0);
    then
        $p.setDesc("少年");
        retract($p);
end
 
rule "youth"
    salience 2
    when
        $p : Person(age > 12);
    then
        $p.setDesc("青年");
        retract($p);
end
 
rule "midlife"
    salience 3
    when
        $p : Person(age > 24);
    then
        $p.setDesc("中年");
        retract($p);
end
 
rule "old"
    salience 4
    when
        $p : Person(age > 60);
    then
        $p.setDesc("老年");
        retract($p);
end


kmodule.xml

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
    <kbase name="SimpleRuleKBase" packages="rule">
        <ksession name="simpleRuleKSession" default="true"/>
    </kbase>
</kmodule>

Person.java

package com.lala.bean;
 
public class Person 
{
    private String name;
    private int age;
    private String desc;
     
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
    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 getDesc() {
        return desc;
    }
    public void setDesc(String desc) {
        this.desc = desc;
    }
    public String toString()
    {
        return "[name="+name+",age="+age+",desc="+desc+"]";
    }
}

测试文件:

package com.lala.mydrools;

import org.kie.api.KieServices;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieSession;

import com.lala.bean.Person;

public class Test 
{
	static KieSession getSession()
    {
        KieServices ks = KieServices.Factory.get();
         
        KieContainer kc = ks.getKieClasspathContainer();
 
        return kc.newKieSession("simpleRuleKSession");
    }
    public static void main(String[] args) 
    {
        KieSession ks = getSession();
         
        Person p1 = new Person("白展堂", 68);
        Person p2 = new Person("李大嘴", 32);
        Person p3 = new Person("佟湘玉", 18);
        Person p4 = new Person("郭芙蓉", 8);
        
        System.out.println("before p1 : " + p1);
        System.out.println("before p2 : " + p2);
        System.out.println("before p3 : " + p3);
        System.out.println("before p4 : " + p4);
         
        ks.insert(p1);
        ks.insert(p2);
        ks.insert(p3);
        ks.insert(p4);
         
        int count = ks.fireAllRules();
        System.out.println("总执行了"+count+"条规则");
        System.out.println("after p1 : " + p1);
        System.out.println("after p2 : " + p2);
        System.out.println("after p3 : " + p3);
        System.out.println("after p4 : " + p4);
        ks.dispose();
    }
}


运行,输出为:


before p1 : [name=白展堂,age=68,desc=null]
before p2 : [name=李大嘴,age=32,desc=null]
before p3 : [name=佟湘玉,age=18,desc=null]
before p4 : [name=郭芙蓉,age=8,desc=null]
总执行了4条规则
after p1 : [name=白展堂,age=68,desc=老年]
after p2 : [name=李大嘴,age=32,desc=中年]
after p3 : [name=佟湘玉,age=18,desc=青年]
after p4 : [name=郭芙蓉,age=8,desc=少年]


版权声明:本文为博主原创文章,未经博主允许不得转载。

drools6入门例子

标签:drools

原文地址:http://blog.csdn.net/mn960mn/article/details/47754211

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!