[rules-users] Drools get value from stateful session

Wolfgang Laun wolfgang.laun at gmail.com
Sun Nov 10 10:06:17 EST 2013


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
>



More information about the rules-users mailing list