Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

spring boot - How to return the value from Cosequence of drl file to java

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...