javadroolsrulesdrools-flow

Comparing Object (facts) in working memory in drools


I am reading an excel sheet through DRT (rule template) and then converting it in to Object of a class through a static method in Java class. and also inserting it in to working memroty. In this way, working memory is filled up with all the facts from Excel sheet.

Now, I am sending facts through Java class in INSERT() through session. I need to match two similar object and return the matched objects from working memory. As for Example:

Read from Excel and inserted in to the Working memory.

Person(name == "Kumar", Age == 60, status == true);

Now in Java class, through session I am inserting the following object:

Person(name == "Kumar", Age == 60 );

and then I need to set Status = true by comparing the two Objects.

I need to write the rule that will compare both objects.

Please suggest some way.

Thanks Shorav


Solution

  • If we have to assume that it is possible that there are three or more "similar" facts it's best to do it like this:

    rule "find same name and age"
    when
        $p1: Person( $name: name, $age: age, status == false )
        $p2: Person( name == name, age == $age, status == false, this != $p1 )
        accumulate( $p: Person( name == name, age == $age, status == false );
                    $list: collectList( $p ) )
    then
        for( int i = 0; i < $list.size(); i++ ){
            Person p = (Person)$list.get(i);
            update( p ){ setStatus( true ) }
        }
    end