Drools Learning Note 4-the first example

Source: Internet
Author: User
Tags knowledge base

Installation environment has, the following start to do the first example.

Create a new Drools project that will generate a Hello World example when you create it. I looked at this example and didn't look much. Find a more complicated example from the Internet. The concrete contents of the example go to see for themselves. The following is mainly about the problems encountered in doing this example.

Directory structure of the Drools project:


1, about DRL files: Addpoint.drl and SUBPOINT.DRL, put in the Rule folder. Note: There are pacakage in the DRL rules file, such as Com.drools.demo.point in the example. But you can't build the appropriate package in rule, and then put the DRL file in it, which will cause the Java Class (Pointruleegineimpl) to get an error, and that's what I started.

2, about Pointruleengineimpl file: The code in the example is Drools5.2.0. Now our environment is 6.0.0, with a lot of changes compared to the previous version. The location of some classes has changed, which is a minor problem. The point is that Drools6.0.0 uses something called Kie to manage the entire system. For more information about Kie, see here. We only focus on how to use Kie in our example.

Pointruleengine.java

1. Package com.jd.drools.test;	  
2.3.  
4. Import org.kie.api.KieServices;  
5. Import Org.kie.api.runtime.KieContainer;  
6. Import Org.kie.api.runtime.KieSession;	7.8.  
Import Com.jd.drools.test.PointDomain;	9.10.	  
public class Pointruleengineimpl {11.	    12.13.	       public static void Main (string[] args) {14.	        Load up the Knowledge Base 15.  
Kieservices KS = KieServices.Factory.get ();  
Kiecontainer Kcontainer = Ks.getkieclasspathcontainer ();  
Kiesession ksession = kcontainer.newkiesession ("Ksession-rules");  
Pointdomain pointdomain=new Pointdomain ();  
Pointdomain.setusername ("Hello kity");  
Pointdomain.setbackmondy (100d);  
Pointdomain.setbuymoney (500d);  
Pointdomain.setbacknums (1);  
Pointdomain.setbuynums (5);  
Pointdomain.setbillthismonth (5);  
Pointdomain.setbirthday (TRUE); Pointdomain.setpoint (0l);  
Ksession.insert (Pointdomain);  
Ksession.fireallrules ();	    29.30.	  
} 31.  
 32.}

Yes, it's that simple. It's no different from the HelloWorld example. Why is it so simple?

First, there is no code to read the rule file DRL. Kie loads all rule files under the Resource folder and its subfolders by default.

Also, some of the relevant configuration information has been moved to/meta-inf/kmodule.xml. Kie reads Kmodule.xml By default, generates ksession defined in the file, and so on.

At this point, we initially studied drools. Know what Drools is, is the computer in that field of things, the use of what algorithm, how to build the development environment, the rules of the file is what, where, with the rules of the file, how to read and run the rules. Have a preliminary impression on the whole of drools. You can start doing the project below. As for how the rules are defined, the grammar of the rule language, etc., open the side to do the project while learning. These articles are just shared to beginners like me, exposing the problems that are easy to encounter early on, so that we can get started quickly and save time.

PS: Of course, if your project also integrates other frameworks, you'll have to prepare for it. My project has no other framework to work with, so it's ready to start.

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.