I agree; I don't need dynamic rules. I more needed a hint as to the fact that they are supplied and loaded upon mk-session
.
To that end, I tried your advice, I think I did it faithfully, and wasn't able to make it work yet. My adjustments are at this commit: https://github.com/mpettis/clara-ns-ex/tree/0bdb72e98a8ef657de08579af6ee993c9008a738
When I run that, I still get that Myfact
class cannot be found. I think I'm probably missing something about using defrecord
definitions from other namespaces.
Also, for a baseline, this is how I make it work when everything is in the same namespace... https://github.com/mpettis/clara-ns-ex/blob/nonamespace/src/clara_ns_ex/core.clj
@matthew.pettis so you’re issue isn’t just referring to record symbols. Have to follow normal clojure symbol resolution rules.
So you require the ns that defines the records - to ensure it is compiled.
Then you typically would use :import in clj (not cljs) to refer to the record class name as the shortened version
Otherwise you’d have to fully qualify it each reference clara_ns_ex.facts.Myfact
Thanks. I thought about that a bit more after I posted, I'll think a bit more carefully about resolution rules, and think I may get it... Thanks for the help; learning Clojure as I learn Clara here...
No problem. Let me know if more questions
I’d give better examples but typing on mobile is hard hah
1👍I really appreciate the help.
fyi, i think i'm closer, but I can't decipher the location of where the error of can't find Class for Myfact, as it won't tell me at what line it is failing. This is the state that I have it: https://github.com/mpettis/clara-ns-ex/tree/f3d6f67346eacb61294205f6da0e1f3073e7428a
I have the import statements, I tested that it worked. I referenced these pages that went with the hint that @mikerod gave me on using import: https://danielcompton.net/2016/05/04/requiring-records-clojure-clojurescript and https://stackoverflow.com/a/23420084/1022967
OK, I got it to work by playing with what did and did not need namespace qualified names, and where. I don't fully understand the rules of when I should and should not yet, but I have a working example to go off of, so thanks for all of your help! Here is the commit that has a working example: https://github.com/mpettis/clara-ns-ex/tree/d077c141d305ef217043bcf44a328c6921311847
Looks like you've got it figured out mostly. :). You don't need the fact class imports in your core namespace, but rather just in the rule/query namespaces that use them. Clara rule and query defs use the same evaluation semantics as normal Clojure code, that is something used, be it a function, class, etc. needs to be resolvable in the namespace where the rule/query is being defined. Also it's somewhat a matter of personal preference, but I find it more clear where things are coming from when I use :as aliases rather than :refer :all, that might help reduce confusion.
1👍Admittedly there's a fair amount of code in Clara dealing with that evaluation, but the end result is to have the same user-facing semantics.
Awesome! I'll take out the require and import statements in core and use :as aliases in the rest and push a cleaner example. Thanks for the help.
I also advocate for :as aliases too. It helps to make it clear where symbols are coming from. ClojureScript doesn’t even have :refer :all support! Hah
I just discovered 'how to ns' by Sierra, reading that to guide me on that: https://stuartsierra.com/2016/clojure-how-to-ns.html
OK, just for completeness and posterity... For this question, which was how to use namespaced rules, queries, and facts in making a session, I cleaned up the namespace declaration, using Sierra's "how to ns" style recommendations, and removed most :require :refer :all statements to be more precise (except for clara-rules
). Here is the commit that is cleaned up per this comment: https://github.com/mpettis/clara-ns-ex/tree/8b78de1442fd8484341cc68d6ac63b7d78cf85e4
Thanks again for the help. I think it is helpful to have an example for clara-rules that leverages namespaces. I think I'll see if cerner/clara-examples would consider adding it as an example. Or somewhere. Nice to have a battery of examples for some of this...
@matthew.pettis yeah more examples could be good. Not sure when the last time that repo was updated. But perhaps it is the right place. There are also docs that maybe could explain some things more
@mikerod Yep, started there, read the documentation, some of it was vague to me, and not sure if is lack of experience on my part or it just doesn't have attention anymore to curate. Also, fwiw, I am making a different repo of collected small steps for myself for self-training. In this link, I use clara-rules to solve a logic puzzle, much like could be done with core.logic. However, I did it in clara-rules not because it was more efficient (oh, my example is real slow and memory hoggy), but because it mimicked a different scenario I think I want to apply clara-rules to. Here is that particular example... https://github.com/mpettis/clara-lien/blob/master/src/clara_lein/ex05.clj
1👍that’s interesting, could be something nice to add to docs or examples