use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class ExProg2 method main.
public static void main(String[] args) {
Model model = createModel();
Query query = QueryFactory.make();
query.setQuerySelectType();
// See also ExProg1
ElementGroup elg = new ElementGroup();
Var varTitle = Var.alloc("title");
Var varX = Var.alloc("x");
Triple t1 = new Triple(varX, DC.title.asNode(), varTitle);
elg.addTriplePattern(t1);
// Adds a filter. Need to wrap variable in a NodeVar.
Expr expr = new E_Regex(new ExprVar(varTitle), "sparql", "i");
ElementFilter filter = new ElementFilter(expr);
elg.addElementFilter(filter);
// Attach the group to query.
query.setQueryPattern(elg);
// Choose what we want - SELECT ?title
query.addResultVar(varTitle);
// Print query with line numbers
// Prefix mapping just helps serialization
query.getPrefixMapping().setNsPrefix("dc", DC.getURI());
query.serialize(new IndentedWriter(System.out, true));
System.out.println();
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
// Assumption: it's a SELECT query.
ResultSet rs = qexec.execSelect();
// The order of results is undefined.
System.out.println("Titles: ");
for (; rs.hasNext(); ) {
QuerySolution rb = rs.nextSolution();
// Get title - variable names do not include the '?' (or '$')
RDFNode x = rb.get("title");
// Check the type of the result value
if (x instanceof Literal) {
Literal titleStr = (Literal) x;
System.out.println(" " + titleStr);
} else
System.out.println("Strange - not a literal: " + x);
}
}
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class ExProg1 method main.
public static void main(String[] args) {
Model model = createModel();
Query query = QueryFactory.make();
query.setQuerySelectType();
// Build pattern
ElementGroup elg = new ElementGroup();
Var varTitle = Var.alloc("title");
Var varX = Var.alloc("x");
Triple t1 = new Triple(varX, DC.title.asNode(), varTitle);
elg.addTriplePattern(t1);
// Don't use bNodes for anon variables. The conversion is done in parsing.
// BNodes here are assumed to be values from the target graph.
Triple t2 = new Triple(varX, DC.description.asNode(), Var.alloc("desc"));
elg.addTriplePattern(t2);
// Attach the group to query.
query.setQueryPattern(elg);
// Choose what we want - SELECT *
// query.setQueryResultStar(true) ;
query.addResultVar(varTitle);
// Print query with line numbers
// Prefix mapping just helps serialization
query.getPrefixMapping().setNsPrefix("dc", DC.getURI());
query.serialize(new IndentedWriter(System.out, true));
System.out.println();
try (QueryExecution qexec = QueryExecutionFactory.create(query, model)) {
// Assumption: it's a SELECT query.
ResultSet rs = qexec.execSelect();
// The order of results is undefined.
System.out.println("Titles: ");
for (; rs.hasNext(); ) {
QuerySolution rb = rs.nextSolution();
// Get title - variable names do not include the '?' (or '$')
RDFNode x = rb.get("title");
// Check the type of the result value
if (x instanceof Literal) {
Literal titleStr = (Literal) x;
System.out.println(" " + titleStr);
} else
System.out.println("Strange - not a literal: " + x);
}
}
}
Aggregations