Viraj
Viraj

Reputation: 5381

Drools - Modify specific object in a list?

I want to know how to modify a object in a list . I tried following, but it gives a error.

when
        Category( $bookList :  books ) 
        UserProfile( profile == UserProfile.STUDENT )
        $book : Book( student == true )
        $category : Category( books contains $group )
then
        modify( $category.books[$book] ) { setEligible(true) }
end

Book.setEligible is the method i need to call. But i need to call this for selected object in Cagegory.books list. What am i doing wrong ? can anyone help ?

Thanks !

Upvotes: 1

Views: 2338

Answers (3)

Jeremy
Jeremy

Reputation: 3548

I am modifying an item in a nested collection like this... [using "from"]

rule "4G complete" 
salience -1
when 
    $tr: TopTowerResult()
    $ptncascade: PtnCascade() from $tr.cascadeList
    Timestamp() from $ptncascade.cascadeFact.actual4g
then 
    $ptncascade.getCascadeFact().setComplete4g(true);
    $ptncascade.getCascadeFact().setEstimate4g("Completed");
end


Explanation -
This line matches every instance of PtnCascade in $tr.cascadeList
You could add an additional pattern here to limit the selected items from the list.

$ptncascade: PtnCascade() from $tr.cascadeList

The next line operates on every instance of PtnCascade() that was matched.

Timestamp() from $ptncascade.cascadeFact.actual4g

The consequence also operates on the specific PtnCascade that was matched in the list -

$ptncascade.getCascadeFact().setComplete4g(true);

Upvotes: 0

salaboy
salaboy

Reputation: 4133

You also need to make sure that Book is a fact.. that means that you are inserting that Fact into the Ksession.. What is the error that you are getting? which version of drools are you using?

Upvotes: 0

Esteban Aliverti
Esteban Aliverti

Reputation: 6322

First thing: Are you sure you want to include 2 different Category patterns in your rule? If you have 2 different categories, you may end up with 4 executions of that rule.

Second thing, if you want to modify the book, why don't you just do:

modify($book) {
    setEligible(true)
}

Hope it helps,

Upvotes: 1

Related Questions