如何从Java访问规则的结果(RHS)? [英] How to access to the consequence (RHS) of a rule from java?

查看:88
本文介绍了如何从Java访问规则的结果(RHS)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的规则具有以下结构:

I have rules with the following structure:

rule "ins b"
when
    A()
then 
    B $b = new B();
    $b.setName("hello");
    insert($b);
end

我想获取规则添加到的对象(及其属性)

我可以使用以下代码获取LHS对象:

I want to get the objects (and its attributes) that the rule adds to the working memory.
I'm able to get the LHS objects with the following code:

RuleImpl ri = (RuleImpl) kSession.getKieBase().getRule("com.sample", "ins b");
System.out.println("L: " + ri.getLhs());
Pattern rce = (Pattern) ri.getLhs().getChildren().get( 0 );
System.out.println("L: " + rce.getConstraints());

但是我找不到类似的东西来获取规则的RHS。

But I can't find something similar to obtain the RHS of the rule.

我想这样做是因为我试图根据规则自动生成查询。
根据上述规则,我想生成类似以下内容的

I want to do that because I'm trying to generate querys "automatically" based on rules. From the above rule I want to generate something like:

query howToGetA() 
  @Abductive( target = A.class ) 
  $b := B( name == "hello" ) 
end

谢谢。

推荐答案

为什么要这样做?您可以在编写规则代码时知道插入的对象。

Why do you want to do this? You "know" the object that's inserted as you write the code of the rule.

您可以通过调用

RuleImpl ri = ...
Consequence cons = ri.getConsequence();

但是,我认为这不会给您对象(及其属性)规则将添加到工作记忆中。结果就是代码(以某种形式),并且您不会在该代码中找到 object ,因为该对象是在运行时构建的。

However, I don't think that this will give you "the objects (and its attributes) that the rule adds to the working memory". The consequence is code (in some form), and you won't find an object in that code, as the object is built at runtime.

此外,RuleImpl(以及接口Consequence及其实现)都是非公共API的一部分,如有更改,恕不另行通知。

Moreover, RuleImpl (and the interface Consequence and its implementations) are all part of the non-public API and subject to change without notice.

这篇关于如何从Java访问规则的结果(RHS)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆