Alekxios94
Alekxios94

Reputation: 13

Accumulate | Source_pattern | usage of parentheses

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".

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:

  1. Error defining SOURCE_PATTERNS - it throws an error "mismatched input '(' in rule" if I use ():
/incomingMessages( messageProperty.screeningFinancialMessage.messageType == "pacs.008.002", $key : messageProperty.key )
  1. At the same time using [] at least does not throw an error here, but:

Update

Update:

  1. I was not able to make () work in accumulate function, I assume this syntax does not work in OOPath.

  2. "'$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

Answers (0)

Related Questions