问题
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