Search in sources :

Example 6 with QueryResultCollector

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class SPARQLJSONBooleanTest method testBoolean4.

@Test
public void testBoolean4() throws Exception {
    SPARQLBooleanJSONParser parser = new SPARQLBooleanJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    parser.parseQueryResult(this.getClass().getResourceAsStream("/sparqljson/boolean4.srj"));
    assertFalse(handler.getBoolean());
}
Also used : SPARQLBooleanJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLBooleanJSONParser) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOBooleanTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOBooleanTest) Test(org.junit.Test)

Example 7 with QueryResultCollector

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class SPARQLJSONTupleBackgroundTest method testBindings1.

@Test
public void testBindings1() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/bindings1.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be two variables
    assertEquals(2, handler.getBindingNames().size());
    // first must be called "book", the second "title"
    assertEquals("book", handler.getBindingNames().get(0));
    assertEquals("title", handler.getBindingNames().get(1));
    // should be 7 solutions alltogether
    assertEquals(7, handler.getBindingSets().size());
    // Results are ordered, so first should be book6
    assertEquals("http://example.org/book/book6", handler.getBindingSets().get(0).getValue("book").stringValue());
    for (BindingSet b : handler.getBindingSets()) {
        assertNotNull(b.getValue("book"));
        assertNotNull(b.getValue("title"));
        assertTrue(b.getValue("book") instanceof IRI);
        assertTrue(b.getValue("title") instanceof Literal);
        IRI book = (IRI) b.getValue("book");
        if (book.stringValue().equals("http://example.org/book/book6")) {
            assertEquals("Harry Potter and the Half-Blood Prince", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book7")) {
            assertEquals("Harry Potter and the Deathly Hallows", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book5")) {
            assertEquals("Harry Potter and the Order of the Phoenix", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book4")) {
            assertEquals("Harry Potter and the Goblet of Fire", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book2")) {
            assertEquals("Harry Potter and the Chamber of Secrets", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book3")) {
            assertEquals("Harry Potter and the Prisoner Of Azkaban", b.getValue("title").stringValue());
        } else if (book.stringValue().equals("http://example.org/book/book1")) {
            assertEquals("Harry Potter and the Philosopher's Stone", b.getValue("title").stringValue());
        } else {
            fail("Found unexpected binding set in result: " + b.toString());
        }
    }
}
Also used : BindingSet(org.eclipse.rdf4j.query.BindingSet) IRI(org.eclipse.rdf4j.model.IRI) SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) 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 8 with QueryResultCollector

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class SPARQLJSONTupleBackgroundTest method testNonStandardDistinctOrdered.

@Test
public void testNonStandardDistinctOrdered() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/non-standard-distinct-ordered.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be 1 variable
    assertEquals(1, handler.getBindingNames().size());
    // first must be called "Concept", etc.,
    assertEquals("Concept", handler.getBindingNames().get(0));
    // -1 results
    assertEquals(100, handler.getBindingSets().size());
}
Also used : SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) InputStream(java.io.InputStream) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOTupleTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest) Test(org.junit.Test)

Example 9 with QueryResultCollector

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class QueryResultIO method parseBoolean.

/**
 * Parses a boolean query result document and returns the parsed value.
 *
 * @param in
 *        An InputStream to read the query result document from.
 * @param format
 *        The file format of the document to parse.
 * @return A boolean representing the result of parsing the given InputStream.
 * @throws IOException
 *         If an I/O error occured while reading the query result document from the stream.
 * @throws UnsupportedQueryResultFormatException
 *         If an unsupported query result file format was specified.
 */
public static boolean parseBoolean(InputStream in, QueryResultFormat format) throws IOException, QueryResultParseException, UnsupportedQueryResultFormatException {
    BooleanQueryResultParser parser = createBooleanParser(format);
    try {
        QueryResultCollector handler = new QueryResultCollector();
        parser.setQueryResultHandler(handler);
        parser.parseQueryResult(in);
        if (handler.getHandledBoolean()) {
            return handler.getBoolean();
        } else {
            throw new QueryResultParseException("Did not find a boolean result");
        }
    } catch (QueryResultHandlerException e) {
        throw new QueryResultParseException(e);
    }
}
Also used : QueryResultHandlerException(org.eclipse.rdf4j.query.QueryResultHandlerException) TupleQueryResultHandlerException(org.eclipse.rdf4j.query.TupleQueryResultHandlerException) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)

Example 10 with QueryResultCollector

use of org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector in project rdf4j by eclipse.

the class SPARQLJSONTupleTest method testNonStandardOrdered.

@Test
public void testNonStandardOrdered() throws Exception {
    SPARQLResultsJSONParser parser = new SPARQLResultsJSONParser(SimpleValueFactory.getInstance());
    QueryResultCollector handler = new QueryResultCollector();
    parser.setQueryResultHandler(handler);
    InputStream stream = this.getClass().getResourceAsStream("/sparqljson/non-standard-ordered.srj");
    assertNotNull("Could not find test resource", stream);
    parser.parseQueryResult(stream);
    // there must be 1 variable
    assertEquals(1, handler.getBindingNames().size());
    // first must be called "Concept", etc.,
    assertEquals("Concept", handler.getBindingNames().get(0));
    // -1 results
    assertEquals(100, handler.getBindingSets().size());
}
Also used : SPARQLResultsJSONParser(org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser) InputStream(java.io.InputStream) QueryResultCollector(org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector) AbstractQueryResultIOTupleTest(org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest) Test(org.junit.Test)

Aggregations

QueryResultCollector (org.eclipse.rdf4j.query.resultio.helpers.QueryResultCollector)20 Test (org.junit.Test)18 InputStream (java.io.InputStream)10 AbstractQueryResultIOTupleTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOTupleTest)10 SPARQLResultsJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLResultsJSONParser)10 QueryResultParseException (org.eclipse.rdf4j.query.resultio.QueryResultParseException)5 IRI (org.eclipse.rdf4j.model.IRI)4 Literal (org.eclipse.rdf4j.model.Literal)4 BindingSet (org.eclipse.rdf4j.query.BindingSet)4 AbstractQueryResultIOBooleanTest (org.eclipse.rdf4j.query.resultio.AbstractQueryResultIOBooleanTest)4 QueryResultParser (org.eclipse.rdf4j.query.resultio.QueryResultParser)4 SPARQLBooleanJSONParser (org.eclipse.rdf4j.query.resultio.sparqljson.SPARQLBooleanJSONParser)4 ParseErrorCollector (org.eclipse.rdf4j.rio.helpers.ParseErrorCollector)4 BNode (org.eclipse.rdf4j.model.BNode)2 HttpResponse (org.apache.http.HttpResponse)1 QueryResultHandlerException (org.eclipse.rdf4j.query.QueryResultHandlerException)1 TupleQueryResultHandlerException (org.eclipse.rdf4j.query.TupleQueryResultHandlerException)1 BooleanQueryResultFormat (org.eclipse.rdf4j.query.resultio.BooleanQueryResultFormat)1 BooleanQueryResultParser (org.eclipse.rdf4j.query.resultio.BooleanQueryResultParser)1 QueryResultFormat (org.eclipse.rdf4j.query.resultio.QueryResultFormat)1