I have inherited some Drools code and am trying to understand exactly how it is working. The code in question should go through a number of dates from two different sources, and for one source find the earliest and the other source find the latest.
The code is:
when
$flags: Properties()
// these properties contain key/value pairs with dates (as Strings)
$root: Map()
Map.Entry( key == "someRequest", $someRequestValue : value) from $root.entrySet()
$someRequestMap: Map() from $someRequestValue
// this map contains some timestamps as Strings
$earliestTimestamp: String() from accumulate(
$value : String(),
init( String found = null; ),
action( found = found == null? $value : ($value.compareTo(found) < 0? $value: found); ),
result( found )
)
$latestTimestamp: String() from accumulate(
$value : String(),
init( String found = null; ),
action( found = found == null? $value : ($value.compareTo(found) > 0? $value: found); ),
result( found )
)
then
log("earliestTimestamp: {}", $earliestTimestamp);
String latestDate = (String) $flags.get($latestTimestamp);
log("latestDate: {}", latestDate);
end
The $flags Properties are populated as so:
when
$flags: Properties()
$root: Map()
Map.Entry( key == "someRequest", $someRequestValue : value) from $root.entrySet()
$someRequestMap: Map() from $someRequestValue
Map.Entry( key == "timestamp", $timestampValue: value) from $someRequestMap.entrySet()
Map.Entry( key == "data", $dataValue: value) from $someRequestMap.entrySet()
$data: Map() from $dataValue
Map.Entry( key == "simple", $simpleValue: value) from $data.entrySet()
$simple: Map() from $simpleValue
Map.Entry( key == "somedate", $somedateValue: value) from $simple.entrySet()
then
$someRequestMap.remove("data");
update($root);
$flags.put($timestampValue, $somedateValue);
insert ($timestampValue);
end
And the JSON which is input to the rules is as so (with the timestamp strings):
{
"someRequest": {
"data": {
"simple": {
"somedate": "13-01-2016",
}
},
"timestamp": "2016-01-01 12:34:56.789"
}
}
I can see that it gets all the String facts available in working memory to get earliestTimestamp. I think that for latestTimestamp, since it is called in the RHS on $flags.get, it is actually finding all Strings in $flags. However if I insert log outputs inside the two accumulate action sections, all I see is the timestamp strings from working memory (I don't see the $flags strings).
I am comfortable enough using Drools but this bit of code has confused me in terms of how it works with the Properties.
Any info appreciated, thanks.