Search in sources :

Example 16 with Element

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

the class ParserARQ method parseElement.

public static Element parseElement(String string) {
    final Query query = new Query();
    Action action = new Action() {

        @Override
        public void exec(ARQParser parser) throws Exception {
            Element el = parser.GroupGraphPattern();
            query.setQueryPattern(el);
        }
    };
    perform(query, string, action);
    return query.getQueryPattern();
}
Also used : Query(org.apache.jena.query.Query) ARQParser(org.apache.jena.sparql.lang.arq.ARQParser) Element(org.apache.jena.sparql.syntax.Element)

Example 17 with Element

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

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

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

the class AbstractTestUpdateGraph method testModifyInitialBindings.

@Test
public void testModifyInitialBindings() {
    DatasetGraph gStore = getEmptyDatasetGraph();
    defaultGraphData(gStore, data12());
    namedGraphData(gStore, graphIRI, Factory.createDefaultGraph());
    Binding initialBinding = BindingFactory.binding(Var.alloc("o"), o1);
    UpdateModify modify = new UpdateModify();
    Element element = QueryFactory.createElement("{ ?s <http://example/p> ?o }");
    modify.setElement(element);
    modify.getInsertAcc().addQuad(new Quad(graphIRI, triple1));
    modify.getDeleteAcc().addTriple(SSE.parseTriple("(?s <http://example/p> ?o)"));
    modify.getDeleteAcc().addQuad(SSE.parseQuad("(<http://example/graph> ?s <http://example/p> ?o)"));
    UpdateAction.execute(modify, gStore, initialBinding);
    assertFalse(graphEmpty(gStore.getGraph(graphIRI)));
    assertFalse(graphEmpty(gStore.getDefaultGraph()));
    assertTrue(graphContains(gStore.getGraph(graphIRI), triple1));
    assertTrue(graphContains(gStore.getDefaultGraph(), triple2));
    assertFalse(graphContains(gStore.getDefaultGraph(), triple1));
}
Also used : Binding(org.apache.jena.sparql.engine.binding.Binding) Quad(org.apache.jena.sparql.core.Quad) Element(org.apache.jena.sparql.syntax.Element) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

Example 20 with Element

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

the class AbstractTestUpdateGraph method testDelete5.

@Test
public void testDelete5() {
    DatasetGraph gStore = getEmptyDatasetGraph();
    defaultGraphData(gStore, data2());
    namedGraphData(gStore, graphIRI, data1());
    UpdateModify modify = new UpdateModify();
    Element element = QueryFactory.createElement("{ ?s <http://example/p> ?o }");
    modify.setElement(element);
    modify.getDeleteAcc().addQuad(SSE.parseQuad("(<http://example/graph> ?s <http://example/p> 2007 )"));
    UpdateAction.execute(modify, gStore);
    assertTrue("Not empty", graphEmpty(gStore.getGraph(graphIRI)));
    assertFalse(graphEmpty(gStore.getDefaultGraph()));
}
Also used : Element(org.apache.jena.sparql.syntax.Element) DatasetGraph(org.apache.jena.sparql.core.DatasetGraph) Test(org.junit.Test)

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