[rules-users] Drools get value from stateful session

Stephen Masters stephen.masters at me.com
Sun Nov 10 13:54:35 EST 2013


That’s what I meant … if a Java fact is being used, then using .getClass() is a better approach than strings.

I wrote the method to support DRL declared types originally. If I create a declared type, then I know that the simple name will match the name of my declared type once Drools has converted it internally into a Java class. However, I can’t reference the class directly at Java compile time, so a class-based solution won’t work. Additionally, I don’t like to assume that the full class name with packages, that Drools will generate will remain the same across different versions. Whereas I do know that within my code, I won’t be creating 2 facts with the same simple name. 

Admittedly these days, I tend to avoid using declared types that are expected to exist outside of working memory, so there’s not much reason for me to use this String-based finder any more. In fact I wrote a new method earlier today, with a Class argument, as it is a better idea for most of my uses these days. :) 



On 10 Nov 2013, at 15:06, Wolfgang Laun <wolfgang.laun at gmail.com> wrote:

> I just see that I forgot to add "equals":
>    return object.getClass().equals( factClass );
> 
> @Stephen: I don't understand "if a Java fact is being used". Sure, the
> object filter's accept takes an object, so object.getClass() is the
> typical way to go. But why the detour into strings?
> ....StudentBean.class isn't much longer than the class name as a
> string ;-)
> 
> -W
> 
> On 10/11/2013, Stephen Masters <stephen.masters at me.com> wrote:
>> Good point Wolfgang … it’s a bit of my code that I provided. I tend to use
>> SimpleName to make it easy to find declared types as well as Java classes.
>> But if a Java fact is being used, then getClass() is a much better idea.
>> 
>> 
>> On 10 Nov 2013, at 07:27, Wolfgang Laun <wolfgang.laun at gmail.com> wrote:
>> 
>>> Digging a bit into Java's API always helps ;-) Note that
>>> Class.getSimpleName() really does what it says, as opposed to
>>> Class.getName().
>>> 
>>> It would be best to avoid the String representation:
>>> 
>>> Collection findFacts( StatefulKnowledgeSession session, Class factClass
>>> ){...
>>> public boolean accept( Object object ){
>>>  return object.getClass().( factClass );
>>> }...}
>>> 
>>> -W
>>> 
>>> On 10/11/2013, forsakendoll <forsakendoll at hotmail.com> wrote:
>>>> How can I get the value from a stateful session. As they say I can use
>>>> this:
>>>> 
>>>> protected Collection findFacts( final StatefulKnowledgeSession session,
>>>> final String factClass )
>>>> {
>>>>       ObjectFilter filter = new ObjectFilter()
>>>>       {
>>>>           @Override
>>>>           public boolean accept( Object object )
>>>>           {
>>>>               return object.getClass().getSimpleName().equals(
>>>> factClass
>>>> );
>>>>           }
>>>>       };
>>>> 
>>>>       Collection results = session.getObjects( filter );
>>>> 
>>>>       return results;
>>>> }
>>>> 
>>>> to get the value from the rules. Given the fact the code already red the
>>>> rules when I  tried this:
>>>> 
>>>> StatefulKnowledgeSession ksession =
>>>> aKnowledgeBase.newStatefulKnowledgeSession();
>>>> 
>>>> ksession.insert( aBean );
>>>> ksession.fireAllRules();
>>>> 
>>>> Collection result = findFacts( ksession,
>>>> "com.neu.als.thesis.db.beans.StudentBean" );
>>>> 
>>>> ksession.dispose();
>>>> for( Object test : result )
>>>> {
>>>>     System.out.println( test == null );
>>>>     System.out.println( test );
>>>> }
>>>> 
>>>> Nothing is printed. What is the proper way to get bean that I inserted
>>>> in
>>>> the rules?
>>>> 
>>>> 
>>>> 
>>>> 
>>>> --
>>>> View this message in context:
>>>> http://drools.46999.n3.nabble.com/Drools-get-value-from-stateful-session-tp4026712.html
>>>> Sent from the Drools: User forum mailing list archive at Nabble.com.
>>>> _______________________________________________
>>>> rules-users mailing list
>>>> rules-users at lists.jboss.org
>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>> 
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>> 
>> 
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>> 
> 
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users




More information about the rules-users mailing list