此内容没有您所选择的语言版本。
7.4. Working Memory
7.4.1. Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Queries are used to retrieve fact sets based on patterns, as they are used in rules. Patterns may make use of optional parameters. Queries can be defined in the Knowledge Base, from where they are called up to return the matching results. While iterating over the result collection, any identifier bound in the query can be used to access the corresponding fact or fact field by calling the
get
method with the binding variable's name as its argument. If the binding refers to a fact object, its FactHandle can be retrieved by calling getFactHandle
, again with the variable's name as the parameter. Illustrated below is a Query example:
QueryResults results = ksession.getQueryResults( "my query", new Object[] { "string" } ); for ( QueryResultsRow row : results ) { System.out.println( row.get( "varName" ) ); }
QueryResults results =
ksession.getQueryResults( "my query", new Object[] { "string" } );
for ( QueryResultsRow row : results ) {
System.out.println( row.get( "varName" ) );
}
7.4.2. Live Queries 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Invoking queries and processing the results by iterating over the returned set is not a good way to monitor changes over time.
To alleviate this, JBoss Rules provides Live Queries, which have a listener attached instead of returning an iterable result set. These live queries stay open by creating a view and publishing change events for the contents of this view. To activate, start your query with parameters and listen to changes in the resulting view. The
dispose
method terminates the query and discontinues this reactive scenario.
7.4.3. ViewChangedEventListener Implementation Example 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
Note
For an example of Glazed Lists integration for live queries, visit http://blog.athico.com/2010/07/glazed-lists-examples-for-drools-live.html