Search in sources :

Example 16 with QuerySolution

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

the class ResultSetApply method apply.

public void apply() {
    proc.start(rs);
    for (; rs.hasNext(); ) {
        QuerySolution qs = rs.next();
        proc.start(qs);
        for (String varName : rs.getResultVars()) {
            RDFNode node = qs.get(varName);
            // node may be null
            proc.binding(varName, node);
        }
        proc.finish(qs);
    }
    proc.finish(rs);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 17 with QuerySolution

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

the class ResultSetUtils method resultSetToStringList.

/**
     * Extracts a List filled with the binding of selectElement variable for each
     * query solution, turned into a string (URIs or lexical forms).  
     * Exhausts the result set.  Create a rewindable one to use multiple times. 
     * @see org.apache.jena.query.ResultSetFactory
     */
public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) {
    // feature suggested by James Howison
    List<String> items = new ArrayList<>();
    while (rs.hasNext()) {
        QuerySolution qs = rs.nextSolution();
        RDFNode rn = qs.get(selectElement);
        if (rn.isLiteral())
            items.add(((Literal) rn).getLexicalForm());
        else if (rn.isURIResource())
            items.add(((Resource) rn).getURI());
        else if (rn.isAnon()) {
            items.add(((Resource) rn).getId().getLabelString());
        } else
            throw new ARQException("Unknow thing in results : " + rn);
    }
    return items;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ARQException(org.apache.jena.sparql.ARQException) Literal(org.apache.jena.rdf.model.Literal) ArrayList(java.util.ArrayList) Resource(org.apache.jena.rdf.model.Resource) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 18 with QuerySolution

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

the class ResultSetUtils method resultSetToList.

/**
     * Extracts a List filled with the binding of selectElement variable for each
     * query solution as RDFNodes (Resources or Literals).
     * Exhausts the result set.  Create a rewindable one to use multiple times. 
     *   
     * @see org.apache.jena.query.ResultSetFactory   
     */
public static List<RDFNode> resultSetToList(ResultSet rs, String selectElement) {
    // feature suggested by James Howison
    List<RDFNode> items = new ArrayList<>();
    while (rs.hasNext()) {
        QuerySolution qs = rs.nextSolution();
        RDFNode n = qs.get(selectElement);
        items.add(n);
    }
    return items;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ArrayList(java.util.ArrayList) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 19 with QuerySolution

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

the class ExTDB5 method main.

public static void main(String... argv) {
    // Direct way: Make a TDB-back Jena model in the named directory.
    String directory = "MyDatabases/DB1";
    Dataset dataset = TDBFactory.createDataset(directory);
    // Potentially expensive query.
    String sparqlQueryString = "SELECT (count(*) AS ?count) { ?s ?p ?o }";
    // See http://incubator.apache.org/jena/documentation/query/app_api.html
    Query query = QueryFactory.create(sparqlQueryString);
    QueryExecution qexec = QueryExecutionFactory.create(query, dataset);
    try {
        ResultSet results = qexec.execSelect();
        for (; results.hasNext(); ) {
            QuerySolution soln = results.nextSolution();
            int count = soln.getLiteral("count").getInt();
            System.out.println("count = " + count);
        }
    } finally {
        qexec.close();
    }
    // Close the dataset.
    dataset.close();
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 20 with QuerySolution

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

the class SelectBuilderTest method testVarReplacementInSubQuery.

@Test
public void testVarReplacementInSubQuery() 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("?p").addWhere("?x", "?p", "?o").addFilter("?o < ?limit");
    builder.addPrefix("xsd", XSD.getURI()).addVar("?x").addVar("count(?p)", "?c").addWhere("?x", "?p", "?o2").addSubQuery(sb).addGroupBy("?x");
    builder.setVar("?limit", 4);
    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("c"));
        if ("urn:one".equals(soln.get("?x").asResource().getURI())) {
            assertEquals(2, soln.get("?c").asLiteral().getInt());
        } else {
            assertEquals(1, soln.get("?c").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)

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