Database Reference
In-Depth Information
4.
The following set of functions takes each result node and returns a key-value
pair ( result-to-kv ). It uses binding-str to pull the results out of the XML.
Then, accum-hash pushes the key-value pairs into a map. Keys that occur more
than once have their values accumulated in a vector:
(defn binding-str
"This takes a binding, pulls out the first tag's
content, and concatenates it into a string."
([b]
(apply str (:content (first (:content b))))))
(defn result-to-kv
"This takes a result node and creates a key-value
vector pair from it."
([r]
(let [[p o] (:content r)]
[(binding-str p) (binding-str o)])))
(defn accum-hash
([m [k v]]
(if-let [current (m k)]
(assoc m k (str current \space v))
(assoc m k v))))
5.
For the last utility function, we'll deine rekey . This will convert the keys of a map
based on another map:
(defn rekey
"This just flips the arguments for
clojure.set/rename-keys to make it more
convenient."
([k-map map]
(rename-keys
(select-keys map (keys k-map)) k-map)))
6.
Let's now add a function that takes a SPARQL endpoint and subject and returns a
sequence of result nodes. This will use several of the functions we've just deined:
(defn query-sparql-results
"This queries a SPARQL endpoint and returns a
sequence of result nodes."
([sparql-uri subject kb]
(->> kb
;; Build the URI query string.
(make-query subject)
(make-query-uri sparql-uri)
 
Search WWH ::




Custom Search