Search in sources :

Example 6 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 7 with SelectBuilder

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

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

the class WhereClauseTest method testSetVarsInUnion_Node_Variable.

@ContractTest
public void testSetVarsInUnion_Node_Variable() {
    Node v = NodeFactory.createVariable("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, Var.alloc(v));
    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) 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 9 with SelectBuilder

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

the class WhereClauseTest method testAddOptionalGroupPattern_VariableNode.

@ContractTest
public void testAddOptionalGroupPattern_VariableNode() {
    Node s = NodeFactory.createVariable("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(Var.alloc(s), q, n123));
    epb.addTriplePath(tp);
    tp = new TriplePath(new Triple(Var.alloc(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)

Example 10 with SelectBuilder

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

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