rubensworks / jsonld-streaming-parser.js

A fast and lightweight streaming JSON-LD parser for JavaScript
https://www.rubensworks.net/blog/2019/03/13/streaming-rdf-parsers/
MIT License
79 stars 12 forks source link

Graph missing from outer Quads when parsing JSON-LD-star #120

Open futpib opened 10 months ago

futpib commented 10 months ago

Running the code below logs an equivalent of this RDF-star (notice that 2nd and 3rd quads are missing the 4th "graph" element):

<http://foo/bob> <http://foo/knows> <http://foo/alice> <http://foo/graphid> .
<<<http://foo/bob> <http://foo/knows> <http://foo/alice> <http://foo/graphid>>> <http://foo/accordingTo> <http://foo/alice> .
<http://foo/bob> <http://foo/claims> <<<http://foo/bob> <http://foo/knows> <http://foo/alice> <http://foo/graphid>>> .

But it seems that this RDF-star is expected:

<http://foo/bob> <http://foo/knows> <http://foo/alice> <http://foo/graphid> .
<<<http://foo/bob> <http://foo/knows> <http://foo/alice> <http://foo/graphid>>> <http://foo/accordingTo> <http://foo/alice> <http://foo/graphid> .
<http://foo/bob> <http://foo/claims> <<<http://foo/bob> <http://foo/knows> <http://foo/alice> <http://foo/graphid>>> <http://foo/graphid> .
const { Readable } = require('stream');
const { JsonLdParser } = require('jsonld-streaming-parser');

// This is example 6 from https://json-ld.github.io/json-ld-star/#example-reversing-annotations
// modified by wrapping everything into a "@graph"
const document = {
    "@context": {
        "@base": "http://foo/",
        "@vocab": "http://foo/",
        "accordingTo": {"@type": "@id"},
        "claimedBy": {"@reverse": "http://foo/claims", "@type": "@id"}
    },
    "@id": "graphid",
    "@graph": {
        "@id": "bob",
        "knows": {
            "@id": "alice",
            "@annotation": {
                "accordingTo": "alice",
                "claimedBy": "bob"
            }
        }
    }
};

const input = new Readable.from([ JSON.stringify(document) ]);
const parser = (new JsonLdParser()).import(input);

parser.on('data', (quad) => {
    console.log(quad);
});

new Promise((resolve, reject) => {
    parser.on('end', resolve);
    parser.on('error', reject);
});
Exact output

``` Quad { termType: 'Quad', value: '', subject: NamedNode { termType: 'NamedNode', value: 'http://foo/bob' }, predicate: NamedNode { termType: 'NamedNode', value: 'http://foo/knows' }, object: NamedNode { termType: 'NamedNode', value: 'http://foo/alice' }, graph: NamedNode { termType: 'NamedNode', value: 'http://foo/graphid' } } Quad { termType: 'Quad', value: '', subject: Quad { termType: 'Quad', value: '', subject: NamedNode { termType: 'NamedNode', value: 'http://foo/bob' }, predicate: NamedNode { termType: 'NamedNode', value: 'http://foo/knows' }, object: NamedNode { termType: 'NamedNode', value: 'http://foo/alice' }, graph: NamedNode { termType: 'NamedNode', value: 'http://foo/graphid' } }, predicate: NamedNode { termType: 'NamedNode', value: 'http://foo/accordingTo' }, object: NamedNode { termType: 'NamedNode', value: 'http://foo/alice' }, graph: DefaultGraph { termType: 'DefaultGraph', value: '' } } Quad { termType: 'Quad', value: '', subject: NamedNode { termType: 'NamedNode', value: 'http://foo/bob' }, predicate: NamedNode { termType: 'NamedNode', value: 'http://foo/claims' }, object: Quad { termType: 'Quad', value: '', subject: NamedNode { termType: 'NamedNode', value: 'http://foo/bob' }, predicate: NamedNode { termType: 'NamedNode', value: 'http://foo/knows' }, object: NamedNode { termType: 'NamedNode', value: 'http://foo/alice' }, graph: NamedNode { termType: 'NamedNode', value: 'http://foo/graphid' } }, graph: DefaultGraph { termType: 'DefaultGraph', value: '' } } ```

rubensworks commented 10 months ago

Something may be going wrong here: https://github.com/rubensworks/jsonld-streaming-parser.js/blob/master/lib/entryhandler/EntryHandlerPredicate.ts#L88-L102