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

jsonld wrong handling of default graph #310

Closed
jason076 opened this issue Apr 3, 2019 · 6 comments
Closed

jsonld wrong handling of default graph #310

jason076 opened this issue Apr 3, 2019 · 6 comments

Comments

@jason076
Copy link

jason076 commented Apr 3, 2019

I am experimenting with the rdflib and the hydra jsonld API.

myTestcode.ts

import * as rdf from "rdflib"

const store: rdf.IndexedFormula = rdf.graph();
const fetcher: rdf.Fetcher = new rdf.Fetcher(store);
const updateMgr: rdf.UpdateManager = new rdf.UpdateManager(store);
const MY_DOMAIN = rdf.Namespace("https://my-domain.de/vocab#");
const docNode = rdf.sym("https://localhost:8443/companions");
const context = "http://www.w3.org/ns/hydra/context.jsonld";

const jsonDoc ={
    "@context": "http://www.w3.org/ns/hydra/context.jsonld",
    "@id": `${docNode.uri}`,
    "title": "A companion container.",
    "description": "Companions can be managed with ldp container.",
    "operation": [
        {
        "@type": "Operation",
        "method": "POST"
        }
    ]
};

const asNquads = `<https://localhost:8443/companions> <http://www.w3.org/ns/hydra/core#description> "This issue can be deleted with an HTTP DELETE request" .
<https://localhost:8443/companions> <http://www.w3.org/ns/hydra/core#operation> _:b0 .
<https://localhost:8443/companions> <http://www.w3.org/ns/hydra/core#title> "An exemplary issue representation" .
_:b0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/ns/hydra/core#Operation> .
_:b0 <http://www.w3.org/ns/hydra/core#method> "DELETE" .
`
rdf.parse(JSON.stringyfi(jsonldDoc), store, docNode.uri, 'application/ld+json', (err, res) => {
      if (err) {
          throw new Error();
      } else {
          rdf.serialize(docNode, store, docNode.uri, 'application/ld+json', (err, res) => {
              if (err) {
                  throw new Error();
              } else {
                  console.log(res);
              }
          });
     }
});

If i am not mistaken, all nodes should belong to the default graph. If I parse a turtle document, rdflib uses the base-parameter of the parse function as the graph name. But if I parse the jsonld-document rdflib sets nodeobjects as the graph names. In a result I get a rdf dataset with two different graphs instead of one.
The line of code where the why (graphname) gets set is in indexed-formular.js lines 270-273:

if (!why) {
        // system generated
        why = this.fetcher ? this.fetcher.appNode : this.sym(defaultGraphURI);
      }
}

Thus the serializing of docNode does only print 3 rdf-triples instead of 5.

If this behavior is not intended, I could write a fix. I think there must just be a check for the default graph be included in parser.js. If the default graph is detected, why will be set to base.

@josephguillaume
Copy link

It seems the quoted lines are now in the add function in store.ts https://github.com/linkeddata/rdflib.js/blob/master/src/store.ts#L422

I also expected parse to use the base parameter as the graph name, but the current implementation of jsonldparser simply doesn't provide a graph name at all, which is why the default is used
https://github.com/linkeddata/rdflib.js/blob/master/src/jsonldparser.js#L87

See comment on relevant commit: 16daad7#r41499395

@timbl
Copy link
Member

timbl commented Jul 16, 2021

In general, Store.add() should not be called with undefined or null why. Yes, for the top level of a JSON-LD file, the why should be set to a NamedNode of the document being parsed.

@jeff-zucker
Copy link
Contributor

Adding the document URI into jsonldParser's createStatement will allow the store to be correctly filled. Doing that along with Tim's patch to support json-ld in the source pane produces this nice result :
image

@jeff-zucker
Copy link
Contributor

I believe this is solved in 80e6b66. I mistakenly merged it into the jsonld branch directly.

@timbl
Copy link
Member

timbl commented Jul 17, 2021

Looks good

@bourgeoa
Copy link
Contributor

resolved #509

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants