Search in sources :

Example 6 with ElementGroup

use of org.apache.jena.sparql.syntax.ElementGroup 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 7 with ElementGroup

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

the class SelectHandlerTest method testAddAllVarsDistinct.

@Test
public void testAddAllVarsDistinct() {
    AggregationHandler aggHandler = new AggregationHandler(new Query());
    SelectHandler sh = new SelectHandler(aggHandler);
    sh.addVar(Var.alloc("foo"));
    sh.setDistinct(true);
    handler.addAll(sh);
    // make sure warning does not fire.
    query.setQueryPattern(new ElementGroup());
    assertTrue(query.isDistinct());
    assertFalse(query.isQueryResultStar());
    assertEquals(1, query.getResultVars().size());
}
Also used : Query(org.apache.jena.query.Query) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) Test(org.junit.Test)

Example 8 with ElementGroup

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

the class QueryExecutionBase method execDescribe.

@Override
public Model execDescribe(Model model) {
    checkNotClosed();
    if (!query.isDescribeType())
        throw new QueryExecException("Attempt to get a DESCRIBE result from a " + labelForQuery(query) + " query");
    //Was: query.setQueryResultStar(true) ; but why?
    query.setResultVars();
    // If there was no WhereClause, use an empty pattern (one solution, no columns). 
    if (query.getQueryPattern() == null)
        query.setQueryPattern(new ElementGroup());
    Set<RDFNode> set = new HashSet<>();
    //May return null (no query pattern) 
    ResultSet qRes = execResultSet();
    // Prefixes for result (after initialization)
    insertPrefixesInto(model);
    if (qRes != null) {
        for (; qRes.hasNext(); ) {
            QuerySolution rb = qRes.nextSolution();
            for (String varName : query.getResultVars()) {
                RDFNode n = rb.get(varName);
                set.add(n);
            }
        }
    }
    if (query.getResultURIs() != null) {
        // Any URIs in the DESCRIBE
        for (Node n : query.getResultURIs()) {
            // Need to make dataset available to describe handlers.
            RDFNode rNode = ModelUtils.convertGraphNodeToRDFNode(n, dataset.getDefaultModel());
            set.add(rNode);
        }
    }
    // Create new handlers for this process.
    List<DescribeHandler> dhList = DescribeHandlerRegistry.get().newHandlerList();
    getContext().put(ARQConstants.sysCurrentDataset, getDataset());
    // Notify start of describe phase
    for (DescribeHandler dh : dhList) dh.start(model, getContext());
    // Do describe for each resource found.
    for (RDFNode n : set) {
        if (n instanceof Resource) {
            for (DescribeHandler dh : dhList) {
                dh.describe((Resource) n);
            }
        } else {
            // Can't describe literals
            continue;
        }
    }
    for (DescribeHandler dh : dhList) dh.finish();
    this.close();
    return model;
}
Also used : DescribeHandler(org.apache.jena.sparql.core.describe.DescribeHandler) Node(org.apache.jena.graph.Node) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) HashSet(java.util.HashSet)

Example 9 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 10 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)

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