Skip to content

Commit c2bef37

Browse files
authored
Merge pull request #27 from fluree/feature/new-fluree-vocabulary
Feature/new fluree vocabulary
2 parents 7289084 + 232408a commit c2bef37

File tree

7 files changed

+219
-88
lines changed

7 files changed

+219
-88
lines changed

resources/contexts/fluree/v1.edn

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{:protected true
2+
"ledger" {:id "https://ns.flur.ee/ledger#ledger"}
3+
"defaultContext" {:type :json :id "https://ns.flur.ee/ledger#defaultContext" }
4+
"delete" {:type :json :id "https://ns.flur.ee/ledger#delete"}
5+
"insert" {:type :json :id "https://ns.flur.ee/ledger#insert"}
6+
"opts" {:type :json :id "https://ns.flur.ee/ledger#opts"}
7+
"where" {:type :json :id "https://ns.flur.ee/ledger#where"}
8+
"values" {:type :json :id "https://ns.flur.ee/ledger#values"}}

resources/contexts/fluree/v1.json

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"@context": {
3+
"@protected": true,
4+
"ledger": {"@id": "https://ns.flur.ee/ledger#ledger"},
5+
"defaultContext": {
6+
"@type": "@json",
7+
"@id": "https://ns.flur.ee/ledger#defaultContext"
8+
},
9+
"where": {
10+
"@type": "@json",
11+
"@id": "https://ns.flur.ee/ledger#where"
12+
},
13+
"values": {
14+
"@type": "@json",
15+
"@id": "https://ns.flur.ee/ledger#values"
16+
},
17+
"delete": {
18+
"@type": "@json",
19+
"@id": "https://ns.flur.ee/ledger#delete"
20+
},
21+
"insert": {
22+
"@type": "@json",
23+
"@id": "https://ns.flur.ee/ledger#insert"
24+
},
25+
"opts": {
26+
"@type": "@json",
27+
"@id": "https://ns.flur.ee/ledger#opts"
28+
}
29+
}
30+
}

src/fluree/json_ld/impl/external.cljc

+5-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616
"https://w3id.org/openbadges#" "org.w3id.openbadges.edn"
1717
"https://purl.imsglobal.org/spec/clr/vocab#" "org.imsglobal.spec.clr.vocab.edn"})
1818

19-
(def context->file {"https://ns.flur.ee/ledger/v1"
19+
(def context->file {"https://ns.flur.ee"
20+
{:source "contexts/fluree/v1.json"
21+
:parsed "contexts/fluree/v1.edn"}
22+
23+
"https://ns.flur.ee/ledger/v1"
2024
{:source "contexts/fluree/ledger/v1.jsonld"
2125
:parsed "contexts/fluree/ledger/v1.edn"}
2226

src/fluree/json_ld/processor/api.clj

+69-40
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
(parsed [x]))
1818

1919
(extend-protocol Parseable
20+
jakarta.json.EmptyArray
21+
(parsed [x] [])
2022
;; JsonArray
2123
org.glassfish.json.JsonArrayBuilderImpl$JsonArrayImpl
2224
(parsed [x]
@@ -50,39 +52,62 @@
5052
(StringReader.)
5153
(JsonDocument/of)))
5254

53-
(defrecord StaticLoader []
55+
(defn ->document
56+
"Takes a document-loader, which takes a url and options and returns a json string
57+
context document (must have an \"@context\" key with a context as its value).
58+
59+
document-loader: [url options] => json-string
60+
"
61+
[document-loader url options]
62+
(let [url-string (.toString ^URI url)]
63+
(try
64+
(let [json-string (document-loader url-string options)]
65+
(JsonDocument/of (io/input-stream (.getBytes ^String json-string))))
66+
(catch Exception e
67+
(throw (JsonLdError. JsonLdErrorCode/LOADING_REMOTE_CONTEXT_FAILED
68+
(str "Unable to load context: " url-string)))))))
69+
70+
(defrecord PluggableLoader [document-loader]
5471
DocumentLoader
5572
(loadDocument [_ url options]
56-
(if-let [{path :source} (external/context->file (.toString url))]
57-
(-> (FileLoader.)
58-
(.loadDocument (.toURI (io/resource path))
59-
(DocumentLoaderOptions.)))
60-
(throw (JsonLdError. JsonLdErrorCode/LOADING_REMOTE_CONTEXT_FAILED
61-
(str "Unable to load static context: " (.toString url)))))))
73+
(->document document-loader url options)))
74+
75+
(defn static-loader
76+
[url options]
77+
(if-let [{path :source} (external/context->file url)]
78+
(slurp (io/resource path))
79+
(throw (ex-info (str "Unable to load static context: " url)
80+
{:url url}))))
6281

6382
(defn expand
64-
[json-ld]
65-
(-> (->json-document json-ld)
66-
(JsonLd/expand)
67-
(.loader ^DocumentLoader (->StaticLoader))
68-
(.get)
69-
(parsed)))
83+
([json-ld]
84+
(expand json-ld {:document-loader static-loader}))
85+
([json-ld opts]
86+
(-> (->json-document json-ld)
87+
(JsonLd/expand)
88+
(.loader ^DocumentLoader (->PluggableLoader (:document-loader opts)))
89+
(.get)
90+
(parsed))))
7091

7192
(defn compact
72-
[json-ld context]
73-
(-> (->json-document json-ld)
74-
(JsonLd/compact (->json-document context))
75-
(.loader ^DocumentLoader (->StaticLoader))
76-
(.get)
77-
(parsed)))
93+
([json-ld context]
94+
(compact json-ld context {:document-loader static-loader}))
95+
([json-ld context opts]
96+
(-> (->json-document json-ld)
97+
(JsonLd/compact (->json-document context))
98+
(.loader ^DocumentLoader (->PluggableLoader (:document-loader opts)))
99+
(.get)
100+
(parsed))))
78101

79102
(defn flatten
80-
[json-ld]
81-
(-> (->json-document json-ld)
82-
(JsonLd/flatten)
83-
(.loader ^DocumentLoader (->StaticLoader))
84-
(.get)
85-
(parsed)))
103+
([json-ld]
104+
(flatten json-ld {:document-loader static-loader}))
105+
([json-ld opts]
106+
(-> (->json-document json-ld)
107+
(JsonLd/flatten)
108+
(.loader ^DocumentLoader (->PluggableLoader (:document-loader opts)))
109+
(.get)
110+
(parsed))))
86111

87112
(defn- ->statement
88113
[^RdfNQuad quad]
@@ -114,23 +139,27 @@
114139
" ."))
115140

116141
(defn to-rdf
117-
[json-ld]
118-
(-> (->json-document json-ld)
119-
(JsonLd/toRdf)
120-
(.loader ^DocumentLoader (->StaticLoader))
121-
(.get)
122-
(.toList)
123-
(->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) ""))))
142+
([json-ld]
143+
(to-rdf json-ld {:document-loader static-loader}))
144+
([json-ld opts]
145+
(-> (->json-document json-ld)
146+
(JsonLd/toRdf)
147+
(.loader ^DocumentLoader (->PluggableLoader (:document-loader opts)))
148+
(.get)
149+
(.toList)
150+
(->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) "")))))
124151

125152
(defn canonize
126-
[json-ld]
127-
(-> (->json-document json-ld)
128-
(JsonLd/toRdf)
129-
(.loader ^DocumentLoader (->StaticLoader))
130-
(.get)
131-
(RdfNormalize/normalize)
132-
(.toList)
133-
(->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) ""))))
153+
([json-ld]
154+
(canonize json-ld {:document-loader static-loader}))
155+
([json-ld opts]
156+
(-> (->json-document json-ld)
157+
(JsonLd/toRdf)
158+
(.loader ^DocumentLoader (->PluggableLoader (:document-loader opts)))
159+
(.get)
160+
(RdfNormalize/normalize)
161+
(.toList)
162+
(->> (reduce (fn [doc quad] (str doc (->statement quad) "\n")) "")))))
134163

135164
(comment
136165
;; These work up to translating the resulting json-ld back into edn

src/fluree/json_ld/processor/api.cljs

+62-37
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,79 @@
33
(:require ["jsonld" :as jldjs]
44
[fluree.json-ld.impl.external :as external]))
55

6+
(defn pluggable-loader
7+
"Takes a document-loader, which takes a url and options and returns a json string
8+
context document (must have an \"@context\" key with a context as its value).
9+
10+
document-loader: [url options] => json-string
11+
"
12+
[document-loader]
13+
(fn [url options]
14+
(js/Promise.
15+
(fn [resolve reject]
16+
(try
17+
(let [json-string (document-loader url options)]
18+
(resolve #js{"contextUrl" nil, "document" json-string, "documentUrl" url}))
19+
(catch js/Error e
20+
(reject (throw #js{"name" "jsonld.LoadDocumentError"
21+
"message" (str "Unable to load static context: " url)
22+
"details" {"code" "loading remote context failed"
23+
"url" url}}))))))))
24+
625
(defn static-loader
726
[url options]
8-
(js/Promise.
9-
(fn [resolve reject]
10-
(if-let [context (get external/inlined-contexts url)]
11-
(resolve #js{"contextUrl" nil, "document" context, "documentUrl" url})
12-
(reject (throw #js{"name" "jsonld.LoadDocumentError"
13-
"message" (str "Unable to load static context: " url)
14-
"details" {"code" "loading remote context failed"
15-
"url" url}}))))))
27+
(get external/inlined-contexts url))
1628

1729
(defn compact
18-
[json-ld context]
19-
(-> json-ld
20-
(clj->js)
21-
(jldjs/compact (clj->js context) #js{"documentLoader" static-loader})
22-
(.then (fn [result] (js->clj result)))))
30+
([json-ld context]
31+
(compact json-ld context {:document-loader static-loader}))
32+
([json-ld context opts]
33+
(-> json-ld
34+
(clj->js)
35+
(jldjs/compact (clj->js context) #js{"documentLoader" (pluggable-loader (:document-loader opts))})
36+
(.then (fn [result] (js->clj result))))))
2337

2438
(defn expand
25-
[json-ld]
26-
(-> json-ld
27-
(clj->js)
28-
(jldjs/expand #js{"documentLoader" static-loader})
29-
(.then (fn [result] (js->clj result)))))
39+
([json-ld]
40+
(expand json-ld {:document-loader static-loader}))
41+
([json-ld opts]
42+
(-> json-ld
43+
(clj->js)
44+
(jldjs/expand #js{"documentLoader" (pluggable-loader (:document-loader opts))})
45+
(.then (fn [result] (js->clj result))))))
3046

3147
(defn flatten
32-
[json-ld]
33-
(-> json-ld
34-
(clj->js)
35-
(jldjs/flatten #js{"documentLoader" static-loader})
36-
(.then (fn [result] (js->clj result)))))
48+
([json-ld]
49+
(flatten json-ld {:document-loader static-loader}))
50+
([json-ld opts]
51+
(-> json-ld
52+
(clj->js)
53+
(jldjs/flatten #js{"documentLoader" (pluggable-loader (:document-loader opts))})
54+
(.then (fn [result] (js->clj result))))))
3755

3856
(defn from-rdf
39-
[n-quads]
40-
(-> n-quads
41-
(clj->js)
42-
(jldjs/fromRDF #js{"format" "application/n-quads" "documentLoader" static-loader})
43-
(.then (fn [result] (js->clj result)))))
57+
([n-quads]
58+
(from-rdf n-quads {:document-loader static-loader}))
59+
([n-quads opts]
60+
(-> n-quads
61+
(clj->js)
62+
(jldjs/fromRDF #js{"format" "application/n-quads"
63+
"documentLoader" (pluggable-loader (:document-loader opts))})
64+
(.then (fn [result] (js->clj result))))))
4465

4566
(defn to-rdf
46-
[json-ld]
47-
(-> json-ld
48-
(clj->js)
49-
(jldjs/expand #js{"documentLoader" static-loader})
50-
(.then (fn [expanded] (jldjs/toRDF expanded #js{"format" "application/n-quads"})))))
67+
([json-ld]
68+
(to-rdf json-ld {:document-loader static-loader}))
69+
([json-ld opts]
70+
(-> json-ld
71+
(clj->js)
72+
(jldjs/expand #js{"documentLoader" (pluggable-loader (:document-loader opts))})
73+
(.then (fn [expanded] (jldjs/toRDF expanded #js{"format" "application/n-quads"}))))))
5174

5275
(defn canonize
53-
[json-ld]
54-
(-> (to-rdf json-ld)
55-
(.then (fn [rdf]
56-
(jldjs/canonize rdf #js {"algorithm" "URDNA2015" "inputFormat" "application/n-quads"})))))
76+
([json-ld]
77+
(canonize json-ld {:document-loader static-loader}))
78+
([json-ld opts]
79+
(-> (to-rdf json-ld opts)
80+
(.then (fn [rdf]
81+
(jldjs/canonize rdf #js {"algorithm" "URDNA2015" "inputFormat" "application/n-quads"}))))))

test/fluree/json_ld/processor/api_test.clj

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns fluree.json-ld.processor.api-test
22
(:require [fluree.json-ld.processor.api :as jld-processor]
33
[clojure.test :as t :refer [deftest is testing]]
4-
[clojure.string :as str]))
4+
[clojure.string :as str]
5+
[jsonista.core :as json]))
56

67
(def context {"@version" 1.1,
78
"address" "fluree:address",
@@ -41,15 +42,30 @@
4142
(is (= expanded
4243
result))))
4344

45+
(testing "static context"
46+
(is (= expanded
47+
(jld-processor/expand (assoc commit "@context"
48+
["https://ns.flur.ee/ledger/v1"
49+
{"cool" {"@id" "fluree:cool" "@type" "xsd:boolean"}}]))))
50+
51+
(is (= "Unable to load context: http://failure.foo"
52+
(try (jld-processor/expand (assoc commit "@context" "http://failure.foo"))
53+
(catch Exception e
54+
(:cause (Throwable->map e)))))))
55+
4456
(testing "remote context"
45-
(let [result (jld-processor/expand (assoc commit "@context"
46-
[ "https://ns.flur.ee/ledger/v1"
47-
{"cool" {"@id" "fluree:cool" "@type" "xsd:boolean"}}]))]
48-
(is (= expanded
49-
result))))
57+
(let [test-docloader (fn [_ _] (json/write-value-as-string {"@context" {"foo" "http://example.com/foo#"}}))]
58+
(is (= [{"http://example.com/foo#bar" [{"@value" 1}]}]
59+
(jld-processor/expand {"@context" "foo:context" "foo:bar" 1}
60+
{:document-loader test-docloader})))
61+
(is (= "Unable to load context: foo:context"
62+
(try (jld-processor/expand {"@context" "foo:context" "foo:bar" 1}
63+
{:document-loader (fn [_ _] (throw (ex-info "borken loader" {})))})
64+
(catch Exception e
65+
(:cause (Throwable->map e))))))))
5066

5167
(testing "remote context failure"
52-
(is (= "Unable to load static context: http://failure.foo"
68+
(is (= "Unable to load context: http://failure.foo"
5369
(try (jld-processor/expand (assoc commit "@context" "http://failure.foo"))
5470
(catch Exception e
5571
(:cause (Throwable->map e))))))))

0 commit comments

Comments
 (0)