0

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?

Anthon
  • 69,918
  • 32
  • 186
  • 246
Ysak
  • 2,601
  • 6
  • 29
  • 53
  • Providing the Java API you describe is a lot more work than the single rule Esteban described. Hopefully you need many more validation rules - for this single validation the use of an RBS (Drools) is an overkill. – laune Mar 19 '15 at 11:25

1 Answers1

1

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,

Esteban Aliverti
  • 6,259
  • 2
  • 19
  • 31
  • Let me try this, since I am new to Drools its takes time for me to understand, thanks for your help – Ysak Mar 19 '15 at 09:52
  • Can you direct me a good documentation where I can use an drools expression like above and validate an input dynamically. I am first time starting with drools and could not able to make a move – Ysak Mar 19 '15 at 10:33
  • [Drools documentation](http://docs.jboss.org/drools/release/6.2.0.CR4/drools-docs/html_single/) is a good starting point. A better source of information could be [Drools 5.x developer guide book](https://www.packtpub.com/application-development/drools-jboss-rules-5x-developer%E2%80%99s-guide). The update for Drools 6.x is already on its way. – Esteban Aliverti Mar 19 '15 at 11:00