You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by Miguel Bento Alves <mb...@gmail.com> on 2014/08/08 11:15:30 UTC

extract query patterns

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?

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


Miguel

Re: extract query patterns

Posted by Andy Seaborne <an...@apache.org>.
On 12/08/14 11:24, Miguel Bento Alves wrote:
> Hi Andy,
>
> with your example I can extract the triples but I can’t extract the sub-queries. Is there a simple way to extract the sub-queries from a query pattern?
>
> Miguel

The code needs to walk down the syntax tree from the top looking for 
ElementSubQuery that signals a subquery.

If in the algrebra, its a case of looking for a "modifier" but somtimes 
there just isn't a subquery any more:


SELECT * { { SELECT * {?s ?p ?o}} }

==>

(bgp (triple ?s ?p ?o))


The inner "SELECT *" did nothing and no sign of it remains.

	Andy

>
>
> On 08 Aug 2014, at 21:10, Andy Seaborne <an...@apache.org> wrote:
>
>> 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
>>>
>>
>


Re: extract query patterns

Posted by Miguel Bento Alves <mb...@gmail.com>.
Hi Andy,

with your example I can extract the triples but I can’t extract the sub-queries. Is there a simple way to extract the sub-queries from a query pattern?

Miguel


On 08 Aug 2014, at 21:10, Andy Seaborne <an...@apache.org> wrote:

> 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
>> 
> 


Re: extract query patterns

Posted by Andy Seaborne <an...@apache.org>.
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
>