The use of Drools decision tables

Source: Internet
Author: User

Decision table We have a brief introduction to the Drools Rule engine, here is mainly about how to use Java code to associate this Excel file with Drools, how to make it to achieve the desired effect.

This assumes that we have such a file under the Resources directory:/drools/decisiontable/mydecisiontable.xls

Then the use of the decision table in the http://docs.jboss.org/drools/release/6.4.0.Final/drools-docs/html_single/index.html#d0e5549 is also mentioned, There must be a corresponding

Kmodule.xml, here we do not provide this file, because in the introduction and use of KIE-API we already know that can be obtained through the program.

The specific code is as follows:

1. Get a Kieservices
Kieservices kieservices = KieServices.Factory.get ();
2. Create the Kiemodule XML corresponding class
Kiemodulemodel Kiemodulemodel = Kieservices.newkiemodulemodel ();
3. Create a Kiefilesystem virtual file system
Kiefilesystem Kiefilesystem = Kieservices.newkiefilesystem ();

4. Add a specific kiebase tag
Kiebasemodel Kiebasemodel = Kiemodulemodel.newkiebasemodel ("Mydecision").
Addpackage ("Mydecision");//kie folder for resource files in FileSystem
<KieBase></KieBase> Tag Add Kiesession property
Kiebasemodel.newkiesessionmodel ("Kiession-mydecision");//a
5. Adding kiemodule.xml files to the virtual file system
String kiemodulemodelxml = Kiemodulemodel.toxml ();
Kiefilesystem.writekmodulexml (kiemodulemodelxml);//kiemodulemodel
6. Load the rule file into the virtual file system
Resource Resource = getresource ("Drools/decisiontable/mydecisiontable.xls");
String fileName = "mydecision-table" + resource.getresourcetype (). Getdefaultextension ();
Here is the rule file added to the virtual system, the first parameter is the path of the file in the virtual system, where the file directory and 4. The addpackage must be the same, or fail
String Kiefilepath = new StringBuilder ("src/main/resources/").
Append ("Mydecision"). Append ("/"). Append (FileName). toString ();
Kiefilesystem.write (Kiefilepath, Resource);

7. Build all the kiebase and add all the kiebase to the warehouse
Kieservices.newkiebuilder (Kiefilesystem). Buildall ();
Kiecontainer Kiecontainer = Kieservices.newkiecontainer (Kieservices.getrepository (). GetDefaultReleaseId ());// Create a Kie container

8. Get a session from the container, where a key is added here and a, or you cannot find any one that will report an exception
Kiesession kiesession = kiecontainer.newkiesession ("kiession-mydecision");
Kiesession.insert (New Student (One, "Max"));
Kiesession.insert (New Student ("Max"));
Kiesession.fireallrules ();

Look at the comments should be able to understand the basic. Some of the components of the KIE-API are used here, which can be found in

Using the Drools decision table

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.