本文整理汇总了Java中org.apache.jena.query.QuerySolution类的典型用法代码示例。如果您正苦于以下问题:Java QuerySolution类的具体用法?Java QuerySolution怎么用?Java QuerySolution使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
QuerySolution类属于org.apache.jena.query包,在下文中一共展示了QuerySolution类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLabel
import org.apache.jena.query.QuerySolution; //导入依赖的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());
}
示例2: getProperties
import org.apache.jena.query.QuerySolution; //导入依赖的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;
}
示例3: getResultSetAsStringList
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
static List<String> getResultSetAsStringList(ResultSet resultSet, String variableName, boolean literalRequired) {
List<String> resultStrings = new ArrayList<>();
RDFNode node;
if (resultSet != null) {
while (resultSet.hasNext()) {
QuerySolution result = resultSet.next();
if (result != null) {
node = result.get(variableName);
if (literalRequired) {
String literal = getStringLiteral(node);
if (literal != null) {
resultStrings.add(literal);
}
} else {
if (node != null) {
resultStrings.add(node.toString());
}
}
}
}
}
return resultStrings;
}
示例4: getLabelOfType
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
static String getLabelOfType(String type) {
type = "<" + type + ">";
String queryString = Constants.PREFIX_RDFS + Constants.PREFIX_FOAF;
String variable = "name";
queryString += "SELECT ?" + variable + " FROM <http://dbpedia.org> WHERE {\n" +
"{" + type + " rdfs:label ?name .}\n" +
Constants.UNION +
"{" + type + " foaf:name ?name .}\n" +
" filter (langMatches(lang(?name), \"EN\")) ." +
"}";
ResultSet results = null;
try {
results = SPARQLClient.runSelectQuery(queryString, Constants.DBPEDIA_SPARQL_SERVICE);
} catch (Exception e) {
e.printStackTrace();
}
if (results != null) {
if (results.hasNext()) {
QuerySolution result = results.next();
return getStringLiteral(result.get(variable));
}
}
return null;
}
示例5: getCount
import org.apache.jena.query.QuerySolution; //导入依赖的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;
}
示例6: writeInputs
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
/**
* Writes a set of inputs from a workflow to the Writer
* @param workflowUri The URI of the workflow in the model
* @throws IOException Any errors in writing which may have occurred
*/
private void writeInputs(String workflowUri) throws IOException {
// Start of subgraph with styling
writeLine(" subgraph cluster_inputs {");
writeLine(" rank = \"same\";");
writeLine(" style = \"dashed\";");
writeLine(" label = \"Workflow Inputs\";");
// Write each of the inputs as a node
ResultSet inputs = rdfService.getInputs(workflowUri);
while (inputs.hasNext()) {
QuerySolution input = inputs.nextSolution();
writeInputOutput(input);
}
// End subgraph
writeLine(" }");
}
示例7: writeOutputs
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
/**
* Writes a set of outputs from a workflow to the Writer
* @param workflowUri The URI of the workflow in the model
* @throws IOException Any errors in writing which may have occurred
*/
private void writeOutputs(String workflowUri) throws IOException {
// Start of subgraph with styling
writeLine(" subgraph cluster_outputs {");
writeLine(" rank = \"same\";");
writeLine(" style = \"dashed\";");
writeLine(" labelloc = \"b\";");
writeLine(" label = \"Workflow Outputs\";");
// Write each of the outputs as a node
ResultSet outputs = rdfService.getOutputs(workflowUri);
while (outputs.hasNext()) {
QuerySolution output = outputs.nextSolution();
writeInputOutput(output);
}
// End subgraph
writeLine(" }");
}
示例8: writeInputOutput
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
/**
* Writes a single input or output to the Writer
* @param inputOutput The input or output
* @throws IOException Any errors in writing which may have occurred
*/
private void writeInputOutput(QuerySolution inputOutput) throws IOException {
// List of options for this node
List<String> nodeOptions = new ArrayList<>();
nodeOptions.add("fillcolor=\"#94DDF4\"");
// Label for the node
String label;
if (inputOutput.contains("label")) {
label = inputOutput.get("label").toString();
} else {
label = rdfService.labelFromName(inputOutput.get("name").toString());
}
nodeOptions.add("label=\"" + label + "\"");
// Write the line for the node
String inputOutputName = rdfService.stepNameFromURI(gitPath, inputOutput.get("name").toString());
writeLine(" \"" + inputOutputName + "\" [" + String.join(",", nodeOptions) + "];");
}
示例9: queryData
import org.apache.jena.query.QuerySolution; //导入依赖的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;
}
示例10: getMostSpecificType
import org.apache.jena.query.QuerySolution; //导入依赖的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();
}
示例11: BindingHashMapOverwrite
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
/**
* Constructs a new binding from a query solution.
*
* @param binding -
*/
public BindingHashMapOverwrite(final QuerySolution binding) {
var = null;
node = null;
if (binding == null) {
parent = null;
} else {
final BindingHashMap p = new BindingHashMap();
for (Iterator<String> it = binding.varNames(); it.hasNext();) {
final String varName = it.next();
if (binding.get(varName) != null) {
p.add(Var.alloc(varName), binding.get(varName).asNode());
}
}
parent = p;
}
// LOG.trace("New binding #" + System.identityHashCode(this) + " copies " + binding);
}
示例12: createQueryExecution
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
public QueryExecution createQueryExecution(Query query, Dataset dataset, QuerySolution initialBinding) {
if(!query.getGraphURIs().isEmpty() || !query.getNamedGraphURIs().isEmpty()) {
dataset = new FromDataset(dataset, query);
}
if ( LOG_QUERIES ) {
// And the data - can be long.
// System.err.println("~~ ~~");
// RDFDataMgr.write(System.err, dataset.getDefaultModel(), Lang.TTL);
System.err.println("~~ ~~");
System.err.println(initialBinding);
System.err.println(query);
}
QueryExecution qexec = QueryExecutionFactoryFilter.get().create(query, dataset, initialBinding);
adjustQueryExecution(qexec);
return qexec;
}
示例13: asBinding
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
/**
* Turns a QuerySolution into a Binding.
* @param map the input QuerySolution
* @return a Binding or null if the input is null
*/
public static Binding asBinding(final QuerySolution map) {
if(map != null) {
BindingHashMap result = new BindingHashMap();
Iterator<String> varNames = map.varNames();
while(varNames.hasNext()) {
String varName = varNames.next();
RDFNode node = map.get(varName);
if(node != null) {
result.add(Var.alloc(varName), node.asNode());
}
}
return result;
}
else {
return null;
}
}
示例14: invokeExpression
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
/**
* Calls a SPARQL expression and returns the result, using some initial bindings.
*
* @param expression the expression to execute (must contain absolute URIs)
* @param initialBinding the initial bindings for the unbound variables
* @param dataset the query Dataset or null for default
* @return the result or null
*/
public static Node invokeExpression(String expression, QuerySolution initialBinding, Dataset dataset) {
if (dataset == null) {
dataset = ARQFactory.get().getDataset(ModelFactory.createDefaultModel());
}
Query query = ARQFactory.get().createExpressionQuery(expression);
try(QueryExecution qexec = ARQFactory.get().createQueryExecution(query, dataset, initialBinding)) {
ResultSet rs = qexec.execSelect();
Node result = null;
if (rs.hasNext()) {
QuerySolution qs = rs.next();
String firstVarName = rs.getResultVars().get(0);
RDFNode rdfNode = qs.get(firstVarName);
if (rdfNode != null) {
result = rdfNode.asNode();
}
}
return result;
}
}
示例15: addDefaultMessages
import org.apache.jena.query.QuerySolution; //导入依赖的package包/类
private void addDefaultMessages(ValidationEngine engine, Resource messageHolder, Resource fallback, Resource result,
QuerySolution bindings, QuerySolution solution) {
boolean found = false;
for(Statement s : messageHolder.listProperties(SH.message).toList()) {
if(s.getObject().isLiteral()) {
QuerySolutionMap map = new QuerySolutionMap();
map.addAll(bindings);
map.addAll(solution);
engine.addResultMessage(result, s.getLiteral(), map);
found = true;
}
}
if(!found && fallback != null) {
addDefaultMessages(engine, fallback, null, result, bindings, solution);
}
}