use of org.openrdf.model.ValueFactory in project incubator-rya by apache.
the class FilterEvaluatorTest method matches.
@Test
public void matches() throws Exception {
// Read the filter object from a SPARQL query.
final Filter filter = getFilter("SELECT * " + "WHERE { " + "FILTER(?age < 10)" + "?person <urn:age> ?age " + "}");
// Create the input binding set.
final ValueFactory vf = new ValueFactoryImpl();
final MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("age", vf.createLiteral(9));
final VisibilityBindingSet visBs = new VisibilityBindingSet(bs);
// Test the evaluator.
assertTrue(FilterEvaluator.make(filter).filter(visBs));
}
use of org.openrdf.model.ValueFactory in project incubator-rya by apache.
the class ProjectionEvaluatorTest method addsBlankNodeBinding.
/**
* This projection creates a Binding Set that represents a Statement that has a blank node added to it.
*/
@Test
public void addsBlankNodeBinding() throws Exception {
// Read the projection object from a SPARQL query.
final Projection projection = getProjection("CONSTRUCT { ?person <urn:hasChild> _:b } " + "WHERE {" + "?person <urn:hasGrandchild> ?grandchild ." + "}");
// Create a Binding Set that contains the result of the WHERE clause.
final ValueFactory vf = new ValueFactoryImpl();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("person", vf.createURI("urn:Alice"));
bs.addBinding("hasGrandchild", vf.createURI("urn:Bob"));
final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
// Execute the projection.
final VisibilityBindingSet result = ProjectionEvaluator.make(projection).project(original);
// The expected binding set represents a statement. We need to get the blank node's id from the
// result since that is different every time.
bs = new MapBindingSet();
bs.addBinding("subject", vf.createURI("urn:Alice"));
bs.addBinding("predicate", vf.createURI("urn:hasChild"));
bs.addBinding("object", result.getValue("object"));
final VisibilityBindingSet expected = new VisibilityBindingSet(bs, "a|b");
assertEquals(expected, result);
}
use of org.openrdf.model.ValueFactory in project incubator-rya by apache.
the class GeoFunctionsIT method GeoDistance.
@Test
public void GeoDistance() throws Exception {
final String sparql = "PREFIX geo: <http://www.opengis.net/ont/geosparql#> " + "PREFIX geof: <http://www.opengis.net/def/function/geosparql/> " + "PREFIX uom: <http://www.opengis.net/def/uom/OGC/1.0/> " + "SELECT ?cityA ?cityB " + "WHERE { " + "?cityA <urn:containedIn> ?continent. " + "?cityB <urn:containedIn> ?continent. " + "?cityA geo:asWKT ?coord1 . " + "?cityB geo:asWKT ?coord2 . " + // from brussels 173km to amsterdam
" FILTER ( 500000 > geof:distance(?coord1, ?coord2, uom:metre) ) . " + " FILTER ( !sameTerm (?cityA, ?cityB) ) " + "}";
final ValueFactory vf = new ValueFactoryImpl();
final URI wktTypeUri = vf.createURI("http://www.opengis.net/ont/geosparql#wktLiteral");
final URI asWKT = vf.createURI("http://www.opengis.net/ont/geosparql#asWKT");
final Set<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#dakar"), asWKT, vf.createLiteral("Point(-17.45 14.69)", wktTypeUri)), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#dakar2"), asWKT, vf.createLiteral("Point(-17.45 14.69)", wktTypeUri)), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#canberra"), asWKT, vf.createLiteral("Point(149.12 -35.31)", wktTypeUri)), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#brussels"), asWKT, vf.createLiteral("Point(4.35 50.85)", wktTypeUri)), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#amsterdam"), asWKT, vf.createLiteral("Point(4.9 52.37)", wktTypeUri)), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#amsterdam"), vf.createURI("urn:containedIn"), vf.createLiteral("Europe")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#dakar"), vf.createURI("urn:containedIn"), vf.createLiteral("Africa")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#dakar2"), vf.createURI("urn:containedIn"), vf.createLiteral("Africa")), vf.createStatement(vf.createURI("tag:rya.apache.org,2017:ex#brussels"), vf.createURI("urn:containedIn"), vf.createLiteral("Europe")));
// The expected results of the SPARQL query once the PCJ has been computed.l
final Set<BindingSet> expectedResults = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("cityA", vf.createURI("tag:rya.apache.org,2017:ex#dakar"));
bs.addBinding("cityB", vf.createURI("tag:rya.apache.org,2017:ex#dakar2"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("cityA", vf.createURI("tag:rya.apache.org,2017:ex#dakar2"));
bs.addBinding("cityB", vf.createURI("tag:rya.apache.org,2017:ex#dakar"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("cityA", vf.createURI("tag:rya.apache.org,2017:ex#brussels"));
bs.addBinding("cityB", vf.createURI("tag:rya.apache.org,2017:ex#amsterdam"));
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("cityA", vf.createURI("tag:rya.apache.org,2017:ex#amsterdam"));
bs.addBinding("cityB", vf.createURI("tag:rya.apache.org,2017:ex#brussels"));
expectedResults.add(bs);
runTest(sparql, statements, expectedResults);
}
use of org.openrdf.model.ValueFactory 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.model.ValueFactory in project incubator-rya by apache.
the class GeoFunctionsIT method withTemporal.
@Test
public void withTemporal() throws Exception {
// Find all stored dates.
final String sparql = "PREFIX time: <http://www.w3.org/2006/time#> " + "PREFIX xml: <http://www.w3.org/2001/XMLSchema#> " + "PREFIX tempo: <tag:rya-rdf.org,2015:temporal#> " + "SELECT ?event ?time { " + "?event time:inXSDDateTime ?time . " + // after 3 seconds
"FILTER(?time > '2001-01-01T01:01:03-08:00'^^xml:dateTime) " + // 2006/12/31 include 2006, not 2007,8
"FILTER('2007-01-01T01:01:01+09:00'^^xml:dateTime > ?time ) " + "}";
// create some resources and literals to make statements out of
final ValueFactory vf = new ValueFactoryImpl();
final DatatypeFactory dtf = DatatypeFactory.newInstance();
final URI dtPredUri = vf.createURI("http://www.w3.org/2006/time#inXSDDateTime");
final URI eventz = vf.createURI("<http://eventz>");
final Set<Statement> statements = Sets.newHashSet(vf.createStatement(eventz, vf.createURI("http://www.w3.org/1999/02/22-rdf-syntax-ns#type"), vf.createURI("<http://www.w3.org/2006/time#Instant>")), // 1 second
vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2001-01-01T01:01:01-08:00"))), // 2 seconds
vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2001-01-01T04:01:02.000-05:00"))), // 3 seconds
vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2001-01-01T01:01:03-08:00"))), // 4 seconds
vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2001-01-01T01:01:03.999-08:00"))), // 5 seconds
vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2001-01-01T09:01:05Z"))), vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2006-01-01T05:00:00.000Z"))), vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2007-01-01T05:00:00.000Z"))), vf.createStatement(eventz, dtPredUri, vf.createLiteral(dtf.newXMLGregorianCalendar("2008-01-01T05:00:00.000Z"))));
final Set<BindingSet> expectedResults = new HashSet<>();
MapBindingSet bs = new MapBindingSet();
bs.addBinding("time", vf.createLiteral(dtf.newXMLGregorianCalendar("2001-01-01T09:01:05.000Z")));
bs.addBinding("event", eventz);
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("time", vf.createLiteral(dtf.newXMLGregorianCalendar("2006-01-01T05:00:00.000Z")));
bs.addBinding("event", eventz);
expectedResults.add(bs);
bs = new MapBindingSet();
bs.addBinding("time", vf.createLiteral(dtf.newXMLGregorianCalendar("2001-01-01T09:01:03.999Z")));
bs.addBinding("event", eventz);
expectedResults.add(bs);
runTest(sparql, statements, expectedResults);
}
Aggregations