Reputation: 13
I am new to programming and Drools specifically and trying to learn the workings of the instrument as I progress through the documentation.
As of this moment I am struggling to understand logic of defining source_pattern for the accumulate
function: in the examples provided on the official website, there are two variants for source_pattern - using [] or () on the Rule rule "Average profit".
'/orderItems[ order == $order, $cost : cost, $price : price ]';
'/orderItems( order == $order, $cost : cost, $price : price )'. What are the differences between both of them? [] constraints are more or less documented while application of () in accumulate I was not able to find =(
Link to documentation I am referencing - https://docs.drools.org/latestFinal/drools-docs/drools/language-reference/index.html
To learn I tried to use accumulate function with my data but encountered unexpected behavior:
Rule itself:
rule "Return message key if it fits the rule"
when
accumulate(
/incomingMessages[ messageProperty.screeningFinancialMessage.messageType == "pacs.008.002", $key : messageProperty.key ];
collectList($key)
)
then
// Create a new ScreeningRuleData object
ScreeningRuleData newRuleData = new ScreeningRuleData(
drools.getRule().getName(), // ruleId
true // messagePropertyMatched
);
controlSet.add(newRuleData);
//System.out.println("Hits: " + $hitList.toString());
System.out.println("Items: " + $key);
end
/incomingMessages:
public class IncomingMessageUnit implements RuleUnitData {
private final DataStore<IncomingMessage> incomingMessages;
....
IncomingMessage:
public class IncomingMessage {
private List<Item> items;
private MessageProperty messageProperty;
// Getters and Setters
public List<Item> getItems() {
return items;
}
public void setItems(List<Item> items) {
this.items = items;
}
public MessageProperty getMessageProperty() {
return messageProperty;
}
public void setMessageProperty(MessageProperty messageProperty) {
this.messageProperty = messageProperty;
}
}
Results:
/incomingMessages( messageProperty.screeningFinancialMessage.messageType == "pacs.008.002", $key : messageProperty.key )
Update:
I was not able to make () work in accumulate function, I assume this syntax does not work in OOPath.
"'$key cannot be resolved to a variable'" after some testing I was able to fix this error by binding a variable for FUNCTION part of accumulate: accumulate( /messageProperties[ screeningFinancialMessage.messageType == "pacs.008.002", $keybind : key ]; $resultkey : collectList($keybind) ) Here I assume, that bind variable inside a constraint of SOURCE_PATTERN cannot be used in RHS of the rule (akin to collect function) Feel free to correct my assumptions=)
Upvotes: 0
Views: 72