hello. In clara, how can i write recursive queries?
here is an example of the data I have:
{:type :foo
:desc "'Foo' node"
:parent :D}
{:type :D
:desc "'D' node"
:parent :C}
{:type :C
:desc "'C' node"
:parent :B}
{:type :B
:desc "'B' node"
:parent :A}
{:type :A
:desc "'A' node"}
here is what i would like the output of the query to look like:
'(:?node {:type :foo
:desc "'Foo' node"
:parent :D}
:?parents [{:type :D
:desc "'D' node"
:parent :C}
{:type :C
:desc "'C' node"
:parent :B}
{:type :B
:desc "'B' node"
:parent :A}
{:type :A
:desc "'A' node"}])
is this possible with a clara query?
Not with just a query, you'll need to introduce some facts.
So you'd have one rule that's (heavily abbreviating the code): [:test (no-children? ?node)] => (insert! (->TreeRep ?node ?node))). And one that's [:test (children? ?node)] ?children <- (acc/all) :from [Node (= parent ?node)] => (insert! (->TreeRep ?node (add-children ?node ?children))).
Assuming your graph is acyclic. If it isn't, this will be bad.
It might be doable with one rule, actually. But the point is, AFAICT, there's no way to recurse without inserting facts.
Ah understood.
Will give that a go. Thank you!
Oh, you want to (acc/all) :from [TreeRep ...], probably.
You could also do this with a custom accumulator, but that would be much harder, I'm guessing.
@eraserhd I was thinking along those same lines
about having to introduce some facts to model it
By the way, đź‘‹ clara people! We are starting to rely heavily on this for work, and it is a good thing.
Nice. What sort of things are you using it for (if you can say) and clj or cljs?
clj, only
We are storing data in Datomic, and importing the facts, and deriving a bunch of stuff from them.
This includes constraint validation, what we are calling "synthetic attributes", which are computed attributes.
This is all business data about the online degree programs we are hosting.
And this system drives the stand up of new systems.
oh that’s cool
there was a datomic “datom” integration lib with clara
was that related or something you’ve seen?
We built our own, and we have a library we are attempting to Open Source 🤞
Not specifically about Datoms
ah ok
I’m trying to find the github link I had for that before, but was interesting
but would be really interesting if you open sourced hah
ah yeah, it was this clara-eav https://github.com/clyfe/clara-eav
@mikerod This actually looks pretty neat. Our library does a different thing, regarding bulk loading facts from datomic and caching intermediate sessions to save work. It seems like we could use them together.
interesting
caching intermediate sessions seems interesting too
seems like some durability stuff I messed with before
(well I wrote that stuff in clara originally to try some things out)