Search code examples
javafunctionjpacalldrools

How to call a Java function in drools?


I want to call a Java function from a Utils class which calls a JPA Repository method for retrieving a custom object.

I want to call this function from a Drools decision table. Now, this simple function is giving Null Pointer Exception and I have already lost several hours on this.

I have a Functions field declared under "Import" section of the decision table, and there I have declared a simple function which calls this particular Java function with the repository method.

Can you provide me with a solution?


Solution

  • Here is an example of calling java method from drools. setLocalTax() method is available from ItemCity class. Using the '$item' object we can invoke.

    package droolsexample
    
    // list any import classes here.
    import com.sample.ItemCity;
    import java.math.BigDecimal;
    
    // declare any global variables here
    dialect "java"
    rule "Pune Medicine Item"
    
       when
          $item : ItemCity (purchaseCity == ItemCity.City.PUNE,
                           typeofItem == ItemCity.Type.MEDICINES)
    
       then
          BigDecimal tax = new BigDecimal(0.0);
          $item.setLocalTax(tax.multiply($item.getSellPrice()));
    end