Skip to content
zmaril edited this page Feb 2, 2013 · 17 revisions

Hermes provides a variety of functions via the hermes.edge namespace for working with edges.

connect!

Given two vertices, a label, and, optionally, a property map, hermes.edge/connect! creates an edge between the two vertices.

(e/connect! (v/create!) (v/create!) "test-label")
;;#<PersistLabeledTitanEdge e[261:496:1022][496-test-label->500]>
(e/connect! (v/create!) (v/create!) "test-label" {:a 1})
;;#<PersistLabeledTitanEdge e[87:148:326][148-test-label->152]>

endpoints

hermes.edge/endpoints finds the vertices on either side of the edge.

(def edge (e/endpoints (e/connect! (v/create! {:name "start"}) 
                                   (v/create! {:name "end"}) "test-label")))
;;[#<PersistStandardTitanVertex v[228]> #<PersistStandardTitanVertex v[236]>]
(map v/prop-map edge)
;;({:id 248, :name "start"} {:id 256, :name "end"})

edges-between

Given two vertices, hermes.edge/edges-between returns a set of all the edges between the two vertices. Optionally, if a label is provided, then edges-between finds the set of all edges between the two vertices that have that label.

(def a (v/create!)) 
;;#'hermes.example/a 
(def b (v/create!))
;;#'hermes.example/b 
(e/edges-between a b) 
;;nil 
(e/connect! a b "test-label") 
;;#<PersistLabeledTitanEdge e[151:292:326][292-test-label->296]> 
(e/edges-between a b)
;;(#<PersistLabeledTitanEdge e[151:292:326][292-test-label->296]>)
(e/connect! a b "test-label") 
;;#<PersistLabeledTitanEdge e[153:292:326][292-test-label->296]> 
(e/edges-between a b)
;;(#<PersistLabeledTitanEdge e[153:292:326][292-test-label->296]>
#<PersistLabeledTitanEdge e[151:292:326][292-test-label->296]>) 

connected?

Given two vertices, hermes.edge/connected? returns whether the vertices are connected. Optionally, if a label is provided, connected? only checks if they are connected by an edge with the provided label.

(def a (v/create!))
;;#'hermes.example/a 
(def b (v/create!)) 
;;#'hermes.example/b
(e/connected? a b) 
;;false 
(e/connect! a b "test-label")
;;#<PersistLabeledTitanEdge e[163:316:326][316-test-label->320]>
(e/connected? a b) 
;;true 
(e/connected? a b "false-label") 
;;false 

upconnect!

hermes.edge/upconnect! takes the idea of an upsert operation on a object and provides an analog for edges. Provided two vertices, a label, and, optionally, a property map, upconnect! will look for all the edges between the two vertices. If it finds edges and a property map is provided, then it merges the properties of the map into the properties of all the discovered edges. If it doesn't find any edges with the given label, upconnect! will create a edge with that label and the given property map.

(def a (v/create!))
;;#'hermes.example/a
(def b (v/create!))
;;#'hermes.example/b
(e/edges-between a b)
;;nil
(e/upconnect! a b "test-label" {:a 1})
;;#{#<PersistLabeledTitanEdge e[169:328:326][328-test-label->332]>}
(map v/prop-map (e/edges-between a b))
;;({:id #<RelationIdentifier 169:328:326>, :a 1})
(e/upconnect! a b "test-label" {:b 1})
;;(#<PersistLabeledTitanEdge e[169:328:326][328-test-label->332]>)
(map v/prop-map (e/edges-between a b))
;;({:id #<RelationIdentifier 169:328:326>, :a 1, :b 1})
(e/connect! a b "test-label" {:c 2})
;;#<PersistLabeledTitanEdge e[179:328:326][328-test-label->332]>
(e/upconnect! a b "test-label" {:b 5})
;;(#<PersistLabeledTitanEdge e[179:328:326][328-test-label->332]> #<PersistLabeledTitanEdge e[169:328:326][328-test-label->332]>)
(map v/prop-map (e/edges-between a b))
;;({:id #<RelationIdentifier 179:328:326>, :c 2, :b 5} {:id #<RelationIdentifier 169:328:326>, :a 1, :b 5})