Search in sources :

Example 46 with ElementPathBlock

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

the class WhereHandlerTest method testAddOptionalWhereHandler.

@Test
public void testAddOptionalWhereHandler() {
    WhereHandler pattern = new WhereHandler(new Query());
    Var s = Var.alloc("s");
    Node q = NodeFactory.createURI("urn:q");
    Node v = NodeFactory.createURI("urn:v");
    Var x = Var.alloc("x");
    Node n123 = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(123));
    pattern.addWhere(new TriplePath(new Triple(s, q, n123)));
    pattern.addWhere(new TriplePath(new Triple(s, v, x)));
    handler.addOptional(pattern);
    handler.build();
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(new Triple(s, q, n123));
    epb.addTriplePath(tp);
    tp = new TriplePath(new Triple(s, v, x));
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(optional);
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) Var(org.apache.jena.sparql.core.Var) 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) Test(org.junit.Test)

Example 47 with ElementPathBlock

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

the class WhereHandlerTest method testAddOptionalObjects.

@Test
public void testAddOptionalObjects() {
    handler.addOptional(new TriplePath(new Triple(NodeFactory.createURI("one"), ResourceFactory.createResource("two").asNode(), ResourceFactory.createLangLiteral("three", "en-US").asNode())));
    handler.build();
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(new Triple(NodeFactory.createURI("one"), ResourceFactory.createResource("two").asNode(), ResourceFactory.createLangLiteral("three", "en-US").asNode()));
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(optional);
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 48 with ElementPathBlock

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

the class WhereClauseTest method addGraph_GSPO.

public void addGraph_GSPO() {
    final Node s = NodeFactory.createURI("s");
    final Node p = NodeFactory.createURI("p");
    final Node o = NodeFactory.createURI("o");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addGraph("<g>", s, p, o);
    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)

Example 49 with ElementPathBlock

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

the class WhereClauseTest method testAddUnion.

@ContractTest
public void testAddUnion() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addVar("?x").addWhere("<one>", "<two>", "three");
    WhereClause<?> whereClause = getProducer().newInstance();
    whereClause.getWhereHandler().addWhere(new TriplePath(Triple.ANY));
    AbstractQueryBuilder<?> builder = whereClause.addUnion(sb);
    ElementUnion union = new ElementUnion();
    ElementPathBlock epb = new ElementPathBlock();
    union.addElement(epb);
    epb.addTriple(Triple.ANY);
    Query subQuery = new Query();
    ElementSubQuery esq = new ElementSubQuery(subQuery);
    union.addElement(esq);
    epb = new ElementPathBlock();
    subQuery.setQuerySelectType();
    subQuery.addProjectVars(Arrays.asList("x"));
    subQuery.setQueryPattern(epb);
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"));
    epb.addTriple(t);
    WhereValidator visitor = new WhereValidator(union);
    Query result = builder.build();
    result.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    assertEquals("uri", result.getPrefixMapping().getNsPrefixURI("pfx"));
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementUnion(org.apache.jena.sparql.syntax.ElementUnion) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) TriplePath(org.apache.jena.sparql.core.TriplePath) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 50 with ElementPathBlock

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

the class WhereClauseTest method testSetVarsInSubQuery.

@ContractTest
public void testSetVarsInSubQuery() {
    Var v = Var.alloc("v");
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addSubQuery(sb);
    Query subQuery = new Query();
    subQuery.setQuerySelectType();
    subQuery.setQueryResultStar(true);
    ElementSubQuery esq = new ElementSubQuery(subQuery);
    ElementPathBlock epb = new ElementPathBlock();
    subQuery.setQueryPattern(epb);
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createVariable("v"));
    epb.addTriple(t);
    WhereValidator visitor = new WhereValidator(esq);
    Query result = builder.build();
    result.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    builder.setVar(v, NodeFactory.createURI("three"));
    subQuery = new Query();
    subQuery.setQuerySelectType();
    subQuery.setQueryResultStar(true);
    esq = new ElementSubQuery(subQuery);
    epb = new ElementPathBlock();
    subQuery.setQueryPattern(epb);
    t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
    epb.addTriple(t);
    visitor = new WhereValidator(esq);
    result = builder.build();
    result.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) Query(org.apache.jena.query.Query) ExprVar(org.apache.jena.sparql.expr.ExprVar) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

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