0
votes

I have simple rule in which, I want to use external java function which returns integer value. And I want to use this return value in rule LHS for comparison.

rule "testRuleBTW"
 when
    $cfd        : FactsData()
    condition   : RuleData( ageCondition != null, 
                                    ageCondition.value1 != null, 
                                    ageCondition.value2 != null, 
                                    "BTW" == ageCondition.conditionCode, 
                                    $returnValue : Utils.foo($cfd.dateOfBirth, ageCondition.ageUnitCode) //Want to use this value in next c
                                    ageCondition.value1 <= $returnValue, $returnValue <= ageCondition.value2)
then
//do something

But getting below error

java.lang.RuntimeException: Error while creating KieBase[Message [id=1, level=ERROR, path=com/test/rules/ageRule.drl, line=13, column=0 text=[ERR 101] Line 13:42 no viable alternative at input '$cfd' in rule "testRuleBTW"], Message [id=2, level=ERROR, path=com/test/rules/ageRule.drll, line=0, column=0 text=Parser returned a null Package]] at org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:433) at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:587) at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:558) at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1183) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1099) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:989) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4913) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5223) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:752) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:728) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:734) at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:952) at org.apache.catalina.startup.HostConfig$DeployWar.run(HostConfig.java:1823) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) at java.util.concurrent.FutureTask.run(FutureTask.java:262) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at java.lang.Thread.run(Thread.java:745)

2

2 Answers

1
votes

You can't bind a variable to an arbitrary expression.

Go one small step further and implement

class Util {

    public static boolean fooInRange( dob Date, uc, UnitCode, ac AgeCondition ){
        if( ac.value1 == null ||
            ac.value2 == null ) return false;
        Value value = foo( dob, uc );
        return ac.value <= value && value <= ac.value2;
    }

     // ...
}

And use this in your rule.

0
votes

After lot of googling I managed to do this as below.

rule "testRuleBTW"
 when
    $cfd        : FactsData()
    condition  : RuleData( ageCondition != null, 
                                    ageCondition.value1 != null, 
                                    ageCondition.value2 != null, 
                                    "BTW" == ageCondition.conditionCode)
$ageValue  : Integer() from Utils.foo($cfd.dateOfBirth, condition.ageCondition.unitCode)
$ctc        : FactsDataCondition(value1 <= $ageValue && $ageValue <= value2) from condition.ageCondition
then
//do something