9 Replies Latest reply on Feb 2, 2009 6:29 PM by Ronald van Kuijk

    How to invoke rules(drools) from a jBPM

    ruchika garg Newbie

      Hello,

      I am using eclipse3.4, jbpm-jpdl-3.2.3, and drools-5.0.0.MR2-bin.
      I was trying to invoke rules from from my jbpm project.
      I am geting the below error:

      8:21:31,500 [main] INFO JbpmConfiguration : using jbpm configuration resource 'jbpm.cfg.xml'
      18:21:31,515 [main] DEBUG JbpmConfiguration : loading defaults in jbpm configuration
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'default.jbpm.context'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.hibernate.cfg.xml'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.business.calendar'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.default.modules'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.converter'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.action.types'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.node.types'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.parsers'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.varmapping'
      18:21:31,718 [main] DEBUG ObjectFactoryImpl : adding object info 'resource.mail.templates'
      18:21:31,734 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpm.byte.block.size'
      18:21:31,734 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpm.task.instance.factory'
      18:21:31,734 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpm.variable.resolver'
      18:21:31,734 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpm.mail.smtp.host'
      18:21:31,734 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpm.mail.address.resolver'
      18:21:31,734 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpm.mail.from.address'
      18:21:31,750 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpm.job.executor'
      18:21:31,750 [main] DEBUG JbpmConfiguration : loading specific configuration...
      18:21:31,750 [main] DEBUG ObjectFactoryImpl : adding object info 'jbpmConfiguration'
      18:21:31,765 [main] INFO StaleObjectLogConfigurer : stale object exceptions will be hidden from logging
      18:21:31,859 [main] DEBUG JpdlParser$JpdlEntityResolver : resolving schema reference publicId(null) systemId(http://jbpm.org/jpdl-3.2.xsd)
      18:21:31,859 [main] DEBUG JpdlParser$JpdlEntityResolver : providing input source to local 'jpdl-3.2.xsd' resource
      18:21:32,140 [main] DEBUG GraphElement : event 'process-start' on 'ProcessDefinition(JbpmDroolsExample)' for 'Token(/)'
      18:21:32,156 [main] DEBUG GraphElement : event 'before-signal' on 'StartState(start)' for 'Token(/)'
      18:21:32,156 [main] DEBUG GraphElement : event 'node-leave' on 'StartState(start)' for 'Token(/)'
      18:21:32,156 [main] DEBUG GraphElement : event 'transition' on 'Transition(148cc8c)' for 'Token(/)'
      18:21:32,156 [main] DEBUG GraphElement : event 'node-enter' on 'Node(ship)' for 'Token(/)'
      18:21:32,156 [main] DEBUG GraphElement : executing action 'action[fireRules]'
      18:21:32,156 [main] DEBUG Token : token[0] is locked by token[0]
      java.lang.NullPointerException
      at java.io.Reader.(Reader.java:61)
      at java.io.InputStreamReader.(InputStreamReader.java:55)
      at com.sample.action.FireRulesActionHandler.readRule(FireRulesActionHandler.java:44)
      at com.sample.action.FireRulesActionHandler.execute(FireRulesActionHandler.java:28)
      at org.jbpm.graph.def.Action.execute(Action.java:122)
      at org.jbpm.graph.def.GraphElement.executeAction(GraphElement.java:259)
      at org.jbpm.graph.def.GraphElement.executeActions(GraphElement.java:215)
      at org.jbpm.graph.def.GraphElement.fireAndPropagateEvent(GraphElement.java:185)
      at org.jbpm.graph.def.GraphElement.fireEvent(GraphElement.java:169)
      at org.jbpm.graph.def.Node.enter(Node.java:302)
      at org.jbpm.graph.def.Transition.take(Transition.java:151)
      at org.jbpm.graph.def.Node.leave(Node.java:393)
      at org.jbpm.graph.node.StartState.leave(StartState.java:70)
      at org.jbpm.graph.exe.Token.signal(Token.java:192)
      at org.jbpm.graph.exe.Token.signal(Token.java:140)
      at org.jbpm.graph.exe.ProcessInstance.signal(ProcessInstance.java:270)
      at com.sample.test.TestDroolsInvocation.main
      (TestDroolsInvocation.java:17)
      18:21:32,202 [main] DEBUG Token : token[0] is unlocked by token[0]
      18:21:32,218 [main] DEBUG GraphElement : event 'node-leave' on 'Node(ship)' for 'Token(/)'
      18:21:32,218 [main] DEBUG GraphElement : event 'transition' on 'Transition(6d084b)' for 'Token(/)'
      18:21:32,218 [main] DEBUG GraphElement : event 'node-enter' on 'EndState(end)' for 'Token(/)'
      18:21:32,218 [main] DEBUG GraphElement : event 'process-end' on 'ProcessDefinition(JbpmDroolsExample)' for 'Token(/)'
      18:21:32,218 [main] DEBUG GraphElement : event 'after-signal' on 'StartState(start)' for 'Token(/)'
      Ended!!!

      Even when I am hard coding the .drl path its not being picked.
      Any help will be greatly appreciated.

      Regards,
      Ruchika

        • 1. Re: How to invoke rules(drools) from a jBPM
          Mauricio Salatino Master

          Can you show us your code?
          this stack only show us the nullpointer exception because he coudn' t find the correct path to you drl file..


          com.sample.action.FireRulesActionHandler


          • 2. Re: How to invoke rules(drools) from a jBPM
            ruchika garg Newbie

            Below are the files:

            1. File for invoking jbpm process
            package com.sample.test;

            import org.jbpm.graph.def.ProcessDefinition;
            import org.jbpm.graph.exe.ProcessInstance;

            public class TestDroolsInvocation {

            /**
            * @param args
            */
            public static void main(String[] args) {

            ProcessDefinition definition = ProcessDefinition.parseXmlResource("JbpmDroolsExample/processdefinition.xml");

            ProcessInstance instance = new ProcessInstance(definition);

            instance.signal();

            //instance.signal();

            System.out.println("Ended!!!");
            }

            }

            2. File where rules are invoked(Actionhandler)

            package com.sample.action;

            import org.jbpm.graph.def.ActionHandler;
            import org.jbpm.graph.exe.ExecutionContext;
            import org.drools.RuleBase;
            import org.drools.RuleBaseFactory;
            import org.drools.WorkingMemory;
            import org.drools.compiler.DrlParser;
            import org.drools.compiler.PackageBuilder;
            import org.drools.lang.descr.PackageDescr;
            import org.drools.rule.Package;

            import java.io.*;

            public class FireRulesActionHandler implements ActionHandler {

            private static final long serialVersionUID = 1L;
            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 {
            try {

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

            //go !
            Message message = new Message();
            message.setMessage( "Hello World" );
            message.setStatus( Message.LikesCheese );
            workingMemory.insert( message );
            workingMemory.fireAllRules();

            } catch (Throwable t) {
            t.printStackTrace();
            }
            }
            private static RuleBase readRule() throws Exception {
            //read in the source
            Reader source = new InputStreamReader( FireRulesActionHandler.class.getResourceAsStream("E:/jBPM/workspace/InvokingDrools/src/main/java/com/sample/rules/CheeseRules.drl"));

            //Reader source = new InputStreamReader(FireRulesActionHandler.class.getResourceAsStream(ruleFile));
            DrlParser parser = new DrlParser();
            PackageDescr packageDescr = parser.parse(source);

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


            /*PackageBuilder builder = new PackageBuilder();

            builder.addPackageFromDrl( source );

            Package pkg = builder.getPackage();*/

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

            public static class Message {
            public static final int LikesCheese = 0;
            public static final int GOODBYE = 1;

            private String message;

            private int status;

            public String getMessage() {
            return this.message;
            }

            public void setMessage(String message) {
            this.message = message;
            }

            public int getStatus() {
            return this.status;
            }

            public void setStatus( int status ) {
            this.status = status;
            }
            }
            }

            3. CheeseRules.drl
            #created on: Nov 4, 2008
            package src.main.rules

            import com.sample.action.FireRulesActionHandler.Message;


            rule "Bob Likes Cheese"

            when
            m : Message( status == Message.LikesCheese, message : message )
            then
            System.out.println( message );
            m.setMessage( "Bob enjoys eating cheese" );
            m.setStatus( Message.GOODBYE );
            update( m );
            end

            rule "GoodBye"
            when
            Message( status == Message.GOODBYE, message : message )
            then
            System.out.println( message );
            end

            Everything is present in jbpm project.

            Regards,
            Ruchika

            • 3. Re: How to invoke rules(drools) from a jBPM
              Mauricio Salatino Master

              I have a posted example in my blog.. where I use the following line:

               builder.addPackageFromDrl(new InputStreamReader(this.getClass().getResourceAsStream("/categorizar.drl"));
              


              Look that I take the drl file from my classpath.
              Take a look at the post.. is in Spanish but you can translate it with:
              translate.google.com

              Here is the link to the post:
              http://salaboy.wordpress.com/2008/09/01/jugando-con-jbpm-12-jbpm-drools/

              • 4. Re: How to invoke rules(drools) from a jBPM
                ruchika garg Newbie

                what is the location of your .drl.

                The url is blocked for me :-(

                Regards,
                Ruchika

                • 5. Re: How to invoke rules(drools) from a jBPM
                  Mauricio Salatino Master

                  it must be in the class path.. in your case you need to be shure that the drl file is in your application classpath..
                  In my case I include it in my par archive because I deploy the process to a JBoss Server.

                  • 6. Re: How to invoke rules(drools) from a jBPM
                    ruchika garg Newbie

                    It may sound stupid but how to add .drl into classpath of my application. :-(

                    Regards,
                    Ruchika

                    • 7. Re: How to invoke rules(drools) from a jBPM
                      ruchika garg Newbie

                      sorry for my earlier post. It can be ignored.
                      I am able to fire rules.
                      Thanks so much. Your help is greatly appreciated.

                      Regards,
                      Ruchika

                      • 8. Re: How to invoke rules(drools) from a jBPM
                        Venkata Sai Muralidhar Baliwada Newbie

                        Hi,


                        I have roles to be given to each user for each UI. In my application, user dynamically updates the user roles in security.drl file which is located in the JBOSS default/conf folder. I need to refresh the updated version of the security.drl file without restarting the server and Application.


                        How JBOSS can be refreshed at runtime without restarting?


                        Please do the needful. Thanks in Advance.


                        Regards,
                        Sai.