How to use extension functions #856
-
Question: I'm trying to have the example provided in the section: https://comunica.dev/docs/query/advanced/extension_functions/ working, but still no success. const newEngine = require('@comunica/actor-init-sparql').newEngine;
const myEngine = newEngine();
const df = require('rdf-data-factory');
async function main() {
const DF = new df.DataFactory();
const result = await myEngine.query(`
PREFIX func: <http://example.org/functions#>
SELECT ?caps WHERE {
?s ?p ?o.
BIND (func:to-upper-case(?o) AS ?caps)
}
`, {
sources: ['https://www.rubensworks.net/'],
extensionFunctions: {
'http://example.org/functions#to-upper-case'(args) {
const arg = args[0];
if (arg.termType === 'Literal' && arg.datatype.value === 'http://www.w3.org/2001/XMLSchema#string') {
return DF.literal(arg.value.toUpperCase(), arg.datatype);
}
return arg;
},
},
});
}
main(); The following error is being returned:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Are you sure you're using the latest version of Comunica? (1.22.0) |
Beta Was this translation helpful? Give feedback.
-
Hello again, I have different issue regarding using extension function inside FILTER in a SPARQL query. Starting from the following dummy example: const newEngine = require('@comunica/actor-init-sparql').newEngine;
const myEngine = newEngine();
const df = require('rdf-data-factory');
const N3 = require('n3');
const store = new N3.Store();
const parser = new N3.Parser();
const DF = new df.DataFactory();
async function call() {
let startDate = '2015-09-03T08:43:23.000Z';
let file = `
@prefix m: <http://www.example.de/m/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
m:a m:dateFrom "2014-07-03T08:43:23.000Z"^^xsd:dateTime .
m:b m:dateFrom "2015-07-03T08:43:23.000Z"^^xsd:dateTime .
m:c m:dateFrom "2016-07-03T08:43:23.000Z"^^xsd:dateTime .
`;
parser.parse(file,
(error, quad, prefixes) => {
if (quad)
store.addQuad(quad);
else
return Promise.resolve();
});
const query =`
PREFIX func: <http://example.org/functions#>
PREFIX m: <http://www.example.de/m/>
SELECT ?s ?from
WHERE { ?s m:dateFrom ?from . FILTER(func:getTimeLessThan(?from, '${startDate}'))}`;
// Initiate the update
const result = await myEngine.query(
query , {
sources: [store],
extensionFunctions: {
'http://example.org/functions#getTimeLessThan'(args) {
const timeIntersectValue = timeLessThan(args[0].value, args[1].value);
return DF.literal(timeIntersectValue, DF.namedNode('http://www.w3.org/2001/XMLSchema#boolean'));
},
},
});
console.log((await result.bindings()).length);
}
function timeLessThan(dataParam, upperTimeLimit) {
const timeIntersect = ((new Date(dataParam)).getTime() < (new Date(upperTimeLimit)).getTime());
console.log(timeIntersect);
return timeIntersect;
}
call(); I'm expecting to get two results; however nothing is returned. The length of the returned result array is 0. Is my expectation to get two rows of results is wrong here? Or what I'm missing to add? Thanks for the help |
Beta Was this translation helpful? Give feedback.
Are you sure you're using the latest version of Comunica? (1.22.0)