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);
}
}
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);
}
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);
}
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);
}
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());
}
Aggregations