What's this?
Generally, we can use Drools API load drools and run in a J2SE environment, as below figure depicted:
Figure 1-1 Load drools Directly
In this condition, we load drools(Sample.drl) directly, then fire the file, run the rule. But in practical production environment, we have great numbers of drools, usually this drools saved in a repository, management by Guvnor, as showed in below figure:
Figure 1-2 Run drools via Agent
In this condition, drools(Sample.drl) hold by BRMS repository, we load drools via KnowledgeAgent, then fire and run rules.
About Sample.drl
You can either use Red Hat JBoss Develop Stutio(download from https://access.redhat.com), or eclipse install JBoss Tools(http://www.jboss.org/tools), then create Drools Project with demo drools as showed below:
Figure 1-3 Create Drools Project
This will generate drools project with DroolsTest.java and Sample.drl. DroolsTest.java is a example for load drools directly(Figure 1-1), Sample.drl content like below:
package com.sample import com.sample.DroolsTest.Message; rule "Hello World" when m : Message( status == Message.HELLO, myMessage : message ) then System.out.println( myMessage ); m.setMessage( "Goodbye cruel world" ); m.setStatus( Message.GOODBYE ); update( m ); end rule "GoodBye" when Message( status == Message.GOODBYE, myMessage : message ) then System.out.println( myMessage ); end
Sample.drl has a Message Object, which Message java class like below:
public static class Message { public static final int HELLO = 0; public static final int GOODBYE = 1; private String message; private int status; ... }
- This single class used in this Sample.drl has two fields: the message, which is a String, and the status which can be one of the two integers HELLO or GOODBYE.
When Sample.drl be loaded and fired, a single Message object is created with the message text and the status, if the status is HELLO, then print the message text, update the Message Object to "Goodbye cruel world", set Message Object status to GOODBYE; If the status is GOODBYE, only print the message text.
Synchronize Sample.drl to Guvnor
- Start BRMS Platform
- Create com.sample package(http://localhost:8080/jboss-brms -> Knowledge Bases -> Create New -> New Package)
- In JBDS or Eclipse select Windows -> Show View -> Guvnor Repositories, then click 'Add a Guvnor Repository connection' as below:
4. Synchronize Sample.drl to Guvnor, in Drools Project, select Sample.drl -> Guvnor -> Add, select com.sample package then finish.
Use KnowledgeAgent to get PKG from Guvnor
1. Update Message object as POJO Model jar
- Make changes to the POJO and then export as a jar (i.e. using JBDS)
- Go to JBoss BRMS, select Knowledge Bases -> Create New -> Upload POJO Model jar, relate model jar with Sample.drl as below:
- Browse step 1 exported jar, click Upload button complete upload. After sucessful upload, Model view can be see from Knowledge Bases > Packages > com.sample.
2. Build com.sample package
In Guvnor UI, select Knowledge Bases > Packages > com.sample -> Edit, click 'Build Package' package
3. Create snapshot for deployment
In the same page(Step 2), click button 'Create snapshot for deployment', input TestSample as below:
then Sample.drl will be access via http://localhost:8080/jboss-brms/org.drools.guvnor.Guvnor/package/com.sample/HelloWorldDrools
4. Run Sample.drl in J2SE via Agent
- Create changeset.xml in classpath with the following content:
<change-set xmlns='http://drools.org/drools-5.0/change-set' xmlns:xs='http://www.w3.org/2001/XMLSchema-instance' xs:schemaLocation='http://drools.org/drools-5.0/change-set drools-change-set-5.0.xsd' > <add> <resource source='http://localhost:8080/jboss-brms/org.drools.guvnor.Guvnor/package/com.sample/HelloWorldDrools' type='PKG' basicAuthentication="enabled" username="admin" password="admin" /> </add> </change-set>
- Run com.sample.DroolsTestAgent as Application will run drools
Note that: DroolsTestAgent can be found from https://github.com/kylinsoong/jBPM-Drools-Example.git (drools/drools-agent), DroolsTestAgent.java content as below:
public class DroolsTestAgent { public static final void main(String[] args) { try { KnowledgeBase kbase = DroolsTestAgent.readKnowledgeBase(); StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession(); KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test"); Message message = new Message(); message.setMessage("Hello World"); message.setStatus(Message.HELLO); ksession.insert(message); ksession.fireAllRules(); logger.close(); } catch (Throwable t) { t.printStackTrace(); } } private static KnowledgeBase readKnowledgeBase() throws Exception { final KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("kagent"); ResourceFactory.getResourceChangeNotifierService().start(); ResourceFactory.getResourceChangeScannerService().start(); kagent.setSystemEventListener(new PrintStreamSystemEventListener()); kagent.applyChangeSet(ResourceFactory.newClassPathResource("changeset.xml")); KnowledgeBase kbase = kagent.getKnowledgeBase(); return kbase; } }
Comments