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.My guess is that this is not adding that var to the clara-session?
@petr it is supported and you don’t even need vars
but you can use vars if that is convenient for you
if you do use vars, the question then becomes how is it “auto detected” via Clara’s default namespace scanning impl
and that is done via metadata
https://github.com/cerner/clara-rules/blob/0.21.0/src/main/clojure/clara/rules.cljc#L271-L305
@mikerod I would use vars or not. It just feels better than using a macro to generate defrules
this is what it does to scan built-in
so specifically for vars
if you have meta
:
:rule
, :query
, or :production-seq
it is treated as a rule container of some sort
:rule
and :query
are a single rule/query
:production-seq
is a collection of rules/queries - aka “productions”
you don’t need macros
and you don’t need vars
if you have a more data-oriented case
mk-session
takes collections of rule/query (aka production) structures
I'm open to really any approach here
well, you just have to choose what is best suited
Oh cool. so I could actually pass those "rule" maps to the mk-session
?
vars are awkward to create if you’re doing it like at runtime
I agree
if you look at tests
https://github.com/cerner/clara-rules/blob/0.21.0/src/test/clojure/clara/test_rules.clj#L144
as example
you see they are almost all just based on passing rule/query maps in a collection to mk-session
so this is probably all you are really looking for
vars with metadata are basically just a convenience
for automatic rule/query detection in namespaces
which works better for cases where you are more statically writing rules via macros/DSL
Ohhh that looks useful, thank you for showing me that
no problem
I looked briefly on http://clara-rules.org and do not see any actual good documentation around “rule sources”
which I think could be improved
@mikerod thank you so much for that. This has made my day a lot easier
I think it’s lacking if we don’t include other major variants of how to load rules
We’ve taken special care in a lot of the clara stuff to make it where the DSL is optional
and to have the API accept other data-oriented options
Yeah, this is clearly very well thought out
just DSL + var/ns scan-loading is built-in for convenience and the typical, most easy examples