Search in sources :

Example 41 with ValueFactoryImpl

use of org.openrdf.model.impl.ValueFactoryImpl in project incubator-rya by apache.

the class PeriodicNotificationApplicationIT method periodicApplicationWithAggAndGroupByTest.

@Test
public void periodicApplicationWithAggAndGroupByTest() throws Exception {
    final String sparql = // n
    "prefix function: <http://org.apache.rya/function#> " + // n
    "prefix time: <http://www.w3.org/2006/time#> " + // n
    "select ?type (count(?obs) as ?total) where {" + // n
    "Filter(function:periodic(?time, 1, .25, time:minutes)) " + // n
    "?obs <uri:hasTime> ?time. " + // n
    "?obs <uri:hasObsType> ?type } group by ?type";
    // make data
    final int periodMult = 15;
    final ValueFactory vf = new ValueFactoryImpl();
    final DatatypeFactory dtf = DatatypeFactory.newInstance();
    // results more predictable
    while (System.currentTimeMillis() % (periodMult * 1000) > 500) {
        ;
    }
    final ZonedDateTime time = ZonedDateTime.now();
    final ZonedDateTime zTime1 = time.minusSeconds(2 * periodMult);
    final String time1 = zTime1.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime2 = zTime1.minusSeconds(periodMult);
    final String time2 = zTime2.format(DateTimeFormatter.ISO_INSTANT);
    final ZonedDateTime zTime3 = zTime2.minusSeconds(periodMult);
    final String time3 = zTime3.format(DateTimeFormatter.ISO_INSTANT);
    final Collection<Statement> statements = Sets.newHashSet(vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time1))), vf.createStatement(vf.createURI("urn:obs_1"), vf.createURI("uri:hasObsType"), vf.createLiteral("ship")), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time1))), vf.createStatement(vf.createURI("urn:obs_2"), vf.createURI("uri:hasObsType"), vf.createLiteral("airplane")), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time2))), vf.createStatement(vf.createURI("urn:obs_3"), vf.createURI("uri:hasObsType"), vf.createLiteral("ship")), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time2))), vf.createStatement(vf.createURI("urn:obs_4"), vf.createURI("uri:hasObsType"), vf.createLiteral("airplane")), vf.createStatement(vf.createURI("urn:obs_5"), vf.createURI("uri:hasTime"), vf.createLiteral(dtf.newXMLGregorianCalendar(time3))), vf.createStatement(vf.createURI("urn:obs_5"), vf.createURI("uri:hasObsType"), vf.createLiteral("automobile")));
    try (FluoClient fluo = FluoClientFactory.getFluoClient(conf.getFluoAppName(), Optional.of(conf.getFluoTableName()), conf)) {
        final Connector connector = ConfigUtils.getConnector(conf);
        final PeriodicQueryResultStorage storage = new AccumuloPeriodicQueryResultStorage(connector, conf.getTablePrefix());
        final CreatePeriodicQuery periodicQuery = new CreatePeriodicQuery(fluo, storage);
        final String id = FluoQueryUtils.convertFluoQueryIdToPcjId(periodicQuery.createPeriodicQuery(sparql, registrar).getQueryId());
        addData(statements);
        app.start();
        final Multimap<Long, BindingSet> actual = HashMultimap.create();
        try (KafkaConsumer<String, BindingSet> consumer = new KafkaConsumer<>(kafkaProps, new StringDeserializer(), new BindingSetSerDe())) {
            consumer.subscribe(Arrays.asList(id));
            final long end = System.currentTimeMillis() + 4 * periodMult * 1000;
            long lastBinId = 0L;
            long binId = 0L;
            final List<Long> ids = new ArrayList<>();
            while (System.currentTimeMillis() < end) {
                final ConsumerRecords<String, BindingSet> records = consumer.poll(periodMult * 1000);
                for (final ConsumerRecord<String, BindingSet> record : records) {
                    final BindingSet result = record.value();
                    binId = Long.parseLong(result.getBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID).getValue().stringValue());
                    if (lastBinId != binId) {
                        lastBinId = binId;
                        ids.add(binId);
                    }
                    actual.put(binId, result);
                }
            }
            final Map<Long, Set<BindingSet>> expected = new HashMap<>();
            final Set<BindingSet> expected1 = new HashSet<>();
            final QueryBindingSet bs1 = new QueryBindingSet();
            bs1.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(0)));
            bs1.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
            bs1.addBinding("type", vf.createLiteral("airplane"));
            final QueryBindingSet bs2 = new QueryBindingSet();
            bs2.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(0)));
            bs2.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
            bs2.addBinding("type", vf.createLiteral("ship"));
            final QueryBindingSet bs3 = new QueryBindingSet();
            bs3.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(0)));
            bs3.addBinding("total", new LiteralImpl("1", XMLSchema.INTEGER));
            bs3.addBinding("type", vf.createLiteral("automobile"));
            expected1.add(bs1);
            expected1.add(bs2);
            expected1.add(bs3);
            final Set<BindingSet> expected2 = new HashSet<>();
            final QueryBindingSet bs4 = new QueryBindingSet();
            bs4.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(1)));
            bs4.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
            bs4.addBinding("type", vf.createLiteral("airplane"));
            final QueryBindingSet bs5 = new QueryBindingSet();
            bs5.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(1)));
            bs5.addBinding("total", new LiteralImpl("2", XMLSchema.INTEGER));
            bs5.addBinding("type", vf.createLiteral("ship"));
            expected2.add(bs4);
            expected2.add(bs5);
            final Set<BindingSet> expected3 = new HashSet<>();
            final QueryBindingSet bs6 = new QueryBindingSet();
            bs6.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(2)));
            bs6.addBinding("total", new LiteralImpl("1", XMLSchema.INTEGER));
            bs6.addBinding("type", vf.createLiteral("ship"));
            final QueryBindingSet bs7 = new QueryBindingSet();
            bs7.addBinding(IncrementalUpdateConstants.PERIODIC_BIN_ID, vf.createLiteral(ids.get(2)));
            bs7.addBinding("total", new LiteralImpl("1", XMLSchema.INTEGER));
            bs7.addBinding("type", vf.createLiteral("airplane"));
            expected3.add(bs6);
            expected3.add(bs7);
            expected.put(ids.get(0), expected1);
            expected.put(ids.get(1), expected2);
            expected.put(ids.get(2), expected3);
            Assert.assertEquals(3, actual.asMap().size());
            for (final Long ident : ids) {
                Assert.assertEquals(expected.get(ident), actual.get(ident));
            }
        }
        final Set<BindingSet> expectedResults = new HashSet<>();
        try (CloseableIterator<BindingSet> results = storage.listResults(id, Optional.empty())) {
            results.forEachRemaining(x -> expectedResults.add(x));
            Assert.assertEquals(0, expectedResults.size());
        }
    }
}
Also used : Connector(org.apache.accumulo.core.client.Connector) FluoClient(org.apache.fluo.api.client.FluoClient) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Set(java.util.Set) HashSet(java.util.HashSet) BindingSet(org.openrdf.query.BindingSet) HashMap(java.util.HashMap) AccumuloPeriodicQueryResultStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPeriodicQueryResultStorage) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ArrayList(java.util.ArrayList) ZonedDateTime(java.time.ZonedDateTime) CreatePeriodicQuery(org.apache.rya.indexing.pcj.fluo.api.CreatePeriodicQuery) PeriodicQueryResultStorage(org.apache.rya.indexing.pcj.storage.PeriodicQueryResultStorage) AccumuloPeriodicQueryResultStorage(org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPeriodicQueryResultStorage) HashSet(java.util.HashSet) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) DatatypeFactory(javax.xml.datatype.DatatypeFactory) Statement(org.openrdf.model.Statement) StringDeserializer(org.apache.kafka.common.serialization.StringDeserializer) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ValueFactory(org.openrdf.model.ValueFactory) BindingSetSerDe(org.apache.rya.periodic.notification.serialization.BindingSetSerDe) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Test(org.junit.Test)

Example 42 with ValueFactoryImpl

use of org.openrdf.model.impl.ValueFactoryImpl 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));
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Filter(org.openrdf.query.algebra.Filter) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 43 with ValueFactoryImpl

use of org.openrdf.model.impl.ValueFactoryImpl 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);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Projection(org.openrdf.query.algebra.Projection) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 44 with ValueFactoryImpl

use of org.openrdf.model.impl.ValueFactoryImpl 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);
}
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) MapBindingSet(org.openrdf.query.impl.MapBindingSet) URI(org.openrdf.model.URI) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 45 with ValueFactoryImpl

use of org.openrdf.model.impl.ValueFactoryImpl 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)

Aggregations

ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)190 ValueFactory (org.openrdf.model.ValueFactory)187 Test (org.junit.Test)174 Statement (org.openrdf.model.Statement)106 MapBindingSet (org.openrdf.query.impl.MapBindingSet)91 HashSet (java.util.HashSet)68 URI (org.openrdf.model.URI)67 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)66 Value (org.openrdf.model.Value)55 BindingSet (org.openrdf.query.BindingSet)43 Resource (org.openrdf.model.Resource)35 RdfToRyaConversions.convertStatement (org.apache.rya.api.resolver.RdfToRyaConversions.convertStatement)34 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)32 ContextStatementImpl (org.openrdf.model.impl.ContextStatementImpl)32 ArrayList (java.util.ArrayList)30 UUID (java.util.UUID)29 StatementImpl (org.openrdf.model.impl.StatementImpl)26 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)25 LinearRing (com.vividsolutions.jts.geom.LinearRing)24 Polygon (com.vividsolutions.jts.geom.Polygon)24