You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Wei Zhang <we...@adelaide.edu.au> on 2014/09/01 07:09:12 UTC

How to parse queries top-down?

Hi All,

I want to parse the SPARQL query and get every element of a query and all the features in order to rewrite the query. So I need to know all the triple patterns and their level in the query.
For example, for a query like:
select * where {
{ bgp1}
UNION
{ bgp2.
  bgp3}
OPTIONAL
{ bgp4}
}
I want to get bgp1-4, and also the relationships between them. e.g.  bgp2 and bgp3 is the conjunctive relationship.  bgp1 and { bgp2. bgp3} are union relationship.
Using algebra cannot get their relationships.

Currently, I just decompose the first level as groups, then to find the pattern in each element:

    Query query = QueryFactory.create(strQuery);
    Element qp = query.getQueryPattern();
List<Element> listElement = ((ElementGroup) qp).getElements();

    for (int i = 0; i < listElement.size() ; i++){
          Element e = listElement.get(i)
         if  ( e instanceof ElementUnion) {...}
         if (e instanceof ElementGroup){...}
          ...
     }

But there are two problems:

1)      I  cannot get further level of elements. Take the query example,  { bgp2. bgp3} is considered as one element and cannot be decomposed further. But what I want is bgp2, bgp3 and their conjunctive relationship

2)      I cannot get subquery in this way. I found Andy said ElementSubQuery can be used to get subquery (https://www.mail-archive.com/dev@jena.apache.org/msg06993.html) but I cannot find more detailed documents or examples regarding to how to use ElementSubQuery.

Could anyone help on this or give me any reference to use ElementSubQuery?

Thank you very much for your time and help!

Best Regards,
Wei Zhang