Version 2

    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, or eclipse install JBoss Tools(, then create Drools Project with demo drools as showed below:


       Figure 1-3 Create Drools Project


    This will generate drools project with and Sample.drl. 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"
            m : Message( status == Message.HELLO, myMessage : message )
            System.out.println( myMessage );
            m.setMessage( "Goodbye cruel world" );
            m.setStatus( Message.GOODBYE );
            update( m );
    rule "GoodBye"
            Message( status == Message.GOODBYE, myMessage : message )
            System.out.println( myMessage );

    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

    1. Start BRMS Platform
    2. Create com.sample package(http://localhost:8080/jboss-brms -> Knowledge Bases -> Create New -> New Package)
    3. 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=''
        xs:schemaLocation=' drools-change-set-5.0.xsd' >
            <resource source='http://localhost:8080/jboss-brms/org.drools.guvnor.Guvnor/package/com.sample/HelloWorldDrools' type='PKG' 
              basicAuthentication="enabled"  username="admin" password="admin" />


    • Run com.sample.DroolsTestAgent as Application will run drools

            Note that: DroolsTestAgent can be found from (drools/drools-agent), 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");
                } catch (Throwable t) {
         private static KnowledgeBase readKnowledgeBase() throws Exception {
                final KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent("kagent");    
                kagent.setSystemEventListener(new PrintStreamSystemEventListener());
                KnowledgeBase kbase = kagent.getKnowledgeBase();
                return kbase;