├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── doc └── intro.md ├── project.clj ├── src └── jena_clj │ └── triplestore.clj └── test └── jena_clj └── core_test.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | All notable changes to this project will be documented in this file. This change log follows the conventions of [keepachangelog.com](http://keepachangelog.com/). 3 | 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jena-clj 2 | 3 | An idiomatic clojure wrapper to the Jena ontology management library. 4 | 5 | ## Install 6 | 7 | ### Leiningen / Boot: 8 | 9 | ```clj 10 | [jena-clj "0.1.0"] 11 | ``` 12 | 13 | ### Gradle 14 | 15 | ``` gradle 16 | compile "jena-clj:jena-clj:0.1.0" 17 | ``` 18 | 19 | ### Maven 20 | ``` xml 21 | 22 | jena-clj 23 | jena-clj 24 | 0.1.0 25 | 26 | ``` 27 | 28 | 29 | ## Usage 30 | 31 | Some usage examples: 32 | 33 | ``` clj 34 | (require '[jena-clj.triplestore :as ts]) 35 | (import '[org.apache.jena.query ReadWrite]) 36 | (defonce db (ts/init-database "path/to/triplestore") ; If it doesn't exist, it creates one 37 | 38 | (with-transaction db ReadWrite/WRITE 39 | (ts/insert-rdf db "path/to/rdf/or/ttl/file")) ; Loads a whole RDF file into triplestore 40 | 41 | (with-transaction db ReadWrite/READ 42 | (ts/select-query 43 | "prefix rdf: 44 | SELECT ?uri ?name 45 | WHERE {?uri rdf:name ?name}")) ; Returns a lazy sequence with all results. 46 | ``` 47 | Take a look at triplestore.clj source for more! 48 | 49 | ### Features 50 | 51 | - TDB Triplestore management 52 | - SPARQL queries 53 | 54 | ## License 55 | 56 | Copyright © 2017 setzer22 57 | 58 | Distributed under the GNU General Public License v3.0 59 | 60 | -------------------------------------------------------------------------------- /doc/intro.md: -------------------------------------------------------------------------------- 1 | # Introduction to sparql 2 | 3 | TODO: write [great documentation](http://jacobian.org/writing/what-to-write/) 4 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject jena-clj "0.1.0" 2 | :description "An idiomatic Clojure wrapper to the Jena API" 3 | :url "https://github.com/setzer22/jena-clj" 4 | :repositories {"mvnrepository" {:url "https://mvnrepository.com/"} 5 | "jenarepo" {:url "https://repository.apache.org/content/repositories/releases/"}} 6 | :license {:name "GNU General Public License v3.0" 7 | :url "https://www.gnu.org/licenses/gpl-3.0.en.html"} 8 | :dependencies [[org.clojure/clojure "1.8.0"] 9 | [org.apache.jena/jena-core "3.2.0"] 10 | [org.apache.jena/jena-arq "3.2.0"] 11 | [org.apache.jena/jena-iri "3.2.0"] 12 | [org.apache.jena/jena-tdb "3.2.0"]] 13 | :main ^:skip-aot sparql.core 14 | :target-path "target/%s" 15 | :profiles {:uberjar {:aot :all}}) 16 | -------------------------------------------------------------------------------- /src/jena_clj/triplestore.clj: -------------------------------------------------------------------------------- 1 | ;; This module consists of an idiomatic clojure wrapper for the Jena API. 2 | 3 | (ns jena-clj.triplestore 4 | (:import [clojure.lang Keyword] 5 | [org.apache.jena.query Dataset] 6 | [org.apache.jena.rdf.model Model ModelFactory ResourceFactory] 7 | [org.apache.jena.query Query QueryFactory ResultSet QueryExecution QueryExecutionFactory ReadWrite] 8 | [org.apache.jena.tdb TDBFactory])) 9 | 10 | (defmacro with-transaction 11 | "Custom statement. Syntax: 12 | (with-transaction ReadWrite/[READ|WRITE] db 13 | ... body ...) 14 | The body is wrapped in a transaction of the supplied type (read or write) that 15 | works inside the supplied Jena Dataset. If an exception is thrown from whithin 16 | the body, the transaction is aborted and the exception is thrown. Otherwise 17 | the last expression in the body is returned. 18 | 19 | WARNING: Do not return the database or its contents (e.g. Dataset, Model) from 20 | this statement or the database will escape the scope of the transaction throwing 21 | an exception. 22 | 23 | WARNING2: Beware when creating a lazy sequence of the resultset. You must consume it 24 | fully before trying to add new triples into the triplestore. Use doall if needed." 25 | [dataset rw-type & body] 26 | `(do (assert (and (instance? Dataset ~dataset) (instance? ReadWrite ~rw-type)) 27 | (str "Error in with-transaction: Wrong argument types: " (type ~dataset) ", " (type ~rw-type) "\n\n")) 28 | (.begin ~dataset ~rw-type) 29 | (try (let [result# (do ~@body)] (.commit ~dataset) result#) 30 | (catch Throwable e# (.printStackTrace e#) (.abort ~dataset) (println "abort") (throw e#))))) 31 | 32 | (defn init-database 33 | "Connects to the triplestore at given filesystem path and returns a Jena 34 | Dataset object that represents the connection." 35 | [^String path] 36 | (TDBFactory/createDataset path)) 37 | 38 | (defn mk-string-literal 39 | "Creates a Jena string literal object from a string" 40 | [^String s] (ResourceFactory/createStringLiteral s)) 41 | 42 | (defn mk-uri 43 | "Creates a Jena Resource (URI) object from a string" 44 | [^String s] (ResourceFactory/createResource s)) 45 | 46 | (defn mk-property 47 | "Creates a Jena Property object from a string" 48 | [^String s] (ResourceFactory/createProperty s)) 49 | 50 | ;; NOTE: insert-rdf, insert-model and insert triple explicitly return nil because 51 | ;; when used in a last step of a transaction, they would return the inner 52 | ;; model, which would escape the scope of the transaction and then cause an 53 | ;; exception. 54 | 55 | (defn insert-rdf 56 | "Takes a tdb dataset and a path to an rdf file and inserts all the triples 57 | from the rdf file into the triplestore" 58 | [^Dataset dataset ^String rdf-path] 59 | (.read (.getDefaultModel dataset) rdf-path) 60 | nil) 61 | 62 | (defn insert-model 63 | "Takes a tdb dataset and a model and inserts all triples from the model 64 | into the triplestore" 65 | [^Dataset dataset ^Model model] 66 | (.add (.getDefaultModel dataset) model) 67 | nil) 68 | 69 | (defn insert-triple 70 | [^Dataset dataset, subject, predicate, object] 71 | (.add (.getDefaultModel dataset) 72 | (ResourceFactory/createStatement 73 | subject predicate object)) 74 | nil) 75 | 76 | (defn resultset->seq 77 | "Returns a clojure lazy sequence from a Jena ResultSet" 78 | [^ResultSet resultset] 79 | (when (.hasNext resultset) 80 | (lazy-seq (cons (.nextSolution resultset) (resultset->seq resultset))))) 81 | 82 | (defn- query 83 | "Private function, runs a query of query-type in the specified Jena dataset" 84 | [^Keyword query-type ^Dataset dataset, ^String sparql-query] 85 | (assert (#{:select :construct} query-type) "Query must be one of the following: [:select, :construct]") 86 | (let [compiled-query (QueryFactory/create sparql-query) 87 | execution (QueryExecutionFactory/create compiled-query dataset)] 88 | (({:select #(resultset->seq (.execSelect %)) 89 | :construct #(.execConstruct %)} query-type) execution))) 90 | 91 | (defn select-query 92 | "Runs a SELECT query in the specified Jena dataset." 93 | [^Dataset dataset, ^String sparql-query] 94 | (query :select dataset sparql-query)) 95 | 96 | (defn construct-query 97 | "Runs a CONSTRUCT query in the specified Jena dataset." 98 | [^Dataset dataset, ^String sparql-query] 99 | (query :construct dataset sparql-query)) 100 | -------------------------------------------------------------------------------- /test/jena_clj/core_test.clj: -------------------------------------------------------------------------------- 1 | (ns jena-clj.core-test 2 | (:require [clojure.test :refer :all] 3 | [sparql.core :refer :all])) 4 | 5 | "TODO: Add tests" 6 | 7 | (deftest a-test 8 | (testing "FIXME, I fail." 9 | (is (= 0 1)))) 10 | --------------------------------------------------------------------------------