0 Replies Latest reply on Dec 17, 2010 4:45 AM by Shobhit Tyagi

    Integration with Drools

    Shobhit Tyagi Master

      Hi, I am trying to invoke the rules with jBPM. My class definition is following :

       

      package com.jboss.dvd.seam;

       

      import org.drools.PackageIntegrationException;
      import org.drools.RuleBase;
      import org.drools.RuleBaseFactory;
      import org.drools.RuleIntegrationException;
      import org.drools.WorkingMemory;
      import org.drools.compiler.DrlParser;
      import org.drools.compiler.DroolsParserException;
      import org.drools.compiler.PackageBuilder;
      import org.drools.lang.descr.PackageDescr;
      import org.drools.reteoo.*;
      import org.drools.rule.InvalidPatternException;
      import org.drools.rule.Package;
      import org.jbpm.context.exe.ContextInstance;
      import org.jbpm.graph.def.ActionHandler;
      import org.jbpm.graph.exe.ExecutionContext;
      import java.io.*;
      import java.util.Iterator;
      import java.util.List;

       

      public class FireRulesActionHandler implements ActionHandler {

       

        private static final long serialVersionUID = 1L;

       

        public List objectNames;
        public static String ruleFile;

       

        /**
         * The FireRulesActionHandler gets variables from the Instance, and asserts
         * them into the Rules Engine and invokes the rules.
         */
        public void execute(ExecutionContext executionContext) throws Exception {

       

          // load up the rulebase
          RuleBase ruleBase = readRule();
          WorkingMemory workingMemory = ruleBase.newWorkingMemory();

       

          // load the data
          Object object = null;
          System.out.println(objectNames.toString());

       

          Iterator iter = objectNames.iterator();
          String objectName = "";
          ContextInstance ci = executionContext.getContextInstance();
          while (iter.hasNext()) {
            objectName = (String) iter.next();
            object = ci.getVariable(objectName);
            System.out.println("object name is: " + objectName);
            // assert the object into the rules engine
            workingMemory.assertObject(object);
          }
          // assert the contextInstance so that it may be used to set results
          workingMemory.assertObject(ci);
          workingMemory.fireAllRules();

       

        }

       

        /**
         * Please note that this is the "low level" rule assembly API.
         */
        private static RuleBase readRule() throws IOException, DroolsParserException, RuleIntegrationException, PackageIntegrationException, InvalidPatternException {
          // read in the source
          Reader reader = new InputStreamReader(FireRulesActionHandler.class.getResourceAsStream(ruleFile));
          DrlParser parser = new DrlParser();
          PackageDescr packageDescr = parser.parse(reader);

       

          // pre build the package
          PackageBuilder builder = new PackageBuilder();
          builder.addPackage(packageDescr);
          Package pkg = builder.getPackage();

       

          // add the package to a rulebase
          RuleBase ruleBase = RuleBaseFactory.newRuleBase();
          ruleBase.addPackage(pkg);
          return ruleBase;

       

        }
      }

       

      1. First thing is that, it is not able to recognize the function newWorkingMemory().

      2. Secondly the function assertObject(Object) is also reported undefined.

       

      Please Help.

      Thanks