Search in sources :

Example 96 with QuerySolution

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

the class GenericPropertyFunctionTest method testExecEvaluated_unbound.

/**
 * Test of execEvaluated method, of class GenericPropertyFunction.
 */
@Test
public void testExecEvaluated_unbound() {
    String query = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n" + "\n" + "SELECT ?subj ?obj\n" + "WHERE{\n" + "    ?subj geo:sfContains ?obj .\n" + "}ORDER by ?subj ?obj";
    List<Resource> subjects = new ArrayList<>();
    List<Resource> objects = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Resource subject = qs.getResource("subj");
            subjects.add(subject);
            Resource object = qs.getResource("obj");
            objects.add(object);
        }
    }
    // Blank nodes limit a value check.
    boolean expResult = true;
    boolean result = subjects.size() == 25 && objects.size() == 25;
    // 
    // 
    assertEquals(expResult, result);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 97 with QuerySolution

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

the class GenericPropertyFunctionTest method testExecEvaluated_subject_bound_geometry.

/**
 * Test of execEvaluated method, of class GenericPropertyFunction.
 */
@Test
public void testExecEvaluated_subject_bound_geometry() {
    String query = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n" + "\n" + "SELECT ?obj\n" + "WHERE{\n" + "    BIND(<http://example.org#GeometryA> AS ?subj) \n" + "    ?subj geo:sfContains ?obj .\n" + "}ORDER by ?obj";
    List<Resource> objects = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Resource result = qs.getResource("obj");
            objects.add(result);
        }
    }
    // Blank nodes limit a value check.
    int expResult = 7;
    int result = objects.size();
    assertEquals(expResult, result);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 98 with QuerySolution

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

the class GenericPropertyFunctionTest method testExecEvaluated_both_bound_geo.

/**
 * Test of execEvaluated method, of class GenericPropertyFunction.
 */
@Test
public void testExecEvaluated_both_bound_geo() {
    String query = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n" + "\n" + "SELECT ?subj ?obj\n" + "WHERE{\n" + "    BIND(<http://example.org#GeoFeatureY> AS ?subj) \n" + "    BIND(<http://example.org#GeoFeatureZ> AS ?obj) \n" + "    ?subj geo:sfContains ?obj .\n" + "}ORDER by ?subj ?obj";
    List<Resource> subjects = new ArrayList<>();
    List<Resource> objects = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Resource subject = qs.getResource("subj");
            subjects.add(subject);
            Resource object = qs.getResource("obj");
            objects.add(object);
        }
    }
    boolean expResult = true;
    List<Resource> expSubjects = Arrays.asList(GEO_FEATURE_Y);
    List<Resource> expObjects = Arrays.asList(GEO_FEATURE_Z);
    boolean result = subjects.equals(expSubjects) && objects.equals(expObjects);
    assertEquals(expResult, result);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

Example 99 with QuerySolution

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

the class GenericPropertyFunctionTest method testExecEvaluated_subject_bound.

/**
 * Test of execEvaluated method, of class GenericPropertyFunction.
 */
@Test
public void testExecEvaluated_subject_bound() {
    String query = "PREFIX geo: <http://www.opengis.net/ont/geosparql#>\n" + "\n" + "SELECT ?obj\n" + "WHERE{\n" + "    BIND(<http://example.org#FeatureA> AS ?subj) \n" + "    ?subj geo:sfContains ?obj .\n" + "}ORDER by ?obj";
    List<Resource> objects = new ArrayList<>();
    try (QueryExecution qe = QueryExecutionFactory.create(query, dataset)) {
        ResultSet rs = qe.execSelect();
        while (rs.hasNext()) {
            QuerySolution qs = rs.nextSolution();
            Resource result = qs.getResource("obj");
            objects.add(result);
        }
    }
    // Blank nodes limit a value check.
    int expResult = 6;
    int result = objects.size();
    // 
    assertEquals(expResult, result);
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) Resource(org.apache.jena.rdf.model.Resource) ArrayList(java.util.ArrayList) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution) Test(org.junit.Test)

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

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