问题
If rule matches then i am executing the java function in consequence of drl and i want that result of function in consequence to be returned back to the function which calls to execute the drl.
回答1:
You need to make sure that the side effect of the rule is passed back to the caller. How you do this depends on your particular rule set and the objects you pass into it.
One solution, for example, might be to change a value on an object you have in working memory.
rule "Example rule changing a value"
when
$purchase: Purchase( includesAlcohol == true )
then
$purchase.shouldIncludeExciseTax(true); // set a value on the object in working memory.
end
Another alternative, considered a not-so-good practice by some, would be to use a global. This was pretty common back in the Drools 5.0 and earlier days.
global Discounts appliedDiscounts;
rule "Example rule setting a property on a global"
when
DiscountCode( value == "FOOBAR~DISCOUNT" )
then
appliedDiscounts.addDiscount( 0.3 );
end
Basically whatever you do on the right hand side needs to be visible to the caller. In the first example, you should still have a reference to the object you inserted into working memory when you called the rules. So once the rules finish executing, you'll be able to take that reference and query values from it.
The second example similarly uses an object passed into the rules, but as a global instead of being in working memory. There are subtle differences here between the two when using stateful session, but they're both leveraging the same basic principles.
来源:https://stackoverflow.com/questions/60504675/how-to-return-the-value-from-cosequence-of-drl-file-to-java