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