Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement metadata for ubergraphs #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/ubergraph/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@
force-add-directed-edge force-add-undirected-edge remove-edges
equal-graphs? hash-graph build-graph)

(def-map-type Ubergraph [node-map allow-parallel? undirected? attrs cached-hash]
(def-map-type Ubergraph [node-map allow-parallel? undirected? attrs cached-hash metadata]
AbstractMap
lg/Graph
(nodes [g] (keys (:node-map g)))
Expand Down Expand Up @@ -166,7 +166,7 @@
(add-edges* [g edge-definitions] (reduce (fn [g edge] (add-edge g edge)) g edge-definitions))
(remove-nodes* [g nodes] (reduce remove-node g nodes))
(remove-edges* [g edges] (reduce remove-edge g edges))
(remove-all [g] (Ubergraph. {} allow-parallel? undirected? {} (atom -1)))
(remove-all [g] (Ubergraph. {} allow-parallel? undirected? {} (atom -1) metadata))

la/AttrGraph
(add-attr [g node-or-edge k v]
Expand Down Expand Up @@ -235,16 +235,16 @@
default-value))
(assoc [this key value]
(case key
:node-map (Ubergraph. value allow-parallel? undirected? attrs cached-hash)
:allow-parallel? (Ubergraph. node-map value undirected? attrs cached-hash)
:undirected? (Ubergraph. node-map allow-parallel? value attrs cached-hash)
:attrs (Ubergraph. node-map allow-parallel? undirected? value cached-hash)
:cached-hash (Ubergraph. node-map allow-parallel? undirected? attrs value)
:node-map (Ubergraph. value allow-parallel? undirected? attrs cached-hash metadata)
:allow-parallel? (Ubergraph. node-map value undirected? attrs cached-hash metadata)
:undirected? (Ubergraph. node-map allow-parallel? value attrs cached-hash metadata)
:attrs (Ubergraph. node-map allow-parallel? undirected? value cached-hash metadata)
:cached-hash (Ubergraph. node-map allow-parallel? undirected? attrs value metadata)
this))
(dissoc [this key] this)
(keys [this] [:node-map :allow-parallel? :undirected? :attrs :cached-hash])
(meta [this] nil)
(with-meta [this meta] this)
(meta [this] metadata)
(with-meta [this meta] (Ubergraph. node-map allow-parallel? undirected? attrs cached-hash meta))

(hasheq [this] (hash-graph this))
(equiv [this other] (and (instance? Ubergraph other)
Expand Down Expand Up @@ -398,7 +398,7 @@
(terminal #(fconj % edge))]
[:in-degree (terminal finc)])])
node-map)]
(Ubergraph. new-node-map (:allow-parallel? g) (:undirected? g) new-attrs (atom -1))))
(Ubergraph. new-node-map (:allow-parallel? g) (:undirected? g) new-attrs (atom -1) (meta g))))

(defn- add-undirected-edge [g src dest attributes]
(let [g (-> g (add-node src) (add-node dest))
Expand All @@ -425,7 +425,7 @@
[:in-degree (terminal finc)]
[:out-degree (terminal finc)])])
node-map)]
(Ubergraph. new-node-map (:allow-parallel? g) (:undirected? g) new-attrs (atom -1))))
(Ubergraph. new-node-map (:allow-parallel? g) (:undirected? g) new-attrs (atom -1) (meta g))))

(defn- number->map [n]
(if (number? n) {:weight n} n))
Expand Down Expand Up @@ -556,7 +556,7 @@
(defn- swap-edge [edge]
(assoc edge :src (:dest edge) :dest (:src edge)))

(defn- transpose-impl [{:keys [node-map allow-parallel? undirected? attrs reverse-edges]}]
(defn- transpose-impl [{:keys [node-map allow-parallel? undirected? attrs reverse-edges] :as g}]
(let [new-node-map
(into {} (for [[node {:keys [in-edges out-edges in-degree out-degree]}] node-map
:let [new-in-edges
Expand All @@ -568,7 +568,7 @@
new-attrs (into {} (for [[o attr] attrs]
(if (edge? o) [(swap-edge o) attr] [o attr])))]

(Ubergraph. new-node-map allow-parallel? undirected? new-attrs (atom -1))))
(Ubergraph. new-node-map allow-parallel? undirected? new-attrs (atom -1) (meta g))))

(defn add-directed-edges
"Adds directed edges, regardless of whether the underlying graph is directed or undirected"
Expand Down Expand Up @@ -687,29 +687,29 @@
(defn multigraph
"Multigraph constructor. See build-graph for description of valid inits"
[& inits]
(apply build-graph (->Ubergraph {} true true {} (atom -1)) inits))
(apply build-graph (->Ubergraph {} true true {} (atom -1) {}) inits))

(defn multidigraph
"Multidigraph constructor. See build-graph for description of valid inits"
[& inits]
(apply build-graph (->Ubergraph {} true false {} (atom -1)) inits))
(apply build-graph (->Ubergraph {} true false {} (atom -1) {}) inits))

(defn graph
"Graph constructor. See build-graph for description of valid inits"
[& inits]
(apply build-graph (->Ubergraph {} false true {} (atom -1)) inits))
(apply build-graph (->Ubergraph {} false true {} (atom -1) {}) inits))

(defn digraph
"Digraph constructor. See build-graph for description of valid inits"
[& inits]
(apply build-graph (->Ubergraph {} false false {} (atom -1)) inits))
(apply build-graph (->Ubergraph {} false false {} (atom -1) {}) inits))

(defn ubergraph
"General ubergraph construtor. Takes booleans for allow-parallel? and undirected? to
call either graph, digraph, multigraph, or multidigraph.
See build-graph for description of valid inits"
[allow-parallel? undirected? & inits]
(apply build-graph (->Ubergraph {} allow-parallel? undirected? {} (atom -1)) inits))
(apply build-graph (->Ubergraph {} allow-parallel? undirected? {} (atom -1) {}) inits))

;; Serialize/deserialize to an edn Clojure data structure

Expand Down
12 changes: 11 additions & 1 deletion test/ubergraph/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -452,4 +452,14 @@
(remove-attr 1 :keyword)
(remove-attr 1 [:1 :2])
(remove-attr 1 {:k :v})
:attrs))))))
:attrs))))))

(deftest metadata-test
(let [g1 (graph [1 2])
g2 (with-meta g1 {:a 1})
g3 (vary-meta g2 assoc :b 2)]
(testing "Ubergraphs have metadata"
(is (= (meta g1) {}))
(is (= (meta g2) {:a 1}))
(is (= (meta g3) {:a 1 :b 2}))
(is (= g1 g2 g3)))))