Search in sources :

Example 66 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 67 with QuerySolution

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

the class FusekiConfig method servicesAndDatasets.

private static List<DataAccessPoint> servicesAndDatasets(Model model) {
    // Old style configuration file : server to services.
    DatasetDescriptionRegistry dsDescMap = FusekiServer.registryForBuild();
    // ---- Services
    ResultSet rs = FusekiLib.query("SELECT * { ?s fu:services [ list:member ?service ] }", model);
    List<DataAccessPoint> accessPoints = new ArrayList<>();
    if (!rs.hasNext())
        // No "fu:services ( .... )" so try looking for services directly.
        // This means Fuseki2, service configuration files (no server section) work for --conf. 
        rs = FusekiLib.query("SELECT ?service { ?service a fu:Service }", model);
    for (; rs.hasNext(); ) {
        QuerySolution soln = rs.next();
        Resource svc = soln.getResource("service");
        DataAccessPoint acc = FusekiBuilder.buildDataAccessPoint(svc, dsDescMap);
        accessPoints.add(acc);
    }
    return accessPoints;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) ArrayList(java.util.ArrayList)

Example 68 with QuerySolution

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

the class FusekiBuilder method addServiceEP.

private static void addServiceEP(DataService dataService, OperationName opName, Resource svc, Property property) {
    String p = "<" + property.getURI() + ">";
    ResultSet rs = query("SELECT * { ?svc " + p + " ?ep}", svc.getModel(), "svc", svc);
    for (; rs.hasNext(); ) {
        QuerySolution soln = rs.next();
        String epName = soln.getLiteral("ep").getLexicalForm();
        Endpoint operation = new Endpoint(opName, epName);
        addServiceEP(dataService, opName, epName);
    //log.info("  " + opName.name + " = " + dataAccessPoint.getName() + "/" + epName) ;
    }
}
Also used : Endpoint(org.apache.jena.fuseki.server.Endpoint) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet)

Example 69 with QuerySolution

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

the class TextOutput method colWidths.

private int[] colWidths(ResultSetRewindable rs) {
    int numCols = rs.getResultVars().size();
    int numRows = 0;
    int[] colWidths = new int[numCols];
    // Widths at least that of the variable name.  Assumes we will print col headings.
    for (int i = 0; i < numCols; i++) colWidths[i] = (rs.getResultVars().get(i)).length();
    // Preparation pass : find the maximum width for each column
    for (; rs.hasNext(); ) {
        numRows++;
        QuerySolution rBind = rs.nextSolution();
        int col = -1;
        for (String s1 : rs.getResultVars()) {
            col++;
            String rVar = s1;
            String s = getVarValueAsString(rBind, rVar);
            if (colWidths[col] < s.length()) {
                colWidths[col] = s.length();
            }
        }
    }
    rs.reset();
    return colWidths;
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution)

Example 70 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)

Aggregations

QuerySolution (org.apache.jena.query.QuerySolution)125 ResultSet (org.apache.jena.query.ResultSet)103 QueryExecution (org.apache.jena.query.QueryExecution)96 ArrayList (java.util.ArrayList)70 Test (org.junit.Test)69 Dataset (org.apache.jena.query.Dataset)44 Query (org.apache.jena.query.Query)44 Resource (org.apache.jena.rdf.model.Resource)43 Literal (org.apache.jena.rdf.model.Literal)21 RDFNode (org.apache.jena.rdf.model.RDFNode)20 Model (org.apache.jena.rdf.model.Model)12 HashMap (java.util.HashMap)7 ResultSetRewindable (org.apache.jena.query.ResultSetRewindable)6 Extractor (infoeval.main.WikiData.Extractor)4 Node (org.apache.jena.graph.Node)4 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)4 IOException (java.io.IOException)3 Date (java.sql.Date)3 SimpleDateFormat (java.text.SimpleDateFormat)3 LinkedList (java.util.LinkedList)3