use of org.openrdf.query.algebra.BindingSetAssignment in project incubator-rya by apache.
the class OneOfVisitorTest method testOneOf.
@Test
public void testOneOf() throws Exception {
// Configure a mock instance engine with an ontology:
final InferenceEngine inferenceEngine = mock(InferenceEngine.class);
when(inferenceEngine.isEnumeratedType(SUITS)).thenReturn(true);
when(inferenceEngine.getEnumeration(SUITS)).thenReturn(CARD_SUIT_ENUMERATION);
when(inferenceEngine.isEnumeratedType(RANKS)).thenReturn(true);
when(inferenceEngine.getEnumeration(RANKS)).thenReturn(CARD_RANK_ENUMERATION);
// Query for a Suits and rewrite using the visitor:
final Projection query = new Projection(new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", SUITS)), new ProjectionElemList(new ProjectionElem("s", "subject")));
query.visit(new OneOfVisitor(conf, inferenceEngine));
// Expected structure: BindingSetAssignment containing the enumeration:
// BindingSetAssignment(CLUBS, DIAMONDS, HEARTS, SPADES)
// Collect the arguments to the BindingSetAssignment:
assertTrue(query.getArg() instanceof BindingSetAssignment);
final BindingSetAssignment bsa = (BindingSetAssignment) query.getArg();
final Iterable<BindingSet> iterable = bsa.getBindingSets();
final Iterator<BindingSet> iter = iterable.iterator();
assertBindingSet(iter, CARD_SUIT_ENUMERATION.iterator());
// Query for a Ranks and rewrite using the visitor:
final Projection query2 = new Projection(new StatementPattern(new Var("s"), new Var("p", RDF.TYPE), new Var("o", RANKS)), new ProjectionElemList(new ProjectionElem("s", "subject")));
query2.visit(new OneOfVisitor(conf, inferenceEngine));
// Expected structure: BindingSetAssignment containing the enumeration:
// BindingSetAssignment(ACE, 2, 3, 4, 5, 6, 7, 8, 9, 10, JACK, QUEEN, KING)
// Collect the arguments to the BindingSetAssignment:
assertTrue(query2.getArg() instanceof BindingSetAssignment);
final BindingSetAssignment bsa2 = (BindingSetAssignment) query2.getArg();
final Iterable<BindingSet> iterable2 = bsa2.getBindingSets();
final Iterator<BindingSet> iter2 = iterable2.iterator();
assertBindingSet(iter2, CARD_RANK_ENUMERATION.iterator());
}
use of org.openrdf.query.algebra.BindingSetAssignment in project incubator-rya by apache.
the class OneOfVisitor method meetSP.
@Override
protected void meetSP(final StatementPattern node) throws Exception {
final Var subVar = node.getSubjectVar();
final Var predVar = node.getPredicateVar();
final Var objVar = node.getObjectVar();
final Var conVar = node.getContextVar();
if (predVar != null && objVar != null && objVar.getValue() != null && objVar.getValue() instanceof Resource && RDF.TYPE.equals(predVar.getValue()) && !EXPANDED.equals(conVar)) {
final Resource object = (Resource) objVar.getValue();
if (inferenceEngine.isEnumeratedType(object)) {
final Set<BindingSet> solutions = new LinkedHashSet<>();
final Set<Resource> enumeration = inferenceEngine.getEnumeration(object);
for (final Resource enumType : enumeration) {
final QueryBindingSet qbs = new QueryBindingSet();
qbs.addBinding(subVar.getName(), enumType);
solutions.add(qbs);
}
if (!solutions.isEmpty()) {
final BindingSetAssignment enumNode = new BindingSetAssignment();
enumNode.setBindingSets(solutions);
node.replaceWith(enumNode);
log.trace("Replacing node with inferred one of enumeration: " + enumNode);
}
}
}
}
Aggregations