Search code examples
droolsrule-engine

Creating rules having complex conditions using multiple data objects


Assume that I have two data objects Person and Address. Person object has the fields name and gender and Address object has the fields city and state. Now I want to take some action based on this condition :

when
    (person.name == 'jayram' && address.city == 'barhiya') || 
    (person.gender == 'M' && address.state == 'bihar')
then
    do something

How to accomplish this in drools rule file?


Solution

  • Maybe this should be the solution:

    package com.sample
    
    dialect "mvel"
    
    import com.sample.Person;
    import com.sample.Address;
    
    rule "Hello World"
        when
            person : Person( status == Message.HELLO)
            Address((person.name == 'jayram' && city == 'barhiya') ||
     (person.gender == 'M' && state == 'bihar'))
        then
            // Do something
    end