Question

I can get the algebra form from sparql query string using ARQ algebra (com.hp.hpl.jena.sparql.algebra):

String queryStr = 
   "PREFIX foaf:  <http://xmlns.com/foaf/0.1/>" + 
              "SELECT DISTINCT ?name ?nick" +
              "{?x foaf:mbox <mailt:person@server> ." + 
              "?x foaf:name ?name" +
              "OPTIONAL { ?x foaf:nick ?nick }}";

Query query = QueryFactory.create(queryStr);
Op op = Algebra.compile(query);

Print the returned value of op:

(distinct
(project (?name ?nick)
(join
  (bgp
    (triple ?x <http://xmlns.com/foaf/0.1/mbox> <mailt:person@server>)
    (triple ?x <http://xmlns.com/foaf/0.1/name> ?nameOPTIONAL)
  )
  (bgp (triple ?x <http://xmlns.com/foaf/0.1/nick> ?nick)))))

Returned value is an Op type, but I can't find any direct methods that can parse the op into elements, e.g., basic graph patterns of s, p, o, and the relations between these graph patterns.

Any hint is appreciated, thanks.

Was it helpful?

Solution

Why serialise out the algebra at all?

If your aim is to walk the algebra tree and extract the BGPs then you can do this using the OpVisitor interface of which there are various implementations of that will get you started. The particular method you would care about is visit(OpBgp opBgp) since then you can access the methods of the OpBgp class to extract the pattern information

OTHER TIPS

It might be too late but still as I am not seeing final answer so I am writing the code for printing all BGP.

For that create the class as follows that extends OpVisitorBase and override the public void visit(final OpBGP) function as given below in the code.

And from your code simply call the function:

MyOpVisitorBase.myOpVisitorWalker(op);

public class MyOpVisitorBase extends OpVisitorBase
{
    public static void myOpVisitorWalker(Op op)
    {
        OpWalker.walk(op, this);
    }

    @Override
    public void visit(final OpBGP opBGP) {
        final List<Triple> triples = opBGP.getPattern().getList();
        int i = 0;
        for (final Triple triple : triples) {
            System.out.println("Triple: "+triple.toString());
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top