Skip to content

Commit

Permalink
Merge pull request #34 from ontodev/existential-blank-nodes
Browse files Browse the repository at this point in the history
Existential blank nodes in LDTab
  • Loading branch information
ckindermann authored Nov 12, 2024
2 parents 8d4f94f + 87e5548 commit 85ae634
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 3 deletions.
34 changes: 33 additions & 1 deletion src/ldtab/thick_rdf.clj
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
(translate-predicate-map (dissoc x "meta") prefix-2-base model)))))
bnode))


(defn parse-json
[json]
(let [success (try
Expand All @@ -145,6 +146,7 @@
success (or (map? success)
(coll? success)
(seq? success))]

(if success
(cs/parse-string json)
json)))
Expand All @@ -154,6 +156,35 @@
(and (string? input)
(str/starts-with? input "<wiring:blanknode")))

(defn blanknode-triple-map
[blanknode-triples]
(cs/generate-string
(into {}
(map (fn [{:keys [predicate object datatype]}]
[predicate [{"object" (parse-json object),
"datatype" datatype}]])
blanknode-triples))))


(defn merge-existential-blanknodes
"Merge thin triples belonging to the same existential blank nodes into a 'raw' LDTab triple."
[triples]
(let [blanknodes (filter #(is-wiring-blanknode (:subject %)) triples)
blanknode-2-triples (group-by :subject blanknodes)
complex-blanknodes (into {} (filter (fn [[k v]] (> (count v) 1)) blanknode-2-triples))
triples (remove #(contains? complex-blanknodes (:subject %)) triples)
raw-blank-nodes (map (fn [[k v]] {:assertion (:assertion (first v)),
:retraction (:retraction (first v)),
:graph (:graph (first v)),
:subject k,
:predicate "unknown",
:object (blanknode-triple-map v),
:datatype "_JSONMAP",
:annotation (:annotation (first v))})
complex-blanknodes)
final (concat triples raw-blank-nodes)]
final))

(defn thick-2-rdf-model ^Model
[thick-triple prefixes]
(let [;{:keys [assertion retraction graph s p o datatype annotation]} thick-triple
Expand All @@ -178,7 +209,8 @@

(defn triples-2-rdf-model-stream
[thick-triples prefixes output]
(let [out-stream (io/output-stream output)
(let [thick-triples (merge-existential-blanknodes thick-triples)
out-stream (io/output-stream output)
model (set-prefix-map (ModelFactory/createDefaultModel) prefixes)
prefix-map (.lock model)
writer-stream (StreamRDFWriter/getWriterStream out-stream RDFFormat/TURTLE_BLOCKS)]
Expand Down
30 changes: 28 additions & 2 deletions src/ldtab/thin2thick.clj
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@
datatype))
:else "ERROR")))


(defn existential-blanknode-2-triples
[existential-blanknode]
;(print "existblanknode: " existential-blanknode)
(let [blanknode (:subject existential-blanknode)
object (:object existential-blanknode)
datatype (:datatype existential-blanknode)
triples (if (= datatype "_JSONMAP")
(map (fn [[k v]] {:subject blanknode,
:predicate k,
:object (get (first v) "object"),
:datatype (get (first v) "datatype")}) object)
[existential-blanknode])]
;(print "translated: " triples)
triples))

(defn split-existential-blanknode-encoding
[triples]
(let [existential-blanknodes (filter (fn [x] (is-wiring-blanknode (:subject x))) triples)
triples (remove (fn [x] (is-wiring-blanknode (:subject x))) triples)
existential-blanknode-triples (mapcat existential-blanknode-2-triples existential-blanknodes)
triples (concat existential-blanknode-triples triples)]
triples))

(defn encode-object
"Given a triple t = [s p o] and a map from subject nodes to its triples,
returns predicate map for the o"
Expand Down Expand Up @@ -286,7 +310,8 @@
rdf-lists (map rdf-list/encode-rdf-list annotations)
sorted (map sort-json rdf-lists)
hashed (map hash-existential-subject-blanknode sorted)
normalised (map #(cs/parse-string (cs/generate-string %)) hashed)];TODO: stringify keys - this is a (probably an inefficient?) workaround
split (split-existential-blanknode-encoding hashed)
normalised (map #(cs/parse-string (cs/generate-string %)) split)];TODO: stringify keys - this is a (probably an inefficient?) workaround
normalised))
([triples iri2prefix]
(let [raw-thick-triples (thin-2-thick-raw triples iri2prefix)
Expand All @@ -301,5 +326,6 @@
rdf-lists (map rdf-list/encode-rdf-list annotations)
sorted (map sort-json rdf-lists)
hashed (map hash-existential-subject-blanknode sorted)
normalised (map #(cs/parse-string (cs/generate-string %)) hashed)];TODO: stringify keys - this is a (probably an inefficient?) workaround
split (split-existential-blanknode-encoding hashed)
normalised (map #(cs/parse-string (cs/generate-string %)) split)];TODO: stringify keys - this is a (probably an inefficient?) workaround
normalised)))

0 comments on commit 85ae634

Please sign in to comment.