public Set<OWLObjectProperty> getAllObjectProperties() { Set<OWLObjectProperty> properties = new TreeSet<>(); String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p WHERE {?p a owl:ObjectProperty}"; SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); ResultSet q = sq.send(false); while (q.hasNext()) { QuerySolution qs = q.next(); properties.add(df.getOWLObjectProperty(IRI.create(qs.getResource("p").getURI()))); } return properties; }
public SortedResultSet(final ResultSet results, final List<SortCondition> sortConditions) { resultVars = results.getResultVars(); sortedRows = new ArrayList<>(); while (results.hasNext()) sortedRows.add(results.nextBinding()); final BindingComparator cmp = new BindingComparator(sortConditions); Collections.sort(sortedRows, cmp); iterator = sortedRows.iterator(); row = 0; }
/** * Execute, expecting the result to be one row, one column. Return that one. * RDFNode or throw an exception. * Use with {@code try ( QueryExecution qExec = ....)}. */ public static RDFNode getExactlyOne(QueryExecution qExec, String varname) { ResultSet rs = qExec.execSelect() ; if ( !rs.hasNext() ) throw new ARQException("Not found: var ?" + varname) ; QuerySolution qs = rs.nextSolution() ; RDFNode r = qs.get(varname) ; if ( rs.hasNext() ) throw new ARQException("More than one: var ?" + varname) ; return r ; }
private List<QuerySolution> getSolutionList(ResultSet resultSet) { List<QuerySolution> solnList = new ArrayList<QuerySolution>(); while (resultSet.hasNext()) { QuerySolution soln = resultSet.nextSolution(); solnList.add(soln); } return solnList; }
public Set<OWLClass> getTypes(String namespace, boolean omitEmptyTypes) { Set<OWLClass> types = new TreeSet<>(); String query = "SELECT DISTINCT ?cls WHERE {[] a ?cls ." + (omitEmptyTypes ? "[] a ?cls ." : "" ) + (namespace != null ? ("FILTER(REGEX(?cls,'^" + namespace + "'))") : "") + "}"; ResultSet rs = executeSelectQuery(query); while(rs.hasNext()){ QuerySolution qs = rs.next(); types.add(df.getOWLClass(IRI.create(qs.getResource("cls").getURI()))); } return types; }
@Override public Set<OWLIndividual> getRelatedIndividualsImpl(OWLIndividual individual, OWLObjectProperty objectProperty) { Set<OWLIndividual> individuals = new HashSet<>(); String query = String.format("SELECT ?ind WHERE {<%s> <%s> ?ind, FILTER(isIRI(?ind))}", individual.toStringID(), objectProperty.toStringID()); ResultSet rs = executeSelectQuery(query); while(rs.hasNext()){ QuerySolution qs = rs.next(); individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("ind").getURI()))); } return individuals; }
public static Resource getPropertyResource(Resource uri){ String whereClause = "?prop " + " " + SPARQLHelper.toSPARQL(RDFS.range) + SPARQLHelper.toSPARQL(uri) + " . "; Model m = InternalModelConf.getFlatModel(); String query = SPARQLHelper.SELECT_STATEMENT.replace("[variables]", "?prop").replace("[whereClauses]", whereClause); Resource r = null; Query qry = QueryFactory.create(query); QueryExecution qe = QueryExecutionFactory.create(qry, m); ResultSet rs = qe.execSelect(); while (rs.hasNext()){ r = rs.next().get("prop").asResource(); } return r; }
public static Iterator<Node> storeGraphNames(Store store) { List<Node> x = new ArrayList<Node>() ; String qs = "SELECT ?g { GRAPH ?g { }}" ; QueryExecution qExec = QueryExecutionFactory.create(qs, SDBFactory.connectDataset(store)) ; ResultSet rs = qExec.execSelect() ; Var var_g = Var.alloc("g") ; while(rs.hasNext()) { Node n = rs.nextBinding().get(var_g) ; x.add(n) ; } return x.iterator() ; }
@Override public Set<OWLObjectPropertyAssertionAxiom> getPositiveExamples(EvaluatedAxiom<T> evAxiom) { T axiom = evAxiom.getAxiom(); posExamplesQueryTemplate.setIri("p", axiom.getProperty().asOWLObjectProperty().toStringID()); Set<OWLObjectPropertyAssertionAxiom> posExamples = new TreeSet<>(); ResultSet rs = executeSelectQuery(posExamplesQueryTemplate.toString()); List<String> vars = rs.getResultVars(); boolean onlySubject = vars.size() == 1; while (rs.hasNext()) { QuerySolution qs = rs.next(); OWLIndividual subject = df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI())); OWLIndividual object = df.getOWLNamedIndividual(IRI.create(qs.getResource(onlySubject ? "s" : "o").getURI())); posExamples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, subject, object)); } return posExamples; }
public Set<OWLDataProperty> getAllDataProperties() { Set<OWLDataProperty> properties = new TreeSet<>(); String query = "PREFIX owl: <http://www.w3.org/2002/07/owl#> SELECT ?p WHERE {?p a owl:DatatypeProperty}"; SparqlQuery sq = new SparqlQuery(query, sparqlEndpoint); ResultSet q = sq.send(false); while (q.hasNext()) { QuerySolution qs = q.next(); properties.add(df.getOWLDataProperty(IRI.create(qs.getResource("p").getURI()))); } return properties; }
private Set<OWLIndividual> computePosExamples(OWLClass cls) { List<OWLIndividual> posExamples = new ArrayList<>(); String query = String.format("SELECT ?s WHERE {?s a <%s>}", cls.toStringID()); try(QueryExecution qe = qef.createQueryExecution(query)) { ResultSet rs = qe.execSelect(); while(rs.hasNext()) { QuerySolution qs = rs.next(); posExamples.add(new OWLNamedIndividualImpl(IRI.create(qs.getResource("s").getURI()))); } } Collections.shuffle(posExamples, rnd); return new TreeSet<>(posExamples.subList(0, Math.min(posExamples.size(), maxNrOfPosExamples))); }
@SuppressWarnings("unchecked") protected Set<S> getExamples(ParameterizedSparqlString queryTemplate, EvaluatedAxiom<T> evAxiom) { ResultSet rs = executeSelectQuery(queryTemplate.toString()); Set<OWLObject> negExamples = new TreeSet<>(); while(rs.hasNext()){ RDFNode node = rs.next().get("s"); if(node.isResource()){ negExamples.add(df.getOWLNamedIndividual(IRI.create(node.asResource().getURI()))); } else if(node.isLiteral()){ negExamples.add(convertLiteral(node.asLiteral())); } } return (Set<S>) negExamples; }
@Test public void testReuseQueryObject2() { String queryString = "SELECT (count(?o) AS ?c) {?s ?p ?o} GROUP BY ?s"; Query q = QueryFactory.create(queryString) ; try(QueryExecution qExec = QueryExecutionFactory.create(q, m)) { ResultSet rs = qExec.execSelect() ; QuerySolution qs = rs.nextSolution() ; assertEquals(3, qs.getLiteral("c").getInt()) ; } try(QueryExecution qExec = QueryExecutionFactory.create(q, m)) { ResultSet rs = qExec.execSelect() ; QuerySolution qs = rs.nextSolution() ; assertEquals(3, qs.getLiteral("c").getInt()) ; } }
private SortedSet<String> getAllResources(String query){ SortedSet<String> resources = new TreeSet<>(); query = query + " LIMIT 1000"; QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); QuerySolution qs; while(rs.hasNext()){ qs = rs.next(); resources.add(qs.getResource("x0").getURI()); } qe.close(); return resources; }
@Override public SPARQLExecutionResult call() { Map<String, Set<String>> resultSet = new HashMap<>(); markers.forEach(marker -> resultSet.put(marker, new HashSet<>())); Model unionModel = ModelFactory.createDefaultModel(); SPARQLServiceConverter converter = new SPARQLServiceConverter(schema); String sparqlQuery = converter.getSelectQuery(query, inputSubset, rootType); logger.info(sparqlQuery); Query jenaQuery = QueryFactory.create(sparqlQuery); QueryExecution qexec = QueryExecutionFactory.create(jenaQuery, model); ResultSet results = qexec.execSelect(); results.forEachRemaining(solution -> { markers.forEach(marker ->{ if(solution.contains(marker)) { resultSet.get(marker).add(solution.get(marker).asResource().getURI()); } }); Model model = this.sparqlEndpointService.getModelFromResults(query, solution, schema); unionModel.add(model); }); return new SPARQLExecutionResult(resultSet, unionModel); }
private int getKBSize() { String query = "SELECT (COUNT(*) AS ?cnt) WHERE {[] a ?type . ?type a <http://www.w3.org/2002/07/owl#Class> .}"; QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); int size = rs.next().get("cnt").asLiteral().getInt(); qe.close(); return size; }
private Collection<String> getReferenceSet(QueryExecutionFactory model, String query) { Collection<String> references = new ArrayList<>(); Query q = QueryFactory.create(query); try (QueryExecution qe = model.createQueryExecution(q)) { qe.execSelect().forEachRemaining( row -> references.add("<" + row.get("reference").toString() + ">") ); } return references; }
@Test public void testInitialBindings7() { // JENA-1354 Query query = QueryFactory.create("SELECT DISTINCT ?x WHERE {}"); Dataset ds = DatasetFactory.create(); QuerySolutionMap initialBinding = new QuerySolutionMap(); initialBinding.add("a", ResourceFactory.createTypedLiteral(Boolean.TRUE)); try ( QueryExecution qexec = QueryExecutionFactory.create(query, ds, initialBinding) ) { assertFalse(qexec.execSelect().next().contains("a")); } }
public int getPropertyFrequency(String property) { // number of triples with predicate String query = String.format("SELECT (COUNT(*) AS ?cnt) WHERE {?s <%s> ?o .}", property); QueryExecution qe = qef.createQueryExecution(query); int frequency = qe.execSelect().next().getLiteral("cnt").getInt(); qe.close(); return frequency; }
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() ; } }