DROOLS6 Introductory Example

Source: Internet
Author: User

The project structure is as follows:



Pom.xml dependencies:

<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>


The PERSON.DRL rule file is:

Package Com.person; Import Com.lala.bean.Person; Rule ' Boy '    salience 1    when        $p: person (age > 0);    Then        $p. Setdesc ("juvenile");        Retract ($p); End rule "Youth"    salience 2    when        $p: Who (age >);    Then        $p. Setdesc ("Youth");        Retract ($p); End rule "Midlife"    salience 3    when        $p: Who (age >);    Then        $p. Setdesc ("middle age");        Retract ($p); end rule ' old '    salience 4    when        $p: person (age >);    Then        $p. Setdesc ("old age");        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.) {        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+ "]";    }}

Test file:

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 () {K                 Ieservices 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 ("Bai Zhantong", 68);        person P2 = new person ("Li Mouth", 32);        Person P3 = new Person ("Tong Xiang Yu", 18);                person P4 = new Person ("Guofu", 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 ("Always executed" +count+ "rule");        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 (); }}


Run, output is:


Before P1: [Name= Bai Zhantong, age=68,desc=null]before p2: [Name= li da Zui, age=32,desc=null]before p3: [Name= Shang, Age=18,desc=null] Before P4: [Name= Guofu, Age=8,desc=null] always performed 4 rules after P1: [Name= Bai Zhantong, age=68,desc= old]after p2: [Name= li da Zui, age=32,desc= middle age] After P3: [Name= Shang, age=18,desc= youth]after P4: [Name= Guofu, age=8,desc= teenager]


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

DROOLS6 Introductory Example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.