Commits

Karsten Schmidt committed 156fd49

adding node-target-tree function

  • Participants
  • Parent commits 9db1233

Comments (0)

Files changed (1)

File src/estuary/core.clj

   ([state re]
     (map (fn [v] (val v)) (filter #(re-matches re (name (key %))) state))))
 
-(defn- filter-targets
+(defn filter-targets
   ([targets key]
     (filter (fn[t] (get t key)) targets))
   ([targets key v]
   [graph]
   (map #(trigger (node-for-id graph %)) (emitters graph)))
 
+(defn node-target-tree
+  "Constructs a nested vector of all direct & indirect target IDs
+  for the given node. The graph MUST be acyclic.
+
+  For node A in the following graph this structure is produced:
+
+    A---F
+   / \\
+  B   C      -> [B [D [E]] [C [E]] F]
+  |    \\
+  D-----E"
+  [n]
+  (let [tlist (:targets @n)]
+    (if (= (count tlist) 0)
+      nil
+      (reduce
+        (fn[branch t]
+          (let [a (:agent t)
+                b (node-target-tree a)]
+            (if (nil? b)
+              (conj branch (:id @a))
+              (conj branch (:id @a) b))))
+          [] tlist))))
+
 (defn conn!
   "Creates a connection from the given source node to a target node.
 The nodes need to be either supplied as node references or UUIDs.