Search in sources :

Example 96 with QueryBindingSet

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

the class OneOfVisitorTest method assertBindingSet.

private static void assertBindingSet(final Iterator<BindingSet> bindingSetIter, final Iterator<Resource> expectedValues) {
    while (expectedValues.hasNext()) {
        final Resource expectedValue = expectedValues.next();
        assertTrue(bindingSetIter.hasNext());
        final BindingSet bindingSet = bindingSetIter.next();
        assertTrue(bindingSet instanceof QueryBindingSet);
        assertEquals(1, bindingSet.getBindingNames().size());
        final Binding binding = bindingSet.getBinding("s");
        assertNotNull(binding);
        final Value actualValue = binding.getValue();
        assertEquals(expectedValue, actualValue);
    }
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) Resource(org.eclipse.rdf4j.model.Resource) Value(org.eclipse.rdf4j.model.Value) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Example 97 with QueryBindingSet

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

the class StatementPatternMatcherTest method matchesPredicate.

@Test
public void matchesPredicate() throws Exception {
    // Create the matcher against a pattern that matches a specific predicate.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "?s <urn:talksTo> ?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("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 98 with QueryBindingSet

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

the class StatementPatternMatcherTest method matchesObject.

@Test
public void matchesObject() throws Exception {
    // Create the matcher against a pattern that matches a specific object.
    final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "?s ?p <urn:Bob> ." + "}"));
    // 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"));
    // 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 99 with QueryBindingSet

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

the class PipelineResultIteration method docToBindingSet.

private QueryBindingSet docToBindingSet(Document result) {
    QueryBindingSet bindingSet = new QueryBindingSet(bindings);
    Document valueSet = result.get(AggregationPipelineQueryNode.VALUES, Document.class);
    Document typeSet = result.get(AggregationPipelineQueryNode.TYPES, Document.class);
    if (valueSet != null) {
        for (Map.Entry<String, Object> entry : valueSet.entrySet()) {
            String fieldName = entry.getKey();
            String valueString = entry.getValue().toString();
            String typeString = typeSet == null ? null : typeSet.getString(fieldName);
            String varName = varToOriginalName.getOrDefault(fieldName, fieldName);
            Value varValue;
            if (typeString == null || typeString.equals(XMLSchema.ANYURI.stringValue())) {
                varValue = VF.createIRI(valueString);
            } else {
                varValue = VF.createLiteral(valueString, VF.createIRI(typeString));
            }
            Binding existingBinding = bindingSet.getBinding(varName);
            // If this variable is not already bound, add it.
            if (existingBinding == null) {
                bindingSet.addBinding(varName, varValue);
            } else // If it's bound to something else, the solutions are incompatible.
            if (!existingBinding.getValue().equals(varValue)) {
                return null;
            }
        }
    }
    return bindingSet;
}
Also used : Binding(org.eclipse.rdf4j.query.Binding) Value(org.eclipse.rdf4j.model.Value) Document(org.bson.Document) Map(java.util.Map) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet)

Example 100 with QueryBindingSet

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

the class AccumuloDocIdIndexer method deserializeKey.

private QueryBindingSet deserializeKey(final Key key, final StarQuery sq, final BindingSet currentBs, final Set<String> unCommonVar) {
    final QueryBindingSet currentSolutionBs = new QueryBindingSet();
    final Text row = key.getRow();
    final Text cq = key.getColumnQualifier();
    final String[] cqArray = cq.toString().split(DocIndexIteratorUtil.DOC_ID_INDEX_DELIM);
    boolean commonVarSet = false;
    // if common Var is constant there is no common variable to assign a value to
    if (sq.commonVarConstant()) {
        commonVarSet = true;
    }
    if (!commonVarSet && sq.isCommonVarURI()) {
        final RyaIRI rIRI = new RyaIRI(row.toString());
        currentSolutionBs.addBinding(sq.getCommonVarName(), RyaToRdfConversions.convertValue(rIRI));
        commonVarSet = true;
    }
    for (final String s : sq.getUnCommonVars()) {
        final byte[] cqBytes = cqArray[sq.getVarPos().get(s)].getBytes(StandardCharsets.UTF_8);
        final int firstIndex = Bytes.indexOf(cqBytes, DELIM_BYTE);
        final int secondIndex = Bytes.lastIndexOf(cqBytes, DELIM_BYTE);
        final int typeIndex = Bytes.indexOf(cqBytes, TYPE_DELIM_BYTE);
        final String tripleComponent = new String(Arrays.copyOfRange(cqBytes, firstIndex + 1, secondIndex), StandardCharsets.UTF_8);
        final byte[] cqContent = Arrays.copyOfRange(cqBytes, secondIndex + 1, typeIndex);
        final byte[] objType = Arrays.copyOfRange(cqBytes, typeIndex, cqBytes.length);
        if (tripleComponent.equals("object")) {
            final byte[] object = Bytes.concat(cqContent, objType);
            org.eclipse.rdf4j.model.Value v = null;
            try {
                v = RyaToRdfConversions.convertValue(RyaContext.getInstance().deserialize(object));
            } catch (final RyaTypeResolverException e) {
                e.printStackTrace();
            }
            currentSolutionBs.addBinding(s, v);
        } else if (tripleComponent.equals("subject")) {
            if (!commonVarSet) {
                final byte[] object = Bytes.concat(row.getBytes(), objType);
                org.eclipse.rdf4j.model.Value v = null;
                try {
                    v = RyaToRdfConversions.convertValue(RyaContext.getInstance().deserialize(object));
                } catch (final RyaTypeResolverException e) {
                    e.printStackTrace();
                }
                currentSolutionBs.addBinding(sq.getCommonVarName(), v);
                commonVarSet = true;
            }
            final RyaIRI rIRI = new RyaIRI(new String(cqContent, StandardCharsets.UTF_8));
            currentSolutionBs.addBinding(s, RyaToRdfConversions.convertValue(rIRI));
        } else {
            throw new IllegalArgumentException("Invalid row.");
        }
    }
    for (final String s : unCommonVar) {
        currentSolutionBs.addBinding(s, currentBs.getValue(s));
    }
    return currentSolutionBs;
}
Also used : RyaIRI(org.apache.rya.api.domain.RyaIRI) Text(org.apache.hadoop.io.Text) QueryBindingSet(org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet) Value(org.apache.accumulo.core.data.Value) RyaTypeResolverException(org.apache.rya.api.resolver.RyaTypeResolverException)

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