On 08/08/14 10:15, Miguel Bento Alves wrote:
Hi all,

In a Sparql query, we can extract the query pattern ( q.getQueryPattern() ). 
For instance, in the sparql command:
        Select ?x ?p ?z where {?x ?p ?y . ?x <eg:p> ?z.}
we can extract:
        { ?x ?p ?y .
          ?x <eg:p> ?z
        }

Is there anyway to extract as TriplePattern?

If you are working with the parsed object, you have to test/cast the query pattern:

        Query query = QueryFactory.create("SELECT*{?s ?p ?o}") ;
        ElementGroup el = (ElementGroup)query.getQueryPattern() ;
        Element bgp = el.getElements().get(0) ;

        ElementPathBlock elp = (ElementPathBlock)bgp ;
        PathBlock pb = elp.getPattern() ;
        for ( TriplePath tp : pb ) {
            Triple t = tp.asTriple() ;
            if ( t == null )
                System.err.println("Path: "+tp.getPath()) ;
            else
                System.out.println(t) ;
        }

Note in the syntax, you can have paths: { ?x :p/:q ?y }

It is much easier to work with algebra.

        Op op = Algebra.compile(query.getQueryPattern()) ;
        System.out.println(op) ;
        OpBGP x = (OpBGP)op ;
        for ( Triple t : x.getPattern() ) {
            System.out.println("Algebra: -- "+t) ;
        }



Also, we can extract the vars ( PatternVars.vars(qf.getQueryPattern()) ).  Is 
it possible to extract the the non-vars?

There's no code written - you can take a copy of the the code behind PatternVars and modify that.



Miguel


Reply via email to