Collecting all items of nested collections / accumulate nested collections
by Claes, Kenny [JRDBE - Non J&J]
I have a question regarding creating a list of items that are all in
nested collections of my main object.
A high level overview of the model is like this:
public Level0 {
private List<Level1> level1List;
}
public Level1 {
private List<Level2> level1List;
}
public Level2 {
private Date occurence;
}
Now, I add objects of type 0 in the working memory and for each of those
objects, I want to find the latest occurrence of a certain Level2,
regardless of which level1 they belong to.
If there's only 2 levels and I want to find the latest occurrence of
Level2's with a certain name, I can write:
rule "latest level 1"
when
$l0: Level0()
$date: Date() from accumulate (Level1(name in ("name1", "name2"),
$occurence: occurence) from $l0.level1List, maxDate($occurence))
then
// register fact that latest occurrence of name1/name2 level1 was
on $date
end
Now, with 3 levels, since I obviously can't replace $l0.level1List with
$l0.level1List.level2List, is there a way to create an intermediate list
that will contain all Level2 objects? No filtering needs to take place
on Level1, but if there's a way to do that too, that would be nice.
What I currently have is this:
rule "latest level 2"
when
$l0: Level0()
$l1: Level1() from $l1.level1List
$l2List: List(size > 0) from collect( Level2(name in (("name1",
"name2")) from $l1.level2List )
$date: PartialDate() from accumulate (Level2( $occurence:
occurence) from $l2List, maxDate($occurence))
then
// register fact that latest occurrence of name1/name2 level2 was
on $date
end
But this does not work as it treats each Level1 seperately: it creates a
"latest occurrence" for all accumulated level2 facts within a level1,
instead of one for the entire set of level 2's 'owned' by the level0
object.
Any suggestions?