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

Heijink, Hank heijink at audible.com
Fri Jul 29 16:47:09 EDT 2011


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





More information about the rules-users mailing list