本文整理汇总了Java中org.apache.jena.query.QueryExecutionFactory类的典型用法代码示例。如果您正苦于以下问题:Java QueryExecutionFactory类的具体用法?Java QueryExecutionFactory怎么用?Java QueryExecutionFactory使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
QueryExecutionFactory类属于org.apache.jena.query包,在下文中一共展示了QueryExecutionFactory类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: listVocabularies
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的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.QueryExecutionFactory; //导入依赖的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.QueryExecutionFactory; //导入依赖的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.QueryExecutionFactory; //导入依赖的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.QueryExecutionFactory; //导入依赖的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.QueryExecutionFactory; //导入依赖的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.QueryExecutionFactory; //导入依赖的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: runSelectQuery
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的package包/类
static ResultSet runSelectQuery(String queryString, String service, String... defaultGraphs) throws Exception {
QueryEngineHTTP qExec = (QueryEngineHTTP) QueryExecutionFactory.sparqlService(service, queryString);
qExec.addDefaultGraph(Constants.DBPEDIA_URL);
if (defaultGraphs != null) {
for (String defaultGraph : defaultGraphs) {
qExec.addDefaultGraph(defaultGraph);
}
}
qExec.addParam("timeout", Constants.TIMEOUT_VALUE); //100 sec
ResultSet set = null;
try {
set = qExec.execSelect();
set = ResultSetFactory.copyResults(set);
} catch (Exception e) {
QGenLogger.severe("Exception in SELECT\n" + queryString + "\n" + e.getMessage());
} finally {
qExec.close();
}
return set;
}
示例9: getCount
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的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;
}
示例10: queryData
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的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;
}
示例11: LocationMapperAccept
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的package包/类
public LocationMapperAccept(final Model configurationModel) {
Query q = QueryFactory.create("PREFIX lm: <http://jena.hpl.hp.com/2004/08/location-mapping#>"
+ "SELECT * WHERE {"
+ "[] lm:mapping ?e ."
+ "?e lm:name ?name ; lm:altName ?alt ."
+ "OPTIONAL { ?e lm:media ?media . } "
+ "}");
QueryExecutionFactory.create(q, configurationModel).execSelect().forEachRemaining((result) -> {
String name = null, altName = null, media = null;
try {
name = result.getLiteral("name").getString();
altName = result.getLiteral("alt").getString();
media = (result.getLiteral("media") == null ? null : result.getLiteral("media").getString());
altLocations.put(new LookUpRequest(name, media), new LookUpRequest(altName, media));
} catch (Exception ex) {
log.warn("Error while reading mapping in configuration model for name " + name + ", alt " + altName + ", media " + media,ex);
}
});
}
示例12: testSelect
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的package包/类
@Test
public void testSelect() {
QueryExecution qe = QueryExecutionFactory.create(
"select ?s ?p ?o where { ?s ?p ?o}", ds);
ResultSet results = qe.execSelect();
// System.out.println(ResultSetFormatter.asText(results));
while (results.hasNext()) {
QuerySolution qs = results.next();
assertTrue(qs.contains("s"));
assertTrue(qs.contains("p"));
assertTrue(qs.contains("o"));
}
}
示例13: testRulesets
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的package包/类
@Test
public void testRulesets() {
MarkLogicDatasetGraph infTestDsg = getMarkLogicDatasetGraph();
Dataset ds = DatasetFactory.wrap(infTestDsg);
String query = "prefix : <http://example.org/> select ?o where { :r3 a ?o }";
QueryExecution exec = QueryExecutionFactory.create(query, ds);
ResultSet results = exec.execSelect();
List<String> subjects = project(results, "o");
assertEquals("No inference, got back list of size 1", 1,
subjects.size());
infTestDsg.withRulesets(SPARQLRuleset.RDFS);
// MarkLogicQuery inferringQuery = MarkLogicQuery.create(query);
// inferringQuery.setRulesets(SPARQLRuleset.RDFS);
exec = QueryExecutionFactory.create(query, ds);
results = exec.execSelect();
subjects = project(results, "o");
System.out.println(subjects);
assertEquals("Using RDFs got back two class assertions", 2,
subjects.size());
}
示例14: testBaseUri
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的package包/类
@Test
public void testBaseUri() {
MarkLogicDatasetGraph baseTest = getMarkLogicDatasetGraph();
Dataset ds = DatasetFactory.wrap(baseTest);
String query = "select ?o where { <r3> a ?o }";
QueryExecution exec = QueryExecutionFactory.create(query, ds);
ResultSet results = exec.execSelect();
List<String> subjects = project(results, "o");
assertEquals("No base, got back list of size 0", 0, subjects.size());
Query q = QueryFactory.create(query, "http://example.org/");
exec = QueryExecutionFactory.create(q, ds);
results = exec.execSelect();
subjects = project(results, "o");
assertEquals("No base, got back list of size 1", 1, subjects.size());
}
示例15: testUpdateAction
import org.apache.jena.query.QueryExecutionFactory; //导入依赖的package包/类
@Test
public void testUpdateAction() {
DatasetGraph gs = getMarkLogicDatasetGraph();
UpdateRequest update = new UpdateRequest();
update.add("INSERT DATA { <s2> <p1> <o1> }");
update.add("DROP ALL")
.add("CREATE GRAPH <http://example/update1>")
.add("BASE <http://example.org/> INSERT DATA { GRAPH <http://example.org/update2> { <s1> <p1> <o1> } }")
.add("BASE <http://example.org/> INSERT DATA { GRAPH <http://example.org/update3> { <s1> <p1> <o1> } }");
UpdateAction.execute(update, gs);
QueryExecution askQuery = QueryExecutionFactory
.create("BASE <http://example.org/> ASK WHERE { GRAPH <update3> { <s1> <p1> <o1> }}",
DatasetFactory.wrap(gs));
assertTrue("update action must update database.", askQuery.execAsk());
}