[rules-users] Query in stateless knowledge session returns no results

Mark Proctor mproctor at codehaus.org
Sun Jul 31 12:47:54 EDT 2011


On 31/07/2011 17:45, Mark Proctor wrote:
> On 31/07/2011 17:25, Heijink, Hank wrote:
>> Aha! I missed the line about the dispose. That makes a lot of sense. 
>> What I don't understand is why the example 3.37 in the docs would 
>> work. If I understand what you're saying, it shouldn't.
> It should work, as long as the query is executed as part of the same 
> execute batch. We have unit tests f or that too somewhere.
ok I see the problem. Your rules will not fire until the all the command 
shave been executed. i.e. the implicit fireAllRules() is once all 
commands have been executed. Which means the query will be invoked 
before your rule fires to insert the object.

Instead you need to add the FireAllRules command before executing the query.

Mark
>
> mark
>>
>> Thanks!
>> Hank
>>
>> Sent from my iPhone
>>
>> On Jul 30, 2011, at 8:27 AM, "Wolfgang Laun" <wolfgang.laun at gmail.com 
>> <mailto:wolfgang.laun at gmail.com>> wrote:
>>
>>> StatelessKnowledgeSession.execute(...) executes the command(s) and 
>>> /finally calls dispose()/ on the session. Use a stateful session or 
>>> a global (filled with a low-salience rule) or  -  simply use the 
>>> getObjects command.
>>> -W
>>>
>>> On 29 July 2011 22:47, Heijink, Hank <heijink at audible.com 
>>> <mailto:heijink at audible.com>> wrote:
>>>
>>>     Hi all,
>>>
>>>     I'm new to Drools, so please excuse me if I'm asking about the
>>>     obvious--it's certainly not obvious to me. The problem is this:
>>>     I use a stateless knowledge session where a list of facts is
>>>     inserted. The rules that fire create new facts, and after all
>>>     the rules have fired, I'd like to obtain a list of all the facts
>>>     (the old and the new). The best way seemed to use a query. I'm
>>>     using Drools 5.1 on Linux.
>>>     This is part of my .drl file (without the imports):
>>>
>>>     rule "create badge"
>>>     when
>>>            Event ( eventType == EventType.SOME_EVENT_TYPE )
>>>            not BadgeState ( identifier == "badge" )
>>>     then
>>>            insert( new BadgeState("badge") );
>>>     end
>>>
>>>     query "all badges"
>>>            aBadge : BadgeState()
>>>     end
>>>
>>>     This is the Java code:
>>>
>>>                    StatelessKnowledgeSession ksession =
>>>     StatsKnowledgeBase.getKnowledgeBase().newStatelessKnowledgeSession();
>>>
>>>                    // Create a list of share events
>>>                    ArrayList<Event> events = new ArrayList<Event>();
>>>                    Date now = new Date();
>>>                    MobileDevice aDevice = new
>>>     MobileDevice("uniqueId", "deviceType");
>>>                    for (int i = 0; i < 5; i++) {
>>>                            Event anEvent = new Event.Builder(now,
>>>     aDevice, "aCustomer", EventType.SOME_EVENT_TYPE).build();
>>>                            events.add(anEvent);
>>>                    }
>>>
>>>                    // Create the query for the badges
>>>                    List<Command> commands = new ArrayList<Command>();
>>>                  
>>>      commands.add(CommandFactory.newInsertElements(events));
>>>                commands.add(CommandFactory.newQuery("all badges",
>>>     "all badges"));
>>>
>>>                    // Feed the events into Drools
>>>                    KnowledgeRuntimeLogger logger =
>>>     KnowledgeRuntimeLoggerFactory.newConsoleLogger(ksession);
>>>                    ExecutionResults results = ksession.execute(
>>>     CommandFactory.newBatchExecution(commands) );
>>>                    NativeQueryResults queryResults =
>>>     (NativeQueryResults)results.getValue("all badges");
>>>
>>>     // At this point, queryResults is empty.
>>>
>>>                    logger.close();
>>>
>>>                    // Get a list of badges
>>>                    List<BadgeState> badges = new
>>>     ArrayList<BadgeState>();
>>>                    for (Iterator<QueryResultsRow> i =
>>>     queryResults.iterator(); i.hasNext(); ) {
>>>                            QueryResultsRow result = i.next();
>>>                            BadgeState obj =
>>>     (BadgeState)result.get("aBadge");
>>>                            badges.add(obj);
>>>                    }
>>>
>>>     The logger shows me that the BadgeState object is created, but
>>>     the query returns an empty list. I've searched the
>>>     documentation, which suggests that I'm doing it right
>>>     (http://drools.herod.net/drools-expert/html/ch03.html#d0e1956,
>>>     example 3.37), the archives of this mailinglist, and the web, so
>>>     far without success.
>>>
>>>     Clearly, I'm missing something, and I have the nagging feeling
>>>     that it's something simple...
>>>
>>>     Any help is much appreciated!
>>>
>>>     Best,
>>>     Hank
>>>
>>>
>>>     _______________________________________________
>>>     rules-users mailing list
>>>     rules-users at lists.jboss.org <mailto:rules-users at lists.jboss.org>
>>>     https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org <mailto: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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20110731/0fb2e471/attachment.html 


More information about the rules-users mailing list