Search in sources :

Example 26 with ElementPathBlock

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

the class WhereClauseTest method testAddOptionalStringWithPath.

@ContractTest
public void testAddOptionalStringWithPath() {
    WhereClause<?> whereClause = getProducer().newInstance();
    AbstractQueryBuilder<?> builder = whereClause.addOptional("<one>", "<two>/<dos>", "three");
    Path path = new P_Seq(new P_Link(NodeFactory.createURI("two")), new P_Link(NodeFactory.createURI("dos")));
    ElementPathBlock epb = new ElementPathBlock();
    ElementOptional optional = new ElementOptional(epb);
    TriplePath tp = new TriplePath(NodeFactory.createURI("one"), path, NodeFactory.createLiteral("three"));
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(optional);
    builder.build().getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : TriplePath(org.apache.jena.sparql.core.TriplePath) Path(org.apache.jena.sparql.path.Path) P_Seq(org.apache.jena.sparql.path.P_Seq) ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) P_Link(org.apache.jena.sparql.path.P_Link) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) ContractTest(org.xenei.junit.contract.ContractTest)

Example 27 with ElementPathBlock

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

the class WhereHandlerTest method testMakeSubQueryFromAsk.

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

Example 28 with ElementPathBlock

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

the class WhereHandlerTest method testAddWhereStrings.

@Test
public void testAddWhereStrings() {
    handler.addWhere(new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three"))));
    handler.build();
    ElementPathBlock epb = new ElementPathBlock();
    TriplePath tp = new TriplePath(new Triple(NodeFactory.createURI("one"), NodeFactory.createURI("two"), NodeFactory.createURI("three")));
    epb.addTriplePath(tp);
    WhereValidator visitor = new WhereValidator(epb);
    handler.getQueryPattern().visit(visitor);
    assertTrue(visitor.matching);
}
Also used : Triple(org.apache.jena.graph.Triple) WhereValidator(org.apache.jena.arq.querybuilder.WhereValidator) TriplePath(org.apache.jena.sparql.core.TriplePath) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock) Test(org.junit.Test)

Example 29 with ElementPathBlock

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

the class WhereQuadHolder method addOptional.

/**
 * Add an optional triple to the where clause
 *
 * @param t The triple path to add.
 * @throws IllegalArgumentException If the triple is not a valid triple for a
 * where clause.
 */
public void addOptional(TriplePath t) throws IllegalArgumentException {
    testTriple(t);
    ElementPathBlock epb = new ElementPathBlock();
    epb.addTriple(t);
    ElementOptional opt = new ElementOptional(epb);
    getClause().addElement(opt);
}
Also used : ElementOptional(org.apache.jena.sparql.syntax.ElementOptional) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Example 30 with ElementPathBlock

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

the class WhereQuadHolder method addWhere.

/**
 * Add the triple path to the where clause
 *
 * @param t The triple path to add.
 * @throws IllegalArgumentException If the triple path is not a valid triple
 * path for a where clause.
 */
public void addWhere(TriplePath t) throws IllegalArgumentException {
    testTriple(t);
    ElementGroup eg = getClause();
    List<Element> lst = eg.getElements();
    if (lst.isEmpty()) {
        ElementPathBlock epb = new ElementPathBlock();
        epb.addTriple(t);
        eg.addElement(epb);
    } else {
        Element e = lst.get(lst.size() - 1);
        if (e instanceof ElementTriplesBlock && t.isTriple()) {
            ElementTriplesBlock etb = (ElementTriplesBlock) e;
            etb.addTriple(t.asTriple());
        } else if (e instanceof ElementPathBlock) {
            ElementPathBlock epb = (ElementPathBlock) e;
            epb.addTriple(t);
        } else {
            ElementPathBlock etb = new ElementPathBlock();
            etb.addTriple(t);
            eg.addElement(etb);
        }
    }
}
Also used : Element(org.apache.jena.sparql.syntax.Element) ElementTriplesBlock(org.apache.jena.sparql.syntax.ElementTriplesBlock) ElementGroup(org.apache.jena.sparql.syntax.ElementGroup) ElementPathBlock(org.apache.jena.sparql.syntax.ElementPathBlock)

Aggregations

ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)78 Triple (org.apache.jena.graph.Triple)62 WhereValidator (org.apache.jena.arq.querybuilder.WhereValidator)60 TriplePath (org.apache.jena.sparql.core.TriplePath)56 Test (org.junit.Test)44 Query (org.apache.jena.query.Query)32 ElementSubQuery (org.apache.jena.sparql.syntax.ElementSubQuery)27 Node (org.apache.jena.graph.Node)26 ContractTest (org.xenei.junit.contract.ContractTest)25 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)22 FrontsTriple (org.apache.jena.graph.FrontsTriple)22 ElementOptional (org.apache.jena.sparql.syntax.ElementOptional)18 Var (org.apache.jena.sparql.core.Var)14 ElementGroup (org.apache.jena.sparql.syntax.ElementGroup)11 Element (org.apache.jena.sparql.syntax.Element)10 ElementUnion (org.apache.jena.sparql.syntax.ElementUnion)8 ExprVar (org.apache.jena.sparql.expr.ExprVar)7 ElementNamedGraph (org.apache.jena.sparql.syntax.ElementNamedGraph)7 Quad (org.apache.jena.sparql.core.Quad)5 UpdateModify (org.apache.jena.sparql.modify.request.UpdateModify)5