参考官方文档:
https://jena.apache.org/documentation/query/javascript-functions.html
首先写一个自定义函数的js脚本,例如function.js
// CamelCase a string
// Words to be combined are separated by a space in the string.
function toCamelCase(str) {
return str.split(' ')
.map(cc)
.join('');
}
function ucFirst(word) {
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
}
function lcFirst(word) {
return word.toLowerCase();
}
function cc(word,index) {
return (index == 0) ? lcFirst(word) : ucFirst(word);
}
然后可以在Fuseki启动的时候进行调用:
可以使用命令行在启动Fuseki的时候设置调用自定义的js脚本
fuseki --set arq:js-library=functions.js --mem /ds
也可以在Fuseki的配置文件中进行相应的配置:
PREFIX : <#>
PREFIX fuseki: <http://jena.apache.org/fuseki#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ja: <http://jena.hpl.hp.com/2005/11/Assembler#>
[] rdf:type fuseki:Server ;
# Set the server-wide context
ja:context [
ja:cxtName "arq:js-library" ;
ja:cxtValue "/filepath/functions.js"
] ;
.
<#service> rdf:type fuseki:Service;
rdfs:label "Dataset";
fuseki:name "ds";
fuseki:serviceQuery "sparql";
fuseki:dataset <#dataset> ;
.
<#dataset> rdf:type ja:DatasetTxnMem;
ja:data <file:D.trig>;
.
最后向下面这样在查询语句中使用自定义的函数了:
PREFIX js: <http://jena.apache.org/ARQ/jsFunction#>
SELECT ?input (js:toCamelCase(?input) AS ?X)
{
VALUES ?input { "some woRDs to PROCESS" }
}
?注意要在前面增加前缀。
|