1 2 Previous Next 15 Replies Latest reply on Mar 28, 2012 10:52 AM by Mauricio Salatino

    Drools Simple rule evaluation

    ous2k Newbie

      Hi,

      i'am trying to run this simple example using my own rules it seems that things all right but i get not thing in the console view, why he is not evaluating correctly the rule

       

      JAVA CODE

       




      KnowledgeBase eventKbase = readEventKnowledgeBase(); //read.dlr



      StatefulKnowledgeSession eventKsession = eventKbase.newStatefulKnowledgeSession();







      KnowledgeBase kbase = readKnowledgeBase();//read BPMN



      StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();







      Properties properties = new Properties();



      properties.put("drools.processInstanceManagerFactory",DefaultProcessInstanceManagerFactory.class.getName());



      properties.put("drools.processSignalManagerFactory"  ,DefaultSignalManagerFactory.class.getName());



      KnowledgeSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);







      ksession = kbase.newStatefulKnowledgeSession(config, null);



      ksession.addEventListener(new CustomProcessEventListener(eventKsession));



      KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");







      //insering Ticket



      Ticket ticket = new Ticket();



      ticket.setStatus("open");



      ticket.setContent("testing content");



      ticket.setOpeningDate(new Date());



      ticket.setIdTicket(1);







      ksession.insert(ticket);



      ksession.fireAllRules();

       

       

      DROOLS CODE

      import java.util.Date

      import java.util.List

      #declare any global variables here

      #declare Ticket

      #          @role(event)

      #end

       

       

      declare Server

         idTicket : int

         status : int

         openingDate : Date // percentage

         content: Date

      end

       

       

      dialect "mvel"

                rule "Check 5mn condition"

                when

                                       $ticket:Ticket(status=="open");

                then

                        System.out.println("New Ticket is opened");

                end

       

      Best Regards

        • 1. Re: Drools Simple rule evaluation
          Mauricio Salatino Master

          So, what do you mean with: "i get not thing in the console view"??

          If you insert the ticket object into that session that you are creating you will need to run the process in that same session, are you doing that?

          IF you are only trying to evaluate the rule, it should fire . are you sure that you are inserting the fact in the session which contains the rules?

          Did you check for errors in the rule file? For example I notice that you are not importing the Ticket definition into the DRL.. that definitely will cause troubles.

          Cheers

          • 2. Re: Drools Simple rule evaluation
            Mike schwery Novice

            Where is the code for readEventKnowledgeBase().

            I've got the line KnowledgeBase eventKbase = readEventKnowledgeBase() in a class, but eclipse doesn't know what it is so it won't compile.

            • 3. Re: Drools Simple rule evaluation
              ous2k Newbie

              -"i get not thing in the console view" :means no nothing writen in the console

              -i tryied to debug the project the run process is not failing

              -i have already imported the Ticket,  the DLR file syntaxicly correct as shown

              -do you mean the same session StatefulKnowledgeSession ksession variable??

               

              - are you sure that you are inserting the fact in the session?

              normaly in artificial intelligence the fact are objects that we will apply rules on them i have inserted it using   ksession.insert(ticket);

               

              #list any import classes here.

              import org.drools.runtime.process.WorkflowProcessInstance;

              import org.drools.runtime.process.ProcessContext

              import org.drools.event.process.ProcessStartedEvent;

              import com.ocana.workflow.dabviewticketing.server.model.Ticket;

              import java.util.Date

              import java.util.List

              #declare any global variables here

              #declare Ticket

              #          @role(event)

              #end

               

               

              declare Server

                 idTicket : int

                 status : int

                 openingDate : Date // percentage

                 content: Date

              end

               

               

              dialect "mvel"

                        rule "Check 5mn condition"

                        when

                                               $ticket:Ticket(status=="open");

                        then

                                System.out.println("New Ticket is opened");

                                            #$server.getVirtualizations().add($request.getVirtualization());

                        end

              • 4. Re: Drools Simple rule evaluation
                ous2k Newbie

                i've ommited it to make the post clear

                 

                 

                private static KnowledgeBase readEventKnowledgeBase() throws Exception {



                KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();


                kbuilder.add(ResourceFactory.newClassPathResource("OpenStateRules.drl"), ResourceType.DRL);


                KnowledgeBuilderErrors errors = kbuilder.getErrors();


                if (errors.size() > 0) {



                for (KnowledgeBuilderError error: errors) {




                System.err.println(error);



                }



                throw new IllegalArgumentException("Could not parse knowledge.");


                }


                KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();


                kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());


                return kbase;

                }
                • 5. Re: Drools Simple rule evaluation
                  Mauricio Salatino Master

                  I'm not sure what do you mean with this:

                  - are you sure that you are inserting the fact in the session?

                  normaly in artificial intelligence the fact are objects that we will apply rules on them i have inserted it using   ksession.insert(ticket);

                   

                  but.. yes.. that's not for artifitial inteligence in general.. this behavior is related with rules engines only.

                   

                  Some notes about your rule:

                      $ticket:Ticket(status=="open"); -> please remove the semicolon you don't need it

                   

                  Can you attach a KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession); -> to the session that was created with the kbase which contains the rules?

                  and share the output?

                  Cheers

                  • 6. Re: Drools Simple rule evaluation
                    ous2k Newbie

                    Hi,

                    in attachement you can find the log file asked

                    http://www.2shared.com/file/-6pKMnFz/test.html

                    Cheers

                    • 7. Re: Drools Simple rule evaluation
                      Mauricio Salatino Master

                      ouch.. did you do newConsoleLogger or newFileLogger? please use the console logger that doesn't print an extremely verbose XML

                      • 8. Re: Drools Simple rule evaluation
                        Mauricio Salatino Master

                        By the way, in your code you never show that you are starting a process.. but in the log a process is being executed..

                        Come on.. if you want help please make sure that we get the right information to analyze.. if not this is a nightmare

                        Cheers

                        • 9. Re: Drools Simple rule evaluation
                          ous2k Newbie

                          i' m sorry for making things hard to analyse , i think you are talking about   --> ksession.startProcess("openState",params);

                          this all the code

                           

                          package com.ocana.workflow.dabviewticketing.server.bo;

                           

                           

                          import java.util.Date;

                          import java.util.HashMap;

                          import java.util.Map;

                          import java.util.Properties;

                           

                           

                          import org.drools.KnowledgeBase;

                          import org.drools.KnowledgeBaseFactory;

                          import org.drools.builder.KnowledgeBuilder;

                          import org.drools.builder.KnowledgeBuilderError;

                          import org.drools.builder.KnowledgeBuilderErrors;

                          import org.drools.builder.KnowledgeBuilderFactory;

                          import org.drools.builder.ResourceType;

                          import org.drools.io.ResourceFactory;

                          import org.drools.logger.KnowledgeRuntimeLogger;

                          import org.drools.logger.KnowledgeRuntimeLoggerFactory;

                          import org.drools.runtime.KnowledgeSessionConfiguration;

                          import org.drools.runtime.StatefulKnowledgeSession;

                          import org.jbpm.process.instance.event.DefaultSignalManagerFactory;

                          import org.jbpm.process.instance.impl.DefaultProcessInstanceManagerFactory;

                           

                           

                          import com.ocana.workflow.dabviewticketing.server.model.SystemStatus;

                          import com.ocana.workflow.dabviewticketing.server.model.Ticket;

                          //import com.sample.CustomProcessEventListener;

                           

                           

                          //import com.sample.CustomProcessEventListener;

                           

                           

                          public class JBPMClient {

                           

                           

                           

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

                                              try {

                           

                                                        KnowledgeBase eventKbase = readEventKnowledgeBase(); //read.dlr

                                                        StatefulKnowledgeSession eventKsession = eventKbase.newStatefulKnowledgeSession();

                           

                                                        KnowledgeBase kbase = readKnowledgeBase();//read BPMN

                                                        StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

                           

                                                        Properties properties = new Properties();

                                                        properties.put("drools.processInstanceManagerFactory",DefaultProcessInstanceManagerFactory.class.getName());

                                                        properties.put("drools.processSignalManagerFactory"  ,DefaultSignalManagerFactory.class.getName());

                                                        KnowledgeSessionConfiguration config = KnowledgeBaseFactory.newKnowledgeSessionConfiguration(properties);

                           

                                                        ksession = kbase.newStatefulKnowledgeSession(config, null);

                                                        ksession.addEventListener(new CustomProcessEventListener(eventKsession));

                                                        KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");

                           

                                                        //insering Ticket

                                                        Ticket ticket = new Ticket();

                                                        ticket.setStatus("open");

                                                        ticket.setContent("testing content");

                                                        ticket.setOpeningDate(new Date());

                                                        ticket.setIdTicket(1);

                           

                                                        ksession.insert(ticket);

                                                        ksession.fireAllRules();

                                                        Map<String, Object> params = new HashMap<String, Object>();

                                                        params.put("ticket", ticket);

                                                        ksession.startProcess("openState",params);

                           

                                                        logger.close();

                                              } catch (Throwable t) {

                                                        t.printStackTrace();

                                              }

                                    }

                           

                                    private static KnowledgeBase readKnowledgeBase() throws Exception {

                                              KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

                                              kbuilder.add(ResourceFactory.newClassPathResource("StateMachine.bpmn"), ResourceType.BPMN2);

                                              return kbuilder.newKnowledgeBase();

                                    }

                           

                                    private static KnowledgeBase readEventKnowledgeBase() throws Exception {

                                              KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();

                                              kbuilder.add(ResourceFactory.newClassPathResource("OpenStateRules.drl"), ResourceType.DRL);

                                              KnowledgeBuilderErrors errors = kbuilder.getErrors();

                                              if (errors.size() > 0) {

                                                        for (KnowledgeBuilderError error: errors) {

                                                                  System.err.println(error);

                                                        }

                                                        throw new IllegalArgumentException("Could not parse knowledge.");

                                              }

                                              KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();

                                              kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());

                                              return kbase;

                                    }

                          }

                           

                           

                           

                          #created on: Mar 7, 2012

                          #package com.hemilco;

                          #list any import classes here.

                          import org.drools.runtime.process.WorkflowProcessInstance;

                          import org.drools.runtime.process.ProcessContext

                          import org.drools.event.process.ProcessStartedEvent;

                          import com.ocana.workflow.dabviewticketing.server.model.Ticket;

                          import java.util.Date

                          import java.util.List

                          #declare any global variables here

                          declare Ticket

                                    @role(event)

                          end

                           

                           

                          declare Server

                             idTicket : int

                             status : int

                             openingDate : Date // percentage

                             content: Date

                          end

                           

                           

                          dialect "mvel"

                                    rule "Check 5mn condition"

                                    when

                                                           $ticket:Ticket(status=="open");

                                    then

                                            System.out.println("New Ticket is opened")

                                                        #$server.getVirtualizations().add($request.getVirtualization());

                                    end

                           

                           

                          package com.ocana.workflow.dabviewticketing.server.model;

                           

                           

                          import java.io.Serializable;

                          import java.util.Date;

                           

                           

                          import javax.persistence.Column;

                          import javax.persistence.Entity;

                          import javax.persistence.GeneratedValue;

                          import javax.persistence.GenerationType;

                          import javax.persistence.Id;

                           

                           

                          @Entity(name="Ticket")

                          public class Ticket implements Serializable

                          {

                                    @Id

                                    @GeneratedValue(strategy = GenerationType.AUTO) 

                                    long idTicket;

                           

                                    @Column

                                    String status;

                           

                                    @Column

                                    Date openingDate;

                           

                                    @Column

                                    String content;

                           

                           

                                    public Ticket(){

                           

                                    }

                           

                           

                           

                           

                                    public long getIdTicket() {

                                              return idTicket;

                                    }

                           

                           

                           

                           

                           

                           

                                    public void setIdTicket(long idTicket) {

                                              this.idTicket = idTicket;

                                    }

                           

                           

                           

                           

                           

                           

                                    public String getStatus() {

                                              return status;

                                    }

                           

                           

                                    public void setStatus(String status) {

                                              this.status = status;

                                    }

                           

                           

                                    public Date getOpeningDate() {

                                              return openingDate;

                                    }

                           

                           

                                    public void setOpeningDate(Date openingDate) {

                                              this.openingDate = openingDate;

                                    }

                           

                           

                                    public String getContent() {

                                              return content;

                                    }

                           

                           

                                    public void setContent(String content) {

                                              this.content = content;

                                    }

                          }

                          • 10. Re: Drools Simple rule evaluation
                            ous2k Newbie

                            when i thougth about nightmare my mind was opened  i think it was session pb like you said @Mauricio it worked

                             

                            package com.ocana.workflow.dabviewticketing.server.bo;

                             

                             

                            import java.util.Date;

                            import java.util.HashMap;

                            import java.util.Map;

                            import java.util.Properties;

                             

                             

                            import org.drools.KnowledgeBase;

                            import org.drools.KnowledgeBaseFactory;

                            import org.drools.builder.KnowledgeBuilder;

                            import org.drools.builder.KnowledgeBuilderError;

                            import org.drools.builder.KnowledgeBuilderErrors;

                            import org.drools.builder.KnowledgeBuilderFactory;

                            import org.drools.builder.ResourceType;

                            import org.drools.io.ResourceFactory;

                            import org.drools.io.impl.ClassPathResource;

                            import org.drools.logger.KnowledgeRuntimeLogger;

                            import org.drools.logger.KnowledgeRuntimeLoggerFactory;

                            import org.drools.runtime.KnowledgeSessionConfiguration;

                            import org.drools.runtime.StatefulKnowledgeSession;

                            import org.jbpm.process.instance.event.DefaultSignalManagerFactory;

                            import org.jbpm.process.instance.impl.DefaultProcessInstanceManagerFactory;

                            import org.drools.KnowledgeBase;

                            import org.drools.builder.KnowledgeBuilder;

                            import org.drools.builder.KnowledgeBuilderError;

                            import org.drools.builder.KnowledgeBuilderFactory;

                            import org.drools.builder.ResourceType;

                            import org.drools.io.impl.ClassPathResource;

                            import org.drools.logger.KnowledgeRuntimeLoggerFactory;

                            import org.drools.runtime.StatefulKnowledgeSession;

                            import org.drools.runtime.help.QuartzHelper;

                            import org.junit.Test;

                             

                             

                            import com.ocana.workflow.dabviewticketing.server.model.SystemStatus;

                            import com.ocana.workflow.dabviewticketing.server.model.Ticket;

                             

                             

                            public class JBPMClient {

                             

                             

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

                                                try {

                             

                             

                                                          StatefulKnowledgeSession ksession = createKnowledgeSession();

                                                          KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);

                             

                             

                                                          Ticket ticket = new Ticket();

                                                          ticket.setStatus("open");

                                                          ticket.setContent("testing content");

                                                          ticket.setOpeningDate(new Date());

                                                          ticket.setIdTicket(1);

                                              ksession.insert(ticket);

                                              ksession.fireAllRules();

                             

                                                } catch (Throwable t) {

                                                          t.printStackTrace();

                                                }

                                      }

                             

                             

                                      private static StatefulKnowledgeSession createKnowledgeSession() {

                                                KnowledgeBuilder kbuilder = KnowledgeBuilderFactory

                                                                    .newKnowledgeBuilder();

                                                kbuilder.add(ResourceFactory.newClassPathResource("OpenStateRules.drl"),

                                                                    ResourceType.DRL);

                             

                             

                                                if (kbuilder.hasErrors()) {

                                                          if (kbuilder.getErrors().size() > 0) {

                                                                    for (KnowledgeBuilderError kerror : kbuilder.getErrors()) {

                                                                              System.err.println(kerror);

                                                                    }

                                                          }

                                                }

                             

                             

                                                KnowledgeBase kbase = kbuilder.newKnowledgeBase();

                                                StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();

                                                return ksession;

                                      }

                             

                             

                            }

                            • 11. Re: Drools Simple rule evaluation
                              Mike schwery Novice

                              I'm getting the following error.

                               

                              org.drools.RuntimeDroolsException: invalid package name

                                  at org.jbpm.compiler.ProcessBuilderImpl.buildProcess(ProcessBuilderImpl.java:173)

                                  at org.jbpm.compiler.ProcessBuilderImpl.addProcessFromXml(ProcessBuilderImpl.java:252)

                                  at org.drools.compiler.PackageBuilder.addProcessFromXml(PackageBuilder.java:564)

                                  at org.drools.compiler.PackageBuilder.addKnowledgeResource(PackageBuilder.java:608)

                                  at org.drools.builder.impl.KnowledgeBuilderImpl.add(KnowledgeBuilderImpl.java:37)

                                  at com.edpm.demo.JBPMClient.readKnowledgeBase(JBPMClient.java:72)

                                  at com.edpm.demo.JBPMClient.main(JBPMClient.java:38)

                              [5,9]: [ERR 102] Line 5:9 mismatched input 'DeliveryStatus' in rule "RuleFlow-Split-com.edpm.demo.LoanProcess-3-4-DROOLS_DEFAULT"

                              [12,9]: [ERR 102] Line 12:9 mismatched input 'DeliveryStatus' in rule "RuleFlow-Split-com.edpm.demo.LoanProcess-3-5-DROOLS_DEFAULT"

                              [0,0]: Parser returned a null Package

                              ProcessLoadError: unable to parse xml : Exception class org.drools.RuntimeDroolsException : invalid package name

                              java.lang.IllegalArgumentException: Could not parse knowledge.

                                  at org.drools.builder.impl.KnowledgeBuilderImpl.newKnowledgeBase(KnowledgeBuilderImpl.java:67)

                                  at com.edpm.demo.JBPMClient.readKnowledgeBase(JBPMClient.java:73)

                                  at com.edpm.demo.JBPMClient.main(JBPMClient.java:38)

                               

                              Rule

                              #created on: Mar 27, 2012

                              package com.edpm.demo

                               

                              import com.edpm.demo.Loan;

                              import org.drools.event.process.ProcessStartedEvent;

                               

                              declare ProcessStartedEvent

                              @role(event)

                              end

                               

                              rule "Spike alert"

                              when

                              Number (nbProcesses : intValue > 3)

                              from accumulate(

                                e: ProcessStartedEvent(processInstance.processId == "com.edpm.demo.LoanProcess")

                                over window:size(1h),

                                count(e))

                                loan:Loan(true)

                                then

                                loan.setProcessId(nbProcesses);

                                loan.setDeliveryStatus(false);

                                end

                               

                              rule "Spike alert1"

                              when

                              Number (nbProcesses : intValue < 3)

                              from accumulate(

                                e: ProcessStartedEvent(processInstance.processId == "com.edpm.demo.LoanProcess")

                                over window:size(1h),

                                count(e))

                                loan:Loan(false)

                                then

                                loan.setProcessId(nbProcesses);

                                loan.setDeliveryStatus(false);

                                end

                               

                              Test code

                              Loan loan = new Loan();

                              ksession.insert(loan);

                              ksession.fireAllRules();

                              Map<String, Object> params = new HashMap<String, Object>();

                              params.put("myLoan",loan);

                              ksession.startProcess("LoanProcess",params);

                               

                              BPM settings

                              Id                  com.ebpm.demo.LoanProcess

                              Name           LoanProcess

                              Package      org.drools.bpmn2

                              swimlanes   loan

                              variables      myLoan

                              • 12. Re: Drools Simple rule evaluation
                                ous2k Newbie

                                in attachment you can find the project rar file it contains all the files

                                i 've tried the example its working : just fix jdk library and and JBPMRuntime and run it

                                 

                                http://www.2shared.com/file/lJTeUxx7/customerselection.html

                                • 13. Re: Drools Simple rule evaluation
                                  Mauricio Salatino Master

                                  Is this question related with all the previous one? you should create a different thread if it's an entirely different problem.

                                  Cheers

                                  • 14. Re: Drools Simple rule evaluation
                                    ous2k Newbie

                                    normally  Mike schwery should a create new question there is no relation bettween, but i answered him

                                    1 2 Previous Next