Ysak
Ysak

Reputation: 2765

Custom rules definition using drools

Can I use Drools for a definition like below

> pay_type in(NB,CC) and (brand in (VISA,MASTER) or bank in(HDFC,CITI))

operations - and , or
keyword - in
tags - pay_type , brand , bank 

I need to provide a java api with 3 inputs validateAgainstRule('NB', 'VISA', 'AXIS') should return me true; and validateAgainstRule('NB', 'AMEX', 'AXIS') should return me false;

Can I achieve this using drools?

Upvotes: 0

Views: 685

Answers (1)

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

First thing you need if you want to use Drools is a class model. A simplistic class model for your scenario could be:

public class Payment {
    private String payType;
    private String brand;
    private String bank;

    private boolean valid = false; //the initial state of a payment is invalid

    //getters an setters, of course
    //...
}

Then, for your use case, you need just a single rule:

rule "validate payment"
when
    $p: Payment(
        payType in ("NB", "CC") &&
        (brand in ("VISA", "MASTER") || bank in ("HDFC", "CITI"))
    )
then
   $p.setValid(true);
end 

Given that, in your scenario, a payment is either valid or not, you don't need to create a different rule to mark payment objects as invalid.

Hope it helps,

Upvotes: 1

Related Questions