0

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

Sabir Khan
  • 9,826
  • 7
  • 45
  • 98
Kumar
  • 955
  • 5
  • 20
  • 50
  • What would be a "similar" object? One that has the same name and age? Should status be set to true in both facts? Also, if there is a third, fourth, fifth,... "similar" fact with same name and age, should they all be set to true? – laune Sep 15 '14 at 10:01
  • Yes, if all the passing facts i.e. Age and Name is matching, I need to set that object status to true. ad return it in to java classes. – Kumar Sep 15 '14 at 10:10
  • if more than one facts would be passed; all should set to be true/false. – Kumar Sep 15 '14 at 10:11
  • instead of generating rules from Excel sheet through DRT; I am writing rules for comparing the objects and finding appropriate Object from Working memory. – Kumar Sep 15 '14 at 10:12

1 Answers1

1

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
laune
  • 31,114
  • 3
  • 29
  • 42
  • I will be implementing it. BTW, How can I read excel and then insert the data as a fact. Thanks for your time. – Kumar Sep 15 '14 at 12:31
  • IF you dig into the Drools source, you should find code for reading an Excel file: org/drools/decisiontable/parser/xls/ExcelParser.java, org/drools/decisiontable/SpreadsheetCompiler.java etc. - It's based on Listeners, which may not be the straightforward approach you might have in mind, but it should contain references to some useful classes. – laune Sep 15 '14 at 19:39