Search in sources :

Example 21 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class SelectBuilderTest method testAggregatorsInSubQuery.

@Test
public void testAggregatorsInSubQuery() throws ParseException {
    Model m = ModelFactory.createDefaultModel();
    Resource r = m.createResource("urn:one");
    m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(1));
    m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(3));
    m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(5));
    r = m.createResource("urn:two");
    m.add(r, m.getProperty("urn:p:one"), m.createTypedLiteral(2));
    m.add(r, m.getProperty("urn:p:two"), m.createTypedLiteral(4));
    m.add(r, m.getProperty("urn:p:three"), m.createTypedLiteral(6));
    SelectBuilder sb = new SelectBuilder().addVar("?x").addVar("max(?o)", "?max").addWhere("?x", "?p", "?o").addGroupBy("?x");
    builder.addPrefix("xsd", XSD.getURI()).addVar("?x").addVar("min(?o2)", "?min").addWhere("?x", "?p2", "?o2").addSubQuery(sb).addFilter("?max = '6'^^xsd:int").addGroupBy("?x");
    QueryExecution qexec = QueryExecutionFactory.create(builder.build(), m);
    ResultSet results = qexec.execSelect();
    assertTrue(results.hasNext());
    for (; results.hasNext(); ) {
        QuerySolution soln = results.nextSolution();
        assertTrue(soln.contains("x"));
        assertTrue(soln.contains("min"));
        assertEquals("urn:two", soln.get("?x").asResource().getURI());
        assertEquals(2, soln.get("?min").asLiteral().getInt());
    }
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test) AbstractRegexpBasedTest(org.apache.jena.arq.AbstractRegexpBasedTest)

Example 22 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class JenaOSGITest method runQuery.

private void runQuery(Dataset dataset) {
    Query query = QueryFactory.create("" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "SELECT ?bob WHERE { " + "  GRAPH <http://example.com/graph> { " + "      ?alice foaf:knows ?bob . " + "  }" + "}");
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        ResultSet results = qexec.execSelect();
        assertTrue(results.hasNext());
        QuerySolution r = results.next();
        assertEquals(bob, r.get("bob").asResource());
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 23 with QuerySolution

use of org.apache.jena.query.QuerySolution in project jena by apache.

the class TestDatasetWithLuceneStoredLiterals method doTestSearchWithLiteralsMultiple.

protected List<Node> doTestSearchWithLiteralsMultiple(String turtle, String queryString, String expectedEntityURI) {
    List<Node> literals = new LinkedList<>();
    Model model = dataset.getDefaultModel();
    Reader reader = new StringReader(turtle);
    dataset.begin(ReadWrite.WRITE);
    model.read(reader, "", "TURTLE");
    dataset.commit();
    Query query = QueryFactory.create(queryString);
    dataset.begin(ReadWrite.READ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        ResultSet results = qexec.execSelect();
        int count;
        for (count = 0; results.hasNext(); count++) {
            QuerySolution soln = results.nextSolution();
            String entityURI = soln.getResource("s").getURI();
            assertEquals(expectedEntityURI, entityURI);
            Literal literal = soln.getLiteral("literal");
            assertNotNull(literal);
            literals.add(literal.asNode());
        }
    } finally {
        dataset.end();
    }
    return literals;
}
Also used : Query(org.apache.jena.query.Query) Node(org.apache.jena.graph.Node) Reader(java.io.Reader) StringReader(java.io.StringReader) LinkedList(java.util.LinkedList) QueryExecution(org.apache.jena.query.QueryExecution) QuerySolution(org.apache.jena.query.QuerySolution) Literal(org.apache.jena.rdf.model.Literal) Model(org.apache.jena.rdf.model.Model) StringReader(java.io.StringReader) ResultSet(org.apache.jena.query.ResultSet)

Aggregations

QuerySolution (org.apache.jena.query.QuerySolution)23 ResultSet (org.apache.jena.query.ResultSet)11 RDFNode (org.apache.jena.rdf.model.RDFNode)9 QueryExecution (org.apache.jena.query.QueryExecution)8 ArrayList (java.util.ArrayList)5 Query (org.apache.jena.query.Query)5 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)5 Model (org.apache.jena.rdf.model.Model)5 Resource (org.apache.jena.rdf.model.Resource)5 Literal (org.apache.jena.rdf.model.Literal)4 Test (org.junit.Test)4 Extractor (infoeval.main.WikiData.Extractor)3 Date (java.sql.Date)3 AbstractRegexpBasedTest (org.apache.jena.arq.AbstractRegexpBasedTest)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 SimpleDateFormat (java.text.SimpleDateFormat)2 SqlTablesFiller (infoeval.main.WikiData.SqlTablesFiller)1 TableEntry (infoeval.main.mysql.TableEntry)1 HashMap (java.util.HashMap)1