Keyword "From" allows for the retrieval of facts from outside the rule engine, whereas "memberOf" works upon fact attributes.
 
So these would (should!) be other alternatives:-
 
when
    $p : Property() from (em.createNativeQuery("select PROP from SAMPLEOBJECT").getResultList()))
    SampleObject(properties contains $p )
then
    ...
end
 
when
    $p : Property() from (em.createNativeQuery("select PROP from SAMPLEOBJECT").getResultList()))
    SampleObject($p memberOf properties )
then
    ...
end

With kind regards,
 
Mike

From: rules-users-bounces@lists.jboss.org [mailto:rules-users-bounces@lists.jboss.org] On Behalf Of David Siefert
Sent: 07 July 2008 16:21
To: Rules Users List
Subject: Re: [rules-users] Calling EntityManager from DRL--DRL Parser attemptsto parse sql query instead of passing as string tocreateNativeQuery method

Thanks Wilson for the fast reply.
 
I did this and almost got it working (mostly due to my newb knowledge of writing rules).  However, I tinkered with the rule a bit further, and it turns out i got my original rule post working by simply using memberOf instead of from!  Not sure why that is....
 
-David

On Mon, Jul 7, 2008 at 10:45 AM, Wilson O Ojwang <wojwang@alcatel-lucent.com> wrote:
David,

Here is what I did to make this work.

This example assume that you have a Person POJO and you want to query all persons with the age lest than or equal to 20.

public class DroolsTest {

    public static final void main(String[] args) {
        try {
           
            //load up the rulebase
            RuleBase ruleBase = readRule();
            WorkingMemory workingMemory = ruleBase.newStatefulSession();
       
         // Start EntityManagerFactory
            EntityManagerFactory emf =
                    Persistence.createEntityManagerFactory("persons");

            // Second unit of work
            EntityManager newEm = emf.createEntityManager();
            EntityTransaction newTx = newEm.getTransaction();
            newTx.begin();
           
            workingMemory.setGlobal("entity",newEm);
            ArrayList<Person> presult = new ArrayList<Person>();
            workingMemory.setGlobal("presult",presult);
            workingMemory.fireAllRules();
           
            System.out.println("=============List Results=============");
            for ( Person p : presult ){
                 System.out.println("Person: \"" + p.getFirstName() +
                         "\", " + p.getLastName() +           
                                     "\", " + p.getAge());
            }
            System.out.println("=============End List Results=====\n\n");        
            newTx.commit();
            newEm.close();
         // Shutting down the application
            emf.close();
         
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }
   /**
     * Please note that this is the "low level" rule assembly API.
     */
    private static RuleBase readRule() throws Exception {
        //read in the source
        Reader source = new InputStreamReader( DroolsTest.class.getResourceAsStream( "/Sample.drl" ) );     
        PackageBuilder builder = new PackageBuilder(); 
        builder.addPackageFromDrl( source );

        //get the compiled package (which is serializable)
        Package pkg = builder.getPackage();
       
        //add the package to a rulebase (deploy the rule package).
        RuleBase ruleBase = RuleBaseFactory.newRuleBase();
        ruleBase.addPackage( pkg );
        return ruleBase;
    }
}

==============
Here is the rule sample.drl

package com.sample
 
import com.sample.DroolsTest.Message;
import com.sample.Person;
import javax.persistence.EntityManager;
global javax.persistence.EntityManager entity;
global java.util.ArrayList presult;


rule "age"
    when
        $p: Person() from entity.createQuery("from Person p where p.age <= 20").getResultList();
    then
        presult.add($p);
end

==========

Wilson


David Siefert wrote:
Hello,
 
I am trying to use the EntityManager within a rule.  I pass the EntityManager instance in from my test (currently just a mock of the interface using EasyMock) using setGlobal("em", entitymanager);
 
so in my drls, I have:
 
<code>
global javax.persistence.EntityManager em;
</code>
 
And then I use this in my LHS of the rule with a from:
 
<code>
rule "query-db"
  when
    SampleObject(property from (em.createNativeQuery("select PROP from SAMPLEOBJECT").getResultList()))
  then
    System.out.println("Got an object!");
end
</code>
 
However, when I run my JUnit test, I get an error saying "unexpected token 'PROP'".  So it looks like the Drools parser is trying to interpret what is in my string.  Is there a special way to pass a string to the Java operation?  Or can I simply not do this?  How would I be able to get a result set from the db to use in pattern matching?  The rule is dependent upon data from a table to make a decision.
 
Thanks,
 
David Siefert

_______________________________________________ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users

_______________________________________________
rules-users mailing list
rules-users@lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users