droolskie-serverjava-annotations

Drools externalize condition


In my organization we have decided to use KieServer for running drools. We are not using kieworkbench, this is because we need more control over the deployment and should be aligned with related application. The ask is to externalize the condition check.

for example in rule like below, the check for "2008" will be stored in a Database

when 
    $customer:Customer(membersince <= "2008")
then
    $customer.setOfferPercent("50%")

I have figured out a way to get this value 2008 from the database/inmemory cache implementation and change it as below. This will allow my operations/business to change such values without a deployment in the kie server and reduce lot of efforts

when 
    $customer:Customer(membersince <= cache.get("Member_Since_Elite"))
then
    $customer.setOfferPercent("50%")

My Question is, Is there any other way to declare as global and auto populate the values when its changed in the database. I am hoping to do something like below by using annotations, and need some help.

declare Properties
    member_since_elite : String @Property("member_since_elite")
end
when 
    $customer:Customer(membersince <= Properties.member_since_elite )
then
    $customer.setOfferPercent("50%")

This way it will help me to auto inject the values from database/cache when ever its value changes.


Solution

  • You can create one (or more) classes just for holding parameters, e.g., Limit, with fields such as memberSinceElite. Then you can formulate the rule

    when
        Limit( $mse: memberSinceElite )
        $c: Customer( membersince <= $mse, offerPercent != 50 )
    then
        modify( $customer ){ setOfferPercent( 50 ) }
    

    This permits dynamic update during an ongoing session. If you can autopopulate a Java object, you can insert the Limit object just before you start the session. Updating depends on how you can relate a DB update to the session.

    If you don't need that, you can use a global. Make sure to set it before inserting any facts. It'll last for the session. Autopopulate as before.

    (You can also use templating to insert the values into DRL code. This will need regeneration and compilation to become effective.)