Search in sources :

Example 1 with Function

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);
}
Also used : Function(org.openrdf.query.algebra.evaluation.function.Function) Test(org.junit.Test)

Example 2 with Function

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);
}
Also used : Function(org.openrdf.query.algebra.evaluation.function.Function) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Value(org.openrdf.model.Value) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Function

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);
}
Also used : MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) URI(org.openrdf.model.URI) BigDecimal(java.math.BigDecimal) Function(org.openrdf.query.algebra.evaluation.function.Function) Literal(org.openrdf.model.Literal) Value(org.openrdf.model.Value) MapBindingSet(org.openrdf.query.impl.MapBindingSet) ValueExprEvaluationException(org.openrdf.query.algebra.evaluation.ValueExprEvaluationException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with Function

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);
}
Also used : Function(org.openrdf.query.algebra.evaluation.function.Function) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)4 Function (org.openrdf.query.algebra.evaluation.function.Function)4 HashSet (java.util.HashSet)2 Statement (org.openrdf.model.Statement)2 Value (org.openrdf.model.Value)2 ValueFactory (org.openrdf.model.ValueFactory)2 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)2 BindingSet (org.openrdf.query.BindingSet)2 ValueExprEvaluationException (org.openrdf.query.algebra.evaluation.ValueExprEvaluationException)2 MapBindingSet (org.openrdf.query.impl.MapBindingSet)2 BigDecimal (java.math.BigDecimal)1 Literal (org.openrdf.model.Literal)1 URI (org.openrdf.model.URI)1