(PS JENA-664 is the open JIRA for GeoSPARQL)
A complete example with imports etc:
https://gist.github.com/afs/a8dfe6680324110bdb675190f9c73035
and also below.
This is for doing everything in one java program. It is the simplest way
and runs in an IDE for debugging.
My main problems in understandig are:
- where to put the java code and how to name it
It will have a URI name.
From the example below:
// Register the function
FunctionRegistry ref = FunctionRegistry.get();
ref.put("http://my/num", MyFunction.class);
using the global function registry (you can have one uique to the
dataset if you want as well but the joy of globally unique URIs is that
putting it the JVM-registry just works.
- how to include it with Fuseki
The example below is the simplest way using embedded Fuseki. It's a
plain Java program and avoids the need to repack jar files which for
development makes things easier and can be debugged in an IDE.
If you want to create a packaged standalone jar file, this is a a
template: "basic" is a standalone jar using embedded Fuseki and with a
command line interface.
https://github.com/apache/jena/tree/master/jena-fuseki2/jena-fuseki-basic
It makes:
http://jena.apache.org/documentation/fuseki2/fuseki-embedded.html#fuseki-basic
so using that, adding your own code and making the command line start
register the function should work.
- how to call it in the SPARQL query
Functions are invoked like:
<http://my/num>(?Z)
which is URI + arguments. The URI can a prefixed name as well; prefixes
and expanded during parsing and it is the URI that matters.
BIND (<http://my/num>(?Z) AS ?X )
FILTER(<http://my/num>(?Z) = "number")
Andy
------------------------
https://gist.github.com/afs/a8dfe6680324110bdb675190f9c73035
and with slight reformatting:
public class FuFunctionEx {
/** Our function */
public static class MyFunction extends FunctionBase1 {
@Override
public NodeValue exec(NodeValue v) {
if ( v.isNumber() )
return NodeValue.makeString("number");
return NodeValue.makeString("not a number");
}
}
public static void main(String...a) {
FusekiLogging.setLogging();
// Register the function
FunctionRegistry ref = FunctionRegistry.get();
ref.put("http://my/num", MyFunction.class);
int PORT = FusekiLib.choosePort();
// Some empty dataset
DatasetGraph dsg = DatasetGraphFactory.createTxnMem();
FusekiServer server = FusekiServer.create()
.setPort(PORT)
.add("/ds", dsg)
.build();
server.start();
// Test query.
String queryString = StrUtils.strjoinNL(
"SELECT * { "
, " VALUES ?Z { 123 'abc'}"
, " BIND (<http://my/num>(?Z) AS ?X )"
,"}"
);
try {
String url = "http://localhost:"+PORT+"/ds";
// Connect to the server and execute the query.
try ( RDFConnection conn =
RDFConnectionFactory.connect(url) ) {
// Using Java8 features.
conn.queryResultSet(queryString,
ResultSetFormatter::out);
// Without Java8 features
try ( QueryExecution qExec = conn.query(queryString) ) {
ResultSet rs = qExec.execSelect();
ResultSetFormatter.out(rs);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally { server.stop(); }
}
}
Andy