javajbossdroolsrule-enginejboss-rules

What is wrong with Drools rule?


I have a long time experience working with JBOSS Drools. current project I'm working with uses Drools 4.

here is the one of the rules I have in project

rule "testcase"
   salience 300
    when
        $item : Item(itemTypeId in (Item.ITEM_TYPE_A, Item.ITEM_TYPE_B), targetId < 0)
        not Tegret(targetId == $item.targetId)
    then
        retract ($item);
end

idea is to retract such Items from working memory that does not have associated Target object. I'm testing it with these objects in working memory:

Item {itemId=7305, itemTYpeId=ITEM_TYPE_A, targetId=-1023} Target {targetId = -1023}

in this case rule should not fire, but it does. after lot of experimenting I have found this strange behaviour:

rule "testcase2" fires, while "testcase1" does not.

rule "testcase1"
   salience 300
    when        
        $item : Item(itemTypeId in (Item.ITEM_TYPE_A, Item.ITEM_TYPE_B), targetId < 0)
        Tegret(targetId == $item.targetId)
    then
        ...
end

rule "testcase2"
   salience 300
    when        
        $item : Item(itemTypeId in (Item.ITEM_TYPE_A, Item.ITEM_TYPE_B), targetId < 0)
        Tegret($ti:targetId)
        eval($ti == $item.targetId)

    then
        ...
end

so what is going wrong here? I'm running "testcase1" and "testcase2" separately on different program runs.


Solution

  • Well, after lots of experimenting and fiddling here is what I found: I don't know if is it bug or not, but this was happening due to type differences between "linking" fields.

    Tagret.targetId is int primitive. Item.targetId is long primitive.

    rewriting rule this way solved the problem:

    rule "testcase"
       salience 300
        when
            $item : Item(itemTypeId in (Item.ITEM_TYPE_A, Item.ITEM_TYPE_B), targetId < 0)
            not Tegret(targetId == ((long)$item.targetId))
        then
            retract ($item);
    end
    

    re-read documentation to find any hints about field "compatibility" restriction. no result