private Set<OWLIndividual> getRandomIndividuals(OWLClass cls){ Set<OWLIndividual> individuals = new HashSet<>(); String query = "SELECT ?s WHERE {?s a <" + cls.toStringID() + ">} LIMIT " + maxNrOfIndividuals; QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); QuerySolution qs; while(rs.hasNext()){ qs = rs.next(); if(qs.get("s").isURIResource()){ individuals.add(df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI()))); } } qe.close(); return individuals; } }
@Override public Map<OWLIndividual, SortedSet<OWLLiteral>> getDatatypeMembersImpl(OWLDataProperty dataProperty) { Map<OWLIndividual, SortedSet<OWLLiteral>> subject2objects = new HashMap<>(); String query = String.format(SPARQLQueryUtils.SELECT_PROPERTY_RELATIONSHIPS_QUERY, dataProperty.toStringID()); ResultSet rs = executeSelectQuery(query); while(rs.hasNext()){ QuerySolution qs = rs.next(); OWLIndividual sub = df.getOWLNamedIndividual(IRI.create(qs.getResource("var1").getURI())); OWLLiteral obj = OwlApiJenaUtils.getOWLLiteral(qs.getLiteral("var2")); subject2objects.computeIfAbsent(sub, k -> new TreeSet<>()).add(obj); } return subject2objects; }
@Override protected void getExistingAxioms() { ResultSet rs = executeSelectQuery(existingAxiomsTemplate.toString()); QuerySolution qs; while (rs.hasNext()) { qs = rs.next(); if(qs.get("cls_dis").isResource()){ OWLClass disjointClass = df.getOWLClass(IRI.create(qs.getResource("cls_dis").getURI())); existingAxioms.add(df.getOWLDisjointClassesAxiom(entityToDescribe, disjointClass)); } else { logger.warn("We do not support complex disjoint classes."); } } }
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 SortedSet<OWLClass> getMostSpecificClasses() { SortedSet<OWLClass> classes = new TreeSet<>(); String query = "SELECT ?cls WHERE {?cls a <http://www.w3.org/2002/07/owl#Class>. " + "FILTER NOT EXISTS{?sub <http://www.w3.org/2000/01/rdf-schema#subClassOf> ?cls. FILTER(?sub != <http://www.w3.org/2002/07/owl#Nothing>)}}"; ResultSet rs = executeSelectQuery(query); QuerySolution qs; while(rs.hasNext()){ qs = rs.next(); classes.add(df.getOWLClass(IRI.create(qs.getResource("cls").getURI()))); } return classes; }
@Override public Map<OWLIndividual, SortedSet<Double>> getDoubleDatatypeMembersImpl(OWLDataProperty datatypeProperty) { Map<OWLIndividual, SortedSet<Double>> subject2objects = new HashMap<>(); String query = "SELECT ?s ?o WHERE {" + String.format("?s <%s> ?o.", datatypeProperty.toStringID()) + " FILTER(" + datatypeSparqlFilter(OWLAPIUtils.floatDatatypes) + ")}"; ResultSet rs = executeSelectQuery(query); while(rs.hasNext()){ QuerySolution qs = rs.next(); OWLIndividual sub = df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI())); Literal val = qs.getLiteral("o").asLiteral(); // Virtuoso bug workaround which returns NAN instead of NaN Double obj = "NAN".equals(val.getLexicalForm()) ? Double.NaN : val.getDouble(); subject2objects.computeIfAbsent(sub, k -> new TreeSet<>()).add(obj); } return subject2objects; }
private RDFResourceTree getSimilarTree(RDFResourceTree tree, String property, int maxTreeDepth){ String query = "SELECT ?o WHERE {?s <" + property + "> ?o. FILTER(isURI(?o) && ?o != <" + tree.getData() + ">)} LIMIT 1"; QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); if(rs.hasNext()){ Resource object = rs.next().getResource("o"); Model cbd = cbdGen.getConciseBoundedDescription(object.getURI(), maxTreeDepth); RDFResourceTree similarTree = queryTreeFactory.getQueryTree(object, cbd, maxTreeDepth); similarTree.setData(object.asNode()); return similarTree; } return null; }
@Override protected void run() { ResultSet rs = executeSelectQuery(QUERY.toString()); QuerySolution qs; while (rs.hasNext()) { qs = rs.next(); // candidate OWLObjectProperty candidate = df.getOWLObjectProperty(IRI.create(qs.getResource("p_inv").getURI())); // frequency int frequency = qs.getLiteral("cnt").getInt(); // score AxiomScore score = computeScore(popularity, frequency, useSampling); currentlyBestAxioms.add(new EvaluatedAxiom<>(df .getOWLInverseObjectPropertiesAxiom(entityToDescribe, candidate), score )); } }
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; }
ResultSet rs = qexec.execSelect(); while (rs.hasNext()) { QuerySolution qs = rs.next(); if (qs.contains("contentNode")) { contentNodes.add(qs.get("contentNode").asResource());
@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; }
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))); }
Model model = root.getModel() ; "}") ; ParameterizedSparqlString pss = new ParameterizedSparqlString(qs1) ; pss.setIri("eMap", root.getURI()) ; ResultSet rs1 = qexec1.execSelect() ; List<QuerySolution> results = ResultSetFormatter.toList(rs1) ; if ( results.size() == 0 ) { String entityField = qsol1.getLiteral("entityField").getLexicalForm() ; String graphField = qsol1.contains("graphField") ? qsol1.getLiteral("graphField").getLexicalForm() : null; String langField = qsol1.contains("langField") ? qsol1.getLiteral("langField").getLexicalForm() : null; String defaultField = qsol1.contains("dftField") ? qsol1.getLiteral("dftField").getLexicalForm() : null ; String uniqueIdField = qsol1.contains("uidField") ? qsol1.getLiteral("uidField").getLexicalForm() : null; Map<String, Boolean> noIndexDefs = new HashMap<>(); Statement listStmt = root.getProperty(TextVocab.pMap); while (listStmt != null) { RDFNode n = listStmt.getObject();
QueryExecution qexec = QueryExecutionFactory.create(query, model); try { ResultSet results = qexec.execSelect(); while (results.hasNext()) { QuerySolution solution = results.nextSolution(); if (solution.contains("cause")) { RDFNode node = solution.get("cause"); if (node.isResource()) { return node.asResource().getURI(); } else if (node.isLiteral()) { return node.asLiteral().getString(); qexec.close();
private Map<String, Long> getStats(String sparqlQuery, QueryExecutionFactory qef) { Map<String, Long> stats = new HashMap<>(); try (QueryExecution qe = qef.createQueryExecution(sparqlQuery)) { qe.execSelect().forEachRemaining( qs -> { String s = qs.get("stats").toString(); int c = 0; if (qs.contains("count")) { c = qs.get("count").asLiteral().getInt(); } stats.put(s, (long) c); }); } return stats; } }
QueryExecution qe = AGQueryExecutionFactory.create(sparql, model); try { ResultSet results = qe.execSelect(); assertEquals(results.getResourceModel(), model); assertEquals(results.getResultVars().size(), 3); while (results.hasNext()) { QuerySolution result = results.next(); RDFNode s = result.get("s"); RDFNode p = result.get("p"); RDFNode o = result.get("o"); RDFNode g = result.get("g"); assertTrue(s.isResource()); assertTrue(p.isResource()); Iterator<String> vars = result.varNames(); while (vars.hasNext()) { assertTrue(result.contains(vars.next())); Resource s = result.getResource("s"); Resource p = result.getResource("p"); try { result.getResource("o"); fail(); } catch (Exception e) { Literal o = result.getLiteral("o"); Resource g = result.getResource("g"); assertTrue(s.isResource()); assertTrue(p.isResource());
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; }
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 static void updateGoldenAnswers(QueryExecutionFactory qef, IQuestion q) { Set<String> uris = new HashSet<>(); try(QueryExecution qe = qef.createQueryExecution(q.getSparqlQuery())) { ResultSet rs = qe.execSelect(); while(rs.hasNext()) { QuerySolution qs = rs.next(); RDFNode node = qs.get("uri"); if(node != null && node.isResource()) { uris.add(node.asResource().getURI()); } } } q.setGoldenAnswers(uris); }