How to access to the consequence (RHS) of a rule from java?

情到浓时终转凉″ 提交于 2020-01-17 12:12:50

问题


I have rules with the following structure:

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

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());

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

Thanks in advance.


回答1:


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

You can obtain the RHS by calling

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

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.

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



来源:https://stackoverflow.com/questions/28596820/how-to-access-to-the-consequence-rhs-of-a-rule-from-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!