Search in sources :

Example 66 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class EntityQueryNodeIT method evaluate_constantObject.

@Test
public void evaluate_constantObject() throws Exception {
    final EntityStorage storage = new MongoEntityStorage(super.getMongoClient(), "testDB");
    final ValueFactory vf = ValueFactoryImpl.getInstance();
    final RyaURI subject = new RyaURI("urn:SSN:111-11-1111");
    final Entity entity = Entity.builder().setSubject(subject).setExplicitType(PERSON_TYPE.getId()).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:age"), RdfToRyaConversions.convertLiteral(vf.createLiteral(20)))).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:eye"), RdfToRyaConversions.convertLiteral(vf.createLiteral("blue")))).setProperty(PERSON_TYPE.getId(), new Property(new RyaURI("urn:name"), RdfToRyaConversions.convertLiteral(vf.createLiteral("Bob")))).build();
    storage.create(entity);
    // A set of patterns that match a sepecific Entity subject.
    final List<StatementPattern> patterns = getSPs("SELECT * WHERE { " + "<urn:SSN:111-11-1111> <" + RDF.TYPE + "> <urn:person> ." + "<urn:SSN:111-11-1111> <urn:age> ?age . " + "<urn:SSN:111-11-1111> <urn:eye> \"blue\" . " + "<urn:SSN:111-11-1111> <urn:name> ?name . " + "}");
    final EntityQueryNode node = new EntityQueryNode(PERSON_TYPE, patterns, storage);
    final CloseableIteration<BindingSet, QueryEvaluationException> rez = node.evaluate(new MapBindingSet());
    final MapBindingSet expected = new MapBindingSet();
    expected.addBinding("age", vf.createLiteral("20"));
    expected.addBinding("-const-blue", vf.createLiteral("blue"));
    expected.addBinding("name", vf.createLiteral("Bob"));
    while (rez.hasNext()) {
        assertEquals(expected, rez.next());
        break;
    }
}
Also used : Entity(org.apache.rya.indexing.entity.model.Entity) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) MongoEntityStorage(org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage) EntityStorage(org.apache.rya.indexing.entity.storage.EntityStorage) MongoEntityStorage(org.apache.rya.indexing.entity.storage.mongo.MongoEntityStorage) ValueFactory(org.openrdf.model.ValueFactory) RyaURI(org.apache.rya.api.domain.RyaURI) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Property(org.apache.rya.indexing.entity.model.Property) Test(org.junit.Test)

Example 67 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class VisibilityBindingSetStringConverterTest method fromString.

@Test
public void fromString() throws BindingSetConversionException {
    // Setup the String that will be converted.
    final String bindingSetString = "http://b<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://c<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://a<<~>>http://www.w3.org/2001/XMLSchema#anyURI" + VISIBILITY_DELIM + "A&B";
    // Convert it to a BindingSet
    final VariableOrder varOrder = new VariableOrder("y", "z", "x");
    final VisibilityBindingSetStringConverter converter = new VisibilityBindingSetStringConverter();
    final BindingSet bindingSet = converter.convert(bindingSetString, varOrder);
    // Ensure it converted to the expected result.
    final MapBindingSet expected = new MapBindingSet();
    expected.addBinding("z", new URIImpl("http://c"));
    expected.addBinding("y", new URIImpl("http://b"));
    expected.addBinding("x", new URIImpl("http://a"));
    final VisibilityBindingSet visiSet = new VisibilityBindingSet(expected, "A&B");
    assertEquals(visiSet, bindingSet);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BindingSet(org.openrdf.query.BindingSet) MapBindingSet(org.openrdf.query.impl.MapBindingSet) VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 68 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class VisibilityBindingSetStringConverterTest method toString_URIs.

@Test
public void toString_URIs() throws BindingSetConversionException {
    // Setup the binding set that will be converted.
    final MapBindingSet originalBindingSet = new MapBindingSet();
    originalBindingSet.addBinding("x", new URIImpl("http://a"));
    originalBindingSet.addBinding("y", new URIImpl("http://b"));
    originalBindingSet.addBinding("z", new URIImpl("http://c"));
    final VisibilityBindingSet visiSet = new VisibilityBindingSet(originalBindingSet, "A&B&C");
    // Convert it to a String.
    final VariableOrder varOrder = new VariableOrder("y", "z", "x");
    final VisibilityBindingSetStringConverter converter = new VisibilityBindingSetStringConverter();
    final String bindingSetString = converter.convert(visiSet, varOrder);
    // Ensure it converted to the expected result.l
    final String expected = "http://b<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://c<<~>>http://www.w3.org/2001/XMLSchema#anyURI:::" + "http://a<<~>>http://www.w3.org/2001/XMLSchema#anyURI" + VISIBILITY_DELIM + "A&B&C";
    assertEquals(expected, bindingSetString);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) URIImpl(org.openrdf.model.impl.URIImpl) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 69 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class IteratorFactory method getIterator.

public static CloseableIteration<BindingSet, QueryEvaluationException> getIterator(final StatementPattern match, final BindingSet bindings, final String queryText, final SearchFunction searchFunction) {
    return new CloseableIteration<BindingSet, QueryEvaluationException>() {

        private boolean isClosed = false;

        private CloseableIteration<Statement, QueryEvaluationException> statementIt = null;

        private String subjectBinding = match.getSubjectVar().getName();

        private String predicateBinding = match.getPredicateVar().getName();

        private String objectBinding = match.getObjectVar().getName();

        private String contextBinding = null;

        private void performQuery() throws QueryEvaluationException {
            StatementConstraints contraints = new StatementConstraints();
            // get the context (i.e. named graph) of the statement and use that in the query
            QueryModelNode parentNode = match.getSubjectVar().getParentNode();
            if (parentNode instanceof StatementPattern) {
                StatementPattern parentStatement = (StatementPattern) parentNode;
                Var contextVar = parentStatement.getContextVar();
                if (contextVar != null) {
                    contextBinding = contextVar.getName();
                    Resource context = (Resource) contextVar.getValue();
                    contraints.setContext(context);
                }
            }
            // get the subject constraint
            if (match.getSubjectVar().isConstant()) {
                // get the subject binding from the filter/statement pair
                Resource subject = (Resource) match.getSubjectVar().getValue();
                contraints.setSubject(subject);
            } else if (bindings.hasBinding(subjectBinding)) {
                // get the subject binding from the passed in bindings (eg from other statements/parts of the tree)
                Resource subject = (Resource) bindings.getValue(subjectBinding);
                contraints.setSubject(subject);
            }
            // get the predicate constraint
            if (match.getPredicateVar().isConstant()) {
                // get the predicate binding from the filter/statement pair
                Set<URI> predicates = new HashSet<URI>(getPredicateRestrictions(match.getPredicateVar()));
                contraints.setPredicates(predicates);
            } else if (bindings.hasBinding(predicateBinding)) {
                // get the predicate binding from the passed in bindings (eg from other statements/parts of the tree)
                URI predicateUri = (URI) bindings.getValue(predicateBinding);
                Set<URI> predicates = Collections.singleton(predicateUri);
                contraints.setPredicates(predicates);
            }
            statementIt = searchFunction.performSearch(queryText, contraints);
        }

        @Override
        public boolean hasNext() throws QueryEvaluationException {
            if (statementIt == null) {
                performQuery();
            }
            return statementIt.hasNext();
        }

        @Override
        public BindingSet next() throws QueryEvaluationException {
            if (!hasNext() || isClosed) {
                throw new NoSuchElementException();
            }
            Statement statment = statementIt.next();
            MapBindingSet bset = new MapBindingSet();
            if (!subjectBinding.startsWith("-const"))
                bset.addBinding(subjectBinding, statment.getSubject());
            if (!predicateBinding.startsWith("-const"))
                bset.addBinding(predicateBinding, statment.getPredicate());
            if (!objectBinding.startsWith("-const"))
                bset.addBinding(objectBinding, statment.getObject());
            if (contextBinding != null && !contextBinding.startsWith("-const"))
                bset.addBinding(contextBinding, statment.getContext());
            // merge with other bindings.
            for (String name : bindings.getBindingNames()) {
                bset.addBinding(name, bindings.getValue(name));
            }
            return bset;
        }

        @Override
        public void remove() throws QueryEvaluationException {
            throw new UnsupportedOperationException();
        }

        @Override
        public void close() throws QueryEvaluationException {
            if (statementIt != null) {
                statementIt.close();
            }
            isClosed = true;
        }
    };
}
Also used : Set(java.util.Set) MapBindingSet(org.openrdf.query.impl.MapBindingSet) HashSet(java.util.HashSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) Statement(org.openrdf.model.Statement) Resource(org.openrdf.model.Resource) QueryModelNode(org.openrdf.query.algebra.QueryModelNode) URI(org.openrdf.model.URI) CloseableIteration(info.aduna.iteration.CloseableIteration) StatementPattern(org.openrdf.query.algebra.StatementPattern) MapBindingSet(org.openrdf.query.impl.MapBindingSet) NoSuchElementException(java.util.NoSuchElementException) HashSet(java.util.HashSet)

Example 70 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project incubator-rya by apache.

the class KafkaExportIT method count.

@Test
public void count() throws Exception {
    // A query that counts the number of unique items that are in the inventory.
    final String sparql = "SELECT (count(?item) as ?itemCount) { " + "?item <urn:id> ?id . " + "}";
    // Create the Statements that will be loaded into Rya.
    final ValueFactory vf = new ValueFactoryImpl();
    final Collection<Statement> statements = Sets.newHashSet(// Three that are part of the count.
    vf.createStatement(vf.createURI("urn:apple"), vf.createURI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())), vf.createStatement(vf.createURI("urn:gum"), vf.createURI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())), vf.createStatement(vf.createURI("urn:sandwich"), vf.createURI("urn:id"), vf.createLiteral(UUID.randomUUID().toString())), // One that is not.
    vf.createStatement(vf.createURI("urn:sandwich"), vf.createURI("urn:price"), vf.createLiteral(3.99)));
    // Create the PCJ in Fluo and load the statements into Rya.
    final String pcjId = loadDataAndCreateQuery(sparql, statements);
    // Create the expected results of the SPARQL query once the PCJ has been computed.
    final MapBindingSet expectedResult = new MapBindingSet();
    expectedResult.addBinding("itemCount", vf.createLiteral("3", XMLSchema.INTEGER));
    // Ensure the last result matches the expected result.
    final VisibilityBindingSet result = readLastResult(pcjId);
    assertEquals(expectedResult, result);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) Statement(org.openrdf.model.Statement) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Aggregations

MapBindingSet (org.openrdf.query.impl.MapBindingSet)174 Test (org.junit.Test)155 ValueFactory (org.openrdf.model.ValueFactory)99 VisibilityBindingSet (org.apache.rya.api.model.VisibilityBindingSet)96 ValueFactoryImpl (org.openrdf.model.impl.ValueFactoryImpl)91 BindingSet (org.openrdf.query.BindingSet)84 HashSet (java.util.HashSet)81 Statement (org.openrdf.model.Statement)43 URIImpl (org.openrdf.model.impl.URIImpl)31 ArrayList (java.util.ArrayList)30 VisibilityStatement (org.apache.rya.api.model.VisibilityStatement)24 UUID (java.util.UUID)23 PrecomputedJoinStorage (org.apache.rya.indexing.pcj.storage.PrecomputedJoinStorage)20 TopologyFactory (org.apache.rya.streams.kafka.topology.TopologyFactory)20 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)19 RandomUUIDFactory (org.apache.rya.api.function.projection.RandomUUIDFactory)19 Connector (org.apache.accumulo.core.client.Connector)18 AccumuloPcjStorage (org.apache.rya.indexing.pcj.storage.accumulo.AccumuloPcjStorage)16 PcjMetadata (org.apache.rya.indexing.pcj.storage.PcjMetadata)15 RyaURI (org.apache.rya.api.domain.RyaURI)14