clara

http://www.clara-rules.org/
2018-10-23T14:07:29.000100Z

hello. In clara, how can i write recursive queries?

2018-10-23T14:07:44.000100Z

here is an example of the data I have:

2018-10-23T14:07:51.000100Z

{: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"}

2018-10-23T14:09:42.000100Z

here is what i would like the output of the query to look like:

2018-10-23T14:10:00.000100Z

'(:?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"}])

2018-10-23T14:11:51.000100Z

is this possible with a clara query?

eraserhd 2018-10-23T15:47:34.000100Z

Not with just a query, you'll need to introduce some facts.

eraserhd 2018-10-23T15:51:14.000100Z

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))).

eraserhd 2018-10-23T15:52:13.000100Z

Assuming your graph is acyclic. If it isn't, this will be bad.

eraserhd 2018-10-23T15:53:32.000100Z

It might be doable with one rule, actually. But the point is, AFAICT, there's no way to recurse without inserting facts.

2018-10-23T16:01:50.000100Z

Ah understood.

2018-10-23T16:02:04.000100Z

Will give that a go. Thank you!

eraserhd 2018-10-23T16:12:58.000100Z

Oh, you want to (acc/all) :from [TreeRep ...], probably.

eraserhd 2018-10-23T16:13:34.000100Z

You could also do this with a custom accumulator, but that would be much harder, I'm guessing.

2018-10-23T16:16:10.000100Z

@eraserhd I was thinking along those same lines

2018-10-23T16:16:18.000100Z

about having to introduce some facts to model it

eraserhd 2018-10-23T16:25:40.000100Z

By the way, đź‘‹ clara people! We are starting to rely heavily on this for work, and it is a good thing.

đź‘Ť 3
🎉 2
2018-10-23T17:04:09.000100Z

Nice. What sort of things are you using it for (if you can say) and clj or cljs?

eraserhd 2018-10-23T17:16:09.000100Z

clj, only

eraserhd 2018-10-23T17:16:50.000100Z

We are storing data in Datomic, and importing the facts, and deriving a bunch of stuff from them.

eraserhd 2018-10-23T17:17:13.000100Z

This includes constraint validation, what we are calling "synthetic attributes", which are computed attributes.

eraserhd 2018-10-23T17:17:36.000100Z

This is all business data about the online degree programs we are hosting.

eraserhd 2018-10-23T17:17:50.000100Z

And this system drives the stand up of new systems.

2018-10-23T17:20:37.000100Z

oh that’s cool

2018-10-23T17:21:09.000100Z

there was a datomic “datom” integration lib with clara

2018-10-23T17:21:14.000100Z

was that related or something you’ve seen?

eraserhd 2018-10-23T17:22:02.000100Z

We built our own, and we have a library we are attempting to Open Source 🤞

eraserhd 2018-10-23T17:22:17.000100Z

Not specifically about Datoms

2018-10-23T17:22:48.000100Z

ah ok

2018-10-23T17:22:59.000100Z

I’m trying to find the github link I had for that before, but was interesting

2018-10-23T17:23:07.000100Z

but would be really interesting if you open sourced hah

2018-10-23T17:23:27.000100Z

ah yeah, it was this clara-eav https://github.com/clyfe/clara-eav

eraserhd 2018-10-23T17:35:02.000100Z

@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.

2018-10-23T17:39:37.000100Z

interesting

2018-10-23T17:39:43.000100Z

caching intermediate sessions seems interesting too

2018-10-23T17:39:48.000100Z

seems like some durability stuff I messed with before

2018-10-23T17:40:00.000100Z

(well I wrote that stuff in clara originally to try some things out)