Using the Drools Guvnor Repository to store your Rules

Guvnor is the business rules manager included with Drools to manage knowledge and to be a centralized repository for Drools knowledge bases. In this tutorial we will show how to upload and use some rules in its repository.

In order to get started you need at first to install the Guvnor repository into JBoss AS as shown in this tutorial. Once installed start the guvnor by opening your browser to http://localhost:8080/drools-guvnor/
Now in order to create some rules into our repository we will need to perform the following steps:

Create a package for your Model classes

From your Knowledge Bases left menu choose to Create New > Package and enter a name for it.

drools-guvnor1

Upload your Model classes

Let’s compile and package the following class which will be used into our Rule. Package it in a file named Account.jar

package com.sample.model;
public class Account {
    private Integer balance;

    public Account() {}
    public Integer getBalance() {
        return balance;
    }
    public void setBalance(Integer balance) {
        this.balance = balance;
    }
    public Account(Integer balance) {
        super();
        this.balance = balance;
    }
    public void withdraw(int money) {
        balance -= money;
    }
}

Uploading your Facts requires at first selecting Create New > Upload POJO Model

drools-guvnor2
From there enter your Model Name and select the package you have just created. In the next screen of the Wizard pickup the JAR file with the Model.

drools-guvnor3

 

Note: If you don’t want to upload your Model, it is also possible to define your Model class by using the Declarative Model editor which will let you create your facts from the Class fields.

Create your Rules

Now it’s time to insert your first Rule into your Guvnor repository. From the left Menu select Create New > Rule.

drools-guvnor4

In the next screen you can opt for the Business Rule (guided editor) or, if you already have coded your rule, just use the Technical editor and enter the following rule:

import com.sample.model.Account
rule "accountBalanceAtLeast"
  when
  $account : Account( balance < 100 )
  then
  System.out.println("Warning! money running out!");
end

Once you have a stable knowledge you can create a deployment snapshots which freezes the knowledge for when you need it, saving a state of the knowledge that cannot be modified. Of course, you can still modify the knowledge through the Knowledge Bases, but any change will not be reflected in the snapshots.

drools-guvnor5

Select the Package from the Left and enter in the Edit Tab. From there at first build the binary package by clicking on the Build package button. Building a package will collect all the assets, validate and compile into a deployable package.

Next create a snapshot for deployment. Once you have clicked on the button, a pop-up, New snapshot, will appear on the screen. Select a package to generate a snapshot. Now from the Edit Menu, copy the URL location of your package source

drools-guvnor6

The source will be used in the following Test class which loads the accountBalanceAtLeast from the Guvnor Repository:

public class GuvnorTest  {
    @Test
    public void testDroolsWithGuvnor() throws Exception {
        KnowledgeBase knowledgeBase = createKnowledgeBase();
        StatefulKnowledgeSession session = knowledgeBase.newStatefulKnowledgeSession();
        try {
            Account account = new Account();
            account.setBalance(10);
            session.insert(account);

            session.fireAllRules();

        }
        finally {
            session.dispose();
        }
    }

    private static KnowledgeBase createKnowledgeBase() {
        KnowledgeAgentConfiguration kaconf = KnowledgeAgentFactory.newKnowledgeAgentConfiguration();
        kaconf.setProperty( "drools.agent.scanDirectories", "false" );
        KnowledgeAgent kagent = KnowledgeAgentFactory.newKnowledgeAgent( "test agent", kaconf );
        kagent.applyChangeSet( ResourceFactory.newClassPathResource("guvnor-jboss.xml"));
        return kagent.getKnowledgeBase();
    }
}

And here’s the guvnor-jboss.xml file which contains the resource path and the basic authentication info:

<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
    http://anonsvn.jboss.org/repos/labs/labs/jbossrules/trunk/drools-api/src/main/resources/change-set-1.0.0.xsd' >
   <add>
      <resource
      source='http://localhost:8080/drools-guvnor/rest/packages/com.sample.model/source'
      type='DRL' basicAuthentication="enabled" username="admin" password="admin" />
   </add>
</change-set>

That’s all ! Enjoy coding your Rules into the Guvnor Repository.

Found the article helpful? if so please follow us on Socials