6 Replies Latest reply on Jul 20, 2007 4:20 AM by gavin.king

    Drools (JBoss Rules) Hibernate Query and EntityManager

    damianharvey

      Drools rules can use Hibernate queries to perform checks against the database. They look something like this:

      $cheese : Cheese() from session.createQuery("select cheese from Products where ...").setProperties( {"key1" => "value1", "key2" => "value2" }).list()

      How does the session get passed to Drools and is it possible to pass Seam's EntityManager in a similar fashion? Has anyone used this feature of Drools with Seam?

      Thanks,

      Damian.

        • 1. Re: Drools (JBoss Rules) Hibernate Query and EntityManager
          gavin.king

          The current version of Seam in CVS has a Drools GlobalResolver that lets you use any Seam context variable as a drools global, completely transparently :-)

          • 2. Re: Drools (JBoss Rules) Hibernate Query and EntityManager
            ellenzhao

            @damianharvey:

            You can tie Seam and Drools like this:

            In your conversation bean, you say:

            ...
            @In
            private RuleBase myRuleBase; // you must have already this rule base defined in your component.xml. Please refer to Seam manual.
            
            ...
            
            public void eatCheese(){
             // you can also use stateful rule session here. Please refer to Drools' manual
             StatelessSession ruleSession = myRuleBase.newStatelessSession();
             ruleSession.setGlobal("entityManager", entityManager);
             ruleSession.setGlobal("anotherResource", anotherResource);
             ...
             ruleSession.execute(cheeseList);
            }
            
            ...
            
            


            In your .drl file, you can define the entityManager as global so all your rules in that rule package can share this single entityManager. You can ask entityManager to fetch the data you want at LHS and do things like entityManager.flush() at RHS.


            Enjoy!
            Ellen

            • 3. Re: Drools (JBoss Rules) Hibernate Query and EntityManager
              damianharvey

              Thanks to you both for your replies. I'll try it out.

              Cheers,

              Damian.

              • 4. Re: Drools (JBoss Rules) Hibernate Query and EntityManager
                ellenzhao

                Forgot to mention it's better not to mess your rule definition with things like persistence code or rendering code. It's better to do session.setGlobal("cheeseList", cheeseList); , let your rule engine use or modify the cheeseList. The modification is automatically visible to the conversation bean which passed this cheeseList to your rule session. The conversation bean should be the central controller/coordinator for all kinds of "engines" (stuff like jsf can be viewed as rendering engine, stuff like hibernate/JPA persisting engine, jBPM process engine, Drools rule engine....)and let your "engines" to manipulate the state space of your entity's. That said, inside each kind of "engines", it's best to see only entities around and a handle to the central controller, but not any dependencies to any other kind of "engine".

                Ideally, the conversation bean should not manipulate the state space of any entity, but just pass entities around (read only!) to different engines. But this is not practical for applications that are not complex enough.

                • 5. Re: Drools (JBoss Rules) Hibernate Query and EntityManager
                  damianharvey

                  Thanks Ellen. So would you advise not to use Database queries from within a rule then? In this case the query would be against a list of prices (possibly thousands). Is it better to read these in the Bean and pass the whole lot to Drools?

                  Regards,

                  Damian.

                  • 6. Re: Drools (JBoss Rules) Hibernate Query and EntityManager
                    gavin.king

                    Ask Mark Proctor that question in the Drools forum. I know he has been working on this.