Skip to content

Instantly share code, notes, and snippets.

@RickMoynihan
Created January 25, 2015 18:23
Show Gist options
  • Select an option

  • Save RickMoynihan/d35dd2fd78872ce57cd2 to your computer and use it in GitHub Desktop.

Select an option

Save RickMoynihan/d35dd2fd78872ce57cd2 to your computer and use it in GitHub Desktop.
;; An example of using Grafter's Linked Data Sesame bindings to do
;; RDFS Inferencing.
(ns inferencing.core
(:require [grafter.rdf :refer [add statements subject predicate object prefixer]]
[grafter.rdf.io]
[grafter.rdf.formats :refer :all]
[grafter.rdf.ontologies.rdf :refer :all]
[grafter.rdf.repository :refer [memory-store repo query rdfs-inferencer]]
[grafter.rdf.templater :refer [graph]]))
(def ll (prefixer "http://lambdalounge.org.uk/"))
;; Define an RDFS Inferencing strore and populate it with some data.
(def db (let [db (repo (rdfs-inferencer (memory-store)))]
(add db (graph "http://lambdalounge.org.uk/graph/example"
[(ll "Cat") [rdf:a rdfs:Class]]
[(ll "Animal") [rdf:a rdfs:Class]]
[(ll "Cat") [rdfs:subClassOf (ll "Animal")]]
[(ll "Lion") [rdfs:subClassOf (ll "Cat")]]
;; Cats are Animals
;; Lions are Cats
;; Simba is a Lion
[(ll "simba") [rdf:a (ll "Lion")]]))
db))
(defn field-getter [f] (fn [x] (str (get x f))))
;; What are all the subclasses of Lion?
(map (field-getter "subclass")
(query db (str
"SELECT DISTINCT ?subclass WHERE {"
" <" (ll "Lion") "> <" rdfs:subClassOf "> ?subclass ."
"}")))
;; Yields...
;;
;; => ("http://lambdalounge.org.uk/Cat" "http://lambdalounge.org.uk/Animal" "http://www.w3.org/2000/01/rdf-schema#Resource" "http://lambdalounge.org.uk/Lion")
;; What classes is simba a member of?
(map (field-getter "class")
(query db (str
"SELECT DISTINCT ?class WHERE {"
" <" (ll "simba") "> <" rdf:a "> ?class ."
"}")))
;; ... simba is a ...
;;
;; => ("http://lambdalounge.org.uk/Lion" "http://www.w3.org/2000/01/rdf-schema#Resource" "http://lambdalounge.org.uk/Cat" "http://lambdalounge.org.uk/Animal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment