Fork me on GitHub
#datascript
<
2017-11-10
>
Petrus Theron16:11:12

Given a directed acyclic graph (where each node has optional :node/parent), how do I recursively fetch the IDs of all descendant nodes with a Datalog query? I spent about 40 minutes trying to find an example in Datascript code and Datalog, but couldn’t find anything

Petrus Theron18:11:07

Got it working (using Posh and abusing square bracket rule). It doesn’t auto-update unfortunately, but OK for my use-case:

@(p/q '[:find ?c
                                     :in $ % ?parent
                                     :where
                                     [child ?c ?parent]]
                                   conn
                                   '[[(child ?c ?p)
                                      (?c :parent ?p)]
                                     [(child ?c ?p)
                                      (?c1 :parent ?p)
                                      (child ?c ?c1)]]
                                   parent-id)
Am I doing it right?