@Override protected Set<OWLObjectPropertyAssertionAxiom> getExamples(ParameterizedSparqlString queryTemplate, EvaluatedAxiom<OWLObjectPropertyDomainAxiom> evAxiom) { OWLObjectPropertyDomainAxiom axiom = evAxiom.getAxiom(); queryTemplate.setIri("p", entityToDescribe.toStringID()); queryTemplate.setIri("type", axiom.getDomain().asOWLClass().toStringID()); Set<OWLObjectPropertyAssertionAxiom> examples = new TreeSet<>(); ResultSet rs = executeSelectQuery(queryTemplate.toString()); 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("o").getURI())); examples.add(df.getOWLObjectPropertyAssertionAxiom(entityToDescribe, subject, object)); } return examples; }
public Dataset retrieveNeedDataset(String uri) { String queryString = "prefix won: <http://purl.org/webofneeds/model#> select distinct ?g where { " + "GRAPH ?g { ?uri a won:Need. ?a ?b ?c. } }"; ParameterizedSparqlString pps = new ParameterizedSparqlString(); pps.setCommandText(queryString); pps.setIri("uri", uri); Query query = QueryFactory.create(pps.toString()); QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, query); ResultSet results = qexec.execSelect(); Dataset ds = DatasetFactory.createGeneral(); while (results.hasNext()) { QuerySolution qs = results.next(); String graphUri = qs.getResource("g").getURI(); Model model = retrieveModel(graphUri); ds.addNamedModel(graphUri, model); } return ds; }
/** * To start crawling (http modification query) from a certain point in time, take last * modification date from a need known in the database that is in status 'DONE' which means * it has been crawled. * * @param wonNodeUri won node uri for which need modification dates should be retrieved * @return modification date to start crawling from or null if none exists */ public String retrieveNeedModificationDateForCrawling(String wonNodeUri) { String queryString = "SELECT ?modificationDate WHERE {\n" + " ?needUri a won:Need.\n" + " ?needUri won:hasWonNode ?wonNodeUri. \n" + " ?needUri dcterms:modified ?modificationDate. \n" + " ?needUri won:crawlStatus 'DONE'. \n" + "} ORDER BY DESC(?modificationDate) LIMIT 1\n"; ParameterizedSparqlString pps = new ParameterizedSparqlString(); pps.setNsPrefix("won", "http://purl.org/webofneeds/model#"); pps.setNsPrefix("dcterms", "http://purl.org/dc/terms/"); pps.setCommandText(queryString); pps.setIri("wonNodeUri", wonNodeUri); QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, pps.asQuery()); ResultSet results = qexec.execSelect(); String modificationDate = null; if (results.hasNext()) { QuerySolution qs = results.nextSolution(); modificationDate = qs.get("modificationDate").asLiteral().getString(); } qexec.close(); return modificationDate; }
ParameterizedSparqlString queryStr = new SparqlParameterizedString(); queryStr.setNSPrefix("sw", "http://skunkworks.example.com/redacted#"); queryStr.append("SELECT ?a ?b ?c ?d"); queryStr.append("{"); queryStr.append(" ?rawHit sw:key"); queryStr.appendNode(someKey); queryStr.append("."); queryStr.append(" ?rawHit sw:a ?a ."); queryStr.append(" ?rawHit sw:b ?b ."); queryStr.append(" ?rawHit sw:c ?c . "); queryStr.append(" ?rawHit sw:d ?d ."); queryStr.append("} ORDER BY DESC(d)"); Query q = queryStr.asQuery();
/** * Update named graph by first deleting it and afterwards inserting the triples of the new model. * * @param graph named graph to be updated * @param model model that holds triples to set */ public String createUpdateNamedGraphQuery(String graph, Model model) { StringWriter sw = new StringWriter(); RDFDataMgr.write(sw, model, Lang.NTRIPLES); String query = "\nCLEAR GRAPH ?g;\n" + "\nINSERT DATA { GRAPH ?g { " + sw + "}};\n"; ParameterizedSparqlString pps = new ParameterizedSparqlString(); pps.setCommandText(query); pps.setIri("g", graph); return pps.toString(); }
protected int getPopularity() { POPULARITY_COUNT_QUERY.setIri("p", entityToDescribe.toStringID()); String query = POPULARITY_COUNT_QUERY.toString(); ResultSet rs = executeSelectQuery(query); int popularity = rs.next().getLiteral("cnt").getInt(); return popularity; }
@Override protected Set<OWLDataPropertyAssertionAxiom> getExamples(ParameterizedSparqlString queryTemplate, EvaluatedAxiom<OWLDataPropertyDomainAxiom> evAxiom) { OWLDataPropertyDomainAxiom axiom = evAxiom.getAxiom(); queryTemplate.setIri("p", entityToDescribe.toStringID()); queryTemplate.setIri("type", axiom.getDomain().asOWLClass().toStringID()); Set<OWLDataPropertyAssertionAxiom> examples = new TreeSet<>(); ResultSet rs = executeSelectQuery(queryTemplate.toString()); while (rs.hasNext()) { QuerySolution qs = rs.next(); OWLIndividual subject = df.getOWLNamedIndividual(IRI.create(qs.getResource("s").getURI())); OWLLiteral object = OwlApiJenaUtils.getOWLLiteral(qs.getLiteral("o")); examples.add(df.getOWLDataPropertyAssertionAxiom(entityToDescribe, subject, object)); } return examples; }
@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."); } } }
@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 )); } }
@Override protected void run() { // get the frequency for each datatype ResultSet rs = executeSelectQuery(DATATYPE_FREQUENCY_QUERY.toString()); while (rs.hasNext()) { QuerySolution qs = rs.next(); // datatype String datatypeURI = qs.getResource("dt").getURI(); // frequency of datatype int frequency = qs.getLiteral("cnt").getInt(); // precision (A AND B)/B double precision = Heuristics.getConfidenceInterval95WaldAverage(popularity, frequency); // score /* TODO: currently the score is rather simple, but it's not clear whether it makes sense to take also the total number of literals with given datatype into account */ double score = precision; int nrOfPosExamples = frequency; int nrOfNegExamples = popularity - nrOfPosExamples; currentlyBestAxioms.add(new EvaluatedAxiom<>( df.getOWLDataPropertyRangeAxiom(entityToDescribe, df.getOWLDatatype(IRI.create(datatypeURI))), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling))); } }
@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; }
ResultSet rs = executeSelectQuery(CLASS_OVERLAP_QUERY.toString()); ResultSetRewindable rsrw = ResultSetFactory.copyResults(rs); int size = rsrw.size(); rs = rsrw; while (rs.hasNext()) { QuerySolution qs = rsrw.next(); OWLClass candidate = df.getOWLClass(IRI.create(qs.getResource("cls_other").getURI())); logger.debug("Analyzing candidate class " + candidate.toStringID()); progressMonitor.learningProgressChanged(axiomType, rs.getRowNumber(), size); int overlap = qs.getLiteral("overlap").getInt(); df.getOWLDisjointClassesAxiom(entityToDescribe, candidate), new AxiomScore(score, score, nrOfPosExamples, nrOfNegExamples, useSampling)));
PROPERTY_OVERLAP_WITH_RANGE_QUERY.setIri("range", range.asOWLDatatype().toStringID()); query = PROPERTY_OVERLAP_WITH_RANGE_QUERY.toString(); } else { query = PROPERTY_OVERLAP_QUERY.toString(); query = PROPERTY_OVERLAP_QUERY.toString(); int size = rsrw.size(); rs = rsrw; while (rs.hasNext()) { QuerySolution qs = rsrw.next(); progressMonitor.learningProgressChanged(axiomType, rs.getRowNumber(), size); OWLDataProperty candidate = df.getOWLDataProperty(IRI.create(qs.getResource("p_other").getURI())); int overlap = qs.getLiteral("overlap").getInt();
/** * Retrieve resource data of all known won nodes that are saved in the Sparql endpoint. * * @return Set of all known won node resource data */ public Set<WonNodeInfo> retrieveAllWonNodeInfo() { Set<WonNodeInfo> wonNodeInfos = new HashSet<>(); String queryString = "SELECT ?graphUri ?nodeUri WHERE { GRAPH ?graphUri {?nodeUri won:hasUriPrefixSpecification ?c} }"; ParameterizedSparqlString pps = new ParameterizedSparqlString(); pps.setCommandText(queryString); pps.setNsPrefix("won", "http://purl.org/webofneeds/model#"); log.debug("Query SPARQL Endpoint: {}", sparqlEndpoint); log.debug("Execute query: {}", pps.toString()); QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, pps.asQuery()); ResultSet results = qexec.execSelect(); while (results.hasNext()) { QuerySolution qs = results.nextSolution(); RDFNode rdfNode = qs.get("graphUri"); if (rdfNode != null) { String graphUri = rdfNode.asResource().getURI(); Dataset ds = retrieveDataset(graphUri); WonNodeInfo nodeInfo = getWonNodeInfoFromDataset(ds); wonNodeInfos.add(nodeInfo); } } qexec.close(); return wonNodeInfos; }
public Collection<TensorEntry> generateTensorEntries() { ParameterizedSparqlString pss = new ParameterizedSparqlString(); pss.setCommandText(query); for (String key : parameterBindings.keySet()) { pss.setLiteral(key, (String) value); } else if (value instanceof Long || value instanceof Integer) { pss.setLiteral(key, (Long) value); } else if (value instanceof Float || value instanceof Double) { pss.setLiteral(key, (Double) value); } else { throw new IllegalArgumentException("Variable must be of type String/Long/Integer/Float/Double"); Query q = pss.asQuery(); QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, q); ResultSet results = qexec.execSelect(); if (!results.getResultVars().containsAll(Arrays.asList(variableNames))) { throw new DataIntegrityException("sparql query is expected to return variables: " + variableNames);
" LIMIT ?limit"; ParameterizedSparqlString pps = new ParameterizedSparqlString(); pps.setNsPrefix("won", "http://purl.org/webofneeds/model#"); pps.setCommandText(queryTemplate); pps.setLiteral("fromDate", fromDate); pps.setLiteral("toDate", toDate); pps.setLiteral("offset", offset); pps.setLiteral("limit", limit); log.debug("Execute query: {}", pps.toString()); QueryExecution qexec = QueryExecutionFactory.sparqlService(sparqlEndpoint, pps.asQuery()); ResultSet results = qexec.execSelect(); while (results.hasNext()) { QuerySolution qs = results.nextSolution(); String needUri = qs.get("needUri").asResource().getURI(); String wonNodeUri = qs.get("wonNodeUri").asResource().getURI(); long crawlDate = qs.getLiteral("date").getLong();
@Test public void test_param_string_boolean_3() { // Test invalid boolean injection String cmdText = "SELECT * WHERE { ?s ?p ?o }"; ParameterizedSparqlString query = new ParameterizedSparqlString(cmdText); query.setLiteral("o", "xyz", TypeMapper.getInstance().getSafeTypeByName(XSD.xboolean.toString())); // We expect #boolean as booleans with invalid lexical values should not // be formatted as plain literals test(query, new String[] { "xyz", XSD.xboolean.toString() }, new String[] { "?o" }); }
Resource bnode = model.createResource(); bnode.addProperty(RDF.type, OWL.Thing); Assert.assertEquals(1, model.size()); ParameterizedSparqlString pq = new ParameterizedSparqlString(); pq.setCommandText("SELECT * WHERE { ?s ?p ?o }"); pq.setIri("s", "_:" + bnode.getId()); Query q = pq.asQuery(); try(QueryExecution qe = QueryExecutionFactory.create(q, ds)) { ResultSet rset = qe.execSelect(); ParameterizedSparqlString s = new ParameterizedSparqlString(); s.setCommandText("INSERT { ?o ?p ?s } WHERE { ?s ?p ?o }"); s.setIri("s", "_:" + bnode.getId()); UpdateRequest query = s.asUpdate(); UpdateProcessor proc = UpdateExecutionFactory.create(query, ds); proc.execute(); Assert.assertEquals(1, model.listStatements(bnode, null, (RDFNode) null).toList().size());
private Set<Node> getSuperClasses(Node cls){ Set<Node> superClasses = new HashSet<>(); superClassesQueryTemplate.setIri("sub", cls.getURI()); String query = superClassesQueryTemplate.toString(); QueryExecution qe = qef.createQueryExecution(query); ResultSet rs = qe.execSelect(); while(rs.hasNext()){ QuerySolution qs = rs.next(); superClasses.add(qs.getResource("sup").asNode()); } qe.close(); return superClasses; }
private void runBatched() { ParameterizedSparqlString template = strictOWLMode ? CLASS_OVERLAP_BATCH_QUERY_STRICT_OWL : CLASS_OVERLAP_BATCH_QUERY; template.setIri("cls", entityToDescribe.toStringID()); ResultSet rs = executeSelectQuery(template.toString()); while (rs.hasNext()) { QuerySolution qs = rs.next(); RDFNode cls = qs.get("cls_other"); if (!cls.isAnon()) { OWLClass sup = OwlApiJenaUtils.asOWLEntity(cls.asNode(), EntityType.CLASS); int overlap = qs.get("cnt").asLiteral().getInt(); if (!sup.isOWLThing() && !entityToDescribe.equals(sup)) {//omit owl:Thing and the class to describe itself currentlyBestEvaluatedDescriptions.add(new EvaluatedDescription(sup, computeScore(popularity, overlap))); } } else { logger.warn("Ignoring anonymous super class candidate: " + cls); } } }