Search in sources :

Example 1 with SelectBuilder

use of org.apache.jena.arq.querybuilder.SelectBuilder in project jena by apache.

the class WhereHandlerTest method testAddSubQueryWithVarExpressions.

@Test
public void testAddSubQueryWithVarExpressions() throws ParseException {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addVar("count(*)", "?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.allocAggregate(new AggCount()));
    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) AggCount(org.apache.jena.sparql.expr.aggregate.AggCount) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 2 with SelectBuilder

use of org.apache.jena.arq.querybuilder.SelectBuilder in project jena by apache.

the class WhereHandlerTest method testAddSubQueryWithoutVars.

@Test
public void testAddSubQueryWithoutVars() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").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.setQuerySelectType();
    q.setQueryResultStar(true);
    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 3 with SelectBuilder

use of org.apache.jena.arq.querybuilder.SelectBuilder in project jena by apache.

the class WhereHandlerTest method testSetVarsInSubQuery.

@Test
public void testSetVarsInSubQuery() {
    Var v = Var.alloc("v");
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
    handler.addSubQuery(sb);
    handler.build();
    TriplePath tp = new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), v));
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(tp);
    WhereValidator wv = new WhereValidator(epb);
    query.getQueryPattern().visit(wv);
    assertTrue(wv.matching);
    Map<Var, Node> values = new HashMap<>();
    values.put(v, NodeFactory.createURI("three"));
    handler.setVars(values);
    handler.build();
    tp = new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    epb = new ElementPathBlock();
    epb.addTriple(tp);
    wv = new WhereValidator(epb);
    query.getQueryPattern().visit(wv);
    assertTrue(wv.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 4 with SelectBuilder

use of org.apache.jena.arq.querybuilder.SelectBuilder in project jena by apache.

the class WhereHandlerTest method testAddUnionOfOne.

@Test
public void testAddUnionOfOne() {
    Triple t1 = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"));
    SelectBuilder sb = new SelectBuilder().addWhere(t1);
    handler.addUnion(sb);
    handler.build();
    ElementPathBlock epb1 = new ElementPathBlock();
    epb1.addTriple(t1);
    WhereValidator visitor = new WhereValidator(epb1);
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) 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)

Example 5 with SelectBuilder

use of org.apache.jena.arq.querybuilder.SelectBuilder in project jena by apache.

the class WhereHandlerTest method testSetVarsInUnion.

@Test
public void testSetVarsInUnion() {
    Var v = Var.alloc("v");
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
    handler.addUnion(sb);
    SelectBuilder sb2 = new SelectBuilder().addWhere("<uno>", "<dos>", "<tres>");
    handler.addUnion(sb2);
    handler.build();
    Node one = NodeFactory.createURI("one");
    Node two = NodeFactory.createURI("two");
    Node three = NodeFactory.createURI("three");
    Node uno = NodeFactory.createURI("uno");
    Node dos = NodeFactory.createURI("dos");
    Node tres = NodeFactory.createURI("tres");
    ElementUnion union = new ElementUnion();
    ElementPathBlock epb = new ElementPathBlock();
    Triple t = new Triple(one, two, v.asNode());
    epb.addTriple(t);
    union.addElement(epb);
    ElementPathBlock epb2 = new ElementPathBlock();
    t = new Triple(uno, dos, tres);
    epb2.addTriple(t);
    union.addElement(epb2);
    WhereValidator visitor = new WhereValidator(union);
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    Map<Var, Node> values = new HashMap<>();
    values.put(v, three);
    handler.setVars(values);
    handler.build();
    union = new ElementUnion();
    epb = new ElementPathBlock();
    t = new Triple(one, two, three);
    epb.addTriple(t);
    union.addElement(epb);
    epb2 = new ElementPathBlock();
    t = new Triple(uno, dos, tres);
    epb2.addTriple(t);
    union.addElement(epb2);
    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) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Var(org.apache.jena.sparql.core.Var) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) Node(org.apache.jena.graph.Node) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

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