Drools Backward Chaining Example
In this article, we will see a little introduction and example of what is backward chaining and how to implemente it with jboss drools.
This example uses the following technologies and frameworks:
- Maven 3.3.9
- Jboss Studio 10.3
- Drools Engine 7.0
- JDK 1.8.0_71
1. Introduction
Backward chaining, is a concept that allows in a graph structure(derivation tree) to get through each node or find the possible paths between 2 nodes, using recursion and reacting to changing values. The latter is because drools is a reactive rule engine. Drools support 2 types of operations push (reactive operation) and pull (data query). So basically the backward chaining is how each node is connected to his parent and take advantage of that to find relations between them. We will achieve this on this example, by using drools and recursion queries that allows to search on derivation tree structure.
1.1 Example Graph
This graph is to illustrate the data that we will be using on this example to apply backward chaining and determinate that the key is in the envelope.
2. Configure Necessary Tools
Before we continue it is necessary to install the jboss developer studio in order to build the drools project that implement the backward chaining using recursion base on the 1.1 section graph. To download these tools go to jboss devstudio site and install them.
Next Add the drools plugin to the IDE, open the jboss IDE and select the bottom tab called software update as it’s shown below.
Use the search box to find the drools plugin and install it.
2.1 Creating The Maven Project
After the installation of the drools plugin, we can create a new drool project in order to implement the example.
Go to, file -> new -> other menu and at the dialog box search for drools project.
Select the second option, create drools project and populate with some examples.
Add a project name and select the maven support.
2.2 Maven Project Structure
After the project creation wizard, we should have something like this:
The project comes with a default package called, com.sample. This has 3 sample classes for a fast getting started and a configuration file named kmodule.xml inside src/main/resources/META-INF folder, that allows to map our folders that contains the drools rules files (.drl extension) in order to create a drools session and execute the rules code.
3. Backward Chaining Example Implementation
Now we have already our drools dev environment configured to start the backward chaining implementation. The steps the we will follow are:
- Model Class creation (Location class)
- Drools rules file creation (.drl extension file. Derivation query implementation)
- Drools rule file mapping
- Test Class forresults validation
3.1 Model Class Creation
On the jboss studio maven project, right click on com.sample package and create a new one com.sample.model and inside that package create a new class named Location. This class will be a model to represent the location on the graph.
Location.java
package com.sample.model; import org.kie.api.definition.type.Position; public class Location { @Position(0) //to indicate position of each attribute, that allows to the engine identifie the params order to use on the query function private String thing; @Position(1) private String location; public Location(){} public Location(String thing, String location) { super(); this.thing = thing; this.location = location; } public String getThing() { return thing; } public void setThing(String thing) { this.thing = thing; } public String getLocation() { return location; } public void setLocation(String location) { this.location = location; } }
3.2 Drools Rules File Creation
On the jboss studio maven project, right click on src/main/resources folder and create a new folder after called backward_chaining. Right click on the previous folder and create a new rules file named BackwardChaining. Check below how it should look like.
BackwardChaining.drl
package com.sample import com.sample.model.Location; //declare any global variables here query isContainedIn( String x, String y ) Location( x, y; ) // we received the initial values from the rule "go1" and start to search inside the data stored on the engine or ( Location( z, y; ) and isContainedIn( x, z; ) ) //recursive call to the function that allows to search in a derivation tree structure end // rule to print inserted values rule "go" salience 10 when $s : String( ) then System.out.println( $s ); end // rule that invokes the recursive query function to search in our office desk graph and when the condition is true prints "key is in the envelop" rule "go1" when String( this == "go1" ) isContainedIn("key", "envelop"; ) then System.out.println( "key is in the envelop" ); end
3.3 Test Class Creation
On the jboss studio maven project, right click on com.sample package and create a new class named BackwardChainingTest. This class will be on charge of creating the engine session. Populate the engine with the graph data and invoke the rules.
BackwardChainingTest.java
package com.sample; import org.kie.api.KieServices; import org.kie.api.runtime.KieContainer; import org.kie.api.runtime.KieSession; import com.sample.model.Location; public class BackwardChainingTest { public static void main(String[] args) { try { KieServices ks = KieServices.Factory.get(); KieContainer kContainer = ks.getKieClasspathContainer(); KieSession kSession = kContainer.newKieSession("ksession-backward-chaining"); //drools session base on the xml configuration (kmodule.xml) //graph population kSession.insert(new Location("desk", "office")); kSession.insert(new Location("flashlight", "desk")); kSession.insert(new Location("envelop", "desk")); kSession.insert(new Location("key", "envelop")); kSession.insert("go1"); //invoke the rule that calls the query implentation of backward chaining kSession.fireAllRules(); //invoke all the rules } catch (Throwable t) { t.printStackTrace(); } } }
The output of the previous program is:
"go1 key is in the envelop"
This feature added to the drools engine “Backward chaining” is goal driven. That means, that the rule system has a condition that tries to satisfy. If this is not possible search for other possibles values and continue this operation until the condition satisfied.
4. Conclusions
On this example we saw how to create the backward chaining implementation and use it on a project example using jboss dev studio. Also we understood how the data is stored inside the drools engine and how to analyze this, by using graphs in order to get a more clear view.
If you want to read more about this feature please go: Backward Chaining
5. Download the Eclipse Project
This was a Drools backward chaining Example with Eclipse
You can download the full source code of this example here: drools-backward-chaining