Search in sources :

Example 26 with BindingSet

use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.

the class SPARQLJSONTupleBackgroundTest method testBindings2.

@Test
public void testBindings2() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/bindings2.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be 7 variables
    assertEquals(7, handler.getBindingNames().size());
    // first must be called "x", etc.,
    assertEquals("x", handler.getBindingNames().get(0));
    assertEquals("hpage", handler.getBindingNames().get(1));
    assertEquals("name", handler.getBindingNames().get(2));
    assertEquals("mbox", handler.getBindingNames().get(3));
    assertEquals("age", handler.getBindingNames().get(4));
    assertEquals("blurb", handler.getBindingNames().get(5));
    assertEquals("friend", handler.getBindingNames().get(6));
    // 2 results
    assertEquals(2, handler.getBindingSets().size());
    // Results are ordered, so first should be alice
    assertEquals("http://work.example.org/alice/", handler.getBindingSets().get(0).getValue("hpage").stringValue());
    for (BindingSet b : handler.getBindingSets()) {
        assertNotNull(b.getValue("x"));
        assertNotNull(b.getValue("hpage"));
        assertNotNull(b.getValue("name"));
        assertNotNull(b.getValue("mbox"));
        assertNotNull(b.getValue("friend"));
        assertTrue(b.getValue("x") instanceof BNode);
        assertTrue(b.getValue("hpage") instanceof IRI);
        assertTrue(b.getValue("name") instanceof Literal);
        assertTrue(b.getValue("friend") instanceof BNode);
        BNode value = (BNode) b.getValue("x");
        if (value.getID().equals("r1")) {
            assertNotNull(b.getValue("blurb"));
            assertTrue(b.getValue("mbox") instanceof Literal);
            assertTrue(b.getValue("blurb") instanceof Literal);
            assertEquals("http://work.example.org/alice/", b.getValue("hpage").stringValue());
            Literal name = (Literal) b.getValue("name");
            assertEquals("Alice", name.stringValue());
            assertFalse(name.getLanguage().isPresent());
            assertEquals(XMLSchema.STRING, name.getDatatype());
            Literal mbox = (Literal) b.getValue("mbox");
            assertEquals("", mbox.stringValue());
            assertFalse(mbox.getLanguage().isPresent());
            assertEquals(XMLSchema.STRING, mbox.getDatatype());
            Literal blurb = (Literal) b.getValue("blurb");
            assertEquals("<p xmlns=\"http://www.w3.org/1999/xhtml\">My name is <b>alice</b></p>", blurb.stringValue());
            assertFalse(blurb.getLanguage().isPresent());
            assertEquals(RDF.XMLLITERAL, blurb.getDatatype());
        } else if (value.getID().equals("r2")) {
            assertNull(b.getValue("blurb"));
            assertTrue(b.getValue("mbox") instanceof IRI);
            assertEquals("http://work.example.org/bob/", b.getValue("hpage").stringValue());
            Literal name = (Literal) b.getValue("name");
            assertEquals("Bob", name.stringValue());
            assertEquals("en", name.getLanguage().orElse(null));
            assertEquals(RDF.LANGSTRING, name.getDatatype());
            assertEquals("mailto:bob@work.example.org", b.getValue("mbox").stringValue());
        } else {
            fail("Found unexpected binding set in result: " + b.toString());
        }
    }
    assertEquals(1, handler.getLinks().size());
    assertEquals("http://www.w3.org/TR/2013/REC-sparql11-results-json-20130321/#example", handler.getLinks().get(0));
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) BNode(org.eclipse.rdf4j.model.BNode) InputStream(java.io.InputStream) Literal(org.eclipse.rdf4j.model.Literal) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOTupleTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest) Test(org.junit.Test)

Example 27 with BindingSet

use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.

the class SPARQLCrossProductIteration method getNextElement.

@Override
protected BindingSet getNextElement() throws QueryEvaluationException {
    if (currentInputBinding == null) {
        inputBindingsIterator = inputBindings.iterator();
        if (resultIteration.hasNext())
            currentInputBinding = resultIteration.next();
        else
            // no more results
            return null;
    }
    if (inputBindingsIterator.hasNext()) {
        BindingSet next = inputBindingsIterator.next();
        SPARQLQueryBindingSet res = new SPARQLQueryBindingSet(next.size() + currentInputBinding.size());
        res.addAll(next);
        res.addAll(currentInputBinding);
        if (!inputBindingsIterator.hasNext())
            currentInputBinding = null;
        return res;
    }
    return null;
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) SPARQLQueryBindingSet(org.eclipse.rdf4j.repository.sparql.query.SPARQLQueryBindingSet) SPARQLQueryBindingSet(org.eclipse.rdf4j.repository.sparql.query.SPARQLQueryBindingSet)

Example 28 with BindingSet

use of org.eclipse.rdf4j.query.BindingSet in project rdf4j by eclipse.

the class TupleExprBuilder method visit.

@Override
public BindingSetAssignment visit(ASTInlineData node, Object data) throws VisitorException {
    BindingSetAssignment bsa = new BindingSetAssignment();
    List<ASTVar> varNodes = node.jjtGetChildren(ASTVar.class);
    List<Var> vars = new ArrayList<Var>(varNodes.size());
    // preserve order in query
    Set<String> bindingNames = new LinkedHashSet<String>(varNodes.size());
    for (ASTVar varNode : varNodes) {
        Var var = (Var) varNode.jjtAccept(this, data);
        vars.add(var);
        bindingNames.add(var.getName());
    }
    bsa.setBindingNames(bindingNames);
    List<ASTBindingSet> bindingNodes = node.jjtGetChildren(ASTBindingSet.class);
    List<BindingSet> bindingSets = new ArrayList<BindingSet>(bindingNodes.size());
    for (ASTBindingSet bindingNode : bindingNodes) {
        BindingSet bindingSet = (BindingSet) bindingNode.jjtAccept(this, vars);
        bindingSets.add(bindingSet);
    }
    bsa.setBindingSets(bindingSets);
    graphPattern.addRequiredTE(bsa);
    return bsa;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ListBindingSet(org.eclipse.rdf4j.query.impl.ListBindingSet) BindingSet(org.eclipse.rdf4j.query.BindingSet) BindingSetAssignment(org.eclipse.rdf4j.query.algebra.BindingSetAssignment) Var(org.eclipse.rdf4j.query.algebra.Var) ArrayList(java.util.ArrayList)

Aggregations

BindingSet (org.eclipse.rdf4j.query.BindingSet)28 ArrayList (java.util.ArrayList)9 IRI (org.eclipse.rdf4j.model.IRI)8 Value (org.eclipse.rdf4j.model.Value)8 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)6 TupleQueryResult (org.eclipse.rdf4j.query.TupleQueryResult)6 RepositoryException (org.eclipse.rdf4j.repository.RepositoryException)6 IOException (java.io.IOException)5 Literal (org.eclipse.rdf4j.model.Literal)5 Binding (org.eclipse.rdf4j.query.Binding)5 ListBindingSet (org.eclipse.rdf4j.query.impl.ListBindingSet)5 InputStream (java.io.InputStream)4 BNode (org.eclipse.rdf4j.model.BNode)4 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)4 QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)4 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)4 Test (org.junit.Test)4 Resource (org.eclipse.rdf4j.model.Resource)3 Var (org.eclipse.rdf4j.query.algebra.Var)3 InputStreamReader (java.io.InputStreamReader)2