clara-rules icon indicating copy to clipboard operation
clara-rules copied to clipboard

Strange NPE issue

Open victorrodrigueznadq opened this issue 8 months ago • 16 comments

This is my original clara rule, which was converted from a drools implementation:

(defrule add-all-dlp-firm-symbols
  "Add All DLP Firm Symbols"
  {:salience 1000000}
  [?billingDates <- BillingDates
   ]
  [?dlpFirm <- DlpFirm
   (= ?mpid mpid )
   (= ?symbol symbol )
   ]
  [?symbolBillingLink <- SymbolBillingProgramLink
   (= accountId ?mpid)
   (= symbol ?symbol)
   (nil? terminateDate)
   ]
  =>
  (def volumeAccumulation (VolumeAccumulation.))
  (.setAccountIdentifier volumeAccumulation ?mpid)
  (.setSymbol volumeAccumulation ?symbol)
  (.setNumberOfDays volumeAccumulation (.getNumberOfTradingDates ?billingDates))
  (.setTotalVolume volumeAccumulation (long 0))
  (.setAccumulationLevel volumeAccumulation VolumeAccumulation$AccumulationLevel/ACCOUNT_SYMBOL)
  (.setAccumulationCode volumeAccumulation "nqeAddDlp")
  (.setFirmIdentifier volumeAccumulation (.getFirmId ?symbolBillingLink))
  (insert! (Map/of "$volumeAccumulationWriter" volumeAccumulation))
  )

I was having a memory leak because we didn't have a check to prevent the same VolumeAccumulation records from being created and inserted over an over again each time the rules were fired. So then I added a :not check. I kept betting NPE exceptions complaining about firmIdentifier being null, so I kept adding more and more (some?) checks to ensure, supposedly, that that couldn't never happen, but thusfar, I've been unable to erradicate that NPE exception. Here's what the rule currently looks like (added (insert! volumeAccumulation) to the end of the RHS):

(defrule add-all-dlp-firm-symbols
  "Add All DLP Firm Symbols"
  {:salience 1000000}
  [?billingDates <- BillingDates
   ]
  [?dlpFirm <- DlpFirm
   (= ?mpid mpid )
   (= ?symbol symbol )
   ]
  [?symbolBillingLink <- SymbolBillingProgramLink
   (= accountId ?mpid)
   (= symbol ?symbol)
   (nil? terminateDate)
   (some? firmId)
   ]
  [:not [VolumeAccumulation
         (some? numberOfDays)
         (some? firmIdentifier)
         (some? totalVolume)
         (= accountIdentifier ?mpid)
         (= symbol ?symbol)
         (= numberOfDays (.getNumberOfTradingDates ?billingDates))
         (= totalVolume (long 0))
         (= accumulationLevel VolumeAccumulation$AccumulationLevel/ACCOUNT_SYMBOL)
         (= accumulationCode "nqeAddDlp")
         (= firmIdentifier (.getFirmId ?symbolBillingLink))
         ]]
  =>
  (def volumeAccumulation (VolumeAccumulation.))
  (.setAccountIdentifier volumeAccumulation ?mpid)
  (.setSymbol volumeAccumulation ?symbol)
  (.setNumberOfDays volumeAccumulation (.getNumberOfTradingDates ?billingDates))
  (.setTotalVolume volumeAccumulation (long 0))
  (.setAccumulationLevel volumeAccumulation VolumeAccumulation$AccumulationLevel/ACCOUNT_SYMBOL)
  (.setAccumulationCode volumeAccumulation "nqeAddDlp")
  (.setFirmIdentifier volumeAccumulation (.getFirmId ?symbolBillingLink))
  (insert! volumeAccumulation)
  (insert! (Map/of "$volumeAccumulationWriter" volumeAccumulation))
  )

And, here's the exception:

Caused by: java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.firmIdentifier" is null
	at com.nasdaq.edm.rms.accumulation.VolumeAccumulation.getFirmIdentifier(VolumeAccumulation.java:252)
	at clojure.core$eval388$AN_136_AE__507.invoke(NO_SOURCE_FILE:42)
	at clara.rules.engine$alpha_node_matches$iter__4093__4097$fn__4098$fn__4099$fn__4100.invoke(engine.cljc:517)
	... 28 common frames omitted

Any ideas what could possibly be going on?

victorrodrigueznadq avatar Jun 11 '24 16:06 victorrodrigueznadq