Search in sources :

Example 11 with ElementSubQuery

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

the class WhereHandlerTest method testAddSubQueryWithVars.

@Test
public void testAddSubQueryWithVars() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addVar("?x").addWhere("<one>", "<two>", "three");
    handler.addSubQuery(sb);
    handler.build();
    Triple t1 = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"));
    TriplePath tp = new TriplePath(t1);
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(tp);
    Query q = new Query();
    q.addResultVar("x");
    q.setQuerySelectType();
    q.setQueryPattern(epb);
    ElementSubQuery esq = new ElementSubQuery(q);
    WhereValidator wv = new WhereValidator(esq);
    query.getQueryPattern().visit(wv);
    assertTrue(wv.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) 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) Test(org.junit.Test)

Example 12 with ElementSubQuery

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

the class WhereHandlerTest method testAddUnionToExistingWithVar.

@Test
public void testAddUnionToExistingWithVar() {
    handler.addWhere(new TriplePath(new Triple(NodeFactory.createURI("s"), NodeFactory.createURI("p"), NodeFactory.createURI("o"))));
    SelectBuilder sb = new SelectBuilder().addVar("x").addWhere(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    handler.addUnion(sb);
    handler.build();
    Query q = new Query();
    q.setQuerySelectType();
    ElementPathBlock epb1 = new ElementPathBlock();
    epb1.addTriple(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    q.setQueryPattern(epb1);
    q.addProjectVars(Arrays.asList(Var.alloc("x")));
    ElementSubQuery sq = new ElementSubQuery(q);
    ElementPathBlock epb2 = new ElementPathBlock();
    epb2.addTriple(new Triple(NodeFactory.createURI("s"), NodeFactory.createURI("p"), NodeFactory.createURI("o")));
    ElementUnion union = new ElementUnion();
    union.addElement(epb2);
    union.addElement(sq);
    WhereValidator visitor = new WhereValidator(union);
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) ElementSubQuery(org.apache.jena.sparql.syntax.ElementSubQuery) ElementUnion(org.apache.jena.sparql.syntax.ElementUnion) 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) Test(org.junit.Test)

Example 13 with ElementSubQuery

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

use of org.apache.jena.sparql.syntax.ElementSubQuery 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)

Example 15 with ElementSubQuery

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

the class WhereHandlerTest method testMakeSubQueryFromConstruct.

@Test
public void testMakeSubQueryFromConstruct() {
    AbstractQueryBuilder<?> sb = new ConstructBuilder().addConstruct("?x", RDF.type, NodeFactory.createURI("foo")).addWhere("?x", RDF.type, RDF.Alt);
    ElementSubQuery esq = handler.makeSubQuery(sb);
    Triple t1 = new Triple(NodeFactory.createVariable("x"), RDF.type.asNode(), RDF.Alt.asNode());
    TriplePath tp = new TriplePath(t1);
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(tp);
    Query q = new Query();
    q.setQuerySelectType();
    q.setQueryResultStar(true);
    q.setQueryPattern(epb);
    ElementSubQuery esq2 = new ElementSubQuery(q);
    WhereValidator wv = new WhereValidator(esq2);
    esq.visit(wv);
    assertTrue(wv.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) 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) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ConstructBuilder(org.apache.jena.arq.querybuilder.ConstructBuilder) Test(org.junit.Test)

Aggregations

Query (org.apache.jena.query.Query)19 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)19 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)15 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)15 Triple (org.apache.jena.graph.Triple)14 TriplePath (org.apache.jena.sparql.core.TriplePath)13 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)11 Test (org.junit.Test)11 ContractTest (org.xenei.junit.contract.ContractTest)4 FrontsTriple (org.apache.jena.graph.FrontsTriple)3 ElementUnion (org.apache.jena.sparql.syntax.ElementUnion)3 Var (org.apache.jena.sparql.core.Var)2 HttpClient (java.net.http.HttpClient)1 AskBuilder (org.apache.jena.arq.querybuilder.AskBuilder)1 ConstructBuilder (org.apache.jena.arq.querybuilder.ConstructBuilder)1 DescribeBuilder (org.apache.jena.arq.querybuilder.DescribeBuilder)1 WhereBuilder (org.apache.jena.arq.querybuilder.WhereBuilder)1 HttpException (org.apache.jena.atlas.web.HttpException)1 Node (org.apache.jena.graph.Node)1 RegistryHttpClient (org.apache.jena.http.RegistryHttpClient)1