第 83 章 决策引擎查询和实时查询


您可以将查询与决策引擎搭配使用,根据规则中使用的事实模式检索事实集。模式也可以使用可选参数。

要将查询与决策引擎搭配使用,您可以在 DRL 文件中添加查询定义,然后在应用程序代码中获取匹配的结果。虽然查询迭代结果集合,但您可以使用绑定到查询的任何标识符访问对应的事实或事实字段,方法是使用绑定变量名称作为参数调用 get () 方法来访问对应的事实或事实字段。如果绑定引用事实对象,您可以通过调用 getFactHandle (),并将变量名称用作参数来检索事实处理。

DRL 文件中的查询定义示例

query "people under the age of 21"
    $person : Person( age < 21 )
end
Copy to Clipboard Toggle word wrap

获取和迭代查询结果的应用程序代码示例

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" );
}
Copy to Clipboard Toggle word wrap

当您监控随着时间的变化时,通过迭代返回的集合调用查询和处理结果可能比较困难。为了降低持续查询的这一难度,Red Hat Process Automation Manager 提供了 实时查询,它使用附加的监听程序更改事件,而不是返回可迭代的结果集。通过为此视图的内容创建视图并发布更改事件,实时查询保持打开。

要激活实时查询,请使用参数启动查询,并在结果视图中监控更改。您可以使用 dispose () 方法终止查询并停用这种被动场景。

DRL 文件中的查询定义示例

query colors(String $color1, String $color2)
    TShirt(mainColor = $color1, secondColor = $color2, $price: manufactureCost)
end
Copy to Clipboard Toggle word wrap

带有事件监听器和实时查询的应用程序代码示例

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( "$price" ) );
 }

 public void rowRemoved(Row row) {
  removed.add( row.get( "$price" ) );
 }

 public void rowAdded(Row row) {
  added.add( row.get( "$price" ) );
 }
};

// Open the live query:
LiveQuery query = ksession.openLiveQuery( "colors",
                                          new Object[] { "red", "blue" },
                                          listener );
...
...

// Terminate the live query:
query.dispose()
Copy to Clipboard Toggle word wrap

返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat