diff --git a/src/ldtab/thick_rdf.clj b/src/ldtab/thick_rdf.clj index 7ce90a8..f47a5a3 100644 --- a/src/ldtab/thick_rdf.clj +++ b/src/ldtab/thick_rdf.clj @@ -137,6 +137,7 @@ (translate-predicate-map (dissoc x "meta") prefix-2-base model))))) bnode)) + (defn parse-json [json] (let [success (try @@ -145,6 +146,7 @@ success (or (map? success) (coll? success) (seq? success))] + (if success (cs/parse-string json) json))) @@ -154,6 +156,35 @@ (and (string? input) (str/starts-with? input " (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 @@ -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)] diff --git a/src/ldtab/thin2thick.clj b/src/ldtab/thin2thick.clj index 66a1a51..3c344d2 100644 --- a/src/ldtab/thin2thick.clj +++ b/src/ldtab/thin2thick.clj @@ -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" @@ -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) @@ -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)))