Search in sources :

Example 11 with SelectBuilder

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

the class WhereClauseTest method testAddMinus.

@ContractTest
public void testAddMinus() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addVar("?x").addWhere("<one>", "<two>", "three");
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addMinus(sb);
    Query query = builder.build();
    ElementPathBlock epb = new ElementPathBlock();
    ElementMinus minus = new ElementMinus(epb);
    epb.addTriplePath(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"))));
    WhereValidator visitor = new WhereValidator(minus);
    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) Query(org.apache.jena.query.Query) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) ElementMinus(org.apache.jena.sparql.syntax.ElementMinus) 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 12 with SelectBuilder

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

the class WhereClauseTest method testSetVarsInUnion.

@ContractTest
public void testSetVarsInUnion() {
    Var v = Var.alloc("v");
    SelectBuilder sb1 = new SelectBuilder().addPrefix("pfx", "uri").addWhere("<one>", "<two>", v);
    WhereClause<?> whereClause = getProducer().newInstance();
    whereClause.addUnion(sb1);
    SelectBuilder sb2 = new SelectBuilder().addWhere("<uno>", "<dos>", "<tres>");
    AbstractQueryBuilder<?> builder = whereClause.addUnion(sb2);
    Query query = builder.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);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    builder.setVar(v, NodeFactory.createURI("three"));
    query = builder.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);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
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) 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) Node(org.apache.jena.graph.Node) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 13 with SelectBuilder

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

the class WhereHandlerTest method testAddMinus.

@Test
public void testAddMinus() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addWhere("<one>", "<two>", "three");
    handler.addMinus(sb);
    handler.build();
    ElementPathBlock epb = new ElementPathBlock();
    ElementMinus minus = new ElementMinus(epb);
    epb.addTriplePath(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"))));
    WhereValidator visitor = new WhereValidator(minus);
    query.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) ElementMinus(org.apache.jena.sparql.syntax.ElementMinus) 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 14 with SelectBuilder

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

Example 15 with SelectBuilder

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

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