Search in sources :

Example 56 with ElementPathBlock

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

the class WhereClauseTest method testSetVarsInOptional_Node_Variable.

@ContractTest
public void testSetVarsInOptional_Node_Variable() {
    Node v = NodeFactory.createVariable("v");
    Node one = NodeFactory.createURI("one");
    Node two = NodeFactory.createURI("two");
    Node three = NodeFactory.createURI("three");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addOptional(new Triple(one, two, v));
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(new Triple(one, two, Var.alloc(v)));
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    builder.setVar(v, three);
    epb = new ElementPathBlock();
    optional = new ElementOptional(epb);
    tp = new TriplePath(new Triple(one, two, three));
    epb.addTriplePath(tp);
    visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) 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 57 with ElementPathBlock

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

the class WhereClauseTest method testSetVarsInOptional.

@ContractTest
public void testSetVarsInOptional() {
    Var v = Var.alloc("v");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addOptional(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v));
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(NodeFactory.createURI("one"), new P_Link(NodeFactory.createURI("two")), v);
    epb.addTriple(tp);
    WhereValidator visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    builder.setVar(v, NodeFactory.createURI("three"));
    epb = new ElementPathBlock();
    optional = new ElementOptional(epb);
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
    epb.addTriple(t);
    visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) TriplePath(org.apache.jena.sparql.core.TriplePath) P_Link(org.apache.jena.sparql.path.P_Link) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 58 with ElementPathBlock

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

the class WhereClauseTest method testAddOptionalObjects.

@ContractTest
public void testAddOptionalObjects() {
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addOptional(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
    epb.addTriple(t);
    WhereValidator visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 59 with ElementPathBlock

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

the class SelectBuilderTest method testList.

@Test
public void testList() {
    builder.addVar("*").addWhere(builder.list("<one>", "?two", "'three'"), "<foo>", "<bar>");
    Query query = builder.build();
    Node one = NodeFactory.createURI("one");
    Node two = Var.alloc("two").asNode();
    Node three = NodeFactory.createLiteral("three");
    Node foo = NodeFactory.createURI("foo");
    Node bar = NodeFactory.createURI("bar");
    ElementPathBlock epb = new ElementPathBlock();
    Node firstObject = NodeFactory.createBlankNode();
    Node secondObject = NodeFactory.createBlankNode();
    Node thirdObject = NodeFactory.createBlankNode();
    epb.addTriplePath(new TriplePath(new Triple(firstObject, RDF.first.asNode(), one)));
    epb.addTriplePath(new TriplePath(new Triple(firstObject, RDF.rest.asNode(), secondObject)));
    epb.addTriplePath(new TriplePath(new Triple(secondObject, RDF.first.asNode(), two)));
    epb.addTriplePath(new TriplePath(new Triple(secondObject, RDF.rest.asNode(), thirdObject)));
    epb.addTriplePath(new TriplePath(new Triple(thirdObject, RDF.first.asNode(), three)));
    epb.addTriplePath(new TriplePath(new Triple(thirdObject, RDF.rest.asNode(), RDF.nil.asNode())));
    epb.addTriplePath(new TriplePath(new Triple(firstObject, foo, bar)));
    WhereValidator visitor = new WhereValidator(epb);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) Query(org.apache.jena.query.Query) Node(org.apache.jena.graph.Node) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test) AbstractRegexpBasedTest(org.apache.jena.arq.AbstractRegexpBasedTest)

Example 60 with ElementPathBlock

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

the class UpdateBuilderTest method testInsertAndDeleteWithVariableNode.

@Test
public void testInsertAndDeleteWithVariableNode() {
    UpdateBuilder builder = new UpdateBuilder();
    Node v = NodeFactory.createVariable("v");
    builder.addInsert(new Quad(g, s, v, o));
    builder.addDelete(new Triple(s, v, o));
    builder.addWhere(null, v, "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(v, q.getPredicate());
    assertEquals(o, q.getObject());
    assertTrue(Var.isVar(q.getPredicate()));
    quads = um.getDeleteQuads();
    assertEquals(1, quads.size());
    q = quads.get(0);
    assertEquals(Quad.defaultGraphNodeGenerated, q.getGraph());
    assertEquals(s, q.getSubject());
    assertEquals(v, q.getPredicate());
    assertEquals(o, q.getObject());
    assertTrue(Var.isVar(q.getPredicate()));
    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(v, t.getPredicate());
    assertEquals(builder.makeNode("foo"), t.getObject());
    assertTrue(Var.isVar(t.getPredicate()));
}
Also used : Triple(org.apache.jena.graph.Triple) Quad(org.apache.jena.sparql.core.Quad) UpdateModify(org.apache.jena.sparql.modify.request.UpdateModify) Node(org.apache.jena.graph.Node) 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)

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