Posts Tagged ‘JBoss’

Rules hot deploy using Drools / Guvnor – part 2

05/24/2010 7 comments

In the first part of this tutorial, I introduced the basics and the Drools Guvnor, as well as demonstrated the creation of a simple rule in Guvnor. The objective of this part is to show how to consume the rule set, as well as present as expose it as web service.

Preparing rules for consumption

To prepare the rules for consumption, it is necessary to generate a snapshot of the rules. This is done by his own administration interface Guvnor by selecting the package, performing the same build and selecting the option to create snapshot, as shown:

As a rule created to consume

The rules can be consumed by the Drools API itself, as shown in the code below:

package com.alesaudate.drools.sample.client;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.drools.RuleBase;
import org.drools.StatelessSession;
import org.drools.agent.RuleAgent;
import org.drools.definition.type.FactType;

public class Client {
private Properties config;
private String factName;
private Map factValues;
private Map expectedResultsMap;
private static Map agents = new HashMap();

public void consume () throws InstantiationException, IllegalAccessException {
RuleAgent agent = getAgent(getConfig().getProperty("url"));
RuleBase ruleBase = agent.getRuleBase();
FactType factType = ruleBase.getFactType(getFactName());
Object fact = factType.newInstance();
Map values = getFactValues();
for (String key : values.keySet()) {
factType.set(fact, key, values.get(key));
StatelessSession statelessSession = ruleBase.newStatelessSession();
Map expectedResults = getExpectedResultsMap();
for (String key : expectedResults.keySet()) {
Object resultValue = factType.get(fact, key);
expectedResults.put(key, resultValue);
private RuleAgent getAgent(String url) {
if (!agents.containsKey(url)) {
RuleAgent agent = RuleAgent.newRuleAgent(getConfig());
agents.put(url, agent);
return agents.get(url);
// getters and setters…


Where the field config is the configuration of the API (to be explained), the field factName represents the fully qualified name of the POJO that will be used as fact (in our example, is seguradora.Segurado) factValues is a map that will be used to populate fields and the fact expectedResultsMap is a map that will be used to retrieve the desired values of the implementation of the rule (in our case, the status field is interesting).

The field config can be configured using the following data:

  • newInstance: Used to determine whether a new instance of the rule will be created (for the case of rules that keep state)
  • file: to determine which file (local) will be read
  • dir: to establish a directory for reading
  • url: URL to determine a reading (which will be used, in our case)
  • poll: interval in seconds, rereading the selected source (to determine whether the rule needs to be updated on the local machine)
  • name: name of the configuration
  • localCacheDir: directory will be cached remotely read the rule.

For the last class as an example, you can perform the reading of the rule using the following code:

public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Client client = new Client();
		Properties config = new Properties();
		config.put("url", "http://localhost:8080/drools-guvnor/org.drools.guvnor.Guvnor/package/seguradora/LATEST");
		Map factValues = new HashMap();
		factValues.put("idade", 18);
		Map expectedResults = new HashMap();
		expectedResults.put("status", null);

Still using the example code, you can expose the rules as services for two distinct ways:

  • Creating a custom web service (the sample code is available on the downloads page, next to the sample code consumer rules)
  • Using Drools Server, placing a file. properties (similar to the properties file passed as parameter to the client) in the “WEB-INF/classes” Application of Drools Server. In this way, however, the web service will be exposed as REST.

The downloads page , is available for download the client’s consumption and exposure rule as SOAP web service.

The next part will present how to perform deploy a rule dynamically.

Rules hot deploy using Drools / Guvnor – part 1

05/18/2010 2 comments

A SOA well done should be developed along the lines of business for which the architecture is being developed. One example is an insurer, where the services may well be made for each sector of insurance offered, as well as a customer base. Thus, if the insurer will act in a new class of insurance, all she has to do is to reuse the customer service, to offer these new portfolios.

However (and still using the example of insurance companies) should be noted that the selection of new users must abide by certain rules (eg, an insured can not be less than eighteen years), it should also be noted that these rules are not always immutable (and if the insurer decides that insured below eighteen years may be accepted only in life insurance, what if, during the operation of the system, the system user decides that insured under eighteen years can only be accepted with parental consent?).

To reflect this flexibility, the architecture SOA takes the concept of Business Rules. Are rules that are usually understandable by business analysts and can be easily changed. Some engines expose those rules in the form of web services and other options to facilitate the relaxation of such. Some also facilitate hot deploy rules to allow this flexibility.

To illustrate this concept of + hot deploy rules, created this tutorial on how to develop rules from the Drools API, post them on JBoss Guvnor and eat them so performative.

Why exemplify using Drools Guvnor + (or JBoss BRMS)?

The JBoss Guvnor is a system of governance rules that open source has an extremely user-friendly management interface. Also, your engine has, so native systems that facilitate the hot deploy and manage these rules natively through Apache Jackrabbit .

Below is the print screen splash screen Guvnor:

What terms need to know when using Drools?

  • Package: logical organization to hold a collection of rules (similar to packages of Java classes).
  • Model: entity that will store the data input and output rules.
  • Fact finding that will cause a rule to run (similar to a if, in Java).
  • Rule: snippet of code that, given certain facts, it will run.
  • Snapshot: compiling the package at some point, which is what effectively will be used to generate a new snapshot (ie, is a “snapshot” of the package).

What this tutorial applies to versions

This tutorial is based on version 5.0 of Guvnor, based on JBoss AS version 4.2.3. The set is available for download here .

How do I create a rule using Drools?

Syntax (simplified) to create rules in Drools is:

 package seguradora

declare Segurado
    idade : Integer
    status : String

rule "menores de idade não são autorizados"
	seg : Segurado( idade >= 18)
	seg.setStatus ("AUTORIZADO");

In code, you can see the model “insured” who possesses the attributes age (like Integer) and status (of type String – the fact that Integer and String classes belong to the package java.lang, it is not necessary to put the fully qualified name of them). The rule, in the clause “when”, and receives an insured assigns the variable “sec”. The fact here is the excerpt:

Segurado( idade >= 18)

That is, the rule only evaluate the code in the clause then if, and only if the insured has an age attribute with a value greater than or equal to 18. If so, the words:

seg.setStatus ("AUTORIZADO");

will be evaluated. Note that here it is developed purely in Java code, or ceases to be native to the Drools.

How can I make this rule will deploy Guvnor

To accomplish deploy this rule, just follow the steps:

  • Create a new category, the Administration
  • menu Knowledge Bases , create a new package (package );
  • Still

  • Knowledge Bases , select the option to create a new rule, as the example:

  • The written text box that appears, type the text of the rule.
  • Click Validate
  • If everything is OK, click Save changes

There, the rule is set to deploy. For the implementation of the rule, however, you must perform some coding, I’ll post next post. See you there!