Search in sources :

Example 96 with Query

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();
}
Also used : JsonBuilder(org.apache.jena.atlas.json.JsonBuilder) Query(org.apache.jena.query.Query) Syntax(org.apache.jena.query.Syntax) QueryParseException(org.apache.jena.query.QueryParseException)

Example 97 with Query

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());
    }
}
Also used : Query(org.apache.jena.query.Query) QuerySolution(org.apache.jena.query.QuerySolution) ResultSet(org.apache.jena.query.ResultSet) QueryExecution(org.apache.jena.query.QueryExecution)

Example 98 with Query

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);
}
Also used : Query(org.apache.jena.query.Query)

Example 99 with Query

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;
}
Also used : ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) HandlerBlock(org.apache.jena.arq.querybuilder.handlers.HandlerBlock)

Example 100 with Query

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)));
}
Also used : Query(org.apache.jena.query.Query)

Aggregations

Query (org.apache.jena.query.Query)102 Test (org.junit.Test)22 Op (org.apache.jena.sparql.algebra.Op)17 Var (org.apache.jena.sparql.core.Var)11 HashMap (java.util.HashMap)9 QueryExecution (org.apache.jena.query.QueryExecution)9 ContractTest (org.xenei.junit.contract.ContractTest)9 Node (org.apache.jena.graph.Node)8 WhereHandler (org.apache.jena.arq.querybuilder.handlers.WhereHandler)7 ResultSet (org.apache.jena.query.ResultSet)7 VarExprList (org.apache.jena.sparql.core.VarExprList)7 Element (org.apache.jena.sparql.syntax.Element)7 Triple (org.apache.jena.graph.Triple)6 Before (org.junit.Before)6 Map (java.util.Map)5 QuerySolution (org.apache.jena.query.QuerySolution)5 QueryEngineHTTP (org.apache.jena.sparql.engine.http.QueryEngineHTTP)5 Context (org.apache.jena.sparql.util.Context)5 StringReader (java.io.StringReader)4 TriplePath (org.apache.jena.sparql.core.TriplePath)4