Search in sources :

Example 16 with SelectBuilder

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

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

the class WhereHandlerTest method testMakeSubQueryFromSelectWithOutVar.

@Test
public void testMakeSubQueryFromSelectWithOutVar() {
    SelectBuilder sb = new SelectBuilder().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) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 18 with SelectBuilder

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

the class WhereHandlerTest method testAddUnion.

@Test
public void testAddUnion() {
    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 sb1 = new SelectBuilder().addWhere(t1);
    SelectBuilder sb2 = new SelectBuilder().addWhere(t2);
    handler.addUnion(sb1);
    handler.addUnion(sb2);
    handler.build();
    ElementUnion union = new ElementUnion();
    ElementPathBlock epb1 = new ElementPathBlock();
    epb1.addTriple(t1);
    union.addElement(epb1);
    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) 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 19 with SelectBuilder

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

the class WhereHandlerTest method testAddUnionToExisting.

@Test
public void testAddUnionToExisting() {
    handler.addWhere(new TriplePath(new Triple(NodeFactory.createURI("s"), NodeFactory.createURI("p"), NodeFactory.createURI("o"))));
    SelectBuilder sb = new SelectBuilder();
    sb.addWhere(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    handler.addUnion(sb);
    handler.build();
    TriplePath tp1 = new TriplePath(new Triple(NodeFactory.createURI("s"), NodeFactory.createURI("p"), NodeFactory.createURI("o")));
    ElementPathBlock epb1 = new ElementPathBlock();
    epb1.addTriple(tp1);
    TriplePath tp2 = new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    ElementPathBlock epb2 = new ElementPathBlock();
    epb2.addTriple(tp2);
    ElementUnion union = new ElementUnion();
    union.addElement(epb1);
    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) 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 20 with SelectBuilder

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

the class WhereClauseTest method testAddOptionalGroupPattern.

@ContractTest
public void testAddOptionalGroupPattern() {
    Var s = Var.alloc("s");
    Node q = NodeFactory.createURI("urn:q");
    Node v = NodeFactory.createURI("urn:v");
    Var x = Var.alloc("x");
    Node n123 = NodeFactory.createLiteral(LiteralLabelFactory.createTypedLiteral(123));
    SelectBuilder pattern = new SelectBuilder();
    pattern.addWhere(new Triple(s, q, n123));
    pattern.addWhere(new Triple(s, v, x));
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addOptional(pattern);
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(new Triple(s, q, n123));
    epb.addTriplePath(tp);
    tp = new TriplePath(new Triple(s, v, x));
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) FrontsTriple(org.apache.jena.graph.FrontsTriple) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) 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) 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)

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