Search in sources :

Example 1 with BindingSetAssignment

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());
}
Also used : ProjectionElemList(org.openrdf.query.algebra.ProjectionElemList) StatementPattern(org.openrdf.query.algebra.StatementPattern) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) BindingSetAssignment(org.openrdf.query.algebra.BindingSetAssignment) Projection(org.openrdf.query.algebra.Projection) ProjectionElem(org.openrdf.query.algebra.ProjectionElem) Test(org.junit.Test)

Example 2 with BindingSetAssignment

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);
            }
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet) BindingSet(org.openrdf.query.BindingSet) Var(org.openrdf.query.algebra.Var) BindingSetAssignment(org.openrdf.query.algebra.BindingSetAssignment) Resource(org.openrdf.model.Resource) QueryBindingSet(org.openrdf.query.algebra.evaluation.QueryBindingSet)

Aggregations

BindingSet (org.openrdf.query.BindingSet)2 BindingSetAssignment (org.openrdf.query.algebra.BindingSetAssignment)2 Var (org.openrdf.query.algebra.Var)2 QueryBindingSet (org.openrdf.query.algebra.evaluation.QueryBindingSet)2 LinkedHashSet (java.util.LinkedHashSet)1 Test (org.junit.Test)1 Resource (org.openrdf.model.Resource)1 Projection (org.openrdf.query.algebra.Projection)1 ProjectionElem (org.openrdf.query.algebra.ProjectionElem)1 ProjectionElemList (org.openrdf.query.algebra.ProjectionElemList)1 StatementPattern (org.openrdf.query.algebra.StatementPattern)1