Search in sources :

Example 21 with SelectBuilder

use of org.apache.jena.arq.querybuilder.SelectBuilder 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 22 with SelectBuilder

use of org.apache.jena.arq.querybuilder.SelectBuilder 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

SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)22 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)22 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)22 Triple (org.apache.jena.graph.Triple)21 TriplePath (org.apache.jena.sparql.core.TriplePath)15 Query (org.apache.jena.query.Query)14 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)14 Test (org.junit.Test)13 ContractTest (org.xenei.junit.contract.ContractTest)9 FrontsTriple (org.apache.jena.graph.FrontsTriple)8 ElementUnion (org.apache.jena.sparql.syntax.ElementUnion)8 Node (org.apache.jena.graph.Node)7 Var (org.apache.jena.sparql.core.Var)6 ExprVar (org.apache.jena.sparql.expr.ExprVar)4 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 ElementMinus (org.apache.jena.sparql.syntax.ElementMinus)2 ElementOptional (org.apache.jena.sparql.syntax.ElementOptional)2 AggCount (org.apache.jena.sparql.expr.aggregate.AggCount)1 P_Link (org.apache.jena.sparql.path.P_Link)1