Skip to content

Commit

Permalink
Handle property-based indexing with graph containers
Browse files Browse the repository at this point in the history
  • Loading branch information
rubensworks committed Mar 16, 2020
1 parent 8dcfd6f commit 494d1cf
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 4 deletions.
17 changes: 14 additions & 3 deletions lib/containerhandler/ContainerHandlerIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,20 @@ export class ContainerHandlerIndex implements IContainerHandler {
if (indexProperty) {
const indexValues = await util.valueToTerm(context, indexPropertyRaw,
await util.getContainerKey(keys[depth], keys, depth), depth, keys);
for (const indexValue of indexValues) {
await EntryHandlerPredicate.handlePredicateObject(parsingContext, util, keys, depth + 1,
indexProperty, indexValue, false);

if (graphContainer) {
// When we're in a graph container, attach the index to the graph identifier
const graphId = await util.getGraphContainerValue(keys, depth + 1);
for (const indexValue of indexValues) {
parsingContext.emitQuad(depth, util.dataFactory.quad(graphId, indexProperty, indexValue,
util.getDefaultGraph()));
}
} else {
// Otherwise, attach the index to the node identifier
for (const indexValue of indexValues) {
await EntryHandlerPredicate.handlePredicateObject(parsingContext, util, keys, depth + 1,
indexProperty, indexValue, false);
}
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion lib/entryhandler/EntryHandlerContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,16 @@ export class EntryHandlerContainer implements IEntryHandler<{
* @return The graph index.
*/
public static getContainerGraphIndex(containers: {[typeName: string]: boolean}, depth: number, keys: any[]): string {
const isSimpleGraphContainer = EntryHandlerContainer.isSimpleGraphContainer(containers);
let isSimpleGraphContainer = EntryHandlerContainer.isSimpleGraphContainer(containers);
let index = '';
for (let i = depth; i < keys.length; i++) {
if (!isSimpleGraphContainer || typeof keys[i] === 'number') {
index += ':' + keys[i];
}
// Only allow a second 'real' key if in a non-simple graph container.
if (!isSimpleGraphContainer && typeof keys[i] !== 'number') {
isSimpleGraphContainer = true;
}
}
return index;
}
Expand Down
84 changes: 84 additions & 0 deletions test/JsonLdParser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8339,6 +8339,35 @@ describe('JsonLdParser', () => {
]);
});

it('with @id and graph map with @index with multiple lead node values', async () => {
const stream = streamifyString(`
{
"@context": {
"@base": "http://example.com/entries/",
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@container": [ "@graph", "@index" ] },
"value1": "ex:value1",
"value2": "ex:value2"
},
"@id": "http://ex.org/myid",
"p": {
"index0": {
"@id": "value1",
"value1": "1",
"value2": "2"
}
}
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
triple(namedNode('http://ex.org/myid'), namedNode('http://ex.org/pred1'),
blankNode('g1')),
quad(namedNode('http://example.com/entries/value1'), namedNode('http://ex.org/value1'),
literal('1'), blankNode('g1')),
quad(namedNode('http://example.com/entries/value1'), namedNode('http://ex.org/value2'),
literal('2'), blankNode('g1')),
]);
});

it('with @id and graph map with @index and @graph key', async () => {
const stream = streamifyString(`
{
Expand Down Expand Up @@ -8710,6 +8739,61 @@ describe('JsonLdParser', () => {
]);
});

it('with @id and graph map with @index and @index prop', async () => {
const stream = streamifyString(`
{
"@context": {
"@base": "http://example.com/entries/",
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@container": [ "@graph", "@index" ], "@index": "ex:prop" },
"value": "ex:value"
},
"@id": "http://ex.org/myid",
"p": {
"index0": {
"@id": "value1",
"value": "1539"
}
}
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
triple(namedNode('http://ex.org/myid'), namedNode('http://ex.org/pred1'),
blankNode('g1')),
quad(namedNode('http://example.com/entries/value1'), namedNode('http://ex.org/value'),
literal('1539'), blankNode('g1')),
quad(blankNode('g1'), namedNode('http://ex.org/prop'),
literal('index0')),
]);
});

it('with @id and graph map with @index and @index prop as IRI', async () => {
const stream = streamifyString(`
{
"@context": {
"@base": "http://example.com/entries/",
"ex": "http://ex.org/",
"p": { "@id": "http://ex.org/pred1", "@container": [ "@graph", "@index" ], "@index": "ex:prop" },
"value": "ex:value",
"ex:prop": { "@type": "@id" }
},
"@id": "http://ex.org/myid",
"p": {
"index0": {
"@id": "value1",
"value": "1539"
}
}
}`);
return expect(await arrayifyStream(stream.pipe(parser))).toBeRdfIsomorphic([
triple(namedNode('http://ex.org/myid'), namedNode('http://ex.org/pred1'),
blankNode('g1')),
quad(namedNode('http://example.com/entries/value1'), namedNode('http://ex.org/value'),
literal('1539'), blankNode('g1')),
quad(blankNode('g1'), namedNode('http://ex.org/prop'),
namedNode('http://example.com/entries/index0')),
]);
});

it('with @id and graph map with @id', async () => {
const stream = streamifyString(`
{
Expand Down

0 comments on commit 494d1cf

Please sign in to comment.