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(a)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(a)lists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users