Search in sources :

Example 1 with QueryExecution

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

the class QueryCommandAssembler method open.

@Override
public Object open(Assembler a, Resource root, Mode mode) {
    // Query
    Resource queryDesc = getUniqueResource(root, AssemblerVocab.pQuery);
    Query query = (Query) a.open(a, queryDesc, mode);
    // Dataset
    Resource datasetDesc = getUniqueResource(root, AssemblerVocab.pDataset);
    Dataset dataset = (Dataset) a.open(a, datasetDesc, mode);
    // Output format
    String s = GraphUtils.getStringValue(root, AssemblerVocab.pOutputFormat);
    if (s == null)
        s = "text";
    ResultsFormat format = ResultsFormat.lookup(s);
    QueryExecution qExec = QueryExecutionFactory.create(query, dataset);
    return new QExec(query, qExec, format);
}
Also used : Query(org.apache.jena.query.Query) Dataset(org.apache.jena.query.Dataset) Resource(org.apache.jena.rdf.model.Resource) ResultsFormat(org.apache.jena.sparql.resultset.ResultsFormat) QueryExecution(org.apache.jena.query.QueryExecution)

Example 2 with QueryExecution

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

the class StoreUtils method containsGraph.

public static boolean containsGraph(Store store, Node graphNode) {
    String qs = "SELECT * { GRAPH " + FmtUtils.stringForNode(graphNode) + " { ?s ?p ?o }} LIMIT 1";
    Dataset ds = SDBFactory.connectDataset(store);
    try (QueryExecution qExec = QueryExecutionFactory.create(qs, ds)) {
        ResultSet rs = qExec.execSelect();
        return rs.hasNext();
    }
}
Also used : Dataset(org.apache.jena.query.Dataset) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 3 with QueryExecution

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

the class TestServerReadOnly method query_readonly.

@Test
public void query_readonly() {
    Query query = QueryFactory.create("ASK{}");
    QueryExecution qexec = QueryExecutionFactory.sparqlService(serviceQuery(), query);
    qexec.execAsk();
}
Also used : Query(org.apache.jena.query.Query) ServerCtl.serviceQuery(org.apache.jena.fuseki.ServerCtl.serviceQuery) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 4 with QueryExecution

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

the class TestDatasetWithLuceneStoredLiterals method doTestSearchWithLiterals.

protected Map<String, Literal> doTestSearchWithLiterals(String turtle, String queryString, Set<String> expectedEntityURIs) {
    Model model = dataset.getDefaultModel();
    Reader reader = new StringReader(turtle);
    dataset.begin(ReadWrite.WRITE);
    model.read(reader, "", "TURTLE");
    dataset.commit();
    Map<String, Literal> literals = new HashMap<>();
    Query query = QueryFactory.create(queryString);
    dataset.begin(ReadWrite.READ);
    try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
        ResultSet results = qexec.execSelect();
        assertEquals(expectedEntityURIs.size() > 0, results.hasNext());
        int count;
        for (count = 0; results.hasNext(); count++) {
            QuerySolution soln = results.nextSolution();
            String entityUri = soln.getResource("s").getURI();
            assertTrue(expectedEntityURIs.contains(entityUri));
            Literal literal = soln.getLiteral("literal");
            assertNotNull(literal);
            literals.put(entityUri, literal);
        }
        assertEquals(expectedEntityURIs.size(), count);
    } finally {
        dataset.end();
    }
    return literals;
}
Also used : Query(org.apache.jena.query.Query) HashMap(java.util.HashMap) 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) Reader(java.io.Reader) StringReader(java.io.StringReader) QueryExecution(org.apache.jena.query.QueryExecution)

Example 5 with QueryExecution

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

the class QueryEngineTest method testOpenQueryType.

@Test
public void testOpenQueryType() {
    final SecurityEvaluator eval = new MockSecurityEvaluator(true, true, true, true, true, true);
    final SecuredModel model = Factory.getInstance(eval, "http://example.com/securedModel", baseModel);
    try {
        final String query = "prefix fn: <http://www.w3.org/2005/xpath-functions#>  " + " SELECT ?foo ?bar WHERE " + " { ?foo a <http://example.com/class> ; " + "?bar [] ." + "  } ";
        try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
            final ResultSet results = qexec.execSelect();
            int count = 0;
            for (; results.hasNext(); ) {
                count++;
                results.nextSolution();
            }
            Assert.assertEquals(8, count);
        }
    } finally {
        model.close();
    }
}
Also used : MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) ResultSet(org.apache.jena.query.ResultSet) SecurityEvaluator(org.apache.jena.permissions.SecurityEvaluator) MockSecurityEvaluator(org.apache.jena.permissions.MockSecurityEvaluator) SecuredModel(org.apache.jena.permissions.model.SecuredModel) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Aggregations

QueryExecution (org.apache.jena.query.QueryExecution)17 ResultSet (org.apache.jena.query.ResultSet)14 Query (org.apache.jena.query.Query)9 QuerySolution (org.apache.jena.query.QuerySolution)8 Test (org.junit.Test)7 Model (org.apache.jena.rdf.model.Model)5 Node (org.apache.jena.graph.Node)4 Dataset (org.apache.jena.query.Dataset)4 Resource (org.apache.jena.rdf.model.Resource)4 AbstractRegexpBasedTest (org.apache.jena.arq.AbstractRegexpBasedTest)3 MockSecurityEvaluator (org.apache.jena.permissions.MockSecurityEvaluator)3 SecurityEvaluator (org.apache.jena.permissions.SecurityEvaluator)3 SecuredModel (org.apache.jena.permissions.model.SecuredModel)3 Reader (java.io.Reader)2 StringReader (java.io.StringReader)2 Triple (org.apache.jena.graph.Triple)2 Literal (org.apache.jena.rdf.model.Literal)2 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 CmdException (jena.cmd.CmdException)1