Search in sources :

Example 61 with QueryBindingSet

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

the class PeriodicNotificationExporterIT method testExporter.

@Test
public void testExporter() throws InterruptedException {
    final String topic1 = kafkaTestInstanceRule.getKafkaTopicName() + "1";
    final String topic2 = kafkaTestInstanceRule.getKafkaTopicName() + "2";
    kafkaTestInstanceRule.createTopic(topic1);
    kafkaTestInstanceRule.createTopic(topic2);
    final BlockingQueue<BindingSetRecord> records = new LinkedBlockingQueue<>();
    final KafkaExporterExecutor exporter = new KafkaExporterExecutor(new KafkaProducer<String, BindingSet>(createKafkaProducerConfig()), 1, records);
    exporter.start();
    final QueryBindingSet bs1 = new QueryBindingSet();
    bs1.addBinding(PeriodicQueryResultStorage.PeriodicBinId, vf.createLiteral(1L));
    bs1.addBinding("name", vf.createURI("uri:Bob"));
    final BindingSetRecord record1 = new BindingSetRecord(bs1, topic1);
    final QueryBindingSet bs2 = new QueryBindingSet();
    bs2.addBinding(PeriodicQueryResultStorage.PeriodicBinId, vf.createLiteral(2L));
    bs2.addBinding("name", vf.createURI("uri:Joe"));
    final BindingSetRecord record2 = new BindingSetRecord(bs2, topic2);
    records.add(record1);
    records.add(record2);
    final Set<BindingSet> expected1 = new HashSet<>();
    expected1.add(bs1);
    final Set<BindingSet> expected2 = new HashSet<>();
    expected2.add(bs2);
    final Set<BindingSet> actual1 = getBindingSetsFromKafka(topic1);
    final Set<BindingSet> actual2 = getBindingSetsFromKafka(topic2);
    Assert.assertEquals(expected1, actual1);
    Assert.assertEquals(expected2, actual2);
    exporter.stop();
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) BindingSetRecord(org.apache.rya.periodic.notification.api.BindingSetRecord) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 62 with QueryBindingSet

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

the class StatementPatternEvalTest method simpleQueryWithBindingSetConstantContext.

@Test
public void simpleQueryWithBindingSetConstantContext() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    // query is used to build statement that will be evaluated
    String query = "select ?x ?c where{ graph <uri:context1>  {?x <uri:talksTo> <uri:Bob>. }}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    RyaStatement statement1 = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
    dao.add(statement1);
    RyaStatement statement2 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
    dao.add(statement2);
    RyaStatement statement3 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context2"), "", new StatementMetadata());
    dao.add(statement3);
    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    bsConstraint1.addBinding("x", new URIImpl("uri:Doug"));
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(1, bsList.size());
    QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("x", new URIImpl("uri:Doug"));
    Assert.assertEquals(expected, bsList.get(0));
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) URIImpl(org.openrdf.model.impl.URIImpl) RyaType(org.apache.rya.api.domain.RyaType) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaURI(org.apache.rya.api.domain.RyaURI) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Test(org.junit.Test)

Example 63 with QueryBindingSet

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

the class StatementPatternEvalTest method simpleQueryWithBindingSets.

@Test
public void simpleQueryWithBindingSets() throws MalformedQueryException, QueryEvaluationException, RyaDAOException {
    // query is used to build statement that will be evaluated
    String query = "select ?x ?c where{ graph ?c  {?x <uri:talksTo> <uri:Bob>. }}";
    SPARQLParser parser = new SPARQLParser();
    ParsedQuery pq = parser.parseQuery(query, null);
    List<StatementPattern> spList = StatementPatternCollector.process(pq.getTupleExpr());
    RyaStatement statement1 = new RyaStatement(new RyaURI("uri:Joe"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context1"), "", new StatementMetadata());
    dao.add(statement1);
    RyaStatement statement2 = new RyaStatement(new RyaURI("uri:Doug"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context2"), "", new StatementMetadata());
    dao.add(statement2);
    RyaStatement statement3 = new RyaStatement(new RyaURI("uri:Eric"), new RyaURI("uri:talksTo"), new RyaType("uri:Bob"), new RyaURI("uri:context3"), "", new StatementMetadata());
    dao.add(statement3);
    QueryBindingSet bsConstraint1 = new QueryBindingSet();
    bsConstraint1.addBinding("c", new URIImpl("uri:context2"));
    QueryBindingSet bsConstraint2 = new QueryBindingSet();
    bsConstraint2.addBinding("c", new URIImpl("uri:context1"));
    CloseableIteration<BindingSet, QueryEvaluationException> iteration = eval.evaluate(spList.get(0), Arrays.asList(bsConstraint1, bsConstraint2));
    List<BindingSet> bsList = new ArrayList<>();
    while (iteration.hasNext()) {
        bsList.add(iteration.next());
    }
    Assert.assertEquals(2, bsList.size());
    QueryBindingSet expected1 = new QueryBindingSet();
    expected1.addBinding("x", new URIImpl("uri:Joe"));
    expected1.addBinding("c", new URIImpl("uri:context1"));
    QueryBindingSet expected2 = new QueryBindingSet();
    expected2.addBinding("x", new URIImpl("uri:Doug"));
    expected2.addBinding("c", new URIImpl("uri:context2"));
    Set<BindingSet> expected = new HashSet<>(Arrays.asList(expected1, expected2));
    Set<BindingSet> actual = new HashSet<>(bsList);
    Assert.assertEquals(expected, actual);
    dao.delete(Arrays.asList(statement1, statement2, statement3).iterator(), conf);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) StatementMetadata(org.apache.rya.api.domain.StatementMetadata) ArrayList(java.util.ArrayList) RyaStatement(org.apache.rya.api.domain.RyaStatement) URIImpl(org.openrdf.model.impl.URIImpl) RyaType(org.apache.rya.api.domain.RyaType) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) StatementPattern(org.openrdf.query.algebra.StatementPattern) RyaURI(org.apache.rya.api.domain.RyaURI) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 64 with QueryBindingSet

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

the class ParallelEvaluationStrategyImpl method evaluate.

public CloseableIteration<BindingSet, QueryEvaluationException> evaluate(final StatementPattern sp, Collection<BindingSet> bindings) throws QueryEvaluationException {
    final Var subjVar = sp.getSubjectVar();
    final Var predVar = sp.getPredicateVar();
    final Var objVar = sp.getObjectVar();
    final Var cntxtVar = sp.getContextVar();
    List<Map.Entry<Statement, BindingSet>> stmts = new ArrayList<Map.Entry<Statement, BindingSet>>();
    Iteration<? extends Map.Entry<Statement, BindingSet>, QueryEvaluationException> iter;
    if (sp instanceof FixedStatementPattern) {
        Collection<Map.Entry<Statement, BindingSet>> coll = Lists.newArrayList();
        for (BindingSet binding : bindings) {
            Value subjValue = getVarValue(subjVar, binding);
            Value predValue = getVarValue(predVar, binding);
            Value objValue = getVarValue(objVar, binding);
            Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
            for (Statement st : ((FixedStatementPattern) sp).statements) {
                if (!((subjValue != null && !subjValue.equals(st.getSubject())) || (predValue != null && !predValue.equals(st.getPredicate())) || (objValue != null && !objValue.equals(st.getObject())))) {
                    coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
                }
            }
        }
        iter = new IteratorIteration(coll.iterator());
    } else if (sp instanceof TransitivePropertySP && ((subjVar != null && subjVar.getValue() != null) || (objVar != null && objVar.getValue() != null)) && sp.getPredicateVar() != null) {
        // if this is a transitive prop ref, we need to make sure that either the subj or obj is not null
        // TODO: Cannot handle a open ended transitive property where subj and obj are null
        // TODO: Should one day handle filling in the subj or obj with bindings and working this
        // TODO: a lot of assumptions, and might be a large set returned causing an OME
        Set<Statement> sts = null;
        try {
            sts = inferenceEngine.findTransitiveProperty((Resource) getVarValue(subjVar), (URI) getVarValue(predVar), getVarValue(objVar), (Resource) getVarValue(cntxtVar));
        } catch (InferenceEngineException e) {
            throw new QueryEvaluationException(e);
        }
        Collection<Map.Entry<Statement, BindingSet>> coll = new ArrayList();
        for (BindingSet binding : bindings) {
            for (Statement st : sts) {
                coll.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(st, binding));
            }
        }
        iter = new IteratorIteration(coll.iterator());
    } else {
        for (BindingSet binding : bindings) {
            Value subjValue = getVarValue(subjVar, binding);
            Value predValue = getVarValue(predVar, binding);
            Value objValue = getVarValue(objVar, binding);
            Resource contxtValue = (Resource) getVarValue(cntxtVar, binding);
            if ((subjValue != null && !(subjValue instanceof Resource)) || (predValue != null && !(predValue instanceof URI))) {
                continue;
            }
            stmts.add(new RdfCloudTripleStoreUtils.CustomEntry<Statement, BindingSet>(new NullableStatementImpl((Resource) subjValue, (URI) predValue, objValue, contxtValue), binding));
        }
        if (stmts.size() == 0) {
            return new EmptyIteration();
        }
        iter = ((RdfCloudTripleStoreConnection.StoreTripleSource) tripleSource).getStatements(stmts);
    }
    return new ConvertingIteration<Map.Entry<Statement, BindingSet>, BindingSet, QueryEvaluationException>(iter) {

        @Override
        protected BindingSet convert(Map.Entry<Statement, BindingSet> stbs) throws QueryEvaluationException {
            Statement st = stbs.getKey();
            BindingSet bs = stbs.getValue();
            QueryBindingSet result = new QueryBindingSet(bs);
            // contain a Value for that Var name
            if (subjVar != null && !subjVar.isConstant() && !result.hasBinding(subjVar.getName())) {
                result.addBinding(subjVar.getName(), st.getSubject());
            }
            if (predVar != null && !predVar.isConstant() && !result.hasBinding(predVar.getName())) {
                result.addBinding(predVar.getName(), st.getPredicate());
            }
            if (objVar != null && !objVar.isConstant() && !result.hasBinding(objVar.getName())) {
                result.addBinding(objVar.getName(), st.getObject());
            }
            if (cntxtVar != null && !cntxtVar.isConstant() && !result.hasBinding(cntxtVar.getName()) && st.getContext() != null) {
                result.addBinding(cntxtVar.getName(), st.getContext());
            }
            return result;
        }
    };
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Set(java.util.Set) StoreTripleSource(org.apache.rya.rdftriplestore.RdfCloudTripleStoreConnection.StoreTripleSource) ConvertingIteration(info.aduna.iteration.ConvertingIteration) Var(org.openrdf.query.algebra.Var) ArrayList(java.util.ArrayList) URI(org.openrdf.model.URI) FixedStatementPattern(org.apache.rya.rdftriplestore.utils.FixedStatementPattern) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Statement(org.openrdf.model.Statement) EmptyIteration(info.aduna.iteration.EmptyIteration) Resource(org.openrdf.model.Resource) InferenceEngineException(org.apache.rya.rdftriplestore.inference.InferenceEngineException) TransitivePropertySP(org.apache.rya.rdftriplestore.utils.TransitivePropertySP) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) RdfCloudTripleStoreUtils(org.apache.rya.api.RdfCloudTripleStoreUtils) NullableStatementImpl(org.apache.rya.api.utils.NullableStatementImpl) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Value(org.openrdf.model.Value) IteratorIteration(info.aduna.iteration.IteratorIteration) Collection(java.util.Collection) Map(java.util.Map)

Example 65 with QueryBindingSet

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

the class BindingSetSerDe method fromBytes.

private BindingSet fromBytes(final byte[] bsBytes) {
    try {
        final int firstIndex = Bytes.indexOf(bsBytes, DELIM_BYTE);
        final byte[] varOrderBytes = Arrays.copyOf(bsBytes, firstIndex);
        final byte[] bsBytesNoVarOrder = Arrays.copyOfRange(bsBytes, firstIndex + 1, bsBytes.length);
        final VariableOrder varOrder = new VariableOrder(new String(varOrderBytes, StandardCharsets.UTF_8).split(";"));
        return getBindingSet(varOrder, bsBytesNoVarOrder);
    } catch (final Exception e) {
        log.trace("Unable to deserialize BindingSet: " + bsBytes);
        return new QueryBindingSet();
    }
}
Also used : VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) BindingSetConversionException(org.apache.rya.indexing.pcj.storage.accumulo.BindingSetConverter.BindingSetConversionException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) 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