1

I would like to avoid passing SPARQL queries around as Strings. Therefore I use Jena's API for creating my queries. Now I need a PropertyPath in my query, but I can't find any Java class supporting this. Can you give me a hint?

Here's some example code where I would like to insert this (Jena 3.0.1):

private Query buildQuery(final String propertyPath) {
    ElementTriplesBlock triplesBlock = new ElementTriplesBlock();
    triplesBlock.addTriple(
            new Triple(NodeFactory.createURI(this.titleUri.toString()),
                    //How can I set a property path as predicate here?
                    NodeFactory.???,
                    NodeFactory.createVariable("o"))
    );
    final Query query = buildSelectQuery(triplesBlock);
    return query;
}

private Query buildSelectQuery(final ElementTriplesBlock queryBlock) {
    final Query query = new Query();
    query.setQuerySelectType();
    query.setQueryResultStar(true);
    query.setDistinct(true);
    query.setQueryPattern(queryBlock);
    return query;
}
Andreas
  • 303
  • 1
  • 17
  • Just a question, more for code clarity than anything else: might it be easier and clearer to write the query as a string *once*, parse it, and then pass the resulting query object around? You'd still have object, not string, being passed around, but it seems like it's be much easier to debug and modify in the future. – Joshua Taylor Mar 02 '16 at 13:26
  • @Joshua Taylor, yes I think that would be possible. But as the propertyPath is my only variable input, I would have to insert it into the String which is obviously possible. I am looking for a somewhat cleaner solution. In the meantime I found out that I possibly should use this code to parse the PropertyPath: `Path p = SSE.parsePath("dct:creator/gndo:preferredNameForThePerson", PMAP); triplesBlock.addTriplePath( new TriplePath(..., parsedPropertyPath, ... );` But SSE.parsePath runs into an Exception at the moment. – Andreas Mar 02 '16 at 14:24
  • 1
    Asked and answered: http://mail-archives.apache.org/mod_mbox/jena-users/201603.mbox/%3C56D70307020000C20002033D%40gwia.bsb-muenchen.de%3E Summary: Use `PathParser`. – AndyS Mar 03 '16 at 11:27
  • Thanks, Andy, for your kind help. If you post your solution here, I can accept it is an answer and hopefully post a fixed code example tomorrow. – Andreas Mar 03 '16 at 18:33

1 Answers1

0

You can use PathFactory to create property paths

Consider the graph below:

@prefix dc: <http://purl.org/dc/elements/1.1/>.
@prefix ex: <http://example.com/>.

    ex:Manager ex:homeOffice ex:HomeOffice
    ex:HomeOffice dc:title "Home Office Title"

Suppose you want to create a pattern like:

?x ex:homeOffice/dc:title ?title

The code below achieves it:

 //create the path
Path exhomeOffice = PathFactory.pathLink(NodeFactory.createURI("http://example.com/homeOffice"));
 Path dcTitle = PathFactory.pathLink(NodeFactory.createURI("http://purl.org/dc/elements/1.1/title"));
Path fullPath = PathFactory.pathSeq(exhomeOffice,dcTitle);
TriplePath t = new TriplePath(Var.alloc("x"),fullPath,Var.alloc("title"));
Noor
  • 19,638
  • 38
  • 136
  • 254