Search in sources :

Example 36 with QueryBindingSet

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

the class PCJKeyToCrossProductBindingSetIterator method getBindingSet.

/**
 * @param key
 *            - Accumulo key obtained from scan
 * @return - BindingSet satisfying any specified constant constraints
 * @throws BindingSetConversionException
 * @throws QueryEvaluationException
 */
private BindingSet getBindingSet(Key key) throws BindingSetConversionException, QueryEvaluationException {
    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 = null;
        if (pcjVarMap.containsKey(var)) {
            mappedVar = pcjVarMap.get(var);
        } else {
            throw new QueryEvaluationException("PCJ Variable has no mapping to query variable.");
        }
        if (constantConstraintsExist) {
            if (mappedVar.startsWith(ExternalTupleSet.CONST_PREFIX) && constantConstraints.containsKey(mappedVar) && !constantConstraints.get(mappedVar).equals(bindingSet.getValue(var))) {
                return EMPTY_BINDINGSET;
            }
        }
        if (!mappedVar.startsWith(ExternalTupleSet.CONST_PREFIX)) {
            bs.addBinding(mappedVar, bindingSet.getValue(var));
        }
    }
    return bs;
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Example 37 with QueryBindingSet

use of org.openrdf.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.createURI("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" + "}";
    Multiset<BindingSet> expectedSolutions = HashMultiset.create();
    expectedSolutions.add(new EmptyBindingSet());
    // Execute pipeline and verify results
    QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
    SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
    queryTree.visit(visitor);
    Assert.assertTrue(queryTree.getArg() instanceof Projection);
    Projection projection = (Projection) queryTree.getArg();
    Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode);
    AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.getArg();
    Multiset<BindingSet> solutions = HashMultiset.create();
    CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
    while (iter.hasNext()) {
        solutions.add(iter.next());
    }
    Assert.assertEquals(expectedSolutions, solutions);
}
Also used : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) ListBindingSet(org.openrdf.query.impl.ListBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) EmptyBindingSet(org.openrdf.query.impl.EmptyBindingSet) QueryRoot(org.openrdf.query.algebra.QueryRoot) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) Projection(org.openrdf.query.algebra.Projection) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) Test(org.junit.Test)

Example 38 with QueryBindingSet

use of org.openrdf.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 (mappedVar.startsWith(ExternalTupleSet.CONST_PREFIX) && 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 : QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) VariableOrder(org.apache.rya.indexing.pcj.storage.accumulo.VariableOrder) Value(org.openrdf.model.Value) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Example 39 with QueryBindingSet

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

the class AccumuloPeriodicQueryResultStorage method getRowPrefix.

private Text getRowPrefix(final long binId) throws BindingSetConversionException {
    final QueryBindingSet bs = new QueryBindingSet();
    bs.addBinding(PeriodicQueryResultStorage.PeriodicBinId, new LiteralImpl(Long.toString(binId), XMLSchema.LONG));
    return new Text(converter.convert(bs, new VariableOrder(PeriodicQueryResultStorage.PeriodicBinId)));
}
Also used : LiteralImpl(org.openrdf.model.impl.LiteralImpl) Text(org.apache.hadoop.io.Text) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Example 40 with QueryBindingSet

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

the class BindingSetStringConverter method convert.

@Override
public BindingSet convert(final String bindingSetString, final VariableOrder varOrder) {
    requireNonNull(bindingSetString);
    requireNonNull(varOrder);
    // If both are empty, return an empty binding set.
    if (bindingSetString.isEmpty() && varOrder.toString().isEmpty()) {
        return new MapBindingSet();
    }
    // Otherwise parse it.
    final String[] bindingStrings = bindingSetString.split(BINDING_DELIM);
    final String[] varOrderArr = varOrder.toArray();
    checkArgument(varOrderArr.length == bindingStrings.length, "The number of Bindings must match the length of the VariableOrder.");
    final QueryBindingSet bindingSet = new QueryBindingSet();
    for (int i = 0; i < bindingStrings.length; i++) {
        final String bindingString = bindingStrings[i];
        if (!NULL_VALUE_STRING.equals(bindingString)) {
            final String name = varOrderArr[i];
            final Value value = toValue(bindingStrings[i]);
            bindingSet.addBinding(name, value);
        }
    }
    return bindingSet;
}
Also used : Value(org.openrdf.model.Value) MapBindingSet(org.openrdf.query.impl.MapBindingSet) 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