Search in sources :

Example 91 with MapBindingSet

use of org.openrdf.query.impl.MapBindingSet in project blueprints by tinkerpop.

the class SailGraph method executeSparql.

/**
 * Evaluate a SPARQL query against the SailGraph (http://www.w3.org/TR/rdf-sparql-query/). The result is a mapping between the ?-bindings and the bound URI, blank node, or literal represented as a Vertex.
 *
 * @param sparqlQuery the SPARQL query to evaluate
 * @return the mapping between a ?-binding and the URI, blank node, or literal as a Vertex
 * @throws RuntimeException if an error occurs in the SPARQL query engine
 */
public List<Map<String, Vertex>> executeSparql(String sparqlQuery) throws RuntimeException {
    try {
        sparqlQuery = getPrefixes() + sparqlQuery;
        final SPARQLParser parser = new SPARQLParser();
        final ParsedQuery query = parser.parseQuery(sparqlQuery, null);
        boolean includeInferred = false;
        final CloseableIteration<? extends BindingSet, QueryEvaluationException> results = this.sailConnection.get().evaluate(query.getTupleExpr(), query.getDataset(), new MapBindingSet(), includeInferred);
        final List<Map<String, Vertex>> returnList = new ArrayList<Map<String, Vertex>>();
        try {
            while (results.hasNext()) {
                BindingSet bs = results.next();
                Map<String, Vertex> returnMap = new HashMap<String, Vertex>();
                for (Binding b : bs) {
                    returnMap.put(b.getName(), this.getVertex(b.getValue().toString()));
                }
                returnList.add(returnMap);
            }
        } finally {
            results.close();
        }
        return returnList;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : Binding(org.openrdf.query.Binding) Vertex(com.tinkerpop.blueprints.Vertex) MapBindingSet(org.openrdf.query.impl.MapBindingSet) BindingSet(org.openrdf.query.BindingSet) SPARQLParser(org.openrdf.query.parser.sparql.SPARQLParser) ParsedQuery(org.openrdf.query.parser.ParsedQuery) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SailException(org.openrdf.sail.SailException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) RDFHandlerException(org.openrdf.rio.RDFHandlerException) QueryEvaluationException(org.openrdf.query.QueryEvaluationException) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Map(java.util.Map) HashMap(java.util.HashMap)

Example 92 with MapBindingSet

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

the class MultiProjectionEvaluatorTest method singleBlankNode.

@Test
public void singleBlankNode() throws Exception {
    // Read the multi projection object from a SPARQL query.
    final MultiProjection multiProjection = getMultiProjection("CONSTRUCT {" + "_:b a <urn:movementObservation> ; " + "<urn:location> ?location ; " + "<urn:direction> ?direction ; " + "}" + "WHERE {" + "?thing <urn:corner> ?location ." + "?thing <urn:compass> ?direction." + "}");
    // Create a Binding Set that contains the result of the WHERE clause.
    final ValueFactory vf = new ValueFactoryImpl();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("location", vf.createLiteral("South St and 5th St"));
    bs.addBinding("direction", vf.createLiteral("NW"));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
    // Create the expected results.
    final Set<VisibilityBindingSet> expected = new HashSet<>();
    final String blankNodeId = UUID.randomUUID().toString();
    final BNode blankNode = vf.createBNode(blankNodeId);
    bs = new MapBindingSet();
    bs.addBinding("subject", blankNode);
    bs.addBinding("predicate", RDF.TYPE);
    bs.addBinding("object", vf.createURI("urn:movementObservation"));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    bs = new MapBindingSet();
    bs.addBinding("subject", blankNode);
    bs.addBinding("predicate", vf.createURI("urn:location"));
    bs.addBinding("object", vf.createLiteral("South St and 5th St"));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    bs = new MapBindingSet();
    bs.addBinding("subject", blankNode);
    bs.addBinding("predicate", vf.createURI("urn:direction"));
    bs.addBinding("object", vf.createLiteral("NW"));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    // Run the projection evaluator.
    final Set<VisibilityBindingSet> results = MultiProjectionEvaluator.make(multiProjection, () -> blankNodeId).project(original);
    // The expected binding sets.
    assertEquals(expected, results);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) BNode(org.openrdf.model.BNode) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) MultiProjection(org.openrdf.query.algebra.MultiProjection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 93 with MapBindingSet

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

the class MultiProjectionEvaluatorTest method multipleBlanknodes.

@Test
public void multipleBlanknodes() throws Exception {
    // Read the multi projection object from a SPARQL query.
    final MultiProjection multiProjection = getMultiProjection("CONSTRUCT {" + "_:b a <urn:vehicle> . " + "_:b <urn:tiresCount> 4 ." + "_:c a <urn:pet> . " + "_:c <urn:isDead> false . " + "}" + "WHERE {" + "?vehicle <urn:owner> ?owner . " + "?vehicle <urn:plates> ?plates . " + "?pet <urn:owner> ?owner . " + "?pet <urn:isLiving> true . " + "}");
    // Create a Binding Set that contains the result of the WHERE clause.
    final ValueFactory vf = new ValueFactoryImpl();
    MapBindingSet bs = new MapBindingSet();
    bs.addBinding("vehicle", vf.createLiteral("Alice's car"));
    bs.addBinding("owner", vf.createURI("urn:Alice"));
    bs.addBinding("plates", vf.createLiteral("XXXXXXX"));
    bs.addBinding("pet", vf.createURI("urn:Kitty"));
    final VisibilityBindingSet original = new VisibilityBindingSet(bs, "a|b");
    // Run the projection evaluator.
    final Set<VisibilityBindingSet> results = MultiProjectionEvaluator.make(multiProjection, new RandomUUIDFactory()).project(original);
    // Figure out the blank nodes.
    Value vehicalBNode = null;
    Value petBNode = null;
    for (final VisibilityBindingSet result : results) {
        final Value object = result.getValue("object");
        if (object.equals(vf.createURI("urn:vehicle"))) {
            vehicalBNode = result.getValue("subject");
        } else if (object.equals(vf.createURI("urn:pet"))) {
            petBNode = result.getValue("subject");
        }
    }
    // The expected binding sets.
    final Set<VisibilityBindingSet> expected = new HashSet<>();
    bs = new MapBindingSet();
    bs.addBinding("subject", vehicalBNode);
    bs.addBinding("predicate", RDF.TYPE);
    bs.addBinding("object", vf.createURI("urn:vehicle"));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    bs = new MapBindingSet();
    bs.addBinding("subject", vehicalBNode);
    bs.addBinding("predicate", vf.createURI("urn:tiresCount"));
    bs.addBinding("object", vf.createLiteral("4", XMLSchema.INTEGER));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    bs = new MapBindingSet();
    bs.addBinding("subject", petBNode);
    bs.addBinding("predicate", RDF.TYPE);
    bs.addBinding("object", vf.createURI("urn:pet"));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    bs = new MapBindingSet();
    bs.addBinding("subject", petBNode);
    bs.addBinding("predicate", vf.createURI("urn:isDead"));
    bs.addBinding("object", vf.createLiteral(false));
    expected.add(new VisibilityBindingSet(bs, "a|b"));
    assertEquals(expected, results);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) Value(org.openrdf.model.Value) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) MultiProjection(org.openrdf.query.algebra.MultiProjection) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 94 with MapBindingSet

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

the class IterativeJoinTest method naturalJoin_sideDoesNotMatter.

/**
 * This test ensures the same binding sets are created as the result of a
 * {@link IterativeJoin} regardless of which side the notification is triggered on.
 */
@Test
public void naturalJoin_sideDoesNotMatter() {
    // Create the binding sets that will be joined.
    final ValueFactory vf = new ValueFactoryImpl();
    final MapBindingSet bs1 = new MapBindingSet();
    bs1.addBinding("id", vf.createLiteral("some_uid"));
    bs1.addBinding("name", vf.createLiteral("Alice"));
    final VisibilityBindingSet vbs1 = new VisibilityBindingSet(bs1, "a");
    final MapBindingSet bs2 = new MapBindingSet();
    bs2.addBinding("id", vf.createLiteral("some_uid"));
    bs2.addBinding("hair", vf.createLiteral("brown"));
    final VisibilityBindingSet vbs2 = new VisibilityBindingSet(bs2, "b");
    // new vbs1 shows up on the left, matches vbs2 on the right
    final Iterator<VisibilityBindingSet> newLeftIt = join.newLeftResult(vbs1, Collections.singleton(vbs2).iterator());
    final VisibilityBindingSet newLeftResult = newLeftIt.next();
    // new vbs2 shows up on the right, matches vbs1 on the left
    final Iterator<VisibilityBindingSet> newRightIt = join.newRightResult(Collections.singleton(vbs1).iterator(), vbs2);
    final VisibilityBindingSet newRightResult = newRightIt.next();
    // Ensure those two results are the same.
    assertEquals(newLeftResult, newRightResult);
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) ValueFactoryImpl(org.openrdf.model.impl.ValueFactoryImpl) ValueFactory(org.openrdf.model.ValueFactory) MapBindingSet(org.openrdf.query.impl.MapBindingSet) Test(org.junit.Test)

Example 95 with MapBindingSet

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

the class NaturalJoinTest method newRightResult_noLeftMatches.

@Test
public void newRightResult_noLeftMatches() {
    final IterativeJoin naturalJoin = new NaturalJoin();
    // There are no left results.
    final Iterator<VisibilityBindingSet> leftResults = new ArrayList<VisibilityBindingSet>().iterator();
    // There is a new right result.
    final MapBindingSet newRightResult = new MapBindingSet();
    newRightResult.addBinding("name", vf.createLiteral("Bob"));
    // Therefore, there are no new join results.
    final Iterator<VisibilityBindingSet> newJoinResultsIt = naturalJoin.newRightResult(leftResults, new VisibilityBindingSet(newRightResult));
    assertFalse(newJoinResultsIt.hasNext());
}
Also used : VisibilityBindingSet(org.apache.rya.api.model.VisibilityBindingSet) 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