Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public static mapTerm(term: any): RDF.Term {
switch (term.termType) {
case 'NamedNode':
return namedNode(term.value);
case 'BlankNode':
return blankNode(term.value.substr(2)); // Remove the '_:' prefix.
case 'Literal':
return literal(term.value, term.language || term.datatype);
/*case 'Variable':
return variable(term.value);*/ // Variables can not occur in JSON-LD bodies
case 'DefaultGraph':
return defaultGraph();
}
}
it('should match quads by the default graph (implicit)', async function () {
const store = this.store;
const rs = store;
const quads = [
factory.quad(
factory.namedNode('http://ex.com/s0'),
factory.namedNode('http://ex.com/p0'),
factory.literal('o0', 'en-gb')
),
factory.quad(
factory.namedNode('http://ex.com/s1'),
factory.namedNode('http://ex.com/p1'),
factory.literal('o1', 'en-gb'),
factory.namedNode('http://ex.com/g1')
)
];
const source = utils.createArrayStream(quads);
await utils.waitForEvent(store.import(source), 'end', true);
const readStream = rs.match(null, null, null, factory.defaultGraph());
const matchedQuads = await utils.streamToArray(readStream);
stripTermSerializedValue(matchedQuads);
should(matchedQuads).have.length(1);
should(matchedQuads[0].graph).deepEqual(quads[0].graph);
});
it('should match the default graph (explicit)', async function () {
const store = this.store;
const rs = store;
const quads = [
factory.quad(
factory.namedNode('http://ex.com/s0'),
factory.namedNode('http://ex.com/p0'),
factory.literal('o0', 'en-gb'),
factory.defaultGraph()
),
factory.quad(
factory.namedNode('http://ex.com/s1'),
factory.namedNode('http://ex.com/p1'),
factory.literal('o1', 'en-gb'),
factory.namedNode('http://ex.com/g1')
)
];
const source = utils.createArrayStream(quads);
await utils.waitForEvent(store.import(source), 'end', true);
const matchedQuads = await utils.streamToArray(rs.match(null, null, null, factory.defaultGraph()));
stripTermSerializedValue(matchedQuads);
should(matchedQuads).have.length(1);
should(matchedQuads[0]).deepEqual(quads[0]);
});
factory.quad(
factory.namedNode('http://ex.com/s'),
factory.namedNode('http://ex.com/p'),
factory.literal('o', 'en-gb'),
factory.namedNode('http://ex.com/g2')
),
factory.quad(
factory.namedNode('http://ex.com/s'),
factory.namedNode('http://ex.com/p'),
factory.literal('o2', 'en-gb'),
factory.namedNode('http://ex.com/g2')
)
];
const source = utils.createArrayStream(quads);
await utils.waitForEvent(store.import(source), 'end', true);
const object = factory.literal('o2', 'en-gb');
const matchedQuads = await utils.streamToArray(rs.match(null, null, object));
stripTermSerializedValue(matchedQuads);
should(matchedQuads).have.length(1);
should(matchedQuads[0]).deepEqual(quads[1]);
});
it('should match quads by graph', async function () {
const store = this.store;
const rs = store;
const quads = [
factory.quad(
factory.namedNode('http://ex.com/s'),
factory.namedNode('http://ex.com/p'),
factory.literal('o', 'en-gb'),
factory.namedNode('http://ex.com/g')
),
factory.quad(
factory.namedNode('http://ex.com/s'),
factory.namedNode('http://ex.com/p'),
factory.literal('o', 'en-gb'),
factory.namedNode('http://ex.com/g2')
)
];
const source = utils.createArrayStream(quads);
await utils.waitForEvent(store.import(source), 'end', true);
const graph = factory.namedNode('http://ex.com/g2');
const matchedQuads = await utils.streamToArray(rs.match(null, null, null, graph));
stripTermSerializedValue(matchedQuads);
should(matchedQuads).have.length(1);
should(matchedQuads[0]).deepEqual(quads[1]);
it('should match quads by object', async function () {
const store = this.store;
const rs = store;
const quads = [
factory.quad(
factory.namedNode('http://ex.com/s'),
factory.namedNode('http://ex.com/p'),
factory.literal('o', 'en-gb'),
factory.namedNode('http://ex.com/g2')
),
factory.quad(
factory.namedNode('http://ex.com/s'),
factory.namedNode('http://ex.com/p'),
factory.literal('o2', 'en-gb'),
factory.namedNode('http://ex.com/g2')
)
];
const source = utils.createArrayStream(quads);
await utils.waitForEvent(store.import(source), 'end', true);
const object = factory.literal('o2', 'en-gb');
const matchedQuads = await utils.streamToArray(rs.match(null, null, object));
stripTermSerializedValue(matchedQuads);
should(matchedQuads).have.length(1);
should(matchedQuads[0]).deepEqual(quads[1]);
});
const fromPlainTerm = plainTerm => {
switch (plainTerm.termType) {
case "NamedNode":
return rdf.namedNode(plainTerm.value);
case "BlankNode":
return rdf.blankNode(plainTerm.value.substr(2));
case "Literal":
return rdf.literal(
plainTerm.value,
plainTerm.language || rdf.namedNode(plainTerm.datatype.value)
);
case "DefaultGraph":
return rdf.defaultGraph();
default:
throw Error("unknown termType: " + plainTerm.termType);
}
};
term (plainTerm) {
switch (plainTerm.termType) {
case 'NamedNode':
return DataFactory.namedNode(plainTerm.value)
case 'BlankNode':
return DataFactory.blankNode(plainTerm.value.substr(2))
case 'Literal':
return DataFactory.literal(plainTerm.value, plainTerm.language || DataFactory.namedNode(plainTerm.datatype.value))
case 'DefaultGraph':
return DataFactory.defaultGraph()
default:
throw Error('unknown termType: ' + plainTerm.termType)
}
}
async* createResults(activity, document) {
const id = namedNode(new URL(`#${uuid()}`, document).href);
const published = literal(new Date().toISOString(), `${xsd}dateTime`);
activity = { id, published, ...activity };
const insert = replaceVariables(activityTemplate, activity);
yield { id, insert };
}