Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 439 lines (399 sloc) 18.155 kb
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
1 ;; This software is Copyright (c) Franz, 2009.
2 ;; Franz grants you the rights to distribute
3 ;; and use this software as governed by the terms
4 ;; of the Lisp Lesser GNU Public License
5 ;; (http://opensource.franz.com/preamble.html),
6 ;; known as the LLGPL.
7
8 (comment
9 ;; Tutorial code for using Franz AllegroGraph with Clojure.
10
11 ;; Usage:
12
13 ;; Start the clojure repl with the jars in the classpath.
14 ;; agclj.sh: On command line, starts a REPL.
15 ;; Also, can be used with Emacs/Slime to start a REPL.
16 ;; On command line, to run clojure scripts.
17 ;; .project and .classpath: Eclipse/CounterClockWise project.
18 ;; See http://code.google.com/p/counterclockwise/
19
20 ;; Load this file:
21 (require 'com.franz.agraph.tutorial)
22
23 ;; Change to this namespace:
24 (in-ns 'com.franz.agraph.tutorial)
25
26 ;; You may want to set your own connection params:
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
27 (def *connection-params* {:host "localhost"
28 :port 4567
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
29 :username "test" :password "xyzzy"
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
30 :db-dir "/tmp/ag32jee/scratch/"
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
31 :repository "tutorial"})
32
33 ;; Optional, for convenience in the REPL:
34 (use '[clojure.contrib stacktrace repl-utils trace pprint])
35
36 ;; Execute the examples: test1-test16
37 (example1)
38 (examples-all)
39 )
40
41 (ns com.franz.agraph.tutorial
42 "Tutorial code for using Franz AllegroGraph from the Clojure language.
43 Follows the Python and Java tutorial code."
44 (:import [org.openrdf.model ValueFactory Resource Literal]
45 [org.openrdf.model.vocabulary RDF XMLSchema]
46 [org.openrdf.query QueryLanguage]
47 [org.openrdf.query.impl DatasetImpl]
48 [org.openrdf.rio RDFFormat RDFHandler]
49 [org.openrdf.rio.ntriples NTriplesWriter]
50 [org.openrdf.rio.rdfxml RDFXMLWriter]
51 [java.io File OutputStream FileOutputStream])
52 (:use [com.franz util openrdf agraph agraph3]))
53
54 (alter-meta! *ns* assoc :author "Franz Inc <www.franz.com>, Mike Hinchey <mhinchey@franz.com>")
55
56 ;; Clojure equivalents of the Python and Java tests are below.
57 ;; You may use agclj.sh to start the Clojure REPL in a console,
58 ;; or in Emacs/slime/swank-clojure.
59
60 ;; Note, you can also try the Java tutorial in the Clojure REPL.
61 ;; However, in Slime/swank-clojure, the System.out prints to the
62 ;; *inferior-lisp* buffer not the Slime REPL, so it may be easier to
63 ;; use that instead of slime.
64 ;; (import 'tutorial.TutorialExamples)
65 ;; (TutorialExamples/test1)
66
67 (defonce *connection-params* {:host "localhost"
68 :port 4567
69 :username "test" :password "xyzzy"
70 :db-dir "/tmp/ag32jee/scratch/"
71 :repository "tutorial"})
72
73 (def *agraph-java-tutorial-dir* (.getCanonicalPath (java.io.File. "./tutorial/")))
74
75 (defn example1
76 "lists catalogs and more info about the scratch catalog."
77 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
78 (scope1 (let [repo (agraph-repoconn *connection-params*)]
79 (clear! repo)
80 (println "Repository" (:repository *connection-params*) "is up!"
81 "It contains" (repo-size repo) "statements.")
82 (repository repo))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
83
84 (defn example2
85 "demonstrates adding and removing triples."
86 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
87 (scope1 (let [repo (agraph-repoconn *connection-params*)
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
88 f (value-factory repo)
89 ;; create some resources and literals to make statements out of
90 alice (uri f "http://example.org/people/alice")
91 bob (uri f "http://example.org/people/bob")
92 name (uri f "http://example.org/ontology/name")
93 person (uri f "http://example.org/ontology/Person")
94 bobsName (literal f "Bob")
95 alicesName (literal f "Alice")]
96 (clear! repo)
97 (println "Triple count before inserts:" (repo-size repo))
98 (printlns (get-statements repo [nil nil nil]))
99
100 (add! repo [alice RDF/TYPE person] nil)
101 (add! repo [alice name alicesName] nil)
102 (add! repo [bob RDF/TYPE person] nil)
103 (add! repo [bob (uri f "http://example.org/ontology/name") bobsName] nil)
104 (println "Triple count after adding:" (repo-size repo))
105
106 (remove! repo [bob name bobsName])
107 (println "Triple count after removing:" (repo-size repo))
108
109 ;; add it back
110 (add! repo [bob name bobsName] nil)
111 (repository repo))))
112
113 (defn example3
114 "demonstrates a SPARQL query using the data from test2"
115 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
116 (scope1 (let [repo (repo-connection (example2))]
117 (printlns (tuple-query repo QueryLanguage/SPARQL "SELECT ?s ?p ?o WHERE {?s ?p ?o .}"
118 nil)))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
119
120 (defn example4
121 ""
122 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
123 (scope1
124 (let [repo (repo-connection (example2))
125 alice (uri (value-factory repo) "http://example.org/people/alice")]
126 (printlns (get-statements repo [alice nil nil] nil)))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
127
128 (defn example5
129 "Typed Literals"
130 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
131 (scope1
132 (let [repo (agraph-repoconn *connection-params*)
133 f (value-factory repo)
134 exns "http://example.org/people/"
135 alice (uri f "http://example.org/people/alice")
136 age (uri f exns "age")
137 weight (uri f exns, "weight")
138 favoriteColor (uri f exns "favoriteColor")
139 birthdate (uri f exns "birthdate")
140 ted (uri f exns "Ted")
141 red (literal f "Red")
142 rouge (literal f "Rouge" "fr")
143 fortyTwoInt (literal f "42" XMLSchema/INT)
144 fortyTwoLong (literal f "42" XMLSchema/LONG)
145 fortyTwoUntyped (literal f "42")
146 date (literal f "1984-12-06" XMLSchema/DATE)
147 time (literal f "1984-12-06T09:00:00" XMLSchema/DATETIME)]
148 (clear! repo)
149 (add-all! repo
150 [(.createStatement f alice age fortyTwoInt)
151 (.createStatement f ted age fortyTwoUntyped)
152 [alice weight (literal f "20.5")]
153 [ted weight (literal f "20.5" XMLSchema/FLOAT)]
154 [alice favoriteColor red]
155 [ted favoriteColor rouge]
156 [alice birthdate date]
157 [ted birthdate time]])
158 (doseq [obj [nil fortyTwoInt fortyTwoLong fortyTwoUntyped
159 (literal f "20.5" XMLSchema/FLOAT)
160 (literal f "20.5") red rouge]]
161 (println "Retrieve triples matching" obj ".")
162 (printlns (get-statements repo [nil nil obj] nil)))
163 (doseq [obj ["42", "\"42\"", "20.5", "\"20.5\"", "\"20.5\"^^xsd:float"
164 "\"Rouge\"@fr", "\"Rouge\"", "\"1984-12-06\"^^xsd:date"]]
165 (println "Query triples matching" obj ".")
166 (printlns (tuple-query repo QueryLanguage/SPARQL
167 (str "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> "
168 "SELECT ?s ?p ?o WHERE {?s ?p ?o . filter (?o = " obj ")}")
169 nil))))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
170
171 (defn example6
172 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
173 (scope1
174 (let [repo (agraph-repoconn *connection-params*
175 {:namespaces {"vcd" "http://www.w3.org/2001/vcard-rdf/3.0#"}})
176 f (value-factory repo)
177 vcards (File. *agraph-java-tutorial-dir* "/vc-db-1.rdf")
178 kennedy (File. *agraph-java-tutorial-dir* "/kennedy.ntriples")
179 baseURI "http://example.org/example/local"
180 context (uri f "http://example.org#vcards")]
181 (clear! repo)
182 (add-from! repo vcards baseURI RDFFormat/RDFXML context)
183 (add-from! repo kennedy baseURI RDFFormat/NTRIPLES nil)
184 (println "After loading, repository contains " (repo-size repo context)
185 " vcard triples in context '" context "'\n and "
186 (repo-size repo nil) " kennedy triples in context 'nil'.")
187 (repository repo))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
188
189 (defn example7
190 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
191 (scope1
192 (let [repo (repo-connection (example6))]
193 (println "Match all and print subjects and contexts:")
194 (printlns (get-statements repo [nil nil nil] nil))
195
196 (println "Same thing with SPARQL query:")
197 (printlns (tuple-query repo QueryLanguage/SPARQL
198 "SELECT DISTINCT ?s ?c WHERE {graph ?c {?s ?p ?o .} }" nil)))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
199
200 (defn example8
201 "Writing RDF or NTriples to a file"
202 [& [write-to-file?]]
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
203 (scope1 (let [repo (repo-connection (example6))
204 contexts (resource-array [(uri (value-factory repo) "http://example.org#vcards")])]
205 (let [output (if write-to-file?
206 (new FileOutputStream "/tmp/temp.nt")
207 *out*)
208 writer (new NTriplesWriter output)]
209 (println "Writing NTriples to" output)
210 (.export repo writer contexts))
211
212 (let [output (if write-to-file?
213 (new FileOutputStream "/tmp/temp.rdf")
214 *out*)
215 writer (new RDFXMLWriter output)]
216 (println "Writing RDFXML to" output)
217 (.export repo writer contexts)
218 (println)))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
219
220 (defn example9
221 "Writing the result of a statements match to a file."
222 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
223 (scope1 (let [repo (repo-connection (example6))]
224 (.exportStatements repo nil RDF/TYPE nil false (new RDFXMLWriter *out*) (resource-array nil))
225 (println))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
226
227 (defn tutorial-repo
228 "Shortcut for tutorial functions, returns the AGRepositoryConnection."
229 ([] (tutorial-repo nil))
230 ([rcon-args]
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
231 (let [repo (agraph-repoconn *connection-params* rcon-args)]
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
232 repo)))
233
234 (defn example10
235 "Datasets and multiple contexts"
236 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
237 (scope1
238 (let [repo (clear! (tutorial-repo))
239 f (value-factory repo)
240 exns "http://example.org/people/"
241 alice (uri f exns "alice")
242 bob (uri f exns "bob")
243 ted (uri f exns "ted")
244 name (uri f "http://example.org/ontology/name")
245 person (uri f "http://example.org/ontology/Person")
246 alicesName (literal f "Alice")
247 bobsName (literal f "Bob")
248 tedsName (literal f "Ted")
249 context1 (uri f exns "cxt1")
250 context2 (uri f exns "cxt2")]
251 (add-all! repo [[alice RDF/TYPE person context1]
252 [alice name alicesName context1]
253 [bob RDF/TYPE person context2]
254 [bob name bobsName context2]
255 [ted RDF/TYPE person]
256 [ted name tedsName]])
257 (println "All triples in all contexts:")
258 (printlns (get-statements repo [nil nil nil]))
259 (println "Triples in contexts 1 or 2:")
260 (printlns (get-statements repo [nil nil nil] {:contexts [context1 context2]}))
261 (println "Triples in contexts nil or 2:")
262 (printlns (get-statements repo [nil nil nil] {:contexts [nil context2]}))
263
264 (println "Query over contexts 1 and 2:")
265 (printlns (tuple-query repo QueryLanguage/SPARQL
266 "SELECT ?s ?p ?o ?c WHERE { GRAPH ?c {?s ?p ?o . } }"
267 {:dataset (doto (new DatasetImpl)
268 (.addNamedGraph context1)
269 (.addNamedGraph context1))}))
270
271 (println "Query over the null context:")
272 (printlns (tuple-query repo QueryLanguage/SPARQL
273 "SELECT ?s ?p ?o WHERE {?s ?p ?o . }"
274 {:dataset (doto (new DatasetImpl)
275 (.addDefaultGraph nil))})))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
276
277 (defn example11
278 "Namespaces"
279 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
280 (scope1
281 (let [exns "http://example.org/people/"
282 rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
283 repo (agraph-repoconn *connection-params* {:namespaces {"ex" exns
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
284 "rdf" rdf}})
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
285 f (value-factory repo)
286 alice (uri f exns "alice")
287 person (uri f exns "Person")]
288 (clear! repo)
289 (add! repo [alice (uri f rdf "type") person] nil)
290 (printlns (tuple-query repo QueryLanguage/SPARQL
291 "SELECT ?s ?p ?o WHERE { ?s ?p ?o . FILTER ((?p = rdf:type) && (?o = ex:Person) ) }"
292 nil)))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
293
294 (defn example12
295 "Text search"
296 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
297 (scope1
298 (let [exns "http://example.org/people/"
299 repo (agraph-repoconn *connection-params* {:namespaces {"ex" exns}})
300 ;; Note, namespace {'fti' "http://franz.com/ns/allegrograph/2.2/textindex/"} is already built-in
301 f (value-factory repo)
302 alice (uri f exns "alice1")
303 persontype (uri f exns "Person")
304 fullname (uri f exns "fullname")
305 alicename (literal f "Alice B. Toklas")
306 book (uri f exns "book1")
307 booktype (uri f exns "Book")
308 booktitle (uri f exns "title")
309 wonderland (literal f "Alice in Wonderland")]
310 (clear! repo)
311 (.registerFreetextPredicate repo (uri f "http://example.org/people/name"))
312 (.registerFreetextPredicate repo (uri f exns "fullname"))
313 (add-all! repo [[alice RDF/TYPE persontype]
314 [alice fullname alicename]
315 [book RDF/TYPE booktype]
316 [book booktitle wonderland]])
317 (doseq [match ["?s fti:match 'Alice' ."
318 "?s fti:match 'Ali*' ."
319 "?s fti:match '?l?c?' ."
320 ;; TODO: "FILTER regex(?o, \"lic\")"
321 ]]
322 (let [query (str "SELECT ?s ?p ?o WHERE { ?s ?p ?o . " match " }")]
323 (printlns (take 5 (tuple-query repo QueryLanguage/SPARQL query nil))))))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
324
325 (defn example13
326 "Ask, Construct, and Describe queries"
327 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
328 (scope1
329 (let [repo (repo-connection (example2) {:namespaces {"ex" "http://example.org/people/"
330 "ont" "http://example.org/ontology/"}})
331 f (value-factory repo)]
332 (printlns (tuple-query repo QueryLanguage/SPARQL
333 "select ?s ?p ?o where { ?s ?p ?o}" nil))
334 (println "Boolean result:"
335 (query-boolean repo QueryLanguage/SPARQL
336 "ask { ?s ont:name \"Alice\" }" nil))
337 (print "Construct result: ")
338 (doall (map #(print % " ")
339 (query-graph repo QueryLanguage/SPARQL
340 "construct {?s ?p ?o} where { ?s ?p ?o . filter (?o = \"Alice\") } " nil)))
341 (println)
342 (println "Describe result: ")
343 (printlns (query-graph repo QueryLanguage/SPARQL
344 "describe ?s where { ?s ?p ?o . filter (?o = \"Alice\") }" nil)))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
345
346 (defn example14
347 "Parametric Queries"
348 []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
349 (scope1
350 (let [repo (repo-connection (example2))
351 f (value-factory repo)
352 alice (uri f "http://example.org/people/alice")
353 bob (uri f "http://example.org/people/bob")]
354 (println "Facts about Alice:")
355 (printlns (tuple-query repo QueryLanguage/SPARQL
356 "select ?s ?p ?o where { ?s ?p ?o}"
357 {:bindings {"s" alice}}))
358 (println "Facts about Bob:")
359 (printlns (tuple-query repo QueryLanguage/SPARQL
360 "select ?s ?p ?o where { ?s ?p ?o}"
361 {:bindings {"s" bob}})))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
362
363 ;; (defn example16
364 ;; "Federated triple stores."
365 ;; []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
366 ;; (scope1 (let [server (agraph-repoconn *connection-params*)
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
367 ;; cat (ag-catalog server (:catalog *connection-params*))
368 ;; ex "http://www.demo.com/example#"
369 ;; repo-args {:namespaces {"ex" ex}}
370 ;; ;; create two ordinary stores, and one federated store:
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
371 ;; red (clear! (agraph-repoconn cat "redthings" repo-args))
372 ;; green (clear! (agraph-repoconn cat "greenthings" repo-args))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
373 ;; rainbow (repo-federation server
374 ;; "rainbowthings"
375 ;; red green)
376 ;; rf (value-factory red)
377 ;; gf (value-factory green)
378 ;; rbf (value-factory rainbow)]
379 ;; ;; add a few triples to the red and green stores:
380 ;; (doseq [[c f s o] [[red rf "mcintosh" "Apple"]
381 ;; [red rf "reddelicious" "Apple"]
382 ;; [green gf "pippen" "Apple"]
383 ;; [green gf "kermitthefrog" "Frog"]
384 ;; ]]
385 ;; (add! c (uri f (str ex s)) RDF/TYPE (uri rf (str ex o))))
386 ;; ;; query each of the stores; observe that the federated one is the union of the other two:
387 ;; (doseq [[kind repo] [["red" red]
388 ;; ["green" green]
389 ;; ["federated" rainbow]]]
390 ;; (println)
391 ;; (println kind "apples:")
392 ;; (printlns (tuple-query repo QueryLanguage/SPARQL
393 ;; "select ?s where { ?s rdf:type ex:Apple }"
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
394 ;; nil))))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
395
396 ;; (defn test17
397 ;; "Prolog queries"
398 ;; []
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
399 ;; (scope1 (let [rcon (repo-connection (test6))]
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
400 ;; (.deleteEnvironment rcon "kennedys") ;; start fresh
401 ;; (.setEnvironment rcon "kennedys")
402 ;; (.setNamespace rcon "kdy" "http://www.franz.com/simple#")
403 ;; (.setRuleLanguage rcon lang-prolog)
404 ;; (.addRules rcon "
405 ;; (<-- (female ?x) ;; IF
406 ;; (q ?x !kdy:sex !kdy:female))
407 ;; (<-- (male ?x) ;; IF
408 ;; (q ?x !kdy:sex !kdy:male))
409 ;; ")
410 ;; ;; This causes a failure(correctly):
411 ;; ;; conn.deleteRule('male')
412 ;; (with-results [row (prepare-query rcon {:type :tuple :lang lang-prolog} "
413 ;; (select (?person ?name)
414 ;; (q ?person !rdf:type !kdy:person)
415 ;; (male ?person)
416 ;; (q ?person !kdy:first-name ?name)
417 ;; )
418 ;; ")]
c893866 @mikehinchey clojure: simplify function names
mikehinchey authored
419 ;; (println row)))))
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
420
421 (defn examples-all
422 []
423 (example1)
424 (example2)
425 (example3)
426 (example4)
427 (example5)
428 (example6)
429 (example7)
430 (example8)
431 (example9)
432 (example10)
433 (example11)
434 (example12)
435 (example13)
436 (example14)
eab926e @mikehinchey add jars to everything compiles
mikehinchey authored
437 ;;(example16)
8477270 @mikehinchey clojure source copied from agraph40, fixed for agraph32
mikehinchey authored
438 )
Something went wrong with that request. Please try again.