Java Business Rules Engines with Drools Expert 6.x

Business Rules Engines with Drools Expert 6.x

Business rules management systems (BRMS) are needed to execute one or more rules in an enterprise or software system. The ability to maintain and execute rules as separate from the application code is the greatest advantage of such rules engines. Business rules engines are a form of expert system, which allows for human expert-like decision-making abilities.

Business rules engines are primarily of two types and classified on the basis of how rules are scheduled for execution.

Forward Chaining (Data-Driven)

1. Inference Engine: These are based on a set of ‘If-Then’ kind of behaviors or evaluations.

2. Reaction Rules: These are used to process event patterns and perform actions.

Backward Chaining (Goal-Driven)

These engines try to resolve facts based on particular goals.

Here are some examples of both types.

Drools Expert

Drools Expert works on the basis of the Rete/Rete-OO Algorithm. It is an open-source project that has the following major components:

  • Drools Guvnor (business rules manager) – a centralized repository for Drools knowledge bases
  • Drools Expert (rules engine) – Uses the rules to perform reasoning
  • Drools Flow (process/workflow), or jBPM 5 – provides for workflow and business processes
  • Drools Fusion (event processing/temporal reasoning) – provides for complex event processing
  • Drools Planner/OptaPlanner (automated planning) – optimizes automated planning, including NP-hard planning problems

A block diagram of the Rete/Rete-OO is below. The Rete Algorithm requires an extensive discussion, which fortunately we don’t need to get into right here.

Organizations must go beyond a piecemeal approach to networking and security. A broad, integrated, and automated platform that secures all edges addresses challenges now and in the future.

 

Use-case to demonstrate Drools Expert

If the ‘Source IP’ is a specific IP and the ‘Source Port’ is a specific number, then mark the ‘Event’ as ‘Blacklisted’ [unsafe event detection].

I will demonstrate this particular use-case, including how to run the ‘Intelligent Data Loader’, to understand rules engine processing. You may need to do the following before you can download and run the code:

  • Download Drools 6.1.0 Distribution (include in classpath)
  • Download the Eclipse Plugin for Drools (include in classpath)
  • Use JDK 1.8.0 and JEE 1.7 Libraries if required (include in classpath)
  • Read up on MVEL Dialect and Drools Expert

1. Start a Java Project in Eclipse [Classpath]

You may choose to start a Java project only, as opposed to a Drools project. If so, include the following JARs in the classpath:

2. The Directory / folder structure should Include ‘resources’ as source folder

 

3. Create the Drools Expert Configuration File (kmodule.xml)

xml file

4. Create the rule using ‘mvel’ dialect

The creation of your first rule using ‘mvel’ dialect should not be a very difficult task for the experienced Java developer. I am not explaining the use-case implementation in detail, except that it checks if the source ip and source port are equal to a specific number.


  package com.bw2015.sample.biz.re;  
 // list any import classes here.  
 import com.bw2015.sample.biz.vo.SampleEvent;  
 // use case 01  
 // detect if we can blacklist a specific ip and port access  
 rule "Port and IP Blacklist Rule"  
 dialect "mvel"  
 no-loop  
  when  
    $sampleEvent:SampleEvent(eventSourceIp=="216.39.58.18", eventSourcePort=="8080")   
  then  
       System.out.println("***** Blacklisted IP and Port Detected in Event with Remarks - " + $sampleEvent.getEventRemarks());  
 end  

5. Develop the Core rules engine Processing Class

Instantiate the important Drools runtime objects as shown below. You may also understand from the following code how to include and refer the rules file (.drl) from the classpath.


      private static SampleRulesEngine reService = null;  
      // Drools Expert Runtime Configuration  
      private KieServices ks;  
      private KieContainer kContainer;  
      private KieSession kSession;  
      public static SampleRulesEngine getInstance() {  
           if(reService==null) {  
                reService = new SampleRulesEngine();  
                reService.init();  
           }  
           return reService;  
      }  
      public void init() {  
           try {  
                System.out.println("initializing kie runtime for drools expert...");  
                ks = KieServices.Factory.get();  
                kContainer = ks.getKieClasspathContainer();  
                KieSessionConfiguration sessionConfiguration = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();  
                kSession = kContainer.newKieSession("ksession-rules", sessionConfiguration);  
                System.out.println("initialized the kie runtime for drools expert...");  
           } catch (Exception e) {  
                e.printStackTrace();  
           }  
      }  

6. Build a Data Loader (To Inject Positive Test Cases and Load Test)

There is a data loader that starts internally a threaded loading mechanism for sending multiple events to the Drools Expert Runtime. You have to make sure that you ‘Inject’ a positive test case at every interval to test out the functionality of Drools Expert. The Java Event Object in our example is “SampleEvent”.

7. Output of Running the SampleDataLoaderDriver [rules engine Output]

There are other rule forms that you may use such as Decision Tables. It is only a matter of configuration, classpath, regular expressions and a straightforward understanding of .xls files to create Decision Tables. Also, I recommend that you make sure that you use Microsoft Excel to edition the Decision Table Spreadsheet, else you may end up with issues.

Latest Posts

Related Stories