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

Cannot use string context from clojure #335

Closed
dpetran opened this issue Jan 10, 2023 · 5 comments
Closed

Cannot use string context from clojure #335

dpetran opened this issue Jan 10, 2023 · 5 comments
Assignees
Labels
bug Something isn't working as expected

Comments

@dpetran
Copy link
Contributor

dpetran commented Jan 10, 2023

If using a context with string prefixes from clojure, the string prefixes will get clobbered when using the db. As shown below the user's supplied context is used as-is by the conn and the ledger, but the db normalizes the default context and in so doing changes all the prefixes to keywords. This breaks the user's expectations by having shortened IRIs fail to expand.

The normalization happens in fluree.db.json-ld.bootstrap/normalize-default-ctx.

(def conn @(fluree/connect
               {:method :memory
                :defaults {:context {"id" "@id"
                                     "type" "@type"
                                     "schema" "http://schema.org/"
                                     "rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
                                     "rdfs" "http://www.w3.org/2000/01/rdf-schema#"
                                     "wiki" "https://www.wikidata.org/wiki/"
                                     "skos" "http://www.w3.org/2008/05/skos#"
                                     "sh" "http://www.w3.org/ns/shacl#"
                                     "xsd" "http://www.w3.org/2001/XMLSchema#"
                                     "f" "https://ns.flur.ee/ledger#"}
                           :did
                           {:id "did:fluree:TfHgFTQQiJMHaK1r1qxVPZ3Ridj9pCozqnh",
                            :public "03b160698617e3b4cd621afd96c0591e33824cb9753ab2f1dace567884b4e242b0",
                            :private "509553eece84d5a410f1012e8e19e84e938f226aa3ad144e2d12f36df0f51c1e"}}}))

;; conn has the given context
(conn-proto/-context conn)
{"f" "https://ns.flur.ee/ledger#",
   "rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
   "schema" "http://schema.org/",
   "id" "@id",
   "wiki" "https://www.wikidata.org/wiki/",
   "rdfs" "http://www.w3.org/2000/01/rdf-schema#",
   "type" "@type",
   "sh" "http://www.w3.org/ns/shacl#",
   "skos" "http://www.w3.org/2008/05/skos#",
   "xsd" "http://www.w3.org/2001/XMLSchema#"}

(def ledger @(fluree/create conn "test.shacl"  {:context {"test" "fluree:test.shacl/"}}))

;; ledger has the given context
(:context ledger)
{"f" "https://ns.flur.ee/ledger#",
   "rdf" "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
   "schema" "http://schema.org/",
   "id" "@id",
   "wiki" "https://www.wikidata.org/wiki/",
   "rdfs" "http://www.w3.org/2000/01/rdf-schema#",
   "type" "@type",
   "sh" "http://www.w3.org/ns/shacl#",
   "skos" "http://www.w3.org/2008/05/skos#",
   "test" "fluree:test.shacl/",
   "xsd" "http://www.w3.org/2001/XMLSchema#"}

;; db has a transformed context, all operations performed with the db will fail to expand if given string ids instead of keywords.
(-> (fluree/db ledger)
      :schema
      :context)
  {:schema {:id "http://schema.org/"},
   :wiki {:id "https://www.wikidata.org/wiki/"},
   :xsd {:id "http://www.w3.org/2001/XMLSchema#"},
   :type {:id "@type", :type? true},
   :rdfs {:id "http://www.w3.org/2000/01/rdf-schema#"},
   :type-key :type,
   :id {:id "@id"},
   :f {:id "https://ns.flur.ee/ledger#"},
   :sh {:id "http://www.w3.org/ns/shacl#"},
   :skos {:id "http://www.w3.org/2008/05/skos#"},
   :test {:id "fluree:test.shacl/"},
   :rdf {:id "http://www.w3.org/1999/02/22-rdf-syntax-ns#"}}
@bplatz
Copy link
Contributor

bplatz commented Jan 10, 2023

Is there a request to be able to use the string context? from CLJ? It is produced under :context-str so it does exist and could be used by setting a flag or something on the query.

Both cannot be used, because they are use for both expansion of the query, then for compaction of the results. The latter would not be able to produce correct results - so one -or- the other must be used.

@bplatz
Copy link
Contributor

bplatz commented Jan 10, 2023

And of course you can always use any context you want by supplying it - we do "merge" it into the default which could be problematic, and it would make sense to have more control over that. I think merge is what is typically wanted, at least from my perspective... but replace should be possible. That would be a different ticket however, but maybe would address the original concern that prompted this ticket.

@dpetran
Copy link
Contributor Author

dpetran commented Jan 12, 2023

The reason I ran into this was I was trying to put together an example SHACL graph for the nexus folks. I first wrote in the json-ld playground because they won't be using clojure and I wanted to make sure it was correct. For convenience, I used nested prefixes:

{ "@context":
  {
    "sh": "http://www.w3.org/ns/shacl#",
    "xsd": "http://www.w3.org/2001/XMLSchema#",
    "lib": "fluree:library/",
    "book": "lib:book/",
    "author": "lib:author/",
  },
  "@graph":
  [
    {
      "@type": ["sh:NodeShape", "rdfs:Class"],
      "sh:targetClass": "lib:Book",
      "sh:property":
      [
        {
          "sh:path": "book:title",
          "sh:datatype": "xsd:string",
        },
        {
          "sh:path": "book:author",
          "sh:nodeKind": "sh:IRI",
        },
        {
          "sh:path": "book:synopsis",
          "sh:nodeKind": "xsd:string",
          "sh:maxCount": 1
        },
        {
          "sh:path": "book:collection",
          "sh:nodeKind": "sh:IRI",
        },
        {
          "sh:path": "book:tag",
          "sh:nodeKind": "sh:IRI",
        },
      ]
    },
    {
      "@type": ["sh:NodeShape", "rdfs:Class"],
      "sh:targetClass": "lib:Author",
      "sh:property":
      [
        {
          "sh:path": "author:name",
          "sh:datatype": "xsd:string",
        }
      ]
    },
    {
      "@type": ["sh:NodeShape", "rdfs:Class"],
      "sh:targetClass": "lib:Collection",
      "sh:property":
      [
        {
          "sh:path": "lib:collection/name",
          "sh:datatype": "xsd:string",
        }
      ]
    }
  ]
}


I then wanted to try it out and got confused because none of it worked in clojure because of the string/keyword context dichotomy.

Once I figured out that I can't use string prefixes in Clojure I tried translating this into keywords but I haven't been able to get these nested prefixes to expand properly. There's probably a trick to it but I tried three different approaches that didn't work, so I stopped to write it up.

@dpetran dpetran added the bug Something isn't working as expected label Jan 12, 2023
@dpetran dpetran self-assigned this Mar 3, 2023
@dpetran
Copy link
Contributor Author

dpetran commented Mar 14, 2023

Going to wait until the fix for #414 lands to complete this.

@bplatz
Copy link
Contributor

bplatz commented Apr 17, 2023

This issue is closed from #437 .

Now you always use the string context, unless you set the option :context-type to :keyword.

This can be set on ledger as a default, but ultimately is a db level concern and either strings or keywords can get used at any time.

I'll refrain from closing because it isn't assigned to me, but let me know if there are any clarifying questions. Else good to close.

@cap10morgan cap10morgan removed their assignment Apr 21, 2023
@dpetran dpetran closed this as completed May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

3 participants