Search in sources :

Example 16 with QueryExecution

use of org.apache.jena.query.QueryExecution 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)

Example 17 with QueryExecution

use of org.apache.jena.query.QueryExecution 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)

Example 18 with QueryExecution

use of org.apache.jena.query.QueryExecution 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 19 with QueryExecution

use of org.apache.jena.query.QueryExecution 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 20 with QueryExecution

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

the class AlignmentQuery method queryAgrovoc.

/**
 * Cette fonction permet de récupérer les alignements présents sur Agrovoc
 * pour un concept passé en paramètre
 *
 * @param idC
 * @param idTheso
 * @param lexicalValue
 * @param lang
 * @return
 */
private ArrayList<NodeAlignment> queryAgrovoc(String idC, String idTheso, String lexicalValue, String lang, String source) {
    listeAlign = new ArrayList<>();
    lexicalValue = String.valueOf(lexicalValue.charAt(0)).toUpperCase() + lexicalValue.substring(1);
    String sparqlQueryString1 = "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> " + "SELECT * WHERE {" + "       ?uri skos:prefLabel ?pl ." + "       FILTER(regex(?pl,\"" + lexicalValue + "*\"))" + "       FILTER ( (lang(?pl)=\"" + lang + "\") )" + "       OPTIONAL { " + "       ?uri skos:scopeNote ?def ." + "       FILTER ( (lang(?def)=\"" + lang + "\") )" + "       }" + "   }";
    // System.out.println(sparqlQueryString1);
    Query query = QueryFactory.create(sparqlQueryString1);
    QueryExecution qexec = QueryExecutionFactory.sparqlService("URL A DEFINIR", query);
    ResultSet results = qexec.execSelect();
    while (results.hasNext()) {
        QuerySolution qs = results.next();
        NodeAlignment na = new NodeAlignment();
        na.setInternal_id_concept(idC);
        na.setInternal_id_thesaurus(idTheso);
        na.setConcept_target(qs.get("pl").toString());
        na.setDef_target(qs.get("def").toString());
        na.setThesaurus_target(source);
        na.setUri_target(qs.get("uri").toString());
        listeAlign.add(na);
    }
    qexec.close();
    return listeAlign;
}
Also used : NodeAlignment(mom.trd.opentheso.bdd.helper.nodes.NodeAlignment) 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

QueryExecution (org.apache.jena.query.QueryExecution)21 ResultSet (org.apache.jena.query.ResultSet)18 Query (org.apache.jena.query.Query)13 QuerySolution (org.apache.jena.query.QuerySolution)12 Test (org.junit.Test)7 Model (org.apache.jena.rdf.model.Model)5 NodeAlignment (mom.trd.opentheso.bdd.helper.nodes.NodeAlignment)4 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