Search in sources :

Example 21 with QueryBindingSet

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

the class StatementPatternMatcherTest method matchesSubject.

@Test
public void matchesSubject() throws Exception {
    // Create the matcher against a pattern that matches a specific subject.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "<urn:Alice> ?p ?o ." + "}"));
    // Create a statement that matches the pattern.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Statement statement = vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"), vf.createIRI("urn:testGraph"));
    // Create the expected resulting Binding Set.
    final QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("p", vf.createIRI("urn:talksTo"));
    expected.addBinding("o", vf.createIRI("urn:Bob"));
    // Show the expected Binding Set matches the resulting Binding Set.
    final Optional<BindingSet> bs = matcher.match(statement);
    assertEquals(expected, bs.get());
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Statement(org.eclipse.rdf4j.model.Statement) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 22 with QueryBindingSet

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

the class StatementPatternMatcherTest method variableContext.

@Test
public void variableContext() throws Exception {
    // Create a matcher against a pattern that matches a variable context.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "GRAPH ?c {" + "?s ?p ?o ." + "}" + "}"));
    // Create a statement that matches the pattern.
    final ValueFactory vf = SimpleValueFactory.getInstance();
    final Statement statement = vf.createStatement(vf.createIRI("urn:Alice"), vf.createIRI("urn:talksTo"), vf.createIRI("urn:Bob"), vf.createIRI("urn:testGraph"));
    // Create the expected resulting Binding Set.
    final QueryBindingSet expected = new QueryBindingSet();
    expected.addBinding("s", vf.createIRI("urn:Alice"));
    expected.addBinding("p", vf.createIRI("urn:talksTo"));
    expected.addBinding("o", vf.createIRI("urn:Bob"));
    expected.addBinding("c", vf.createIRI("urn:testGraph"));
    // Show the expected Binding Set matches the resulting Binding Set.
    final Optional<BindingSet> bs = matcher.match(statement);
    assertEquals(expected, bs.get());
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Statement(org.eclipse.rdf4j.model.Statement) ValueFactory(org.eclipse.rdf4j.model.ValueFactory) SimpleValueFactory(org.eclipse.rdf4j.model.impl.SimpleValueFactory) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 23 with QueryBindingSet

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

the class PipelineQueryIT method testNoVariableSP.

@Test
public void testNoVariableSP() throws Exception {
    // Insert data
    insert(OWL.THING, RDF.TYPE, OWL.CLASS);
    insert(FOAF.PERSON, RDF.TYPE, OWL.CLASS, 1);
    insert(FOAF.PERSON, RDFS.SUBCLASSOF, OWL.THING);
    insert(VF.createIRI("urn:Alice"), RDF.TYPE, FOAF.PERSON);
    dao.flush();
    // Define query and expected results
    final String query = "SELECT * WHERE {\n" + "  owl:Thing a owl:Class .\n" + "}";
    final Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new EmptyBindingSet());
    // Execute pipeline and verify results
    final QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    final SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Projection);
    final Projection projection = (Projection) queryTree.getArg();
    Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode);
    final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.getArg();
    final Multiset<BindingSet> solutions = HashMultiset.create();
    final CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
Also used : ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) EmptyBindingSet(org.eclipse.rdf4j.query.impl.EmptyBindingSet) QueryRoot(org.eclipse.rdf4j.query.algebra.QueryRoot) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) Projection(org.eclipse.rdf4j.query.algebra.Projection) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 24 with QueryBindingSet

use of org.eclipse.rdf4j.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.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Example 25 with QueryBindingSet

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

the class PCJKeyToJoinBindingSetIterator method getBindingSetEntryAndMatchConstants.

/**
 * @param key
 *            - Accumulo key obtained from scan
 * @return - Entry<String,BindingSet> satisfying the constant constraints
 * @throws BindingSetConversionException
 */
private Map.Entry<String, BindingSet> getBindingSetEntryAndMatchConstants(Key key) throws BindingSetConversionException {
    byte[] row = key.getRow().getBytes();
    String[] varOrder = key.getColumnFamily().toString().split(ExternalTupleSet.VAR_ORDER_DELIM);
    BindingSet bindingSet = converter.convert(row, new VariableOrder(varOrder));
    QueryBindingSet bs = new QueryBindingSet();
    for (String var : bindingSet.getBindingNames()) {
        String mappedVar = pcjVarMap.get(var);
        if (VarNameUtils.isConstant(mappedVar) && constantConstraints.containsKey(mappedVar) && !constantConstraints.get(mappedVar).equals(bindingSet.getValue(var))) {
            return EMPTY_ENTRY;
        } else {
            bs.addBinding(mappedVar, bindingSet.getValue(var));
        }
    }
    String orderedValueString = bindingSet.getValue(varOrder[0]).toString();
    for (int i = 1; i < maxPrefixLen; i++) {
        Value value = bindingSet.getValue(varOrder[i]);
        if (value != null) {
            orderedValueString = orderedValueString + ExternalTupleSet.VALUE_DELIM + value.toString();
        }
    }
    return new RdfCloudTripleStoreUtils.CustomEntry<String, BindingSet>(orderedValueString, bs);
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) Value(org.eclipse.rdf4j.model.Value) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Aggregations

QueryBindingSet (org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)111 Test (org.junit.Test)84 BindingSet (org.eclipse.rdf4j.query.BindingSet)83 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)56 RyaIRI (org.apache.rya.api.domain.RyaIRI)46 RyaStatement (org.apache.rya.api.domain.RyaStatement)45 StatementPattern (org.eclipse.rdf4j.query.algebra.StatementPattern)42 SPARQLParser (org.eclipse.rdf4j.query.parser.sparql.SPARQLParser)42 ParsedQuery (org.eclipse.rdf4j.query.parser.ParsedQuery)41 RyaType (org.apache.rya.api.domain.RyaType)39 ArrayList (java.util.ArrayList)30 HashSet (java.util.HashSet)28 StatementMetadata (org.apache.rya.api.domain.StatementMetadata)26 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)20 TupleExpr (org.eclipse.rdf4j.query.algebra.TupleExpr)16 StatementMetadataNode (org.apache.rya.indexing.statement.metadata.matching.StatementMetadataNode)15 Statement (org.eclipse.rdf4j.model.Statement)15 Value (org.eclipse.rdf4j.model.Value)14 Collection (java.util.Collection)13 BatchWriter (org.apache.accumulo.core.client.BatchWriter)12