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