Namespaced everything.

parent f0a67fae
......@@ -2,60 +2,62 @@
(def version "0.1.0-SNAPSHOT")
(set-env! :resource-paths #{"resources" "src/clj"}
:source-paths #{"test" "src/java"}
:dependencies '[[org.clojure/clojure "RELEASE"]
[adzerk/boot-test "RELEASE" :scope "test"]
[com.rpl/specter "RELEASE"]
[http-kit "2.3.0"]
[cheshire "5.10.0"]
[ring-cors "0.1.13"]
[com.taoensso/tufte "2.1.0"]])
:source-paths #{"test" "src/java"}
:dependencies '[[org.clojure/clojure "RELEASE"]
[adzerk/boot-test "RELEASE" :scope "test"]
[com.rpl/specter "RELEASE"]
[http-kit "2.3.0"]
[cheshire "5.10.0"]
[ring-cors "0.1.13"]
[com.taoensso/tufte "2.1.0"]
[org.apache.jena/apache-jena-libs "3.14.0" :extension "pom"]
])
(task-options!
pom {:project project
:version version
:description "FIXME: write description"
:url "http://example/FIXME"
:scm {:url "https://github.com/yourname/scg"}
:license {"Eclipse Public License"
"http://www.eclipse.org/legal/epl-v10.html"}})
:version version
:description "FIXME: write description"
:url "http://example/FIXME"
:scm {:url "https://github.com/yourname/scg"}
:license {"Eclipse Public License"
"http://www.eclipse.org/legal/epl-v10.html"}})
(deftask build
"Build and install the project locally."
[]
(comp (pom) (javac) (jar) (install)))
"Build and install the project locally."
[]
(comp (pom) (javac) (jar) (install)))
(deftask cooking
"Run a development REPL for the cooking grammar."
[]
(repl :init-ns 'scg.grammars.cooking))
"Run a development REPL for the cooking grammar."
[]
(repl :init-ns 'scg.grammars.cooking))
(require '[scg-server.core :as server])
(deftask dashboard
"Run the application."
[p port PORT int "Port web server listens on"]
(let [port (or port 8080)]
(comp
(with-pre-wrap fileset
(server/run port)
(info (str "Server started on port " port))
(info (str "Opening Dasboard on:\n"
"https://observablehq.com/@bultmann/scg-dashboard?scgport=" port))
(dosh "open" "-a" "Google Chrome" (str "https://observablehq.com/@bultmann/scg-dashboard?scgport=" port))
fileset)
(wait))))
"Run the application."
[p port PORT int "Port web server listens on"]
(let [port (or port 8080)]
(comp
(with-pre-wrap fileset
(server/run port)
(info (str "Server started on port " port))
(info (str "Opening Dasboard on:\n"
"https://observablehq.com/@bultmann/scg-dashboard?scgport=" port))
(dosh "open" "-a" "Google Chrome" (str "https://observablehq.com/@bultmann/scg-dashboard?scgport=" port))
fileset)
(wait))))
(deftask headless
"Run the application without UI."
[p port PORT int "Port web server listens on"]
(let [port (or port 8080)]
(comp
(with-pre-wrap fileset
(server/run port)
(info (str "Server started on port " port))
fileset)
(wait))))
"Run the application without UI."
[p port PORT int "Port web server listens on"]
(let [port (or port 8080)]
(comp
(with-pre-wrap fileset
(server/run port)
(info (str "Server started on port " port))
fileset)
(wait))))
(require '[adzerk.boot-test :refer [test]])
......@@ -298,18 +298,18 @@
(concat
(mapcat
(fn [id w]
[[id :form (string/lower-case (string/replace w #"\p{Punct}" ""))]
[id :isa :word]])
[[id :scg/form (string/lower-case (string/replace w #"\p{Punct}" ""))]
[id :scg/isa :scg/word]])
ids
words)
(map
(fn [[id1 id2]] [id1 :meets id2])
(fn [[id1 id2]] [id1 :scg/meets id2])
(partition 2 1 ids))
#_(mapcat
(fn [ids]
(map
(fn [id]
[id :after (first ids)])
[id :scg/after (first ids)])
(rest ids)))
(take-while seq (iterate rest ids)))))))
......
This diff is collapsed.
......@@ -17,12 +17,13 @@
(when (= (:request-method req) :post)
(let [first-parse-result (profile {}
(first (scg/run-grammar cooking/grammar (scg/sentence-kb (slurp (:body req))))))]
(clojure.pprint/pprint (rdf-ns/to-rdf (:fs first-parse-result)))
{:status 200
:headers {"content-type" "text/json"}
:body (cheshire/generate-string [(map (fn [t] (mapv str t))
(:fs first-parse-result))
(:cxn-path first-parse-result)])})))
{:status 200
:headers {"content-type" "text/json"}
:body (case (get-in req [:headers "accept"])
"text/turtle" (rdf-ns/to-rdf (:fs first-parse-result))
"application/json" (cheshire/generate-string [(map (fn [t] (mapv str t))
(:fs first-parse-result))
(:cxn-path first-parse-result)]))})))
(defn run [port]
(http/run-server
......
(ns scg-server.rdf-ns
(:require [scg.core :as scg]))
(:require [scg.core :as scg]
[scg-server.semantic-web :as semweb]
[clojure.java.io :as io])
(:import org.apache.jena.riot.RDFDataMgr
org.apache.jena.riot.Lang
java.io.StringWriter
java.net.URL))
(def ns {:type "https://www.w3.org/1999/02/22-rdf-syntax-ns#type"
:label "https://www.w3.org/2000/01/rdf-schema#label"
:type-variable "https://gitlab.informatik.uni-bremen.de/vanja/scg#variable"})
(def exceptions {:scg/isa "https://www.w3.org/1999/02/22-rdf-syntax-ns#type"})
(defn expand-variables-to-entities [triples]
(into #{}
(mapcat (fn [t]
(into [(into [] (map (fn [i]
(if (scg/variable? i)
(:id i)
i)))
(def rdf-ns
{"rdf" "https://www.w3.org/1999/02/22-rdf-syntax-ns#"
"rdfs" "https://www.w3.org/2000/01/rdf-schema#"
"scg" "https://gitlab.informatik.uni-bremen.de/vanja/scg#"
"cg" "https://gitlab.informatik.uni-bremen.de/vanja/scg/-/blob/master/src/clj/scg/grammars/cooking.clj#"
"say" "http://www.ease-crc.org/ont/EASE-SAY.owl#"
"li" "https://gitlab.informatik.uni-bremen.de/vanja/preliminary-linguistic-cooking-ontology/raw/master/linguisticInfo.owl#"})
(def expand-variables-to-entities
(mapcat (fn [t]
(into [(into []
(map (fn [i]
(if (scg/variable? i)
(:id i)
i)))
t)]
(mapcat (fn [i]
(when (scg/variable? i)
[[(:id i) (ns :type) (ns :type-variable)],
[(:id i) (ns :label) (:name i)]])))
t)))
triples))
(when (scg/variable? i)
[[(:id i) :isa :scg/variable],
[(:id i) :rdfs/label (:name i)]])))
t))))
(def keywords->uri
(map (fn [f]
(into []
(map (fn [i]
(if (keyword? i)
(URL. (or (exceptions i)
(str (rdf-ns (namespace i)) (name i))))
i)))
f))))
(defn to-rdf [triples]
triples)
(with-open [sw (StringWriter.)]
(as-> triples %
(into #{}
(comp
expand-variables-to-entities
keywords->uri)
%)
(semweb/add! (semweb/model) %)
(RDFDataMgr/write sw % Lang/TURTLE))
(.toString sw)))
(ns scg-server.semantic-web
(:import (org.apache.jena.rdf.model
Model ModelFactory
SimpleSelector Statement
Resource Property RDFNode)
(org.apache.jena.query QueryExecutionFactory QueryExecution QuerySolution DatasetFactory QuerySolutionMap)
(java.util List UUID)
(java.net URI)
(org.apache.jena.rdf.model.impl ResourceImpl)
(org.apache.jena.ontology OntModelSpec)))
(defn model
([] (ModelFactory/createDefaultModel))
([url] (.read ^Model (model) url)))
(defn ontology-model [path]
(let [m (ModelFactory/createOntologyModel OntModelSpec/RDFS_MEM)]
(.read m path)
(.loadImports m)
m))
(defn to-resource [^Model model value]
(let [converted (cond
(instance? UUID value)
(str "urn:uuid:" value)
:else
(str value))]
(.createResource model converted)))
(defn to-predicate [^Model model value]
(let [converted (cond
(instance? UUID value)
(str "urn:uuid:" value)
:else
(str value))]
(.createProperty model converted)))
(defn to-rdf [^Model model value]
(cond
(instance? ResourceImpl value) value
(instance? UUID value)
(.createResource model (str "urn:uuid:" value))
(instance? URI value)
(.createResource model (str value))
:else
(.createTypedLiteral model value)))
(defn from-rdf [node]
(cond
(nil? node) nil
(.isLiteral node)
(.getValue (.asLiteral node))
(.isURIResource node)
(let [uri (.getURI node)
[_ uuid] (re-matches
#"urn:uuid:(\p{XDigit}{8}-\p{XDigit}{4}-\p{XDigit}{4}-\p{XDigit}{4}-\p{XDigit}{12})"
uri)]
(if uuid
(UUID/fromString uuid)
(URI. uri)))
:else
node))
(defn fact [stmt]
[(from-rdf (.getSubject stmt))
(from-rdf (.getPredicate stmt))
(from-rdf (.getObject stmt))])
(defn facts
([^Model model]
(into #{}
(map fact)
(iterator-seq (.listStatements model))))
([^Model model [e a v]]
(into #{}
(map fact)
(iterator-seq
(.listStatements model
(SimpleSelector.
^Resource (when e (to-resource model e))
^Property (when a (to-predicate model a))
^RDFNode (when v (to-rdf model v))))))))
(defn to-statement [^Model model [e a v]]
(when (nil? e)
(throw (ex-info
(str "Entity must not be nil in\n[nil\n" a "\n" v "]\nrdf statement.")
{:triple [e a v]})))
(when (nil? a)
(throw (ex-info
(str "Attribute must not be nil in \n[" e " nil\n" v "]\n rdf statement.")
{:triple [e a v]})))
(when (nil? v)
(throw (ex-info
(str "Value must not be nil in \n[" e "\n" a "\n nil]\n rdf statement.")
{:triple [e a v]})))
(.createStatement
model
^Resource (to-resource model e)
^Property (to-predicate model a)
^RDFNode (to-rdf model v)))
(defn add! [^Model model facts]
(let [statements (into []
(comp (remove nil?)
(map #(to-statement model %)))
facts)]
(.add model ^List statements)))
(defn skolemize-scalar [^Model model new-ids node]
(if (.isAnon node)
(let [bid (.getId node)]
(if (contains? new-ids bid)
[(.createResource model ^String (get new-ids bid))
new-ids]
(let [new-id (str "urn:uuid:" (UUID/randomUUID))]
[(.createResource model new-id)
(assoc new-ids
bid new-id)])))
[(.inModel node model) new-ids]))
(defn skolemize [^Model m]
(let [^Model skolemized-model (model)]
(first
(reduce
(fn [[skolemized-model new-ids] ^Statement stmt]
(let [[e new-ids] (skolemize-scalar skolemized-model new-ids (.getSubject stmt))
a (.inModel (.getPredicate stmt) skolemized-model)
[v new-ids] (skolemize-scalar skolemized-model new-ids (.getObject stmt))]
[(.add skolemized-model
(.createStatement skolemized-model
^Resource e
^Property a
^RDFNode v))
new-ids]))
[skolemized-model {}]
(iterator-seq (.listStatements m))))))
(defn construct [model-or-map bindings & query-string-parts]
(let [query-string
(clojure.string/join "\n" query-string-parts)
dataset (DatasetFactory/create)
binding-map (QuerySolutionMap.)]
(if (map? model-or-map)
(do
(.setDefaultModel dataset (:default model-or-map))
(doseq [[name model] (dissoc model-or-map :default)]
(.addNamedModel dataset name model)))
(.setDefaultModel dataset model-or-map))
(doseq [[k v] bindings]
(.add binding-map (name k) (to-rdf (model) v)))
(with-open [^QueryExecution query-execution (QueryExecutionFactory/create query-string dataset binding-map)]
(.execConstruct query-execution))))
(defn select [model-or-map bindings & query-string-parts]
(let [query-string
(clojure.string/join "\n" query-string-parts)
dataset (DatasetFactory/create)
binding-map (QuerySolutionMap.)]
(if (map? model-or-map)
(do
(.setDefaultModel dataset (:default model-or-map))
(doseq [[name model] (dissoc model-or-map :default)]
(.addNamedModel dataset name model)))
(.setDefaultModel dataset model-or-map))
(doseq [[k v] bindings]
(when v
(.add binding-map (name k) (to-rdf (model) v))))
(with-open [^QueryExecution query-execution (QueryExecutionFactory/create query-string dataset binding-map)]
(let [resultset (.execSelect query-execution)
var-names (.getResultVars resultset)]
(into #{}
(map (fn [r]
(into {}
(map (fn [n]
[(keyword n) (from-rdf (.get r n))]))
var-names)))
(iterator-seq resultset))))))
(defn id [] (str "urn:uuid:" (UUID/randomUUID)))
(defn get-entity [model entity-id]
(if-not (or (instance? UUID entity-id)
(instance? URI entity-id))
entity-id
(let [f (facts model [entity-id nil nil])]
(apply
merge-with
into
{:id entity-id}
(map (fn [[e a v]] {a [v]}) f)))))
(defn new-entity [entity_map]
(let [entity-id (or (:id entity_map) (id))]
(into []
(map (fn [[a v]] [entity-id a v]))
entity_map)))
(defn uri-prefix [prefix]
(fn [fragment]
(str prefix (name fragment))))
(defn sparql-prefix [prefix ns]
(str "PREFIX " prefix ":<" ns ">"))
(def rdf-s (sparql-prefix "rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
(def rdf-u (uri-prefix "http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
(def rdfs-s (sparql-prefix "rdfs" "http://www.w3.org/2000/01/rdf-schema#"))
(def rdfs-u (uri-prefix "http://www.w3.org/2000/01/rdf-schema#"))
(def owl-s (sparql-prefix "owl" "http://www.w3.org/2002/07/owl#"))
(def owl-u (uri-prefix "http://www.w3.org/2002/07/owl#"))
(defn reify-statement [[e a v] & [reification-id]]
(let [reification-id (or reification-id (id))]
[[reification-id (rdf-u :type) (rdf-u :Statement)]
[reification-id (rdf-u :subject) e]
[reification-id (rdf-u :predicate) a]
[reification-id (rdf-u :object) v]]))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment