Search in sources :

Example 11 with Query

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

the class WhereProcessor method parseExpr.

private Expr parseExpr(String expression) {
    Query query = new Query();
    query.setPrefixMapping(prefixHandler.getPrefixes());
    return ExprUtils.parse(query, expression, true);
}
Also used : Query(org.apache.jena.query.Query)

Example 12 with Query

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

the class ExTDB4 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);
    ResultSet results = qexec.execSelect();
    ResultSetFormatter.out(results);
    qexec.close();
    dataset.close();
}
Also used : Query(org.apache.jena.query.Query) Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 13 with Query

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

the class ExTDB_Txn1 method execQuery.

public static void execQuery(String sparqlQueryString, Dataset dataset) {
    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();
    }
}
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 14 with Query

use of org.apache.jena.query.Query in project webofneeds by researchstudio-sat.

the class DatasetToUriListBySparqlFunction method apply.

@Override
public List<URI> apply(Dataset dataset) {
    dataset.begin(ReadWrite.READ);
    Dataset result = DatasetFactory.createGeneral();
    result.begin(ReadWrite.WRITE);
    Query query = QueryFactory.create(sparql);
    List<URI> ret = new ArrayList<URI>();
    try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset)) {
        ResultSet resultSet = queryExecution.execSelect();
        if (!resultSet.getResultVars().contains("uri")) {
            throw new IllegalStateException("Query has no variable named 'uri' (read from: " + sparqlFile + ")");
        }
        while (resultSet.hasNext()) {
            QuerySolution solution = resultSet.next();
            RDFNode uriNode = solution.get("uri");
            if (uriNode == null) {
                throw new IllegalStateException("Query has no variable named 'uri' (read from: " + sparqlFile + ")");
            }
            if (!uriNode.isURIResource()) {
                throw new IllegalStateException("Value of result variable 'uri' is not a resource (read from: " + sparqlFile + ")");
            }
            ret.add(URI.create(uriNode.asResource().getURI().toString()));
        }
    }
    return ret;
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) URI(java.net.URI) QueryExecution(org.apache.jena.query.QueryExecution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Example 15 with Query

use of org.apache.jena.query.Query in project webofneeds by researchstudio-sat.

the class DynamicDatasetToDatasetBySparqlGSPOSelectFunction method apply.

@Override
public Dataset apply(Dataset dataset) {
    dataset.begin(ReadWrite.READ);
    Dataset result = DatasetFactory.createGeneral();
    result.begin(ReadWrite.WRITE);
    Query query = QueryFactory.create(sparql);
    try (QueryExecution queryExecution = QueryExecutionFactory.create(query, dataset, initialBinding)) {
        ResultSet resultSet = queryExecution.execSelect();
        RDFNode currentProposal = null;
        Model currentProposalContent = ModelFactory.createDefaultModel();
        while (resultSet.hasNext()) {
            QuerySolution solution = resultSet.next();
            RDFNode proposalNode = solution.get("g");
            if (currentProposal == null) {
                // first solution: remember uri of first proposal
                currentProposal = proposalNode;
            } else {
                // the currentProposal URI and prepare a new empty model for the next proposal
                if (!currentProposal.equals(proposalNode)) {
                    // we have seen all triples of currentProposal
                    result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
                    currentProposalContent = ModelFactory.createDefaultModel();
                }
                currentProposal = proposalNode;
            }
            // add current triple into currentAgreementModel
            RDFNode s = solution.get("s");
            RDFNode p = solution.get("p");
            RDFNode o = solution.get("o");
            Statement newStatement = new StatementImpl(s.asResource(), new PropertyImpl(p.asResource().getURI()), o);
            currentProposalContent.add(newStatement);
        }
        // add the last model
        if (currentProposal != null) {
            result.addNamedModel(currentProposal.asResource().getURI(), currentProposalContent);
        }
        return result;
    } finally {
        dataset.commit();
        result.commit();
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) Dataset(org.apache.jena.query.Dataset) Statement(org.apache.jena.rdf.model.Statement) StatementImpl(org.apache.jena.rdf.model.impl.StatementImpl) ResultSet(org.apache.jena.query.ResultSet) Model(org.apache.jena.rdf.model.Model) PropertyImpl(org.apache.jena.rdf.model.impl.PropertyImpl) QueryExecution(org.apache.jena.query.QueryExecution) RDFNode(org.apache.jena.rdf.model.RDFNode)

Aggregations

Query (org.apache.jena.query.Query)265 Test (org.junit.Test)78 ContractTest (org.xenei.junit.contract.ContractTest)65 QueryExecution (org.apache.jena.query.QueryExecution)63 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)49 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)42 Var (org.apache.jena.sparql.core.Var)42 Triple (org.apache.jena.graph.Triple)38 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)32 ResultSet (org.apache.jena.query.ResultSet)29 Node (org.apache.jena.graph.Node)28 TriplePath (org.apache.jena.sparql.core.TriplePath)28 QuerySolution (org.apache.jena.query.QuerySolution)26 ExprVar (org.apache.jena.sparql.expr.ExprVar)24 Binding (org.apache.jena.sparql.engine.binding.Binding)22 Model (org.apache.jena.rdf.model.Model)20 HashMap (java.util.HashMap)19 Dataset (org.apache.jena.query.Dataset)15 Op (org.apache.jena.sparql.algebra.Op)15 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)14