Search code examples
javadrools

How to print unmatched parameters in Drools


In the following Drools file, I join two queries in the when expression, and print matched results.

import com.demo.drools.*;

rule "demo"
    when
        $book: BlockTrade()
        $buys : Trade(type=="buy") from $book.trades
        $sells : Trade(type=="sell", $buys.id==id, 
                                     $buys.price==price, 
                                     $buys.trader==trader) from $book.trades
    then
        System.out.println("buys:  " + $buys);
        System.out.println("sells: " + $sells);
    end

It works okay, but I want to log all unmatched trades with an unmatch reason.

For example:

Trade id=1 doesn't match because $buys.type="both" doesn't match any trades in $buys or $sells

// or

Trade id=2 doesn't match because $buys.price=50, and $buys.trader="John" doesn't match any $sells

How can it be implemented?


Solution

  • See this other answer. If you want to log the unmatched trades, you will need to create the rules for that.

    Hope it helps,