The KR library is for working with knowledge representations and knowledge bases. Currently it facilitates use of RDF-based representations backed by triple-/quad- stores. It provides a consistent clojure based way of interacting with its backing implementations, which currently include the Jena and Sesame APIs.
update: see the note on Sesame Versions
The primary api functions you're likely to use come from the kr-core apis:
(use 'edu.ucdenver.ccp.kr.kb)
(use 'edu.ucdenver.ccp.kr.rdf)
(use 'edu.ucdenver.ccp.kr.sparql)
To actually get a KB instance to work with you'll need to make sure the implementation-specific code is loaded:
(require 'edu.ucdenver.ccp.kr.sesame.kb)
;; OR
(require 'edu.ucdenver.ccp.kr.jena.kb)
a kb instance can then be acquired with the kb function, for example:
(kb :sesame-mem) ; an in-memory sesame kb
The kb
function can take keyword arguments such as :sesame-mem
or :jena-mem
or it can take names of several native jena or sesame objects or pre-constructed jena or sesame instances to create a kb
wrapper around (e.g., a jena Model
or a sesame Sail
).
kb's need some help knowing what the namespace mappings are, the server mappings can be brought down from a third party kb by calling (synch-ns-mappings my-kb)
or you can add a few:
(register-namespaces my-kb
'(("ex" "http://www.example.org/")
("rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#")
("foaf" "http://xmlns.com/foaf/0.1/")))
;;the return value is the new modified kb - hang onto it
Once you have a KB you can load rdf triple or files:
;;in parts
(add my-kb 'ex/KevinL 'rdf/type 'ex/Person)
;;as a triple
(add my-kb '(ex/KevinL foaf/name "Kevin Livingston"))
Query for RDF triples:
(ask-rdf my-kb nil nil 'ex/Person)
;;true
(query-rdf my-kb nil nil 'ex/Person)
;;((ex/KevinL rdf/type ex/Person))
Query with triple patterns (SPARQL):
(query my-kb '((?/person rdf/type ex/Person)
(?/person foaf/name ?/name)
(:optional ((?/person foaf/mbox ?/email)))))
;;({?/name "Kevin Livingston", ?/person ex/KevinL})
The examples also provide details on how to interact with a KB, with run-able poms: https://github.com/drlivingston/kr/tree/master/kr-examples
These include examples of connecting to a remote repository and a local in-memory repository.
More detailed uses can be found in the test cases for both the KB, RDF, and SPARQL APIs. They are here: https://github.com/drlivingston/kr/tree/master/kr-core/src/test/clojure/edu/ucdenver/ccp/test/kr
releases are deployed to clojars:
<repository>
<id>clojars.org</id>
<url>http://clojars.org/repo</url>
</repository>
the core dependency is kr-core:
<dependency>
<groupId>edu.ucdenver.ccp</groupId>
<artifactId>kr-core</artifactId>
<version>1.4.8</version>
</dependency>
but the core dependency is unnecessary if you are brining in either the sesame or jena implementations:
<dependency>
<groupId>edu.ucdenver.ccp</groupId>
<artifactId>kr-sesame-core</artifactId>
<version>1.4.8</version>
</dependency>
<dependency>
<groupId>edu.ucdenver.ccp</groupId>
<artifactId>kr-jena-core</artifactId>
<version>1.4.8</version>
</dependency>
open sourced by:
CCP Lab
University of Colorado Denver
primary developer: Kevin Livingston