Search in sources :

Example 91 with QuerySolution

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

the class TestTextMultilingualEnhancements method doTestSearchWithLiterals.

protected Map<String, Literal> doTestSearchWithLiterals(String queryString, Set<String> expectedEntityURIs) {
    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("lit");
            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) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 92 with QuerySolution

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

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

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

the class FusekiConfig method readSystemDatabase.

// ---- System database
/**
 * Read the system database
 */
public static List<DataAccessPoint> readSystemDatabase(Dataset ds) {
    // Webapp only.
    DatasetDescriptionMap dsDescMap = new DatasetDescriptionMap();
    String qs = StrUtils.strjoinNL(FusekiPrefixes.PREFIXES, "SELECT * {", "  GRAPH ?g {", "     ?s fu:name ?name;", "        fu:status ?status .", "  }", "}");
    List<DataAccessPoint> refs = new ArrayList<>();
    ds.begin(ReadWrite.WRITE);
    try {
        ResultSet rs = BuildLib.query(qs, ds);
        for (; rs.hasNext(); ) {
            QuerySolution row = rs.next();
            Resource s = row.getResource("s");
            Resource g = row.getResource("g");
            Resource rStatus = row.getResource("status");
            // String name = row.getLiteral("name").getLexicalForm();
            DataServiceStatus status = DataServiceStatus.status(rStatus);
            Model m = ds.getNamedModel(g.getURI());
            // Rebase the resource of the service description to the containing graph.
            Resource svc = m.wrapAsResource(s.asNode());
            DataAccessPoint ref = buildDataAccessPoint(svc, dsDescMap);
            if (ref != null)
                refs.add(ref);
        }
        ds.commit();
        return refs;
    } catch (Throwable th) {
        th.printStackTrace();
        return refs;
    } finally {
        ds.end();
    }
}
Also used : QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet)

Example 95 with QuerySolution

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

the class FusekiConfig method accEndpointOldStyle.

// Old style.
// fuseki:serviceQuery "sparql";
// or
// fuseki:serviceQuery [ fuseki:name "sparql" ; fuseki:allowedUsers (..) ];
private static void accEndpointOldStyle(Collection<Endpoint> endpoints, Operation operation, Resource svc, Property property) {
    String p = "<" + property.getURI() + ">";
    ResultSet rs = BuildLib.query("SELECT * { ?svc " + p + " ?ep}", svc.getModel(), "svc", svc);
    for (; rs.hasNext(); ) {
        QuerySolution soln = rs.next();
        // No policy yet - set below if one is found.
        AuthPolicy authPolicy = null;
        RDFNode ep = soln.get("ep");
        String endpointName = null;
        if (ep.isLiteral())
            // fuseki:serviceQuery "sparql"
            endpointName = soln.getLiteral("ep").getLexicalForm();
        else if (ep.isResource()) {
            Resource r = (Resource) ep;
            try {
                // [ fuseki:name ""; fuseki:allowedUsers ( "" "" ) ]
                Statement stmt = r.getProperty(FusekiVocab.pEndpointName);
                if (stmt == null)
                    throw new FusekiConfigException("Expected property <" + FusekiVocab.pEndpointName + "> with <" + property.getURI() + "> for <" + svc + ">");
                endpointName = stmt.getString();
                List<RDFNode> x = GraphUtils.multiValue(r, FusekiVocab.pAllowedUsers);
                if (x.size() > 1)
                    throw new FusekiConfigException("Multiple fuseki:" + FusekiVocab.pAllowedUsers.getLocalName() + " for " + r);
                if (!x.isEmpty())
                    authPolicy = allowedUsers(r);
            } catch (JenaException | ClassCastException ex) {
                throw new FusekiConfigException("Failed to parse endpoint: " + r);
            }
        } else {
            throw new FusekiConfigException("Unrecognized: " + ep);
        }
        if (StringUtils.isEmpty(endpointName))
            endpointName = null;
        Endpoint endpoint = Endpoint.create(operation, endpointName, authPolicy);
        endpoints.add(endpoint);
    }
}
Also used : FusekiConfigException(org.apache.jena.fuseki.FusekiConfigException) QuerySolution(org.apache.jena.query.QuerySolution) AuthPolicy(org.apache.jena.fuseki.auth.AuthPolicy) ResultSet(org.apache.jena.query.ResultSet) Collectors.toList(java.util.stream.Collectors.toList) AuthPolicyList(org.apache.jena.fuseki.auth.AuthPolicyList)

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