本文整理汇总了Java中org.apache.jena.query.QueryExecution类的典型用法代码示例。如果您正苦于以下问题:Java QueryExecution类的具体用法?Java QueryExecution怎么用?Java QueryExecution使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
QueryExecution类属于org.apache.jena.query包,在下文中一共展示了QueryExecution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listVocabularies
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
public static Set<String> listVocabularies() {
Set<String> tds = new HashSet<>();
Dataset dataset = ThingDirectory.get().dataset;
dataset.begin(ReadWrite.READ);
try {
String q = "SELECT DISTINCT ?g WHERE { GRAPH ?g { ?o a <http://www.w3.org/2002/07/owl#Ontology> } }";
QueryExecution qexec = QueryExecutionFactory.create(q, dataset);
ResultSet result = qexec.execSelect();
while (result.hasNext()) {
tds.add(result.next().get("g").asResource().getURI());
}
} catch (Exception e) {
throw e;
} finally {
dataset.end();
}
return tds;
}
示例2: getLabel
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
/**
* Return the label for this object in the selected language
* @param language - the language for which the label is requested
* @return - the label in the requested language.
* @throws ModelException - thrown if there are multiple labels present for this object in this language
*/
public Label getLabel(Language language) throws ModelException {
String sparql = "SELECT ?label WHERE { ?objectURI rdfs:label ?label . FILTER(LANG(?label) = STR(?labelLanguage)) }";
ParameterizedSparqlString parameterizedSparql = new ParameterizedSparqlString(model);
parameterizedSparql.setCommandText(sparql);
parameterizedSparql.setParam("objectURI", resource);
parameterizedSparql.setParam("labelLanguage", model.createLiteral(language.getCode(), ""));
Query query = QueryFactory.create(parameterizedSparql.asQuery());
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet resultSet = qexec.execSelect();
if (!resultSet.hasNext()) return null;
QuerySolution querySolution = resultSet.next();
Label label = new Label(querySolution.getLiteral("label"));
if (!resultSet.hasNext()) return label;
throw new ModelException("%s has more than one label in language '%s'", resource.getURI(), language.getCode());
}
示例3: get
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
@Override
public RESTResource get(URI uri, Map<String, String> parameters) throws RESTException {
RESTResource resource = new RESTResource(uri.toString(),this);
Dataset dataset = ThingDirectory.get().dataset;
dataset.begin(ReadWrite.READ);
try {
String q = "SELECT ?str WHERE { <" + uri + "> <" + DC.source + "> ?str }";
QueryExecution qexec = QueryExecutionFactory.create(q, dataset);
ResultSet result = qexec.execSelect();
if (result.hasNext()) {
resource.contentType = "application/ld+json";
resource.content = result.next().get("str").asLiteral().getLexicalForm();
} else {
throw new RESTException();
}
} finally {
dataset.end();
}
return resource;
}
示例4: listThingDescriptions
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
public static List<String> listThingDescriptions(String query) {
List<String> tds = new ArrayList<>();
Dataset dataset = ThingDirectory.get().dataset;
dataset.begin(ReadWrite.READ);
try {
String q = "SELECT DISTINCT ?g WHERE { GRAPH ?g { " + query + " FILTER NOT EXISTS { ?ontology a <http://www.w3.org/2002/07/owl#Ontology> } } }";
try (QueryExecution qexec = QueryExecutionFactory.create(q, dataset)) {
ResultSet result = qexec.execSelect();
while (result.hasNext()) {
tds.add(result.next().get("g").asResource().getURI());
}
}
catch (Exception e) {
throw e;
}
} finally {
dataset.end();
}
return tds;
}
示例5: getThingDescriptionIdFromUri
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
/**
* Returns the ID of a thing description stored in the database given its URI.
* @param uri URI of the thing description we want to return.
* @return the ID of the thing description.
*/
public static String getThingDescriptionIdFromUri(String uri) {
String query = "?td <http://iot.linkeddata.es/def/wot#baseURI> <" + uri + ">";
String id = "NOT FOUND";
Dataset dataset = ThingDirectory.get().dataset;
dataset.begin(ReadWrite.READ);
try {
String q = "SELECT ?g_id WHERE { GRAPH ?g_id { " + query + " }}";
QueryExecution qexec = QueryExecutionFactory.create(q, dataset);
ResultSet result = qexec.execSelect();
while (result.hasNext()) {
id = result.next().get("g_id").toString();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
dataset.end();
}
return id;
}
示例6: listThingDescriptionsUri
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
/**
* Returns a list of the thing descriptions URIs.
* @return a list of URIs stored in the database.
*/
public static List<String> listThingDescriptionsUri() {
List<String> tds = new ArrayList<>();
String query = "?td <http://iot.linkeddata.es/def/wot#baseURI> ?uri";
Dataset dataset = ThingDirectory.get().dataset;
dataset.begin(ReadWrite.READ);
try {
String q = "SELECT ?uri WHERE { GRAPH ?g_id { " + query + " }}";
try (QueryExecution qexec = QueryExecutionFactory.create(q, dataset)) {
ResultSet result = qexec.execSelect();
while (result.hasNext()) {
tds.add(result.next().get("uri").toString());
}
}
catch (Exception e) {
throw e;
}
} finally {
dataset.end();
}
return tds;
}
示例7: getProperties
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
private static List<Pair<String, String>> getProperties(String uri) {
List<Pair<String, String>> results = new ArrayList<>();
String sparqlQuery = "select ?r ?y where {<" + uri + "> ?r ?y}";
//System.out.println(sparqlQuery);
QueryExecution e = QueryExecutionFactory.sparqlService(ENDPOINT, sparqlQuery);
ResultSet rs = e.execSelect();
while (rs.hasNext()) {
QuerySolution nextSolution = rs.nextSolution();
RDFNode ynode = nextSolution.get("y");
if (ynode.isResource()) {
results.add(Pair.of(nextSolution.getResource("r").getURI(), nextSolution.getResource("y").getURI()));
} else {
results.add(Pair.of(nextSolution.getResource("r").getURI(), nextSolution.getLiteral("y").getString().replaceAll("\\n+", " ")));
}
}
e.close();
return results;
}
示例8: setPersonTypes
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
public void setPersonTypes(Set<String> personTypes){
this.personTypes = personTypes;
//get the inferred sub types as well
if(useInference){
Set<String> inferredTypes = new HashSet<>();
String queryTemplate = "select ?sub where{?sub <http://www.w3.org/2000/01/rdf-schema#subClassOf>* <%s>.}";
for (String type : personTypes) {
String query = String.format(queryTemplate, type);
try(QueryExecution qe = qef.createQueryExecution(query)) {
ResultSet rs = qe.execSelect();
while(rs.hasNext()){
inferredTypes.add(rs.next().getResource("sub").getURI());
}
}
}
personTypes.addAll(inferredTypes);
}
}
示例9: isPerson
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
private boolean isPerson(String uri){
if(personTypes.isEmpty()){
return true;
} else {
//g et types of URI
Set<String> types = new HashSet<>();
try {
String query = "SELECT ?type WHERE {<" + uri + "> a ?type.}";
try(QueryExecution qe = qef.createQueryExecution(query)) {
ResultSet rs = qe.execSelect();
while(rs.hasNext()){
types.add(rs.next().getResource("type").getURI());
}
}
} catch (Exception e) {
int code = ((QueryExceptionHTTP)e.getCause()).getResponseCode();
logger.warn("SPARQL query execution failed: " + code + " - " + HttpSC.getCode(code).getMessage());
}
// check for overlap between types of entity and person types
return !Sets.intersection(personTypes, types).isEmpty();
}
}
示例10: getCount
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
public static int getCount(String q, Model m) {
Query query = QueryFactory.create(q);
QueryExecution queryExec = QueryExecutionFactory.create(query, m);
ResultSet rs = queryExec.execSelect();
String vName = "";
for (String v: rs.getResultVars()) {
if (v.contains("count")) {
vName = v;
break;
}
}
while (rs.hasNext()) {
QuerySolution s = rs.nextSolution();
Literal c = s.getLiteral(vName);
queryExec.close();
return c.getInt();
}
queryExec.close();
return 0;
}
示例11: queryData
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
public static List<String> queryData(String query){
Dataset db = TDBFactory.createDataset("temp/dataset");
db.begin(ReadWrite.READ);
Model model = db.getDefaultModel();
Query q = QueryFactory.create(query);
QueryExecution qexec = QueryExecutionFactory.create(query, model);
ResultSet results = qexec.execSelect();
List<String> answer = new ArrayList<String>();
while(results.hasNext()){
QuerySolution t = results.nextSolution();
RDFNode x = t.get("x");
String s = x.toString();
System.out.println(s);
answer.add(s.substring(7));
}
qexec.close();
db.close();
return answer;
}
示例12: getTriples
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
/**
* Gets all triples for resource r and property p. If outgoing is true it
* returns all triples with <r,p,o>, else <s,p,r>
*
* @param r
* the resource
* @param p
* the property
* @param outgoing
* whether to get outgoing or ingoing triples
* @return A set of triples
*/
public Set<Triple> getTriples(Resource r, Property p, boolean outgoing) {
Set<Triple> result = new HashSet<Triple>();
try {
String q;
if (outgoing) {
q = "SELECT ?o where { <" + r.getURI() + "> <" + p.getURI() + "> ?o." + "?o rdfs:label []}";
} else {
q = "SELECT ?o where { ?o <" + p.getURI() + "> <" + r.getURI() + ">." + "?o rdfs:label []}";
}
q += " LIMIT " + maxShownValuesPerProperty + 1;
QueryExecution qe = qef.createQueryExecution(q);
ResultSet results = qe.execSelect();
if (results.hasNext()) {
while (results.hasNext()) {
RDFNode n = results.next().get("o");
result.add(Triple.create(r.asNode(), p.asNode(), n.asNode()));
}
}
qe.close();
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
示例13: getMostSpecificType
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
/**
* Returns the most specific type of a given individual.
*
* @param ind
* @return
*/
private OWLClass getMostSpecificType(OWLIndividual ind) {
logger.debug("Getting the most specific type of " + ind);
String query = String.format("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ "select distinct ?type where {" + " <%s> a ?type ." + "?type rdfs:label []."
// + "?type a owl:Class ." // too strict, thus currently omitted
+ "filter not exists {?subtype ^a <%s> ; rdfs:subClassOf ?type .filter(?subtype != ?type)}}",
ind.toStringID(), ind.toStringID());
SortedSet<OWLClass> types = new TreeSet<OWLClass>();
QueryExecution qe = qef.createQueryExecution(query);
ResultSet rs = qe.execSelect();
while (rs.hasNext()) {
QuerySolution qs = rs.next();
if (qs.get("type").isURIResource()) {
types.add(new OWLClassImpl(IRI.create(qs.getResource("type").getURI())));
}
}
qe.close();
// of more than one type exists, we have to choose one
// TODO
return types.first();
}
示例14: setPersonTypes
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
public void setPersonTypes(Set<String> personTypes) {
this.personTypes = personTypes;
// get the inferred sub types as well
if (useInference) {
Set<String> inferredTypes = new HashSet<>();
String queryTemplate = "select ?sub where{?sub <http://www.w3.org/2000/01/rdf-schema#subClassOf>* <%s>.}";
for (String type : personTypes) {
String query = String.format(queryTemplate, type);
try (QueryExecution qe = qef.createQueryExecution(query)) {
ResultSet rs = qe.execSelect();
while (rs.hasNext()) {
inferredTypes.add(rs.next().getResource("sub").getURI());
}
}
}
personTypes.addAll(inferredTypes);
}
}
示例15: isPerson
import org.apache.jena.query.QueryExecution; //导入依赖的package包/类
private boolean isPerson(String uri) {
if (personTypes.isEmpty()) {
return true;
} else {
// g et types of URI
Set<String> types = new HashSet<>();
try {
String query = "SELECT ?type WHERE {<" + uri + "> a ?type.}";
try (QueryExecution qe = qef.createQueryExecution(query)) {
ResultSet rs = qe.execSelect();
while (rs.hasNext()) {
types.add(rs.next().getResource("type").getURI());
}
}
} catch (Exception e) {
int code = ((QueryExceptionHTTP) e.getCause()).getResponseCode();
logger.warn("SPARQL query execution failed: " + code + " - " + HttpSC.getCode(code).getMessage());
}
// check for overlap between types of entity and person types
return !Sets.intersection(personTypes, types).isEmpty();
}
}