Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jena 4.10 + cleanup #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,5 @@ docs/
!gitkeep

.cpcache
.calva
.calva
.portal/
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,22 @@ When executing against TDB it is recommended to use [transactions](https://jena.
to prevent against data corruption.
You can set the flags yourself on the `Dataset` or use the `with-transaction` macro, although support for this is somewhat lacking.

## Running tests

We use [kaoch test runner](https://github.com/lambdaisland/kaocha)

See https://practical.li/clojure/testing/test-runners/kaocha-test-runner/#run-kaocha


```shell

clojure -X:test/run

clojure -X:test/run :fail-fast? false

```


## TODO
- Authentication support for SPARQL Endpoints
- Support [SPARQL S-Expressions](https://jena.apache.org/documentation/notes/sse.html) (?)
Expand Down
35 changes: 27 additions & 8 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
{:paths ["src" "resources"]
:deps
{cider/cider-nrepl {:mvn/version "0.25.9"}
instaparse/instaparse {:mvn/version "1.4.1"}
org.apache.jena/jena-core {:mvn/version "4.3.1"}
org.apache.jena/jena-arq {:mvn/version "4.3.1"}
org.apache.jena/jena-tdb {:mvn/version "4.3.1"}}
:aliases {:test {:extra-paths []
:extra-deps
{expectations/expectations {:mvn/version "2.1.3"}}}}
{instaparse/instaparse {:mvn/version "1.4.12"}
org.apache.jena/jena-core {:mvn/version "4.10.0"}
org.apache.jena/jena-arq {:mvn/version "4.10.0"}
org.apache.jena/jena-tdb {:mvn/version "4.10.0"}}
:aliases {:dev
{:extra-deps
{cider/cider-nrepl {:mvn/version "0.43.0"}}}
:test/run
{:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.77.1236"}
expectations/expectations {:mvn/version "2.1.10"}}
:main-opts ["-m" "kaocha.runner"]
:exec-fn kaocha.runner/exec-fn
:exec-args {:fail-fast? true
:randomize? false}}

;; Kaocha test runner in watch mode
;; clojure -X:test/watch
:test/watch
{:extra-paths ["test"]
:extra-deps {lambdaisland/kaocha {:mvn/version "1.77.1236"}
expectations/expectations {:mvn/version "2.1.10"}}
:main-opts ["-m" "kaocha.runner" "--watch" "--fail-fast" "--skip-meta" ":slow"]
:exec-fn kaocha.runner/exec-fn
:exec-args {:watch? true
:randomize? false
:fail-fast? true}}}
:mvn/repos {"central" {:url "https://repo1.maven.org/maven2/"}
"clojars" {:url "https://repo.clojars.org/"}
"oss-snapshots-repo" {:url "https://oss.sonatype.org/content/groups/public"}}}
6 changes: 3 additions & 3 deletions src/yesparql/core.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns yesparql.core
(:require [yesparql.util :refer [slurp-from-classpath]]
[yesparql.generate :refer [generate-var]]
[yesparql.queryfile-parser :refer [parse-tagged-queries]]))
(:require [yesparql.generate :refer [generate-var]]
[yesparql.queryfile-parser :refer [parse-tagged-queries]]
[yesparql.util :refer [slurp-from-classpath]]))

;; Most of the non-SPARQL code is directly from [Yesql](https://github.com/krisajenkins/yesql/blob/devel/src/yesql/core.clj) by Kris Jenkins

Expand Down
14 changes: 5 additions & 9 deletions src/yesparql/generate.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
(ns yesparql.generate
(:require
[clojure.set :as set]
[clojure.string :refer [join lower-case]]
[yesparql.util :refer [create-root-var]]
[yesparql.sparql :as sparql]
[yesparql.types :refer [map->Query]])
(:import [yesparql.types Query]
[org.apache.jena.shared PrefixMapping PrefixMapping$Factory]
[org.apache.jena.query ParameterizedSparqlString Syntax]))
(:require [clojure.string :refer [join]]
[yesparql.sparql :as sparql]
[yesparql.util :refer [create-root-var]])
(:import [org.apache.jena.query Syntax]
[org.apache.jena.shared PrefixMapping]))

(defn query-type
[^String name]
Expand Down
5 changes: 3 additions & 2 deletions src/yesparql/instaparse_util.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(ns yesparql.instaparse-util
(:require [instaparse.core :as instaparse])
(:require [instaparse.core :as instaparse]
[instaparse.failure :as failure])
(:import [java.io StringWriter]))

(defn process-instaparse-result
[parse-results context]
(if-let [failure (instaparse/get-failure parse-results)]
(binding [*out* (StringWriter.)]
(instaparse.failure/pprint-failure failure)
(failure/pprint-failure failure)
(throw (ex-info (str *out*)
failure)))
(if (second parse-results)
Expand Down
7 changes: 4 additions & 3 deletions src/yesparql/queryfile_parser.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
(:require [clojure.java.io :as io]
[clojure.string :refer [join trim]]
[instaparse.core :as instaparse]
[instaparse.transform :as transform]
[yesparql.instaparse-util :refer [process-instaparse-result]]
[yesparql.types :refer [map->Query]]
[yesparql.util :refer [str-non-nil]]
[yesparql.instaparse-util :refer [process-instaparse-result]]))
[yesparql.util :refer [str-non-nil]]))

(def parser
(let [url (io/resource "yesparql/queryfile.bnf")]
Expand All @@ -31,7 +32,7 @@
"Parses a string with Yesparql's defqueries syntax into a sequence of maps."
[text]
(process-instaparse-result
(instaparse/transform
(transform/transform
parser-transforms
(instaparse/parses parser
(str text "\n") ;;; TODO This is a workaround for files with no end-of-line marker.
Expand Down
80 changes: 45 additions & 35 deletions src/yesparql/sparql.clj
Original file line number Diff line number Diff line change
@@ -1,30 +1,40 @@
(ns yesparql.sparql
(:import
[java.lang IllegalArgumentException]
[java.net URL URI]
[org.apache.jena.graph Node]
[org.apache.jena.update
Update UpdateAction
UpdateFactory UpdateProcessor
UpdateRequest UpdateExecutionFactory]
[org.apache.jena.rdf.model Model
StmtIterator Statement Resource Property
RDFNode Resource Literal]
[org.apache.jena.shared PrefixMapping]
[org.apache.jena.query Dataset]
[org.apache.jena.sparql.resultset RDFOutput]
[org.apache.jena.rdf.model ModelFactory]
[org.apache.jena.datatypes BaseDatatype]
[org.apache.jena.graph Node Node_Literal]
[org.apache.jena.sparql.core Var ResultBinding]
[org.apache.jena.query
Query QuerySolution QueryExecution Syntax
QueryExecutionFactory QueryFactory QuerySolutionMap
ParameterizedSparqlString ResultSetFactory
ResultSetRewindable ResultSet ResultSetFormatter]))

(defn ^java.io.ByteArrayOutputStream output-stream []
(java.io.ByteArrayOutputStream.))
(:import [java.lang IllegalArgumentException]
[java.net URI URL]
[java.io ByteArrayOutputStream]
[org.apache.jena.datatypes BaseDatatype]
[org.apache.jena.graph Node]
[org.apache.jena.graph Node Node_Literal]
[org.apache.jena.query Dataset]
[org.apache.jena.query
ParameterizedSparqlString
Query
QueryExecution
QueryExecutionFactory
QueryFactory
ResultSet
ResultSetFactory
ResultSetFormatter
ResultSetRewindable
Syntax]
[org.apache.jena.rdf.model
Literal
Model
RDFNode
Statement]
[org.apache.jena.rdf.model ModelFactory]
[org.apache.jena.shared PrefixMapping]
[org.apache.jena.sparql.core ResultBinding Var]
[org.apache.jena.sparql.resultset RDFOutput]
[org.apache.jena.update
UpdateExecutionFactory
UpdateFactory
UpdateProcessor
UpdateRequest]))

(defn output-stream
^ByteArrayOutputStream []
(ByteArrayOutputStream.))

(defn reset-if-rewindable!
"Resets a `RewindableResulSet`
Expand Down Expand Up @@ -137,8 +147,8 @@

(defn keyword-str [kw] (if (keyword? kw) (name kw) kw))

(defn ^Literal clj->literal
[{:keys [value type lang]}]
(defn clj->literal
^Literal [{:keys [value type lang]}]
(cond
type (.createTypedLiteral
default-model value (BaseDatatype. ^String (str type)))
Expand All @@ -147,11 +157,11 @@
:else (.createTypedLiteral
default-model value)))

(defn ^ParameterizedSparqlString parameterized-query
[^String statement]
(defn parameterized-query
^ParameterizedSparqlString [^String statement]
(ParameterizedSparqlString. statement))

(defn ^ParameterizedSparqlString query-with-bindings
(defn query-with-bindings
"The `query` can be provided with a map of `bindings`.
Each binding is a String->URL, String->URI, String->Node or String->RDFNode.
Any other type (e.g. String, Float) will be set as Literal.
Expand All @@ -161,7 +171,7 @@
which will be coerced to the appropriate `Literal` automatically.

Does not warn when setting a binding that does not exist."
[^ParameterizedSparqlString pq ^PrefixMapping prefixes bindings]
^ParameterizedSparqlString [^ParameterizedSparqlString pq ^PrefixMapping prefixes bindings]
(doall
(map
(fn [[var resource]]
Expand Down Expand Up @@ -418,10 +428,10 @@
^UpdateRequest update
^Dataset connection))

(defn ^UpdateRequest as-update
([^String ustr]
(defn as-update
(^UpdateRequest [^String ustr]
(as-update Syntax/defaultUpdateSyntax ustr))
([^Syntax syntax ^String qstr]
(^UpdateRequest [^Syntax syntax ^String qstr]
(UpdateFactory/create qstr syntax)))

(defn update!
Expand Down
13 changes: 6 additions & 7 deletions src/yesparql/tdb.clj
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
(ns yesparql.tdb
(:import
[org.apache.jena.query Dataset DatasetFactory]
[org.apache.jena.tdb TDBFactory TDBLoader StoreConnection TDB]))
(:import [org.apache.jena.query Dataset DatasetFactory]
[org.apache.jena.tdb TDBFactory]))

(defn ^Dataset create-file-based
(defn create-file-based
"Creates a new TDB-backed `Dataset` in the `directory` (absolute path)"
[^String directory]
^Dataset [^String directory]
(TDBFactory/createDataset directory))

(defn ^Dataset create-in-memory
(defn create-in-memory
"Create an in-memory, modifiable TDB `Dataset`"
[]
^Dataset []
(TDBFactory/createDataset))

(defn ^Dataset create-bare
Expand Down
3 changes: 1 addition & 2 deletions src/yesparql/util.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
(ns yesparql.util
(:require [clojure.java.io :as io]
[clojure.string :as string]
[clojure.pprint :refer [pprint]])
[clojure.string :as string])
(:import [java.io FileNotFoundException]))

(defn underscores-to-dashes
Expand Down