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