Prefixes in S-expressions

Hi,

I am surprised because URIs are translated to S-expressions in different
ways.  That means that I can get:

uri1 == uri2  #=> true
uri1.to_sxp   #=> wdt:P31
uri2.to_sxp   #=> <http://www.wikidata.org/prop/direct/P31>

In this case uri1 is translated using a prefix.  To create a URI that is
translated using a prefix, this URI have to be inside a query using that
prefix.  For instance, the following query:

query = <<~QUERY
  PREFIX wdt: <http://www.wikidata.org/prop/direct/>
  PREFIX wd: <http://www.wikidata.org/entity/>
  SELECT *
  WHERE { ?person wdt:P31 wd:Q5 }
QUERY
exp = SPARQL.parse(query).to_sxp_bin

=> [:prefix,
 [[:"wdt:", #<RDF::URI:0xfe4 URI:http://www.wikidata.org/prop/direct/>],
  [:"wd:", #<RDF::URI:0xff0 URI:http://www.wikidata.org/entity/>]],
 [:bgp,
  [:triple,
   #<RDF::Query::Variable:0xfb8(?person)>,
   #<RDF::URI:0x1004 URI:http://www.wikidata.org/prop/direct/P31>,
   #<RDF::URI:0xfbc URI:http://www.wikidata.org/entity/Q5>]]]

Then I can print the query as a S-expression:

puts exp.to_sxp

=> "(prefix ((wdt: <http://www.wikidata.org/prop/direct/>)
             (wd: <http://www.wikidata.org/entity/>))
            (bgp (triple ?person wdt:P31 wd:Q5)))"

An I can print a part of the query:

puts exp[2].to_sxp

=> "(bgp (triple ?person wdt:P31 wd:Q5))"

However, the second sxp is wrong, because does not define the prefixes.
I also noticed that to equivalent URIs can be printed differently:

uri1 = exp[2][1][2]
uri2 = RDF::URI.new uri1.to_s

uri1 == uri2  #=> true
uri1.to_sxp   #=> wdt:P31
uri2.to_sxp   #=> <http://www.wikidata.org/prop/direct/P31>

I see two ways to fix the second pattern:

1. Generate the sxp without prefixes.
   (i.e., print <http://www.wikidata.org/prop/direct/P31>)

2. Add the prefixes to the second expression
   (i.e., add the prefix wd: <http://www.wikidata.org/prop/direct/>
   to the expression.

Hence, I have two questions:

1) Can I get the respresentation without prefixes of an expression.
   For instance, with a parameter like this:

   exp[2].to_sxp(with_prefixes: false)

   => (bgp (triple ?person
                   <http://www.wikidata.org/prop/direct/P31>
                   <http://www.wikidata.org/entity/Q5>))

2) Can I get the prefixed that each URI as follows:

   uri1.get_prefix  #=> [:"wdt:", #<RDF::URI:0xfe4 URI:http://www.wikidata.org/prop/direct/>]


Thanks,
Daniel

Received on Monday, 6 December 2021 14:11:19 UTC