use of org.openrdf.query.algebra.evaluation.function.Function in project incubator-rya by apache.
the class GeoFilterIT method showGeoFunctionsRegistered.
@Test
public void showGeoFunctionsRegistered() {
int count = 0;
final Collection<Function> funcs = FunctionRegistry.getInstance().getAll();
for (final Function fun : funcs) {
if (fun.getURI().startsWith(GEO)) {
count++;
}
}
// There are 30 geo functions registered, ensure that there are 30.
assertEquals(30, count);
}
use of org.openrdf.query.algebra.evaluation.function.Function in project incubator-rya by apache.
the class GeoFunctionsIT method withGeoFilters.
@Test
public void withGeoFilters() throws Exception {
final String sparql = "PREFIX geo: <http://www.opengis.net/ont/geosparql#> " + "PREFIX ryageo: <tag:rya.apache.org,2017:function/geo#> " + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/> " + "SELECT ?feature ?point ?wkt {" + " ?feature a geo:Feature . " + " ?feature geo:hasGeometry ?point . " + " ?point a geo:Point . " + " ?point geo:asWKT ?wkt . " + " FILTER(ryageo:ehContains(?wkt, \"POLYGON((-77 39, -76 39, -76 38, -77 38, -77 39))\"^^geo:wktLiteral)) " + "}";
final ValueFactory vf = new ValueFactoryImpl();
final Set<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#feature"), vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createURI("http://www.opengis.net/ont/geosparql#Feature")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#feature"), vf.createURI("http://www.opengis.net/ont/geosparql#hasGeometry"), vf.createURI("tag:rya.apache.org,2017:ex#test_point")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#test_point"), vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createURI("http://www.opengis.net/ont/geosparql#Point")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#test_point"), vf.createURI("http://www.opengis.net/ont/geosparql#asWKT"), vf.createLiteral("Point(-77.03524 38.889468)", vf.createURI("http://www.opengis.net/ont/geosparql#wktLiteral"))));
// Create a Geo function.
final Function geoFunction = new Function() {
@Override
public String getURI() {
return "tag:rya.apache.org,2017:function/geo#ehContains";
}
@Override
public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException {
if (args.length != 2) {
throw new ValueExprEvaluationException(getURI() + " requires exactly 3 arguments, got " + args.length);
}
return valueFactory.createLiteral(true);
}
};
// Add our new function to the registry
FunctionRegistry.getInstance().add(geoFunction);
// The expected results of the SPARQL query once the PCJ has been computed.
final Set<BindingSet> expectedResults = new HashSet<>();
final MapBindingSet bs = new MapBindingSet();
bs.addBinding("wkt", vf.createLiteral("Point(-77.03524 38.889468)", vf.createURI("http://www.opengis.net/ont/geosparql#wktLiteral")));
bs.addBinding("feature", vf.createURI("tag:rya.apache.org,2017:ex#feature"));
bs.addBinding("point", vf.createURI("tag:rya.apache.org,2017:ex#test_point"));
expectedResults.add(bs);
runTest(sparql, statements, expectedResults);
}
use of org.openrdf.query.algebra.evaluation.function.Function in project incubator-rya by apache.
the class QueryIT method withCustomFilters.
@Test
public void withCustomFilters() throws Exception {
final String sparql = "prefix ryafunc: <tag:rya.apache.org,2017:function#> " + "SELECT ?name ?age " + "{ " + "FILTER( ryafunc:isTeen(?age) ) . " + "?name <http://hasAge> ?age . " + "?name <http://playsSport> \"Soccer\" . " + "}";
// Register a custom Filter.
final Function fooFunction = new Function() {
@Override
public String getURI() {
return "tag:rya.apache.org,2017:function#isTeen";
}
static final int TEEN_THRESHOLD = 20;
@Override
public Value evaluate(final ValueFactory valueFactory, final Value... args) throws ValueExprEvaluationException {
if (args.length != 1) {
throw new ValueExprEvaluationException("isTeen() requires exactly 1 argument, got " + args.length);
}
if (args[0] instanceof Literal) {
final Literal literal = (Literal) args[0];
final URI datatype = literal.getDatatype();
// ABS function accepts only numeric literals
if (datatype != null && XMLDatatypeUtil.isNumericDatatype(datatype)) {
if (XMLDatatypeUtil.isDecimalDatatype(datatype)) {
final BigDecimal bigValue = literal.decimalValue();
return BooleanLiteralImpl.valueOf(bigValue.compareTo(new BigDecimal(TEEN_THRESHOLD)) < 0);
} else if (XMLDatatypeUtil.isFloatingPointDatatype(datatype)) {
final double doubleValue = literal.doubleValue();
return BooleanLiteralImpl.valueOf(doubleValue < TEEN_THRESHOLD);
} else {
throw new ValueExprEvaluationException("unexpected datatype (expect decimal/int or floating) for function operand: " + args[0]);
}
} else {
throw new ValueExprEvaluationException("unexpected input value (expect non-null and numeric) for function: " + args[0]);
}
} else {
throw new ValueExprEvaluationException("unexpected input value (expect literal) for function: " + args[0]);
}
}
};
// Add our new function to the registry
FunctionRegistry.getInstance().add(fooFunction);
// Create the Statements that will be loaded into Rya.
final ValueFactory vf = new ValueFactoryImpl();
final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("http://Alice"), vf.createURI("http://hasAge"), vf.createLiteral(18)), vf.createStatement(vf.createURI("http://Bob"), vf.createURI("http://hasAge"), vf.createLiteral(30)), vf.createStatement(vf.createURI("http://Charlie"), vf.createURI("http://hasAge"), vf.createLiteral(14)), vf.createStatement(vf.createURI("http://David"), vf.createURI("http://hasAge"), vf.createLiteral(16)), vf.createStatement(vf.createURI("http://Eve"), vf.createURI("http://hasAge"), vf.createLiteral(35)), vf.createStatement(vf.createURI("http://Alice"), vf.createURI("http://playsSport"), vf.createLiteral("Soccer")), vf.createStatement(vf.createURI("http://Bob"), vf.createURI("http://playsSport"), vf.createLiteral("Soccer")), vf.createStatement(vf.createURI("http://Charlie"), vf.createURI("http://playsSport"), vf.createLiteral("Basketball")), vf.createStatement(vf.createURI("http://Charlie"), vf.createURI("http://playsSport"), vf.createLiteral("Soccer")), vf.createStatement(vf.createURI("http://David"), vf.createURI("http://playsSport"), vf.createLiteral("Basketball")));
// Create the expected results of the SPARQL query once the PCJ has been computed.
final Set<BindingSet> expectedResults = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("name", vf.createURI("http://Alice"));
bs.addBinding("age", vf.createLiteral("18", XMLSchema.INTEGER));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("name", vf.createURI("http://Charlie"));
bs.addBinding("age", vf.createLiteral("14", XMLSchema.INTEGER));
expectedResults.add(bs);
// Verify the end results of the query match the expected results.
runTest(sparql, statements, expectedResults, ExportStrategy.RYA);
}
use of org.openrdf.query.algebra.evaluation.function.Function in project incubator-rya by apache.
the class TemporalFilterIT method temporalFunctionsRegistered.
@Test
public void temporalFunctionsRegistered() {
int count = 0;
final Collection<Function> funcs = FunctionRegistry.getInstance().getAll();
for (final Function fun : funcs) {
if (fun.getURI().startsWith(TEMPORAL)) {
count++;
}
}
// There are 4 temporal functions registered, ensure that there are 4.
assertEquals(4, count);
}
Aggregations