Search in sources :

Example 31 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class MongoStatementMetadataIT method simpleQueryWithBindingSetJoinPropertyToSubject.

/**
 * Tests to see if correct result is passed back when a metadata statement
 * is joined with a StatementPattern statement (i.e. a common variable
 * appears in a StatementPattern statement and a metadata statement).
 * StatementPattern statements have either rdf:subject, rdf:predicate, or
 * rdf:object as the predicate while a metadata statement is any statement
 * in the reified query whose predicate is not rdf:type and not a
 * StatementPattern predicate.
 *
 * @throws MalformedQueryException
 * @throws QueryEvaluationException
 * @throws RyaDAOException
 */
@Test
public void simpleQueryWithBindingSetJoinPropertyToSubject() throws Exception {
    Sail sail = RyaSailFactory.getInstance(conf);
    MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata1 = new StatementMetadata();
        metadata1.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Doug"));
        metadata1.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
        final StatementMetadata metadata2 = new StatementMetadata();
        metadata2.addMetadata(new RyaURI("http://createdBy"), new RyaURI("http://Bob"));
        metadata2.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-02-04"));
        final RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaURI("http://BurgerShack"), new RyaURI("http://context"), "", metadata1);
        final RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://talksTo"), new RyaURI("http://Betty"), new RyaURI("http://context"), "", metadata1);
        final RyaStatement statement3 = new RyaStatement(new RyaURI("http://Fred"), new RyaURI("http://talksTo"), new RyaURI("http://Amanda"), new RyaURI("http://context"), "", metadata1);
        final RyaStatement statement4 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://talksTo"), new RyaURI("http://Wanda"), new RyaURI("http://context"), "", metadata2);
        dao.add(statement1);
        dao.add(statement2);
        dao.add(statement3);
        dao.add(statement4);
        SailRepositoryConnection conn = new SailRepository(sail).getConnection();
        final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query2).evaluate();
        final Set<BindingSet> expected = new HashSet<>();
        final QueryBindingSet expected1 = new QueryBindingSet();
        expected1.addBinding("b", new URIImpl("http://Betty"));
        expected1.addBinding("a", new URIImpl("http://Joe"));
        expected1.addBinding("c", new URIImpl("http://Doug"));
        expected.add(expected1);
        final Set<BindingSet> bsSet = new HashSet<>();
        while (result.hasNext()) {
            bsSet.add(result.next());
        }
        assertEquals(expected, bsSet);
        dao.delete(statement1, conf);
        dao.delete(statement2, conf);
        dao.delete(statement3, conf);
        dao.delete(statement4, conf);
    } finally {
        dao.destroy();
        sail.shutDown();
    }
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) URIImpl(org.openrdf.model.impl.URIImpl) RyaType(org.apache.rya.api.domain.RyaType) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) RyaURI(org.apache.rya.api.domain.RyaURI) Sail(org.openrdf.sail.Sail) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 32 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class MongoStatementMetadataIT method simpleQueryWithBindingSet.

@Test
public void simpleQueryWithBindingSet() throws Exception {
    Sail sail = RyaSailFactory.getInstance(conf);
    MongoDBRyaDAO dao = new MongoDBRyaDAO();
    try {
        dao.setConf(conf);
        dao.init();
        final StatementMetadata metadata = new StatementMetadata();
        metadata.addMetadata(new RyaURI("http://createdBy"), new RyaType("Joe"));
        metadata.addMetadata(new RyaURI("http://createdOn"), new RyaType(XMLSchema.DATE, "2017-01-04"));
        final RyaStatement statement1 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("CoffeeShop"), new RyaURI("http://context"), "", metadata);
        final RyaStatement statement2 = new RyaStatement(new RyaURI("http://Joe"), new RyaURI("http://worksAt"), new RyaType("HardwareStore"), new RyaURI("http://context"), "", metadata);
        dao.add(statement1);
        dao.add(statement2);
        SailRepositoryConnection conn = new SailRepository(sail).getConnection();
        final TupleQueryResult result = conn.prepareTupleQuery(QueryLanguage.SPARQL, query1).evaluate();
        final Set<BindingSet> expected = new HashSet<>();
        final QueryBindingSet expected1 = new QueryBindingSet();
        expected1.addBinding("x", new LiteralImpl("CoffeeShop"));
        expected1.addBinding("y", new LiteralImpl("Joe"));
        final QueryBindingSet expected2 = new QueryBindingSet();
        expected2.addBinding("x", new LiteralImpl("HardwareStore"));
        expected2.addBinding("y", new LiteralImpl("Joe"));
        expected.add(expected1);
        expected.add(expected2);
        final Set<BindingSet> bsSet = new HashSet<>();
        while (result.hasNext()) {
            bsSet.add(result.next());
        }
        assertEquals(expected, bsSet);
        dao.delete(statement1, conf);
        dao.delete(statement2, conf);
    } finally {
        dao.destroy();
        sail.shutDown();
    }
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SailRepository(org.openrdf.repository.sail.SailRepository) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) RyaStatement(org.apache.rya.api.domain.RyaStatement) RyaType(org.apache.rya.api.domain.RyaType) SailRepositoryConnection(org.openrdf.repository.sail.SailRepositoryConnection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) MongoDBRyaDAO(org.apache.rya.mongodb.MongoDBRyaDAO) RyaURI(org.apache.rya.api.domain.RyaURI) LiteralImpl(org.openrdf.model.impl.LiteralImpl) Sail(org.openrdf.sail.Sail) TupleQueryResult(org.openrdf.query.TupleQueryResult) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 33 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet 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 34 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class PipelineResultIterationTest method testIteration.

@Test
public void testIteration() throws QueryEvaluationException {
    HashMap<String, String> nameMap = new HashMap<>();
    nameMap.put("bName", "b");
    nameMap.put("eName", "e");
    PipelineResultIteration iter = new PipelineResultIteration(documentIterator(new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("b", "urn:Beth")), new Document("<VALUES>", new Document("a", "urn:Alice").append("bName", "urn:Bob")), new Document("<VALUES>", new Document("a", "urn:Alice").append("c", "urn:Carol")), new Document("<VALUES>", new Document("cName", "urn:Carol").append("d", "urn:Dan"))), nameMap, new QueryBindingSet());
    Assert.assertTrue(iter.hasNext());
    BindingSet bs = iter.next();
    Assert.assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
    Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
    Assert.assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
    Assert.assertTrue(iter.hasNext());
    bs = iter.next();
    Assert.assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
    Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
    Assert.assertEquals("urn:Beth", bs.getBinding("b").getValue().stringValue());
    Assert.assertTrue(iter.hasNext());
    bs = iter.next();
    Assert.assertEquals(Sets.newHashSet("a", "b"), bs.getBindingNames());
    Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
    Assert.assertEquals("urn:Bob", bs.getBinding("b").getValue().stringValue());
    Assert.assertTrue(iter.hasNext());
    bs = iter.next();
    Assert.assertEquals(Sets.newHashSet("a", "c"), bs.getBindingNames());
    Assert.assertEquals("urn:Alice", bs.getBinding("a").getValue().stringValue());
    Assert.assertEquals("urn:Carol", bs.getBinding("c").getValue().stringValue());
    bs = iter.next();
    Assert.assertEquals(Sets.newHashSet("cName", "d"), bs.getBindingNames());
    Assert.assertEquals("urn:Carol", bs.getBinding("cName").getValue().stringValue());
    Assert.assertEquals("urn:Dan", bs.getBinding("d").getValue().stringValue());
    Assert.assertFalse(iter.hasNext());
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) HashMap(java.util.HashMap) Document(org.bson.Document) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 35 with QueryBindingSet

use of org.openrdf.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.

the class BindingSetHashJoinIterator method joinBindingSets.

/**
 * This method verifies that all common variables have a common value and
 * then joins the BindingSets together. In the case that the PCJ contains a
 * LeftJoin, if the leftBs and rightBs have a common variable with distinct
 * values and that common variable is unassured (only appears in LeftJoin),
 * this method uses the value corresponding to leftBs.
 *
 * @param leftBs
 *            - BindingSet passed into PCJ
 * @param rightBs
 *            - PCJ BindingSet
 * @return - joined BindingSet
 */
private BindingSet joinBindingSets(BindingSet leftBs, BindingSet rightBs) {
    Set<String> commonVars = Sets.intersection(leftBs.getBindingNames(), rightBs.getBindingNames());
    // add value corresponding to leftBs
    for (String s : commonVars) {
        if (!leftBs.getValue(s).equals(rightBs.getValue(s)) && !unAssuredVariables.contains(s)) {
            return EMPTY_BINDINGSET;
        }
    }
    QueryBindingSet bs = new QueryBindingSet(removeConstants(leftBs));
    rightBs = removeConstants(rightBs);
    // to leftBs, which is effectively performing a LeftJoin.
    for (String s : rightBs.getBindingNames()) {
        if (bs.getValue(s) == null) {
            bs.addBinding(s, rightBs.getValue(s));
        }
    }
    return bs;
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Aggregations

QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)107 Test (org.junit.Test)84 BindingSet (org.openrdf.query.BindingSet)79 QueryEvaluationException (org.openrdf.query.QueryEvaluationException)52 RyaURI (org.apache.rya.api.domain.RyaURI)46 RyaStatement (org.apache.rya.api.domain.RyaStatement)45 SPARQLParser (org.openrdf.query.parser.sparql.SPARQLParser)42 StatementPattern (org.openrdf.query.algebra.StatementPattern)41 ParsedQuery (org.openrdf.query.parser.ParsedQuery)41 RyaType (org.apache.rya.api.domain.RyaType)39 LiteralImpl (org.openrdf.model.impl.LiteralImpl)32 ArrayList (java.util.ArrayList)30 HashSet (java.util.HashSet)28 URIImpl (org.openrdf.model.impl.URIImpl)27 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)26 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)20 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 Statement (org.openrdf.model.Statement)15 TupleExpr (org.openrdf.query.algebra.TupleExpr)14 Collection (java.util.Collection)13