Version 3

    Things to be aware of when calling functions

     

    1. Calling a function from the LHS ie) when block requires the function call to be within an eval as per the following:

    package testing
    
    import java.lang.String
    
    function boolean doSomething(String s) {
         System.out.println(s);
         return true;
    }
    
    rule "testFunctionCallRHS"
    
         when
              ad$ : String()
              eval(doSomething("when do something"));
         then
              doSomething("then do something");
    end
    

     

    Having something like:

     

    package testing
    
    import java.lang.String
    
    function boolean doSomething(String s) {
         System.out.println(s);
         return true;
    }
    
    rule "testFunctionCallRHS"
    
         when
              ad$ : String()
              doSomething("when do something");
         then
              doSomething("then do something");
    end
    
    

     

    is likely to give the following error:

     

    org.drools.rule.InvalidRulePackage: Unable to resolve ObjectType 'doSomething'
    
         at org.drools.rule.Package.checkValidity(Package.java:370)
         at org.drools.reteoo.RuleBaseImpl.addPackage(RuleBaseImpl.java:247)
         at testing.FunctionTest.testFunction(FunctionTest.java:24)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
         at java.lang.reflect.Method.invoke(Unknown Source)
         at junit.framework.TestCase.runTest(TestCase.java:154)
         at junit.framework.TestCase.runBare(TestCase.java:127)
         at junit.framework.TestResult$1.protect(TestResult.java:106)
         at junit.framework.TestResult.runProtected(TestResult.java:124)
         at junit.framework.TestResult.run(TestResult.java:109)
         at junit.framework.TestCase.run(TestCase.java:118)
         at junit.framework.TestSuite.runTest(TestSuite.java:208)
         at junit.framework.TestSuite.run(TestSuite.java:203)
         at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:478)
         at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:344)
         at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    

     

    Note: this is by design and NOT A BUG- the LHS of a rule is designed for constraints and conjunctions over facts. The eval() is provided to allow arbitrary conditions that you can't do any other way, but they are not optimal. If you can think about adjusting your object model to suit, if performance is important (or course, for a lot of people it may not be an issue).