Database Reference
In-Depth Information
How to do it…
As we work through this, we'll deine a series of functions. Finally, we'll create one function,
load-data , to orchestrate everything, and we'll inish by doing the following:
1.
We have to create a Sesame triple store and initialize it with the namespaces we'll
use. For both of these, we'll use the kb-memstore and init-kb functions from
Reading RDF data . We deine a function that takes a URI for a subject in the triple
store and constructs a SPARQL query that returns at most 200 statements about
this subject. The function then ilters out any statements with non-English strings for
objects, but it allows everything else:
(defn make-query
"This creates a query that returns all of the
triples related to asubject URI. It
filters out non-English strings."
([subject kb]
(binding [*kb* kb
*select-limit* 200]
(sparql-select-query
(list '(~subject ?/p ?/o)
'(:or (:not (:isLiteral ?/o))
(!= (:datatype ?/o) rdf/langString)
(= (:lang ?/o) ["en"])))))))
2.
Now that we have the query, we'll need to encode it into a URL in order to retrieve
the results:
(defn make-query-uri
"This constructs a URI for the query."
([base-uri query]
(URL. (str base-uri
"?format="
(URLEncoder/encode "text/xml")
"&query=" (URLEncoder/encode query)))))
3. Once we get a result, we'll parse the XML ile, wrap it in a zipper, and navigate to
the irst result. All of this will be in a function that we'll write in a minute. Right now,
the next function will take this irst result node and return a list of all the results:
(defn result-seq
"This takes the first result and returns a sequence
of this node, plus all of the nodes to the right of
it."
([first-result]
(cons (zip/node first-result)
(zip/rights first-result))))
 
Search WWH ::




Custom Search