Search in sources :

Example 11 with Element

use of org.apache.jena.sparql.syntax.Element in project jena by apache.

the class WhereClauseTest method testBindExprVar.

@ContractTest
public void testBindExprVar() {
    Var v = Var.alloc("foo");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addBind(new E_Random(), v);
    assertContainsRegex(OPEN_CURLY + BIND + OPEN_PAREN + "rand\\(\\)" + SPACE + "AS" + SPACE + var("foo") + CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
    builder.setVar(v, NodeFactory.createURI("three"));
    Query q = builder.build();
    ElementGroup eg = (ElementGroup) q.getQueryPattern();
    List<Element> lst = eg.getElements();
    assertEquals("Should only be one element", 1, lst.size());
    assertTrue("Should have an ElementTriplesBlock", lst.get(0) instanceof ElementTriplesBlock);
    ElementTriplesBlock etb = (ElementTriplesBlock) lst.get(0);
    assertTrue("ElementGroup should be empty", etb.isEmpty());
}
Also used : Query(org.apache.jena.query.Query) E_Random(org.apache.jena.sparql.expr.E_Random) Var(org.apache.jena.sparql.core.Var) Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ContractTest(org.xenei.junit.contract.ContractTest)

Example 12 with Element

use of org.apache.jena.sparql.syntax.Element in project jena by apache.

the class QuerySerializer method visitQueryPattern.

@Override
public void visitQueryPattern(Query query) {
    if (query.getQueryPattern() != null) {
        out.print("WHERE");
        out.incIndent(BLOCK_INDENT);
        out.newline();
        Element el = query.getQueryPattern();
        fmtElement.visitAsGroup(el);
        //el.visit(fmtElement) ;
        out.decIndent(BLOCK_INDENT);
        out.newline();
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element)

Example 13 with Element

use of org.apache.jena.sparql.syntax.Element in project jena by apache.

the class UpdateWriterVisitor method visit.

@Override
public void visit(UpdateModify update) {
    out.ensureStartOfLine();
    if (update.getWithIRI() != null) {
        //out.ensureStartOfLine() ;
        out.print("WITH ");
        output(update.getWithIRI());
    }
    if (update.hasDeleteClause()) {
        List<Quad> deleteQuads = update.getDeleteQuads();
        out.ensureStartOfLine();
        out.print("DELETE ");
        outputQuadsBraced(deleteQuads);
    }
    if (update.hasInsertClause()) {
        List<Quad> insertQuads = update.getInsertQuads();
        out.ensureStartOfLine();
        out.print("INSERT ");
        outputQuadsBraced(insertQuads);
    }
    if (!update.hasInsertClause() && !update.hasDeleteClause()) {
        // Fake a clause to make it legal syntax.
        out.ensureStartOfLine();
        out.println("INSERT { }");
    }
    for (Node x : update.getUsing()) {
        out.ensureStartOfLine();
        out.print("USING ");
        output(x);
    }
    for (Node x : update.getUsingNamed()) {
        out.ensureStartOfLine();
        out.print("USING NAMED ");
        output(x);
    }
    Element el = update.getWherePattern();
    out.ensureStartOfLine();
    out.print("WHERE");
    out.incIndent(BLOCK_INDENT);
    out.newline();
    if (el != null) {
        FormatterElement fmtElement = prepareElementFormatter();
        fmtElement.visitAsGroup(el);
    } else
        out.print("{}");
    out.decIndent(BLOCK_INDENT);
}
Also used : Quad(org.apache.jena.sparql.core.Quad) Node(org.apache.jena.graph.Node) Element(org.apache.jena.sparql.syntax.Element) FormatterElement(org.apache.jena.sparql.serializer.FormatterElement) FormatterElement(org.apache.jena.sparql.serializer.FormatterElement)

Example 14 with Element

use of org.apache.jena.sparql.syntax.Element in project jena by apache.

the class ARQParserBase method createQueryPattern.

protected ElementGroup createQueryPattern(Template t) {
    ElementGroup elg = new ElementGroup();
    Map<Node, BasicPattern> graphs = t.getGraphPattern();
    for (Node n : graphs.keySet()) {
        Element el = new ElementPathBlock(graphs.get(n));
        if (!Quad.defaultGraphNodeGenerated.equals(n)) {
            ElementGroup e = new ElementGroup();
            e.addElement(el);
            el = new ElementNamedGraph(n, e);
        }
        elg.addElement(el);
    }
    return elg;
}
Also used : ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) Node(org.apache.jena.graph.Node) Element(org.apache.jena.sparql.syntax.Element) BasicPattern(org.apache.jena.sparql.core.BasicPattern) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Example 15 with Element

use of org.apache.jena.sparql.syntax.Element in project jena by apache.

the class ExprTransformNodeElement method transform.

@Override
public Expr transform(ExprFunctionOp funcOp, ExprList args, Op opArg) {
    // Syntax phased only - ignore args and opArg
    Element elt = funcOp.getElement();
    Element elt1 = ElementTransformer.transform(elt, elementTransform, this, beforeVisitor, afterVisitor);
    if (elt == elt1)
        return funcOp;
    else {
        if (funcOp instanceof E_Exists)
            return new E_Exists(elt1);
        if (funcOp instanceof E_NotExists)
            return new E_NotExists(elt1);
        throw new InternalErrorException("Unknown ExprFunctionOp: " + funcOp.getFunctionSymbol());
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element) InternalErrorException(org.apache.jena.atlas.lib.InternalErrorException)

Aggregations

Element (org.apache.jena.sparql.syntax.Element)21 Test (org.junit.Test)9 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)8 Query (org.apache.jena.query.Query)7 Quad (org.apache.jena.sparql.core.Quad)6 DatasetGraph (org.apache.jena.sparql.core.DatasetGraph)5 ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)4 Node (org.apache.jena.graph.Node)3 Var (org.apache.jena.sparql.core.Var)3 Binding (org.apache.jena.sparql.engine.binding.Binding)3 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)3 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)2 Triple (org.apache.jena.graph.Triple)2 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)2 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)2 Update (org.apache.jena.update.Update)2 ContractTest (org.xenei.junit.contract.ContractTest)2 InternalErrorException (org.apache.jena.atlas.lib.InternalErrorException)1 BasicPattern (org.apache.jena.sparql.core.BasicPattern)1 E_Random (org.apache.jena.sparql.expr.E_Random)1