Search in sources :

Example 6 with ElementSubQuery

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

the class WhereHandlerTest method testMakeSubQueryFromWhere.

@Test
public void testMakeSubQueryFromWhere() {
    AbstractQueryBuilder<?> sb = new WhereBuilder().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) WhereBuilder(org.apache.jena.arq.querybuilder.WhereBuilder) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 7 with ElementSubQuery

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

the class WhereClauseTest method testSetVarsInSubQuery_Node_Variable.

@ContractTest
public void testSetVarsInSubQuery_Node_Variable() {
    Node v = NodeFactory.createVariable("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);
    TriplePath tp = new TriplePath(NodeFactory.createURI("one"), new P_Link(NodeFactory.createURI("two")), NodeFactory.createVariable("v"));
    epb.addTriple(tp);
    WhereValidator visitor = new WhereValidator(esq);
    builder.build().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);
    tp = new TriplePath(NodeFactory.createURI("one"), new P_Link(NodeFactory.createURI("two")), NodeFactory.createURI("three"));
    epb.addTriple(tp);
    visitor = new WhereValidator(esq);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : 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) Node(org.apache.jena.graph.Node) TriplePath(org.apache.jena.sparql.core.TriplePath) P_Link(org.apache.jena.sparql.path.P_Link) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 8 with ElementSubQuery

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

the class WhereClauseTest method testAddSubQuery.

@ContractTest
public void testAddSubQuery() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "urn:uri:").addVar("?x").addWhere("pfx:one", "pfx:two", "pfx:three");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addSubQuery(sb);
    Query query = builder.build();
    Query q2 = new Query();
    q2.setQuerySelectType();
    q2.addProjectVars(Arrays.asList(Var.alloc("x")));
    ElementPathBlock epb = new ElementPathBlock();
    q2.setQueryPattern(epb);
    epb.addTriplePath(new TriplePath(new Triple(NodeFactory.createURI("urn:uri:one"), NodeFactory.createURI("urn:uri:two"), NodeFactory.createURI("urn:uri:three"))));
    ElementSubQuery esq = new ElementSubQuery(q2);
    WhereValidator visitor = new WhereValidator(esq);
    query.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) 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 9 with ElementSubQuery

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

the class WhereHandlerTest method testMakeSubQueryFromAsk.

@Test
public void testMakeSubQueryFromAsk() {
    AbstractQueryBuilder<?> sb = new AskBuilder().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) AskBuilder(org.apache.jena.arq.querybuilder.AskBuilder) Test(org.junit.Test)

Example 10 with ElementSubQuery

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

the class WhereHandlerTest method testAddUnionWithVar.

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