[rules-users] Stateless session commands and ExecutionResults (5.5.0.Final)

Stephen Masters stephen.masters at me.com
Wed Dec 11 11:51:34 EST 2013


Hmm … so the use of batch execution instead of InsertObjectCommand improved things. However I have noticed that although rules are activating as expected, and facts are being inserted, my queries don’t seem to get any results. For instance with the following code:

Based on a stateless session:

        List<Command> cmds = new ArrayList<Command>();
        cmds.add(CommandFactory.newInsert(new IbanValidationRequest(iban), "request"));
        cmds.add(CommandFactory.newQuery("annotations", "annotations"));

        ExecutionResults results = ksession.execute(CommandFactory.newBatchExecution(cmds));
        
        QueryResults queryResults = ( QueryResults ) results.getValue( "annotations" );
        System.out.println("Found [" + queryResults.size() + "]");

Based on a stateful session:

        statefulSession.insert(new IbanValidationRequest(iban));
        statefulSession.fireAllRules();
        QueryResults queryResults = statefulSession.getQueryResults("annotations");
        System.out.println("Found [" + queryResults.size() + "]");

What I’m finding is that if I invoke a query against a stateful session, I get a result. However, with stateless sessions, I am examining the ExecutionResults, and finding that they do contain a reference to an “annotations” QueryResults. However that collection of query results is empty.

btw - I’m also noticing that the code example in the user guide for batch executions with queries doesn’t use the current syntax for adding a query command. I’m guessing this syntax changed without an associated update to the user guide.

Steve




On 11 Dec 2013, at 11:49, Wolfgang Laun <wolfgang.laun at gmail.com> wrote:

> The mean thing is that the code
>   org.drools.command.CommandFactory
> uses the generic interface
>   org.drools.command.Command<T>
> for all commands without specifying the generic parameter with the many
> newXyz methods. If it were written well, it would tell you what to
> expect from the command execution, since interface
>   org.drools.runtime.CommandExecutor
> has the generic
>   <T> T execute(Command<T> command)
> 
> So you dig around in the code and may detect that ksession.execute(
> cmd ) with cmd being made by newInsertElements(...) returns a
> Collection<FactHandle>. Mighty helpful.
> 
> Fixing the Experts would help a little, avoiding utter stupefaction.
> 
> Cheers
> -W
> 
> 
> 
> On 11/12/2013, Stephen Masters <stephen.masters at me.com> wrote:
>> Thanks for that, Wolfgang. I have been working on something else for a few
>> days, but just switched over to batch execution, and that is giving me some
>> appropriate results (my tests are passing).
>> 
>> Which leads me to the question ...
>> 
>> Given that Example 3.4.1 in the Drools 5.5.0.Final user guide does not
>> compile, is that because there is a bug in the implementation of command
>> execution? Or is it a bug in the example. i.e.
>> The results should just not be assigned to anything?
>> The results (an ArrayList containing the facts which were just inserted)
>> should be assigned to a List of Object?
>> The example should just be removed from the user guide, as it is no longer a
>> valid/recommended approach?
>> 
>> Anybody with thoughts on that? Assuming that the answer is a documentation
>> fix, I’ll be happy to correct it appropriately and submit a pull request.
>> 
>> Cheers,
>> 
>> Steve
>> 
>> 
>> 
>> 
>> On 4 Dec 2013, at 12:24, Wolfgang Laun <wolfgang.laun at gmail.com> wrote:
>> 
>>> I think you have to put your insert command into a list
>>> List<Command<?>> cmds = new ArrayList();
>>> and pass this via batch execution
>>> CommandFactory.newBatchExecution( cmds )
>>> 
>>> -W
>>> 
>>> On 04/12/2013, Stephen Masters <stephen.masters at me.com> wrote:
>>>> Hi folks,
>>>> 
>>>> Can anyone explain how stateless session execution results should work?
>>>> Working through the docs and tracing the code, it’s getting a bit
>>>> confusing.
>>>> 
>>>> Firstly, according to the user manual, one should be able to insert a
>>>> list
>>>> of facts as a Command, and get an instance of ExecutionResults back,
>>>> which
>>>> can then be queried. Here’s the example code:
>>>> StatelessKnowledgeSession ksession =
>>>> kbase.newStatelessKnowledgeSession();
>>>> 
>>>> Command cmd = CommandFactory.newInsertElements( Arrays.asList( Object[]
>>>> {
>>>> 
>>>>                 new Cheese( "stilton" ),
>>>> 
>>>>                 new Cheese( "brie" ),
>>>> 
>>>>                 new Cheese( "cheddar" ),
>>>> 
>>>>             });
>>>> 
>>>> ExecutionResults bresults = ksession.execute( cmd );
>>>> 
>>>> That doesn’t compile, so I fixed it as follows:
>>>> 
>>>>       Command cmd = CommandFactory.newInsertElements( Arrays.asList(
>>>>               new Cheese( "stilton" ),
>>>>               new Cheese( "brie" ),
>>>>               new Cheese( "cheddar" )
>>>>        ));
>>>>       ExecutionResults bresults = ksession.execute( cmd );
>>>> 
>>>> This does now compile, but running it throws a ClassCastException:
>>>> 	java.lang.ClassCastException: java.util.ArrayList cannot be cast to
>>>> org.drools.runtime.ExecutionResults
>>>> 
>>>> Taking a look through the Drools code, it would appear that Command has
>>>> a
>>>> generic type, which in the case of an InsertElementsCommand is:
>>>> GenericCommand<Collection<FactHandle>>
>>>> 
>>>> In turn, the execute command in StatelessKnowledgeSessionImpl casts its
>>>> result to that generic type:
>>>> 	public <T> T execute(Command<T> command) {
>>>> 		Object o = ((GenericCommand) command).execute( context );
>>>> 		...
>>>> 		return (T) o;
>>>> 
>>>> So the ClassCastException is because the execute method returns an
>>>> ArrayList
>>>> of FactHandle instead of an ExecutionResults.
>>>> 
>>>> I’m assuming that I must have got the wrong end of the stick somewhere,
>>>> so
>>>> what should I be doing to get an ExecutionResults from a stateless
>>>> session,
>>>> which I can then query?
>>>> 
>>>> Cheers,
>>>> 
>>>> Steve
>>> 
>>> _______________________________________________
>>> 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/20131211/b9cbf93e/attachment-0001.html 


More information about the rules-users mailing list