Fork me on GitHub
#clara
<
2023-12-05
>
Ben Lieberman18:12:50

Hi there, Clara noob question: I have inserted a fact into memory called TimeLimit. I've tried various simple queries that look correct to me (based on what I can see in documentation), but I get an IllegalArgumentException every time, telling me the query is invalid or not included in the rule base. Do facts also need to have their own rules created by defrule?

ethanc18:12:58

do you have a small sample of how you are making/querying the session?

Ben Lieberman18:12:32

In the RHS of my rule I have

(insert! (->TimeLimit "10 business days" "preliminary"))
and to make my query as dumb as possible I did
(defquery nothing
  []
  [?time-limit <- e/TimeLimit])
exception:
; Execution error (IllegalArgumentException) at clara.rules.platform/throw-error (platform.cljc:5).
; The query {:lhs [{:type e/TimeLimit, :constraints [], :fact-binding :?time-limit}], :params #{}, :name "user/nothing"} is invalid or not included in the rule base.

ethanc18:12:15

is it fair to assume the loading/running of these rules looks something like:

(-> (mk-session [rulename nothing]) 
  (insert (->SomeEntryFact))
  fire-rules
  (query nothing))

Ben Lieberman18:12:57

Does it matter that I've bound my session to a var and then queried it using that name? I would assume that's fine.

ethanc19:12:09

binding it as a variable should be fine

Ben Lieberman19:12:14

hm deep_thinking I'm sure I'm doing something silly

Ben Lieberman19:12:21

Do my queries need to be defined in the same ns I load for my session? That must be what it is

ethanc19:12:58

if you aren’t explicitly providing namespaces or rule/query defs to the mk-session call, then they wouldn’t likely be loaded. something like:

(mk-session)
by default scans the current namespace to load rule/query defs

Ben Lieberman19:12:51

got it, I knew it was gonna be something dumb like that. Thanks.

Ben Lieberman19:12:07

dumb that I was doing I mean

ethanc19:12:47

its not really dumb. Im not a huge fan of default behavior all that much… feels a bit too “magic” to me

Ben Lieberman19:12:42

I definitely made a somewhat arbitrary decision here to have my rules in one ns and queries in another, so lesson learned there but yeah defaults not always my friend either.

ethanc19:12:51

to your credit, you are not the first and certainly not the last to decide to do it that way. I am aware of other implementations that decided to retroactively add “transitive namespace scanning” to their codebase to determine all of the possibly available rules. I highly recommend that everyone avoid that though 😄

😅 1
ethanc04:12:57

> I highly recommend that everyone avoid that though transitively loading namespaces that is. Separation of of rules and queries is likely fine, but when loading them ideally be specific. ex.

(def session (mk-session `rules-namespace  `queires-namespace))
or even more specific by providing individual productions. unraveling the intertwined transitive namespaces can make ones head spin