Fork me on GitHub
#clara
<
2020-10-16
>
PB14:10:02

Is there a way to create rules without the defrule macro? I am currently pulling constraints out of a database and would like to make a bunch of rules based off of those. I noticed that the macro appears to just create a var?

(def
 do-it
 {:ns-name 'woof,
  :lhs '[{:type woof.Person, :constraints [(= name "me")]}],
  :rhs '(do (prn "99999")),
  :name "woof/do-it"})
However creating that var does not seem to create the rule.

PB14:10:49

My guess is that this is not adding that var to the clara-session?

mikerod14:10:26

@petr it is supported and you don’t even need vars

mikerod14:10:30

but you can use vars if that is convenient for you

mikerod14:10:51

if you do use vars, the question then becomes how is it “auto detected” via Clara’s default namespace scanning impl

mikerod14:10:56

and that is done via metadata

PB14:10:35

@mikerod I would use vars or not. It just feels better than using a macro to generate defrules

mikerod14:10:52

this is what it does to scan built-in

mikerod14:10:53

so specifically for vars

mikerod14:10:53

if you have meta : :rule, :query, or :production-seq

mikerod14:10:53

it is treated as a rule container of some sort

mikerod14:10:54

:rule and :query are a single rule/query

mikerod14:10:54

:production-seq is a collection of rules/queries - aka “productions”

mikerod14:10:54

you don’t need macros

mikerod14:10:55

and you don’t need vars

mikerod14:10:56

if you have a more data-oriented case

mikerod14:10:08

mk-session takes collections of rule/query (aka production) structures

PB14:10:13

I'm open to really any approach here

mikerod14:10:39

well, you just have to choose what is best suited

PB14:10:47

Oh cool. so I could actually pass those "rule" maps to the mk-session?

mikerod14:10:49

vars are awkward to create if you’re doing it like at runtime

PB14:10:31

I agree

mikerod14:10:49

if you look at tests

mikerod14:10:50

as example

mikerod14:10:51

you see they are almost all just based on passing rule/query maps in a collection to mk-session

mikerod14:10:51

so this is probably all you are really looking for

mikerod14:10:51

vars with metadata are basically just a convenience

mikerod14:10:51

for automatic rule/query detection in namespaces

mikerod14:10:07

which works better for cases where you are more statically writing rules via macros/DSL

PB14:10:55

Ohhh that looks useful, thank you for showing me that

mikerod15:10:03

no problem

mikerod15:10:16

I looked briefly on http://clara-rules.org and do not see any actual good documentation around “rule sources”

mikerod15:10:22

which I think could be improved

PB15:10:37

@mikerod thank you so much for that. This has made my day a lot easier

mikerod15:10:40

I think it’s lacking if we don’t include other major variants of how to load rules

mikerod15:10:52

We’ve taken special care in a lot of the clara stuff to make it where the DSL is optional

mikerod15:10:11

and to have the API accept other data-oriented options

PB15:10:22

Yeah, this is clearly very well thought out

mikerod15:10:30

just DSL + var/ns scan-loading is built-in for convenience and the typical, most easy examples