Re: getting the query from an SXP expression

Hello,

I implemented such a system a few years ago but didn't make a PR since I
found it was too hackish for open source standards.

But it currently supports 47 SPARQL operators so I think it can be a good
start to work on a better version. I will look at the Jena implementation
to see how they did it.

I have created a gist here :
https://gist.github.com/abrisse/8f9893549f00e94c25cbe0bea93e532d

For example :

query = %q{
PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX pmcore:<http://www.perfect-memory.com/ontology/pmcore/1.0#>

CONSTRUCT
WHERE {
  ?uri rdf:type pmcore:Movie ; pmcore:contains ?element
}
}

puts ::SPARQL::Grammar.parse(query).sparql_fragment

#  generates the following expanded version :

PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX pmcore: <http://www.perfect-memory.com/ontology/pmcore/1.0#>
CONSTRUCT {
  ?uri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
http://www.perfect-memory.com/ontology/pmcore/1.0#Movie> .
  ?uri <http://www.perfect-memory.com/ontology/pmcore/1.0#contains>
?element .
}
WHERE {
  ?uri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <
http://www.perfect-memory.com/ontology/pmcore/1.0#Movie> .
  ?uri <http://www.perfect-memory.com/ontology/pmcore/1.0#contains>
?element .
}

Cheers,
Aymeric


On Thu, Dec 2, 2021 at 12:06 PM Daniel Hernandez <daniel@degu.cl> wrote:

>
> Thank you for your answer.  I think now I understand better the role
> that play SPARQL::Grammar, SPARQL::Algebra, and SPARQL::Client (please
> fix me if I am wrong).  The first two allow for creating instances of an
> algebraic expression from a string (using either the SPARQL or the SSE
> grammars), and can be translated to SSE.  The last provides a DSL to
> define queries, and does not care about the SPARQL algebra.
>
> Gregg, I am going to implement the hypothetical method inside my app,
> for some few cases, and then I am going to share the code to see if this
> is useful for the general implementation.
>
> Daniel Hernández
>
> Gregg Kellogg <gregg@greggkellogg.net> writes:
>
> > The approach Jena takes probably makes it better to have such a
> mechanism be part of the core sparql gem, not restricted to sparq-client. I
> created an issue for it in the sparql repo [1].
> >
> > A hypothetical SPARQL::Algebra::Operator#to_sparql method might method
> that could be used to do this.
> >
> > Gregg Kellogg
> > gregg@greggkellogg.net
> >
> > [1] https://github.com/ruby-rdf/sparql/issues/38 <
> https://github.com/ruby-rdf/sparql/issues/38>
> >
> >> On Dec 1, 2021, at 1:45 PM, Gregg Kellogg <gregg@greggkellogg.com>
> wrote:
> >>
> >> Thanks Andy, that’s what I was looking for. Could be ported to Ruby
> reasonably.
> >>
> >> Gregg Kellogg
> SPARQL::Algebra::Operator>> Sent from my iPhone
> >>
> >>> On Dec 1, 2021, at 1:13 PM, Andy Seaborne <andy@seaborne.org> wrote:
> >>>
> >>> 
> >>>
> >>>> On 01/12/2021 20:30, Gregg Kellogg wrote:
> >>>> (Sorry, previous reply was inadvertently not reply all).
> >>>> I recall some discussion about this several years ago, and it may be
> possible, at least in limited cases. I’m also not aware of any other
> platforms which use SPARQL S-Expressions which do any de-compilation.
> >>>
> >>> Jena tries to - it's quite pragmatic:
> >>>
> >>>
> https://github.com/apache/jena/blob/main/jena-arq/src/main/java/org/apache/jena/sparql/algebra/OpAsQuery.java
> >>>
> >>>   Andy
> >>>
> >>>> I had thought that this would be good for the sparql-client gem,
> which could take an SSE input and turn it into SPARQL Grammar, and for
> trivial use cases it shouldn’t be too difficult to do, but I never pursued
> the issue. I’d be happy for someone to look into this further and maybe
> create a PR, even if it is only a partial solution. To not get lost, it
> might be good to create an issue in
> https://github.com/ruby-rdf/sparql-client/issues <
> https://github.com/ruby-rdf/sparql-client/issues> so that we don’t loose
> discussions again.
> >>>> Typically, the SXP is intended for optimization and execution, not to
> round trip back to the sparql grammar. There may be some other work on
> this, but I’m not aware of any. There’s quire a bit of activity in
> http://rdf.js.org/query-spec/ <http://rdf.js.org/query-spec/>c <
> https://github.com/rdfjs/query-spec> for that JavaScript community, and
> there may be some work there that could be leveraged.
> >>>> In general, I would welcome more collaboration on the Ruby RDF/SPARQL
> gems, which have been fairly quiet for a while, other than for basic
> maintenance. I am planning a 3.2 release, but that will mostly be to
> maintain dependencies and minimum Ruby versions.
> >>>> Gregg Kellogg
> >>>> gregg@greggkellogg.net <mailto:gregg@greggkellogg.net>
> >>>>>> On Dec 1, 2021, at 6:35 AM, Daniel Hernandez <daniel@degu.cl
> <mailto:daniel@degu.cl>> wrote:
> >>>>>
> >>>>>
> >>>>> In the previous example I made a mistake it is `parse` instead of
> `for`
> >>>>> (the question is the same).
> >>>>>
> >>>>> Daniel Hernandez <daniel@degu.cl <mailto:daniel@degu.cl>> writes:
> >>>>>
> >>>>>> Hi all,
> >>>>>>
> >>>>>> I am trying to get the SPARQL query of a sse expression.
> >>>>>> For instance, if I have
> >>>>>>
> >>>>>> exp = SPARQL::Algebra::Expression.for "(project (?x) (bgp (triple
> ?s ?p ?x)))"
> >>>>>>
> >>>>>> then I want the string "SELECT ?x WHERE {?s ?p ?x}".
> >>>>>>
> >>>>>> Thanks,
> >>>>>> Daniel
> >>>>>
> >>>
>
>

Received on Thursday, 2 December 2021 11:46:11 UTC