zero's a life

An extra chance.

Clojure Rhizome Labeled Edge Tutorial

| Comments

I’m using the Rhizome package for Clojure to rapidly prototype the dependency graph for a crafting system. It’s nice to be able to add and remove components while generating visualizations on-the-fly. Clojure’s simple, composable syntax, aided by paredit, makes shuffling around key-value pairs, and even whole dictionaries, quick and easy.

The existing Rhizome documentation shows simple examples for labeling the nodes of a directed graph. In order to show the relationship between nodes on the graph, I’d like to also label the edges, or the arrows connecting the nodes.

Rhizome provides a keyword argument :edge->descriptor to most of its functions. Consulting the source code and the tests for the Rhizome package, I learned that the functions expect :edge->descriptor to be a function accepting two arguments for each edge: src, the source node, and dst, the destination node. In order to label the edges, the anonymous function I’m binding to :edge->descriptor will return a dictionary containing the key :label and the value for the edge’s label.

Here’s an example using the simple graph from the Rhizome documentation:

1
2
3
4
5
6
7
8
9
(use 'rhizome.viz)

(def g
    {:a [:b :c]
     :b [:c]
     :c [:a]})

(view-graph (keys g) g
            :node->descriptor (fn [n] {:label n}))

Now, I’ll use a dictionary of dictionaries to associate the source and destination nodes with a label for each edge.

1
2
3
4
5
6
7
8
9
(def g-edges
  {:a {:b :makes
       :c :takes}
   :b {:c :takes}
   :c {:a :makes}})

(view-graph (keys g) g
            :node->descriptor (fn [n] {:label n})
            :edge->descriptor (fn [src dst] {:label (dst (src g-edges))}))

I’ve submitted a pull request to Rhizome to add this example to the documentation.

Comments