Search in sources :

Example 6 with ElementUnion

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

the class WhereClauseTest method testAddUnion.

@ContractTest
public void testAddUnion() {
    SelectBuilder sb = new SelectBuilder();
    sb.addPrefix("pfx", "uri").addVar("?x").addWhere("<one>", "<two>", "three");
    WhereClause<?> whereClause = getProducer().newInstance();
    whereClause.getWhereHandler().addWhere(new TriplePath(Triple.ANY));
    AbstractQueryBuilder<?> builder = whereClause.addUnion(sb);
    ElementUnion union = new ElementUnion();
    ElementPathBlock epb = new ElementPathBlock();
    union.addElement(epb);
    epb.addTriple(Triple.ANY);
    Query subQuery = new Query();
    ElementSubQuery esq = new ElementSubQuery(subQuery);
    union.addElement(esq);
    epb = new ElementPathBlock();
    subQuery.setQuerySelectType();
    subQuery.addProjectVars(Arrays.asList("x"));
    subQuery.setQueryPattern(epb);
    Triple t = new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createLiteral("three"));
    epb.addTriple(t);
    WhereValidator visitor = new WhereValidator(union);
    Query result = builder.build();
    result.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
    assertEquals("uri", result.getPrefixMapping().getNsPrefixURI("pfx"));
}
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) 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 7 with ElementUnion

use of org.apache.jena.sparql.syntax.ElementUnion 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 8 with ElementUnion

use of org.apache.jena.sparql.syntax.ElementUnion 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 9 with ElementUnion

use of org.apache.jena.sparql.syntax.ElementUnion 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 10 with ElementUnion

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

the class WhereHandler method addUnion.

/**
 * Add a union to the where clause.
 *
 * @param subQuery The subquery to add as the union.
 */
public void addUnion(AbstractQueryBuilder<?> subQuery) {
    ElementUnion union = null;
    ElementGroup clause = getClause();
    // if the last element is a union make sure we add to it.
    if (!clause.isEmpty()) {
        Element lastElement = clause.getElements().get(clause.getElements().size() - 1);
        if (lastElement instanceof ElementUnion) {
            union = (ElementUnion) lastElement;
        } else {
            // clauses is not empty and is not a union so it is the left
            // side of the union.
            union = new ElementUnion();
            union.addElement(clause);
            query.setQueryPattern(union);
        }
    } else {
        // add the union as the first element in the clause.
        union = new ElementUnion();
        clause.addElement(union);
    }
    // otherwise just add the clause.
    if (subQuery instanceof SelectClause && ((SelectClause<?>) subQuery).getVars().size() > 0) {
        union.addElement(makeSubQuery(subQuery));
    } else {
        PrologHandler ph = new PrologHandler(query);
        ph.addPrefixes(subQuery.getPrologHandler().getPrefixes());
        union.addElement(subQuery.getWhereHandler().getClause());
    }
}
Also used : SelectClause(org.apache.jena.arq.querybuilder.clauses.SelectClause) ElementUnion(org.apache.jena.sparql.syntax.ElementUnion) Element(org.apache.jena.sparql.syntax.Element) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup)

Aggregations

ElementUnion (org.apache.jena.sparql.syntax.ElementUnion)11 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)8 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)8 Triple (org.apache.jena.graph.Triple)8 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)8 Query (org.apache.jena.query.Query)5 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)5 Test (org.junit.Test)5 FrontsTriple (org.apache.jena.graph.FrontsTriple)3 Node (org.apache.jena.graph.Node)3 TriplePath (org.apache.jena.sparql.core.TriplePath)3 Element (org.apache.jena.sparql.syntax.Element)3 ContractTest (org.xenei.junit.contract.ContractTest)3 SelectClause (org.apache.jena.arq.querybuilder.clauses.SelectClause)2 Var (org.apache.jena.sparql.core.Var)2 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 ExprVar (org.apache.jena.sparql.expr.ExprVar)1