Chapter 86. Decision engine queries and live queries You can use queries with the decision engine to retrieve fact sets based on fact patterns as they are used in rules. The patterns might also use optional parameters. To use queries with the decision engine, you add the query definitions in DRL files and then obtain the matching results in your application code. While a query iterates over a result collection, you can use any identifier that is bound to the query to access the corresponding fact or fact field by calling the get() method with the binding variable name as the argument. If the binding refers to a fact object, you can retrieve the fact handle by calling getFactHandle() with the variable name as the parameter. Example query definition in a DRL file Example application code to obtain and iterate over query results QueryResults results = ksession.getQueryResults( "people under the age of 21" ); System.out.println( "we have " + results.size() + " people under the age of 21" ); System.out.println( "These people are under the age of 21:" ); for ( QueryResultsRow row : results ) { Person person = ( Person ) row.get( "person" ); System.out.println( person.getName() + "\n" ); } Invoking queries and processing the results by iterating over the returned set can be difficult when you are monitoring changes over time. To alleviate this difficulty with ongoing queries, Red Hat Process Automation Manager provides live queries , which use an attached listener for change events instead of returning an iterable result set. Live queries remain open by creating a view and publishing change events for the contents of this view. To activate a live query, start your query with parameters and monitor changes in the resulting view. You can use the dispose() method to terminate the query and discontinue this reactive scenario. Example query definition in a DRL file Example application code with an event listener and a live query final List updated = new ArrayList(); final List removed = new ArrayList(); final List added = new ArrayList(); ViewChangedEventListener listener = new ViewChangedEventListener() { public void rowUpdated(Row row) { updated.add( row.get( "USDprice" ) ); } public void rowRemoved(Row row) { removed.add( row.get( "USDprice" ) ); } public void rowAdded(Row row) { added.add( row.get( "USDprice" ) ); } }; // Open the live query: LiveQuery query = ksession.openLiveQuery( "colors", new Object[] { "red", "blue" }, listener ); ... ... // Terminate the live query: query.dispose()

Commands:
query "people under the age of 21" USDperson : Person( age < 21 ) end
QueryResults results = ksession.getQueryResults( "people under the age of 21" ); System.out.println( "we have " + results.size() + " people under the age of 21" ); System.out.println( "These people are under the age of 21:" ); for ( QueryResultsRow row : results ) { Person person = ( Person ) row.get( "person" ); System.out.println( person.getName() + "\n" ); }
query colors(String USDcolor1, String USDcolor2) TShirt(mainColor = USDcolor1, secondColor = USDcolor2, USDprice: manufactureCost) end
final List updated = new ArrayList(); final List removed = new ArrayList(); final List added = new ArrayList(); ViewChangedEventListener listener = new ViewChangedEventListener() { public void rowUpdated(Row row) { updated.add( row.get( "USDprice" ) ); } public void rowRemoved(Row row) { removed.add( row.get( "USDprice" ) ); } public void rowAdded(Row row) { added.add( row.get( "USDprice" ) ); } }; // Open the live query: LiveQuery query = ksession.openLiveQuery( "colors", new Object[] { "red", "blue" }, listener ); // Terminate the live query: query.dispose()