Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using accumulator with a custom function #503

Open
adeojo opened this issue Aug 6, 2024 · 3 comments
Open

Using accumulator with a custom function #503

adeojo opened this issue Aug 6, 2024 · 3 comments
Assignees
Labels

Comments

@adeojo
Copy link

adeojo commented Aug 6, 2024

I have the following custom function

(defn calculate-trading-dates
  [effective-date terminate-date billing-dates ]
  (let [effective-date-to-use (if (.isBefore effective-date (.getStartOfMonth billing-dates))
                                (.getStartOfMonth billing-dates)
                                effective-date)
        terminate-date-to-use (if (nil? terminate-date)
                                (.getEndOfMonth billing-dates)
                                terminate-date)
        numerator (double (.getNumberOfTradingDates billing-dates effective-date-to-use terminate-date-to-use true))
        denominator (double (.getNumberOfTradingDates billing-dates))
        final-value (/ numerator denominator)]
    final-value))

The above function is used in a rule as below

(defrule my-rule
    "Sample Rule"
  
    [?billingDates <- BillingDates]
    [?sumCalcTradingDates <- (acc/sum #((calculate-trading-dates (.getEffectiveDate %) (.getTerminateDate %) ?billingDates ) % )) :from [DlpFirm
                                                                                                                               (= mpid ?accountId)
                                                                                                                               (= portId "PrimaryDLP")
                                                                                                                               (= ?effectiveDate effectiveDate )
                                                                                                                               (= ?terminateDate terminateDate )]]
  
    [:test (>= (.doubleValue ?sumCalcTradingDates) 25) ]
  

  
    =>
    (when (true? (get (.getFlags ?item) "baseChargeApplied"))
      (insert! (Map/of "$transactionalBillingChargeWriter" (TransactionalBillingCharge. ^EquityTransactionalBillableItem ?item (.getChargeTypeDTO ?charge-types "ABC100"))))
 ))     

When I test the above rule, I get the error:

    Unable to resolve symbol: ?billingDates in this context

FYI :The DLPFirm POJO does not have BillingDates

Any ideas how I can use the the function with an accumulator ?

@EthanEChristian
Copy link
Contributor

The compilation of the accumulator would occur prior to the execution of rules and outside of that context, which is likely the source of the error:

Unable to resolve symbol: ?billingDates in this context

In the sense that at session creation time, its trying to construct the field accessor function:

(acc/sum #((calculate-trading-dates (.getEffectiveDate %) (.getTerminateDate %) ?billingDates) % ))

which attempts to reference a binding scoped to an execution time variable.

Off the cuff, i'd probably defer that sort of logic to the RHS for calculation rather than the accumulator if possible.

@EthanEChristian
Copy link
Contributor

Other contributors mentioned(off-thread) an alternative of introducing intermediate facts to house this sort of data for easier accumulation.

Something akin to:

(defrecord DlpFirmWithTradingDates [trading-dates dlp-firm])

(r/defrule my-rule
  "Sample Rule"
  [?billingDates <- BillingDates]
  [?dlpFirm <- DlpFirm
    (= portId "PrimaryDLP")
    (= ?tradingDates (calculate-trading-dates effectiveDate terminateDate ?billingDates))]
  =>
  (r/insert! (->DlpFirmWithTradingDates ?tradingDates ?dlpFirm)))

then a subsequent rule to accumulate the intermediate facts.

@adeojo
Copy link
Author

adeojo commented Aug 8, 2024

Will give this a try. thx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants