Search in sources :

Example 26 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class QueryUtils method checkParse.

public static void checkParse(Query query) {
    if (!SPARQLParserRegistry.get().containsFactory(query.getSyntax()))
        return;
    IndentedLineBuffer buff = new IndentedLineBuffer();
    query.serialize(buff, query.getSyntax());
    String tmp = buff.toString();
    Query query2 = null;
    try {
        String baseURI = null;
        if (!query.explicitlySetBaseURI())
            // Not in query - use the same one (e.g. file read from) .
            baseURI = query.getBaseURI();
        query2 = QueryFactory.create(tmp, baseURI, query.getSyntax());
        if (query2 == null)
            return;
    } catch (UnsupportedOperationException ex) {
        // No parser after all.
        return;
    } catch (QueryException ex) {
        System.err.println(tmp);
        throw new QueryCheckException("could not parse output query", ex);
    }
    if (query.hashCode() != query2.hashCode())
        throw new QueryCheckException("reparsed query hashCode does not equal parsed input query \nQuery (hashCode: " + query.hashCode() + ")=\n" + query + "\n\nQuery2 (hashCode: " + query2.hashCode() + ")=\n" + query2);
    if (!query.equals(query2)) {
        if (false) {
            System.err.println(query);
            System.err.println(query2);
        }
        throw new QueryCheckException("reparsed output does not equal parsed input");
    }
}
Also used : QueryException(org.apache.jena.query.QueryException) Query(org.apache.jena.query.Query) QueryCheckException(org.apache.jena.sparql.core.QueryCheckException) IndentedLineBuffer(org.apache.jena.atlas.io.IndentedLineBuffer)

Example 27 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class ApplyElementTransformVisitor method visit.

@Override
public void visit(ElementSubQuery el) {
    Query newQuery = QueryTransformOps.transform(el.getQuery(), transform, exprTransform);
    push(new ElementSubQuery(newQuery));
}
Also used : Query(org.apache.jena.query.Query)

Example 28 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class QueryTransformOps method transform.

/**
 * Transform a query using {@link ElementTransform} and {@link ExprTransform}.
 * It is the responsibility of these transforms to transform to a legal SPARQL query.
 */
public static Query transform(Query query, ElementTransform transform, ExprTransform exprTransform) {
    Query q2 = QueryTransformOps.shallowCopy(query);
    // Mutate the q2 structures which are already allocated and no other code can access yet.
    mutateByQueryType(q2, transform, exprTransform);
    mutateVarExprList(q2.getGroupBy(), exprTransform);
    mutateExprList(q2.getHavingExprs(), exprTransform);
    if (q2.getOrderBy() != null)
        mutateSortConditions(q2.getOrderBy(), exprTransform);
    mutateQueryPattern(q2, transform, exprTransform);
    if (query.isQueryResultStar()) {
        // Reset internal to only what now can be seen.
        q2.resetResultVars();
    }
    return q2;
}
Also used : Query(org.apache.jena.query.Query)

Example 29 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class FormatterElement method visit.

@Override
public void visit(ElementSubQuery el) {
    out.print("{ ");
    out.incIndent(INDENT);
    Query q = el.getQuery();
    // Serialize with respect to the existing context.
    QuerySerializerFactory factory = SerializerRegistry.get().getQuerySerializerFactory(Syntax.syntaxARQ);
    QueryVisitor serializer = factory.create(Syntax.syntaxARQ, context, out);
    q.visit(serializer);
    out.decIndent(INDENT);
    out.print("}");
}
Also used : QueryVisitor(org.apache.jena.query.QueryVisitor) Query(org.apache.jena.query.Query)

Example 30 with Query

use of org.apache.jena.query.Query in project jena by apache.

the class SerializationTest method run.

// A serialization test is:
// Read query in.
// Serialize to string.
// Parse again.
// Are they equal?
@Override
public void run() {
    Query query = SparqlTestLib.queryFromEntry(testEntry);
    // Whatever was read in.
    runTestWorker(query, query.getSyntax());
}
Also used : Query(org.apache.jena.query.Query)

Aggregations

Query (org.apache.jena.query.Query)265 Test (org.junit.Test)78 ContractTest (org.xenei.junit.contract.ContractTest)65 QueryExecution (org.apache.jena.query.QueryExecution)63 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)49 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)42 Var (org.apache.jena.sparql.core.Var)42 Triple (org.apache.jena.graph.Triple)38 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)32 ResultSet (org.apache.jena.query.ResultSet)29 Node (org.apache.jena.graph.Node)28 TriplePath (org.apache.jena.sparql.core.TriplePath)28 QuerySolution (org.apache.jena.query.QuerySolution)26 ExprVar (org.apache.jena.sparql.expr.ExprVar)24 Binding (org.apache.jena.sparql.engine.binding.Binding)22 Model (org.apache.jena.rdf.model.Model)20 HashMap (java.util.HashMap)19 Dataset (org.apache.jena.query.Dataset)15 Op (org.apache.jena.sparql.algebra.Op)15 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)14