Search in sources :

Example 21 with TriplePath

use of org.apache.jena.sparql.core.TriplePath in project jena by apache.

the class WhereHandlerTest method testAddWhereAnonymous.

@Test
public void testAddWhereAnonymous() {
    handler.addWhere(new TriplePath(new Triple(Node.ANY, RDF.first.asNode(), Node.ANY)));
    assertContainsRegex(WHERE + OPEN_CURLY + "ANY" + SPACE + uri("http://www\\.w3\\.org/1999/02/22-rdf-syntax-ns#first") + SPACE + "ANY" + OPT_SPACE + CLOSE_CURLY, query.toString());
}
Also used : Triple(org.apache.jena.graph.Triple) TriplePath(org.apache.jena.sparql.core.TriplePath) Test(org.junit.Test)

Example 22 with TriplePath

use of org.apache.jena.sparql.core.TriplePath in project jena by apache.

the class WhereHandlerTest method testAddOptionalWhereHandler.

@Test
public void testAddOptionalWhereHandler() throws ParseException {
    WhereHandler pattern = new WhereHandler(new Query());
    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));
    pattern.addWhere(new TriplePath(new Triple(s, q, n123)));
    pattern.addWhere(new TriplePath(new Triple(s, v, x)));
    pattern.addFilter("?x>56");
    handler.addOptional(pattern);
    Query expected = QueryFactory.create("SELECT * WHERE { OPTIONAL { ?s <urn:q> '123'^^<http://www.w3.org/2001/XMLSchema#int> . ?s <urn:v> ?x . FILTER(?x>56) }}");
    Assert.assertEquals(expected.getQueryPattern(), query.getQueryPattern());
}
Also used : Triple(org.apache.jena.graph.Triple) Query(org.apache.jena.query.Query) WhereHandler(org.apache.jena.arq.querybuilder.handlers.WhereHandler) Var(org.apache.jena.sparql.core.Var) Node(org.apache.jena.graph.Node) TriplePath(org.apache.jena.sparql.core.TriplePath) Test(org.junit.Test)

Example 23 with TriplePath

use of org.apache.jena.sparql.core.TriplePath 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);
    assertContainsRegex(WHERE + OPEN_CURLY + OPEN_CURLY + uri("s") + SPACE + uri("p") + SPACE + uri("o") + CLOSE_CURLY + OPT_SPACE + UNION + OPEN_CURLY + SELECT + var("x") + SPACE + WHERE + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + uri("three") + OPT_SPACE + CLOSE_CURLY, query.toString());
}
Also used : Triple(org.apache.jena.graph.Triple) TriplePath(org.apache.jena.sparql.core.TriplePath) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) Test(org.junit.Test)

Example 24 with TriplePath

use of org.apache.jena.sparql.core.TriplePath 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);
    assertContainsRegex(WHERE + OPEN_CURLY + OPEN_CURLY + uri("s") + SPACE + uri("p") + SPACE + uri("o") + CLOSE_CURLY + OPT_SPACE + UNION + OPEN_CURLY + uri("one") + SPACE + uri("two") + SPACE + uri("three") + OPT_SPACE + CLOSE_CURLY + CLOSE_CURLY, query.toString());
}
Also used : Triple(org.apache.jena.graph.Triple) TriplePath(org.apache.jena.sparql.core.TriplePath) SelectBuilder(org.apache.jena.arq.querybuilder.SelectBuilder) Test(org.junit.Test)

Example 25 with TriplePath

use of org.apache.jena.sparql.core.TriplePath in project jena by apache.

the class FormatterElement method visit.

@Override
public void visit(ElementPathBlock el) {
    // Write path block - don't put in a final trailing "." 
    if (el.isEmpty()) {
        out.println("# Empty BGP");
        return;
    }
    // Split into BGP-path-BGP-...
    // where the BGPs may be empty.
    PathBlock pBlk = el.getPattern();
    BasicPattern bgp = new BasicPattern();
    // Has anything been output?
    boolean first = true;
    for (TriplePath tp : pBlk) {
        if (tp.isTriple()) {
            bgp.add(tp.asTriple());
            continue;
        }
        if (!bgp.isEmpty()) {
            if (!first)
                out.println(" .");
            flush(bgp);
            first = false;
        }
        if (!first)
            out.println(" .");
        // Path
        printSubject(tp.getSubject());
        out.print(" ");
        PathWriter.write(out, tp.getPath(), context.getPrologue());
        out.print(" ");
        printObject(tp.getObject());
        first = false;
    }
    // Flush any stored triple patterns.
    if (!bgp.isEmpty()) {
        if (!first)
            out.println(" .");
        flush(bgp);
        first = false;
    }
}
Also used : BasicPattern(org.apache.jena.sparql.core.BasicPattern) TriplePath(org.apache.jena.sparql.core.TriplePath) PathBlock(org.apache.jena.sparql.core.PathBlock)

Aggregations

TriplePath (org.apache.jena.sparql.core.TriplePath)33 Triple (org.apache.jena.graph.Triple)22 Test (org.junit.Test)18 Node (org.apache.jena.graph.Node)10 WhereHandler (org.apache.jena.arq.querybuilder.handlers.WhereHandler)4 Query (org.apache.jena.query.Query)4 Path (org.apache.jena.sparql.path.Path)4 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)3 PrefixMapping (org.apache.jena.shared.PrefixMapping)3 PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)3 Var (org.apache.jena.sparql.core.Var)3 HashMap (java.util.HashMap)2 Op (org.apache.jena.sparql.algebra.Op)2 OpPath (org.apache.jena.sparql.algebra.op.OpPath)2 BasicPattern (org.apache.jena.sparql.core.BasicPattern)2 ContractTest (org.xenei.junit.contract.ContractTest)2 FrontsTriple (org.apache.jena.graph.FrontsTriple)1 PathBlock (org.apache.jena.sparql.core.PathBlock)1 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)1