Search in sources :

Example 36 with ElementPathBlock

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

the class WhereClauseTest method testAddGraph_triplePath.

@ContractTest
public void testAddGraph_triplePath() {
    final Node s = NodeFactory.createURI("s");
    final Node p = NodeFactory.createURI("p");
    final Node o = NodeFactory.createURI("o");
    TriplePath tp = new TriplePath(new Triple(s, p, o));
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addGraph("<g>", tp);
    Query query = builder.build();
    ElementPathBlock epb = new ElementPathBlock();
    ElementNamedGraph eng = new ElementNamedGraph(NodeFactory.createURI("g"), epb);
    epb.addTriplePath(new TriplePath(new Triple(s, p, o)));
    WhereValidator visitor = new WhereValidator(eng);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementNamedGraph(org.apache.jena.sparql.syntax.ElementNamedGraph) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) Node(org.apache.jena.graph.Node) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 37 with ElementPathBlock

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

the class UpdateBuilderTest method testInsertAndDelete.

@Test
public void testInsertAndDelete() {
    UpdateBuilder builder = new UpdateBuilder();
    builder.addInsert(new Quad(g, s, p, o));
    builder.addDelete(new Triple(s, p, o));
    builder.addWhere(null, p, "foo");
    Update update = builder.build();
    assertTrue(update instanceof UpdateModify);
    UpdateModify um = (UpdateModify) update;
    List<Quad> quads = um.getInsertQuads();
    assertEquals(1, quads.size());
    Quad q = quads.get(0);
    assertEquals(g, q.getGraph());
    assertEquals(s, q.getSubject());
    assertEquals(p, q.getPredicate());
    assertEquals(o, q.getObject());
    quads = um.getDeleteQuads();
    assertEquals(1, quads.size());
    q = quads.get(0);
    assertEquals(Quad.defaultGraphNodeGenerated, q.getGraph());
    assertEquals(s, q.getSubject());
    assertEquals(p, q.getPredicate());
    assertEquals(o, q.getObject());
    Element e = um.getWherePattern();
    assertTrue(e instanceof ElementGroup);
    ElementGroup eg = (ElementGroup) e;
    assertEquals(1, eg.getElements().size());
    ElementPathBlock epb = (ElementPathBlock) eg.getElements().get(0);
    Triple t = epb.getPattern().get(0).asTriple();
    assertEquals(Node.ANY, t.getSubject());
    assertEquals(p, t.getPredicate());
    assertEquals(builder.makeNode("foo"), t.getObject());
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) Element(org.apache.jena.sparql.syntax.Element) Update(org.apache.jena.update.Update) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 38 with ElementPathBlock

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

the class ElementRewriter method visit.

@Override
public void visit(ElementPathBlock el) {
    ElementPathBlock newBlock = new ElementPathBlock();
    Iterator<TriplePath> tIter = el.patternElts();
    while (tIter.hasNext()) {
        newBlock.addTriplePath(rewrite(tIter.next()));
    }
    push(newBlock);
}
Also used : TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Example 39 with ElementPathBlock

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

the class TestBlankNodeBinary method bNodeSPARQL_Query_1.

// Check SPARQL parsing.
@Test
public void bNodeSPARQL_Query_1() {
    String qs = "SELECT * { ?s ?p <_:ABC>}";
    Query query = QueryFactory.create(qs);
    Element el = ((ElementGroup) query.getQueryPattern()).get(0);
    ElementPathBlock epb = (ElementPathBlock) el;
    TriplePath tp = epb.getPattern().get(0);
    Triple t = tp.asTriple();
    assertEquals("ABC", t.getObject().getBlankNodeLabel());
}
Also used : Query(org.apache.jena.query.Query) Element(org.apache.jena.sparql.syntax.Element) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 40 with ElementPathBlock

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

the class TestBlankNodeBinary method bNodeSPARQL_Query_1.

// Check SPARQL parsing.
@Test
public void bNodeSPARQL_Query_1() {
    String qs = "SELECT * { ?s ?p <_:ABC>}";
    Query query = QueryFactory.create(qs);
    Element el = ((ElementGroup) query.getQueryPattern()).get(0);
    ElementPathBlock epb = (ElementPathBlock) el;
    TriplePath tp = epb.getPattern().get(0);
    Triple t = tp.asTriple();
    assertEquals("ABC", t.getObject().getBlankNodeLabel());
}
Also used : Query(org.apache.jena.query.Query) Element(org.apache.jena.sparql.syntax.Element) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Aggregations

ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)78 Triple (org.apache.jena.graph.Triple)62 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)60 TriplePath (org.apache.jena.sparql.core.TriplePath)56 Test (org.junit.Test)44 Query (org.apache.jena.query.Query)32 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)27 Node (org.apache.jena.graph.Node)26 ContractTest (org.xenei.junit.contract.ContractTest)25 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)22 FrontsTriple (org.apache.jena.graph.FrontsTriple)22 ElementOptional (org.apache.jena.sparql.syntax.ElementOptional)18 Var (org.apache.jena.sparql.core.Var)14 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)11 Element (org.apache.jena.sparql.syntax.Element)10 ElementUnion (org.apache.jena.sparql.syntax.ElementUnion)8 ExprVar (org.apache.jena.sparql.expr.ExprVar)7 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)7 Quad (org.apache.jena.sparql.core.Quad)5 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)5