use of org.apache.jena.query.Query in project jena by apache.
the class QueryValidatorJSON method execute.
public static JsonObject execute(ValidationAction action) {
JsonBuilder obj = new JsonBuilder();
obj.startObject();
final String queryString = getArg(action, paramQuery);
String querySyntax = getArgOrNull(action, paramSyntax);
if (querySyntax == null || querySyntax.equals(""))
querySyntax = "SPARQL";
Syntax language = Syntax.lookup(querySyntax);
if (language == null) {
ServletOps.errorBadRequest("Unknown syntax: " + querySyntax);
return null;
}
boolean outputSPARQL = true;
boolean outputAlgebra = true;
boolean outputQuads = true;
boolean outputOptimized = true;
boolean outputOptimizedQuads = true;
obj.key(jInput).value(queryString);
// Attempt to parse it.
Query query = null;
try {
query = QueryFactory.create(queryString, "http://example/base/", language);
} catch (QueryParseException ex) {
obj.key(jErrors);
// Errors array
obj.startArray();
obj.startObject();
obj.key(jParseError).value(ex.getMessage());
obj.key(jParseErrorLine).value(ex.getLine());
obj.key(jParseErrorCol).value(ex.getColumn());
obj.finishObject();
obj.finishArray();
// Outer object
obj.finishObject();
return obj.build().getAsObject();
}
if (query != null) {
if (outputSPARQL)
formatted(obj, query);
if (outputAlgebra)
algebra(obj, query);
if (outputQuads)
algebraQuads(obj, query);
if (outputOptimized)
algebraOpt(obj, query);
if (outputOptimizedQuads)
algebraOptQuads(obj, query);
}
obj.finishObject();
return obj.build().getAsObject();
}
use of org.apache.jena.query.Query in project jena by apache.
the class JenaOSGITest method runQuery.
private void runQuery(Dataset dataset) {
Query query = QueryFactory.create("" + "PREFIX foaf: <http://xmlns.com/foaf/0.1/>" + "SELECT ?bob WHERE { " + " GRAPH <http://example.com/graph> { " + " ?alice foaf:knows ?bob . " + " }" + "}");
try (QueryExecution qexec = QueryExecutionFactory.create(query, dataset)) {
ResultSet results = qexec.execSelect();
assertTrue(results.hasNext());
QuerySolution r = results.next();
assertEquals(bob, r.get("bob").asResource());
}
}
use of org.apache.jena.query.Query in project jena by apache.
the class WhereHandler method makeSubQuery.
/**
* Convert a subquery into a subquery element.
*
* @param subQuery
* The sub query to convert
* @return THe converted element.
*/
public ElementSubQuery makeSubQuery(AbstractQueryBuilder<?> subQuery) {
Query q = new Query();
SelectHandler sh = subQuery.getHandlerBlock().getSelectHandler();
if (sh != null) {
if (!sh.getProject().isEmpty()) {
q.setQuerySelectType();
}
}
PrologHandler ph = new PrologHandler(query);
ph.addPrefixes(subQuery.getPrologHandler().getPrefixes());
HandlerBlock handlerBlock = new HandlerBlock(q);
handlerBlock.addAll(subQuery.getHandlerBlock());
// remove the prefix mappings from the sub query.
handlerBlock.getPrologHandler().clearPrefixes();
// make sure we have a query pattern before we start building.
if (q.getQueryPattern() == null) {
q.setQueryPattern(new ElementGroup());
}
handlerBlock.build();
return new ElementSubQuery(q);
}
use of org.apache.jena.query.Query in project jena by apache.
the class AbstractQueryBuilder method clone.
/**
* Close the query.
*
* This can be used when the query would not normally parse as is required
* by the Query.clone() method.
*
* @param q2
* The query to clone
* @return A clone of the q2 param.
*/
public static Query clone(Query q2) {
Query retval = new Query();
// set the query type
if (q2.isSelectType()) {
retval.setQuerySelectType();
} else if (q2.isAskType()) {
retval.setQueryAskType();
} else if (q2.isDescribeType()) {
retval.setQueryDescribeType();
} else if (q2.isConstructType()) {
retval.setQueryConstructType();
}
// use the handler block to clone the data
HandlerBlock hb = new HandlerBlock(retval);
HandlerBlock hb2 = new HandlerBlock(q2);
hb.addAll(hb2);
return retval;
}
use of org.apache.jena.query.Query in project jena by apache.
the class ElementRewriter method visit.
@Override
public void visit(ElementSubQuery el) {
Query q = AbstractQueryBuilder.clone(el.getQuery());
push(new ElementSubQuery(AbstractQueryBuilder.rewrite(q, values)));
}
Aggregations