JBoss Drools

JBoss Drools Guvnor Example

In this article, we will see an example of Drools Guvnor. We use Guvnor as ‘Business Rules Manager’. Guvnor is the name of the web and network related components for managing rules with drools.

If you want to more know about Drools Introduction or its setup, read here.

This example uses the following frameworks:

  1. Maven 3.2.3
  2. Guvnor 5.1.1

 
 

1. What is a Drools Guvnor?

In order to know Drools Guvnor, we first need to know what is a business rules manager. Business Rules manager allows people to manage rules in a multi user environment. It is a single point of truth for your business rules. It allows to manage the rules in a controlled fashion. We can keep track of different versions of the rules, its deployment. It also allows multiple users of different skill levels access to edit rules with user friendly interfaces. Guvnor is the name of the web and network related components for managing rules with drools.

2. Installing Guvnor

First you need to install JBoss app server from http://jbossas.jboss.org/downloads/. Once your download is complete, unzip the files to a folder of your choice. To start the JBoss App Server, open the bin folder and double-click on run.bat. Download Drools Guvnor war file. Rename the war file to guvnor.war and copy to {JBossHome}/server/default/deploy. Start Jboss. Open http://localhost:8080/guvnor/org.drools.guvnor.Guvnor/Guvnor.html.

You should see the below login screen:

Guvnor Login
Guvnor Login

You don’t need to enter user/password. Simply click on Ok. You will be taken to the welcome screen.

Guvnor Welcome Screen
Guvnor Welcome Screen

Click on ‘No thanks’.

3. Main Features of Guvnor

Browse the main areas. You will see:

  1. Knowledge Base
  2. QA
  3. Package Snapshots
  4. Administration

Main Features of Guvnor
Main Features of Guvnor

4. Upload Domain Model to Guvnor

Before rules can be defined in Guvnor, a business model(a set of Java classes) jar needs to be uploaded to Guvnor.
go to the “Knowledge Bases” tab. Click on “Create New” right under the tab and choose “New Package”. Give your package a name – I choose “banking” – and press Ok. Then “Create new” – “Upload new Model Jar”.

Create new Package
Create new Package

Create new package.

New package
New package

Create new domain model.

Create new model
Create new model

You can see the new model in the ‘Model’ tab.

Model Tab
Model Tab

Go to shopping car to upload the domain model.

Upload domain model
Upload domain model

Click on choose file to select the jar file.

You can either use maven to create jar file or use eclipse File->Export->Java->Jar file option to generate the domain model Jar file.

Select the domain model jar
Select the domain model jar

Once uploaded you will see a ‘Upload successful’ message.

Upload Successful Message
Upload Successful Message

You can see the model classes in the package tab. Click on ‘Save and validate configuration’ so that the model files become available as facts to the rules.

Package View
Package View

5. Define Rules

Adding a sample rule. Now the model is uploaded, we can start defining rules. To add a rule, you have to create a Category first (Administration>Category>New Category).

New Category
New Category

Create ‘ShoppingCart’ category.

Create new category 'ShoppingCart'
Create new category ‘ShoppingCart’

Once the category is created, will go back to the “Knowledge Bases” tab and choose Create New>New Rule to create our first rule.

Create new rule
Create new rule

Click on the ‘+’ green button to add conditions. To add a condition to the rule, we need to choose fact type as Cart. After choosing this, we’ll automatically be taken back to the guided rule-editing screen. This is where we define the rule.

Add conditions to rule
Add conditions to rule

6. Define the rule

We’ll see that Cart has been added as a condition. Currently, this rule will fire for all carts, we need to add constraint that it fires only when the total price > 5000. To add constraint, we need to click on the ‘+’ button on the WHEN condition. We’ll choose total price as the field and ‘Greater than’ as constraint with value set to 5000. Next, we will add ‘THEN’ part. Click on the green plus sign next to the Then section. The Add a new action dialog box will be displayed. Here we will select ‘call on method’ cart.addDiscount(), set the value to 2%.

Define Rule
Define Rule

7. Create and Run the Test Scenario

After defining the rule, we will create a test scenario to test the rule. Select the package and click on ‘Create new scenario’.

Create Test Scenario
Create Test Scenario

  1. We will have to click on the ‘+’ sign to insert a new GIVEN/EXPECT, the small green arrow to refine the scenario, and the ‘–’ sign to remove.
  2. We will click on GIVEN to insert a new cart fact and then on Add.
  3. Next, we will click on the Add a field button that appears. In the dialog box, select the ‘totalPrice’ field.
  4. Next, we will set a constraint for ‘totalPrice’, that is, if it is greater than 5000.
  5. Click on the green ‘+’ next to EXPECT. In the New Expectation dialog box that is displayed, click on show list and then choose the added rule.
  6. Change the default (that we expect this rule to fire at least once) to Expect Rules, to fire this many times, and then enter ‘1’ in the new text box that appears.
  7. Save this test scenario using the button at the top of the screen.
  8. Finally, we will click on the Run Scenario button, you will get a green bar at the top of the screen saying Results 100%.

Run scenario

8. Download the Eclipse Project

This was an example about Drools Guvnor.

Download
You can download the full source code of this example here: DroolsGuvnorExample.zip

Ram Mokkapaty

Ram holds a master's degree in Machine Design from IT B.H.U. His expertise lies in test driven development and re-factoring. He is passionate about open source technologies and actively blogs on various java and open-source technologies like spring. He works as a principal Engineer in the logistics domain.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Koijam Baleshwor Singh
Koijam Baleshwor Singh
5 years ago

very nice article. i was trying to follow the steps you had mentioned above. had a small challenge deploying the guvnor.war file in jboss server. below is the exception while deploying the war file. 21:31:30,296 INFO [org.jboss.as.server.deployment] (MSC service thread 1-4) JBAS015876: Starting deployment of “guvnor.war” 21:32:30,306 INFO [org.jboss.as.server] (DeploymentScanner-threads – 1) JBAS015870: Deploy of deployment “guvnor.war” was rolled back with failure message Operation cancelled 21:32:30,306 ERROR [org.jboss.as.server.deployment.scanner] (DeploymentScanner-threads – 2) JBAS015052: Did not receive a response to the deployment operation within the allowed timeout period [60 seconds]. Check the server configuration file and the server logs to find more… Read more »

Back to top button