use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class RDFOutput method asRDF.
public Resource asRDF(Model model, ResultSet resultSet, boolean includeRowIndex) {
Resource results = model.createResource();
// This always goes in.
results.addProperty(RDF.type, ResultSetGraphVocab.ResultSet);
for (String vName : resultSet.getResultVars()) results.addProperty(ResultSetGraphVocab.resultVariable, vName);
int count = 0;
for (; resultSet.hasNext(); ) {
count++;
QuerySolution rBind = resultSet.nextSolution();
Resource thisSolution = model.createResource();
if (includeTypeProperties)
thisSolution.addProperty(RDF.type, ResultSetGraphVocab.ResultSolution);
results.addProperty(ResultSetGraphVocab.solution, thisSolution);
if (includeRowIndex) {
// This can lead to equivalent result sets having different graphs
// Best used if and only if query was completely sorted.
Literal x = model.createTypedLiteral(count + "", XSDDatatype.XSDinteger);
thisSolution.addLiteral(ResultSetGraphVocab.index, x);
}
Iterator<String> iter = getAllVars() ? rBind.varNames() : resultSet.getResultVars().iterator();
for (; iter.hasNext(); ) {
Resource thisBinding = model.createResource();
String rVar = iter.next();
RDFNode n = rBind.get(rVar);
if (n == null)
continue;
// }
if (includeTypeProperties)
thisBinding.addProperty(RDF.type, ResultSetGraphVocab.ResultBinding);
thisBinding.addProperty(ResultSetGraphVocab.variable, rVar);
thisBinding.addProperty(ResultSetGraphVocab.value, n);
thisSolution.addProperty(ResultSetGraphVocab.binding, thisBinding);
}
}
results.addProperty(ResultSetGraphVocab.size, model.createTypedLiteral(count));
addPrefixes(model);
return results;
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class ResultSetApply method apply.
public void apply() {
proc.start(rs);
for (; rs.hasNext(); ) {
QuerySolution qs = rs.next();
proc.start(qs);
for (String varName : rs.getResultVars()) {
RDFNode node = qs.get(varName);
// node may be null
proc.binding(varName, node);
}
proc.finish(qs);
}
proc.finish(rs);
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class TestSyntaxTransform method testUpdateModel.
private void testUpdateModel(String input, String output, String varStr, String valStr) {
UpdateRequest req1 = UpdateFactory.create(PREFIX + input);
UpdateRequest reqExpected = UpdateFactory.create(PREFIX + output);
Map<String, RDFNode> map = new HashMap<>();
Node n = SSE.parseNode(valStr);
RDFNode x = ModelUtils.convertGraphNodeToRDFNode(n);
map.put(varStr, x);
UpdateRequest reqTrans = UpdateTransformOps.transformUpdate(req1, map);
// Crude.
String x1 = reqExpected.toString().replaceAll("[ \n\t]", "");
String x2 = reqTrans.toString().replaceAll("[ \n\t]", "");
//assertEquals(reqExpected, reqTrans) ;
assertEquals(x1, x2);
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class ResultSetUtils method resultSetToStringList.
/**
* Extracts a List filled with the binding of selectElement variable for each
* query solution, turned into a string (URIs or lexical forms).
* Exhausts the result set. Create a rewindable one to use multiple times.
* @see org.apache.jena.query.ResultSetFactory
*/
public static List<String> resultSetToStringList(ResultSet rs, String selectElement, String literalOrResource) {
// feature suggested by James Howison
List<String> items = new ArrayList<>();
while (rs.hasNext()) {
QuerySolution qs = rs.nextSolution();
RDFNode rn = qs.get(selectElement);
if (rn.isLiteral())
items.add(((Literal) rn).getLexicalForm());
else if (rn.isURIResource())
items.add(((Resource) rn).getURI());
else if (rn.isAnon()) {
items.add(((Resource) rn).getId().getLabelString());
} else
throw new ARQException("Unknow thing in results : " + rn);
}
return items;
}
use of org.apache.jena.rdf.model.RDFNode in project jena by apache.
the class ResultSetUtils method resultSetToList.
/**
* Extracts a List filled with the binding of selectElement variable for each
* query solution as RDFNodes (Resources or Literals).
* Exhausts the result set. Create a rewindable one to use multiple times.
*
* @see org.apache.jena.query.ResultSetFactory
*/
public static List<RDFNode> resultSetToList(ResultSet rs, String selectElement) {
// feature suggested by James Howison
List<RDFNode> items = new ArrayList<>();
while (rs.hasNext()) {
QuerySolution qs = rs.nextSolution();
RDFNode n = qs.get(selectElement);
items.add(n);
}
return items;
}
Aggregations