drools-portlet

This blog will talk about how to create basic Liferay portlet with maven and integrate drools flow as a rule engine with Liferay portlet.
Drools 5 introduce the Business Logic integration Platform which provides a unified and integrated platform for Rules, Workflow and Event Processing.
To Create Liferay-Drools project please follow the below steps:

  1. To install maven plugins for eclipse refer the following url: http://m2eclipse.sonatype.org/installing-m2eclipse.html
    1. Select Help > Install New Software. This should display the "Install" dialog.
    2. Paste the Update Site URL (http://m2eclipse.sonatype.org/sites/m2e) into the field named "Work with:" and press Enter. Pressing Enter should cause Eclipse to update list of available plugins and components.
    3. Choose the component listed under m2eclipse: "Maven Integration for Eclipse (Required)".
    4. Click Next. Eclipse will then check to see if there are any issues which would prevent a successful installation.
    5. Click Next and agree to the terms of the Eclipse Public License v1.0.
    6. Click Finish to begin the installation process. Eclipse will then downloads and install the necessary components
    7. Once the installation process is finished, Eclipse will ask you if you want to restart the IDE. Sonatype strongly recommends that you restart your IDE after installing m2eclipse
  2. Open eclipse , then go to File --> Maven Project --> Un Check Create Simple Project (skip archetype selection) --> Next -- > type Liferay in filter.
    1. If you find archetype for Liferay then select that archetype and create your project or else please do the below step
    2. Click on Add Maven Archetype, and then enter following things it will download required artifact from https://oss.sonatype.org/content/groups/public
  3.   Select liferay-portlet-archetype --> Click on Next to generate project
  4.  Enter Group Id, Artifact Id, Version, Package then click finish.
    1. Please refer the following links for creating maven based Liferay portlet:
    2. http://www.liferay.com/web/mika.koivisto/blog/-/blogs/liferay-maven-sdk or http://www.liferay.com/web/thiago.moreira/blog/-/blogs/liferay-s-artifact-are-now-mavenized
  5.  To make this project as a drools project add following dependencies in pom.xml

<!-- Drools Core Jars Start -->       

    <dependency>

        <groupId>org.drools</groupId>

        <artifactId>drools-core</artifactId>

        <version>5.0.1</version>

        <type>jar</type>

    </dependency>

    <dependency>

        <groupId>org.drools</groupId>

        <artifactId>drools-compiler</artifactId>

        <version>5.0.1</version>

        <type>jar</type>

    </dependency>

    <dependency>

        <groupId>org.drools</groupId>

        <artifactId>drools-api</artifactId>

        <version>5.0.1</version>

        <type>jar</type>

    </dependency>

<!-- Drools Core Jars End -->

<!-- Decision Tables Start -->   

       

    <dependency>

        <groupId>org.drools</groupId>

        <artifactId>drools-decisiontables</artifactId>

        <version>5.1.1</version>

        <type>jar</type>

    </dependency>

    <dependency>

        <groupId>org.drools</groupId>

        <artifactId>drools-templates</artifactId>

        <version>5.1.1</version>

        <type>jar</type>

    </dependency>

    <dependency>

        <groupId>org.apache.servicemix.bundles</groupId>

        <artifactId>org.apache.servicemix.bundles.drools</artifactId>

        <version>5.1.1_1</version>

    </dependency>

<!-- Decision Tables End -->   

         <dependency>

        <groupId>com.thoughtworks.xstream</groupId>

        <artifactId>xstream</artifactId>

        <version>1.3.1</version>

        <type>jar</type>

    </dependency>

<!-- only if you want to compile with eclipse add the following -->   

    <dependency>

        <groupId>org.eclipse.jdt</groupId>

        <artifactId>core</artifactId>

        <version>3.4.2.v_883_R34x</version>

        <type>jar</type>

    </dependency>

    <dependency>

        <groupId>org.antlr.runtime</groupId>

        <artifactId>antlr-runtime</artifactId>

        <version>3.1.3</version>

        <type>jar</type>

    </dependency>

    <dependency>

        <groupId>org.mvel2</groupId>

        <artifactId>mvel2</artifactId>

        <version>2.0.16</version>

    </dependency>

 6. Add the following repository into pom.xml:

    <repositories>

        <repository>

            <id>JBossRepo</id>

            <url>http://repository.jboss.com/maven2/</url>

        </repository>

    </repositories> 

  7. Download drools runtime from the below URL:

      http://download.jboss.org/drools/release/5.1.1.34858.FINAL/drools-5.1.1-bin.zip 

8. Download eclipse plugin from the below URL:

     http://download.jboss.org/drools/release/5.1.1.34858.FINAL/drools-5.1.1-eclipse-all.zip

     After download unzip drools-5.1.1-eclipse.zip then copy plugins, features into eclipse plugins, features respectively. Restart the eclipse and change to drools perspective.

9. We can create rules by using .drl, .xsl, .rf approaches. In this example I used .xsl approach. So select New Decision Table, enter the name insurance.xsl.

Note: Please make sure that insurance.xsl file should be in src/main/resources otherwise you will get resource not found exception.

 



10. Create New --> Package --> com.liferay.insurance

                   New --> Class -->InsurancePortlet

       create New --> com.liferay.insurance.bean.Driver,com.liferay.insurance.bean.Policy

  Make thae changes in insurance.xsl

 

11. Paste the below code in processAction() of InsurancePortlet.

try {
           
    KnowledgeBase kbase = readKnowledgeBase();
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
                       
    Driver driver = new Driver();
    driver.setAge(age);
    driver.setLocationRiskProfile(locationRiskProfile);
    driver.setPriorClaims(priorClaims);
           
    Policy policy = new Policy();
    policy.setType(policyType);
           
    ksession.insert(driver);
    ksession.insert(policy);
    ksession.fireAllRules();
           
    policy.applyDiscount();
    actionRequest.setAttribute("ATTRIBUTE_NAME", name);
    actionRequest.setAttribute("BASE_PRICE",String.valueOf(policy.getBasePrice())); 
    actionRequest.setAttribute("DISCOUNT",String.valueOf(policy.getDiscount())); 
    actionRequest.setAttribute("FINAL_PRICE",String.valueOf(policy.getFinalPrice()));
           
    logger.close();
} catch (Throwable t) {
    t.printStackTrace();
        }

12. Paste the below code in jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<portlet:defineObjects />

<%
   String name = (String)request.getAttribute("ATTRIBUTE_NAME");
   String basePrice = (String)request.getAttribute("BASE_PRICE"); 
   String discount = (String)request.getAttribute("DISCOUNT"); 
   String finalPrice =(String)request.getAttribute("FINAL_PRICE"); 
%>


13. Place make necessary changes in Liferay-porlet.xml

<portlet>
        <portlet-name>portlet-drools</portlet-name>
        <icon>/icon.png</icon>
        <instanceable>true</instanceable>
        <header-portlet-css>/css/main.css</header-portlet-css>
</portlet>

14. Place the below code in Liferay-display.xml

<display>
    <category name="category.drools">
        <portlet id="portlet-drools" />       
    </category>
</display>

15. Run AS --> Run Configurations --> maven clean package --> deploy war file in server.

 

Download source code from https://drools-portlet.svn.sourceforge.net/svnroot/drools-portlet


 

 

 

 

 

Blogs
I am lost since 10 : how I am supposed to create Driver, Policy and InsurancePortlet ?
Those are normal java files.
1.New --> Package --> com.liferay
2.New --> Class --> com.liferay.portlet.InsurancePortlet extends GenericPortlet
3.New --> Class --> com.liferay.bean.Policy
4.New --> Class --> com.liferay.bean.Driver

then you can place your drools code in doView() or processAction() of InsurancePortlet.