Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Spring+Drools Integration Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring+Drools Integration

    Hello,

    I have been attempting to work with Drools & Spring and have mostly met with frustration.

    On going thru many sources, I could know some info on how to integrate both of them. Well, I have been trying to read the DRL file and display the message available in DRL file.

    I have followed using a RuleBaseBeanFactory (mentioned somewhere on internet) and defining my Spring beans in applicationContext XML file. No error message is coming for me on the application UI, but I could notice that I am unable to read the DRL file as it is displaying some null value which I have already set in DRL file. (For your info, I am only using DRL file, and not any RuleFlow or DSL file.)

    My code involves a POJO, RuleBaseBeanFactory to read the DRL file, applicationContext XML file, a simple JSP. Very simple.

    Can anyone help me out to solve the issue? If I am not clear, let me know. Any sample with Drools and Spring is highly appreciated. [I have gone thru many resources but am unable to get a clear picture on integrating both of them]. Please do the needful.

    Thanks,
    Mercury

  • #2
    You can use the Spring/JSR-94 integration classes like here.

    Comment


    • #3
      Drools + Spring IOC Integration (Abhishek Jha)

      Hey Guys Please use the following way to integrate Drools in your Spring Framework.

      There are two java classes
      1) Member.java
      --------------------------------------------------------------------------
      package com.hp;

      public class Member {
      private String type;
      private int age;
      private int fees;

      public String getType() {
      return type;
      }
      public void setType(String type) {
      this.type = type;
      }
      public int getAge() {
      return age;
      }
      public void setAge(int age) {
      this.age = age;
      }
      public int getFees() {
      return fees;
      }
      public void setFees(int fees) {
      this.fees = fees;
      }
      }
      --------------------------------------------------------------------------

      2) MemberRulesClient.java
      --------------------------------------------------------------------------
      package com.hp;

      import com.hp.Member;
      import org.drools.KnowledgeBase;
      import org.drools.KnowledgeBaseFactory;
      import org.drools.builder.KnowledgeBuilder;
      import org.drools.builder.KnowledgeBuilderFactory;
      import org.drools.builder.ResourceType;
      import org.drools.io.Resource;
      import org.drools.runtime.StatefulKnowledgeSession;
      import org.springframework.beans.factory.BeanFactory;
      import org.springframework.beans.factory.xml.XmlBeanFacto ry;
      import org.springframework.core.io.FileSystemResource;

      public class MemberRulesClient {
      private Resource resourceFile;
      private ResourceType resourceType;

      public Resource getResourceFile() {
      return resourceFile;
      }

      public void setResourceFile(Resource resourceFile) {
      this.resourceFile = resourceFile;
      }

      public ResourceType getResourceType() {
      return resourceType;
      }

      public void setResourceType(ResourceType resourceType) {
      this.resourceType = resourceType;
      }

      public static final void main(String[] args) {

      BeanFactory beanFactory = new XmlBeanFactory(new FileSystemResource(
      "src/main/config/bean-config.xml"));
      MemberRulesClient client = (MemberRulesClient) beanFactory.getBean("MemberRulesClientBean");
      KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
      kbuilder.add(client.resourceFile, client.resourceType);
      KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
      kbase.addKnowledgePackages(kbuilder.getKnowledgePa ckages());
      StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
      Member member = new Member();
      member.setType(args[0].toLowerCase());
      member.setAge(Integer.parseInt(args[1]));
      ksession.insert(member);
      ksession.fireAllRules();
      System.out.println(" Provider Fess : " + (member.getFees())
      + " For Member Type : " + member.getType() + " having age : "
      + member.getAge());


      }
      }
      --------------------------------------------------------------------------

      Corresponding Context Descriptor file for Spring is -->

      bean-config.xml
      --------------------------------------------------------------------------
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:drools="http://drools.org/schema/drools-spring"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >

      <bean id="resType" class="org.drools.io.ResourceFactory" factory-method="newClassPathResource">
      <constructor-arg index="0" type="java.lang.String" value="MemberRules.drl" />
      </bean>

      <bean id="resTypeBean" class="org.drools.builder.ResourceType" factory-method="addResourceTypeToRegistry">
      <constructor-arg index="0" type="java.lang.String" value="DRL" />
      <constructor-arg index="1" type="java.lang.String" value="Drools Rule Language" />
      <constructor-arg index="2" type="java.lang.String" value="drl" />
      </bean>

      <!-- <bean id="MemberRulesClientBean"
      class="com.hp.MemberRulesClient">
      <property name="resourceFile" ref="resType" />
      <property name="resourceType" ref="resTypeBean" />
      </bean>
      -->

      <bean id="MemberRulesClientBean" class="com.hp.MemberRulesClient" autowire="byType">
      </bean>

      </beans>
      --------------------------------------------------------------------------

      and the required DRL rule file is -->
      MemberRules.drl
      --------------------------------------------------------------------------
      package com.hp

      import com.hp.Member;

      rule "Member Type - Bronze"
      no-loop
      when
      m : Member(type == "bronze");
      then
      if(m.getAge() >=1 && m.getAge() <= 30){
      m.setFees(30);
      update(m);
      }else if(m.getAge() >= 31 && m.getAge() <=59){
      m.setFees(25);
      update(m);
      }else{
      m.setFees(20);
      update(m);
      }
      end

      rule "Member Type - Silver"
      no-loop
      when
      m : Member(type == "silver");
      then
      if(m.getAge() >=1 && m.getAge() <= 30){
      m.setFees(25);
      update(m);
      }else if(m.getAge() >= 31 && m.getAge() <=59){
      m.setFees(20);
      update(m);
      }else{
      m.setFees(15);
      update(m);
      }
      end

      rule "Member Type - Gold"
      no-loop
      when
      m : Member(type == "gold");
      then
      if(m.getAge() >=1 && m.getAge() <= 30){
      m.setFees(20);
      update(m);
      }else if(m.getAge() >= 31 && m.getAge() <=59){
      m.setFees(15);
      update(m);
      }else{
      m.setFees(10);
      update(m);
      }
      end
      --------------------------------------------------------------------------
      these above mentioned files (bean.xml and DRL file) must be stored in config folder of your project.
      This will simply integrate Drools With Spring IOC Framework.

      Cheers,
      Abhishek Jha

      Comment

      Working...
      X