use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternMatcherTest method matchesSubject.
@Test
public void matchesSubject() throws Exception {
// Create the matcher against a pattern that matches a specific subject.
final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "<urn:Alice> ?p ?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("p", vf.createIRI("urn:talksTo"));
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());
}
use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class StatementPatternMatcherTest method variableContext.
@Test
public void variableContext() throws Exception {
// Create a matcher against a pattern that matches a variable context.
final StatementPatternMatcher matcher = new StatementPatternMatcher(getSp("SELECT * WHERE {" + "GRAPH ?c {" + "?s ?p ?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("p", vf.createIRI("urn:talksTo"));
expected.addBinding("o", vf.createIRI("urn:Bob"));
expected.addBinding("c", vf.createIRI("urn:testGraph"));
// Show the expected Binding Set matches the resulting Binding Set.
final Optional<BindingSet> bs = matcher.match(statement);
assertEquals(expected, bs.get());
}
use of org.eclipse.rdf4j.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.createIRI("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" + "}";
final Multiset<BindingSet> expectedSolutions = HashMultiset.create();
expectedSolutions.add(new EmptyBindingSet());
// Execute pipeline and verify results
final QueryRoot queryTree = new QueryRoot(PARSER.parseQuery(query, null).getTupleExpr());
final SparqlToPipelineTransformVisitor visitor = new SparqlToPipelineTransformVisitor(getRyaCollection());
queryTree.visit(visitor);
Assert.assertTrue(queryTree.getArg() instanceof Projection);
final Projection projection = (Projection) queryTree.getArg();
Assert.assertTrue(projection.getArg() instanceof AggregationPipelineQueryNode);
final AggregationPipelineQueryNode pipelineNode = (AggregationPipelineQueryNode) projection.getArg();
final Multiset<BindingSet> solutions = HashMultiset.create();
final CloseableIteration<BindingSet, QueryEvaluationException> iter = pipelineNode.evaluate(new QueryBindingSet());
while (iter.hasNext()) {
solutions.add(iter.next());
}
Assert.assertEquals(expectedSolutions, solutions);
}
use of org.eclipse.rdf4j.query.algebra.evaluation.QueryBindingSet in project incubator-rya by apache.
the class BindingSetHashJoinIterator method joinBindingSets.
/**
* This method verifies that all common variables have a common value and
* then joins the BindingSets together. In the case that the PCJ contains a
* LeftJoin, if the leftBs and rightBs have a common variable with distinct
* values and that common variable is unassured (only appears in LeftJoin),
* this method uses the value corresponding to leftBs.
*
* @param leftBs
* - BindingSet passed into PCJ
* @param rightBs
* - PCJ BindingSet
* @return - joined BindingSet
*/
private BindingSet joinBindingSets(BindingSet leftBs, BindingSet rightBs) {
Set<String> commonVars = Sets.intersection(leftBs.getBindingNames(), rightBs.getBindingNames());
// add value corresponding to leftBs
for (String s : commonVars) {
if (!leftBs.getValue(s).equals(rightBs.getValue(s)) && !unAssuredVariables.contains(s)) {
return EMPTY_BINDINGSET;
}
}
QueryBindingSet bs = new QueryBindingSet(removeConstants(leftBs));
rightBs = removeConstants(rightBs);
// to leftBs, which is effectively performing a LeftJoin.
for (String s : rightBs.getBindingNames()) {
if (bs.getValue(s) == null) {
bs.addBinding(s, rightBs.getValue(s));
}
}
return bs;
}
use of org.eclipse.rdf4j.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 (VarNameUtils.isConstant(mappedVar) && 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);
}
Aggregations