Clojure API for RDF and SPARQL - provides consistent access to APIs including Jena and Sesame
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

Clojure API for RDF and SPARQL

The Knowledge Representation and Reasoning Tools library enables easy Clojure use of RDF and SPARQL, provinging a unified interface for both Jena and Sesame. (KR can be extended for other APIs and underlying triplestores.)


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. The library enables easy working with knowledge representations and knowledge bases, and provides support for some common tasks including forward-chaining and reification.

Release Notes

update: see the note on Sesame Versions

Basic Setup

The primary api functions you're likely to use come from the kr-core apis:

(use '
(use '
(use '

To actually get a KB instance to work with you'll need to make sure the implementation-specific code is loaded:

(require '
;; OR
(require '

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" "") 
                       ("rdf" "")
                       ("foaf" "")))
;;the return value is the new modified kb - hang onto it

Basic Use

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)

(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})

More Details

The examples also provide details on how to interact with a KB, with run-able poms:

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:


releases are deployed to clojars:


the core dependency is kr-core:


but the core dependency is unnecessary if you are brining in either the sesame or jena implementations:




open sourced by:
University of Colorado Denver
primary developer: Kevin Livingston