Search in sources :

Example 11 with ElementGroup

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

the class WhereClauseTest method testBindStringVar.

@ContractTest
public void testBindStringVar() throws ParseException {
    Var v = Var.alloc("foo");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addBind("rand()", 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) 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 ElementGroup

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

the class TestParameterizedSparqlString method test_param_string_positional_injection_05.

@Test
public void test_param_string_positional_injection_05() {
    // This injection attempt results in a valid query but a failed
    // injection
    String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ? . }";
    ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
    pss.setLiteral(0, "hello\" . ?s ?p ?o");
    Query q = pss.asQuery();
    Element el = q.getQueryPattern();
    if (el instanceof ElementTriplesBlock) {
        Assert.assertEquals(1, ((ElementTriplesBlock) q.getQueryPattern()).getPattern().size());
    } else if (el instanceof ElementGroup) {
        Assert.assertEquals(1, ((ElementGroup) el).getElements().size());
        el = ((ElementGroup) el).getElements().get(0);
        if (el instanceof ElementTriplesBlock) {
            Assert.assertEquals(1, ((ElementTriplesBlock) el).getPattern().size());
        }
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Test(org.junit.Test)

Example 13 with ElementGroup

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

the class labelSearch method buildSyntax.

// Build SPARQL syntax and compile it.
// Not recommended.
private QueryIterator buildSyntax(QueryIterator input, Node nodeVar, String pattern, ExecutionContext execCxt) {
    Var var2 = createNewVar();
    // Triple patterns for   ?x rdfs:label ?hiddenVar
    ElementTriplesBlock elementBGP = new ElementTriplesBlock();
    Triple t = new Triple(nodeVar, RDFS.label.asNode(), var2);
    elementBGP.addTriple(t);
    // Regular expression for  regex(?hiddenVar, "pattern", "i") 
    Expr regex = new E_Regex(new ExprVar(var2.getName()), pattern, "i");
    ElementGroup elementGroup = new ElementGroup();
    elementGroup.addElement(elementBGP);
    elementGroup.addElement(new ElementFilter(regex));
    // Compile it.
    // The better design is to build the Op structure programmatically,
    Op op = Algebra.compile(elementGroup);
    op = Algebra.optimize(op, execCxt.getContext());
    return QC.execute(op, input, execCxt);
}
Also used : Triple(org.apache.jena.graph.Triple) E_Regex(org.apache.jena.sparql.expr.E_Regex) ExprVar(org.apache.jena.sparql.expr.ExprVar) Op(org.apache.jena.sparql.algebra.Op) Expr(org.apache.jena.sparql.expr.Expr) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Example 14 with ElementGroup

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);
        }
    }
}
Also used : ExprVar(org.apache.jena.sparql.expr.ExprVar) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Triple(org.apache.jena.graph.Triple) E_Regex(org.apache.jena.sparql.expr.E_Regex) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Expr(org.apache.jena.sparql.expr.Expr) ElementFilter(org.apache.jena.sparql.syntax.ElementFilter)

Example 15 with ElementGroup

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);
        }
    }
}
Also used : Triple(org.apache.jena.graph.Triple) IndentedWriter(org.apache.jena.atlas.io.IndentedWriter) Var(org.apache.jena.sparql.core.Var) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Aggregations

ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)15 Element (org.apache.jena.sparql.syntax.Element)8 Var (org.apache.jena.sparql.core.Var)6 ElementTriplesBlock (org.apache.jena.sparql.syntax.ElementTriplesBlock)6 Triple (org.apache.jena.graph.Triple)5 Query (org.apache.jena.query.Query)5 Test (org.junit.Test)5 Node (org.apache.jena.graph.Node)3 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)3 UpdateBuilder (org.apache.jena.arq.querybuilder.UpdateBuilder)2 IndentedWriter (org.apache.jena.atlas.io.IndentedWriter)2 Quad (org.apache.jena.sparql.core.Quad)2 E_Regex (org.apache.jena.sparql.expr.E_Regex)2 Expr (org.apache.jena.sparql.expr.Expr)2 ExprVar (org.apache.jena.sparql.expr.ExprVar)2 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)2 ElementFilter (org.apache.jena.sparql.syntax.ElementFilter)2 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)2 Update (org.apache.jena.update.Update)2 ContractTest (org.xenei.junit.contract.ContractTest)2