Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable RDF result formats; default format should vary with query form #77

Open
VladimirAlexiev opened this issue Apr 10, 2024 · 0 comments

Comments

@VladimirAlexiev
Copy link
Collaborator

VladimirAlexiev commented Apr 10, 2024

(defcustom sparql-default-format "text/csv"
  "The default format of the returned results."
  :group 'sparql
  :type '(choice
          (const :tag "Comma separated values" "text/csv")
          (const :tag "Tab separated values" "text/tab-separated-values")
          (const :tag "JSON" "application/sparql-results+json")
          (const :tag "SPARQL XML" "application/sparql-results+xml")
          (string :tag "Custom")))

(defvar org-babel-default-header-args:sparql
  `((:url . ,sparql-default-base-url)
    (:format . ,sparql-default-format))

This is all nice and good for SELECT and ASK queries.

However, CONSTRUCT and DESCRIBE queries return RDF formats not the above tabular formats.
See this spec: https://www.w3.org/TR/sparql12-protocol/#query-success.
I don't know if there's an authoritative list, so I posted w3c/rdf-concepts#83 and you can see a list of 9 there.

@johanwk's new version https://github.com/johanwk/elot/blob/main/elot-defs.org#execute-sparql-using-robot checks for ttl vs csv depending on query form: but sparql-mode should support the various formats.

(if (string-match-p "\\(turtle\\|ttl\\)" format) 'ttl 'csv)))

In addition, the default format should not be fixed to "text/csv" but should vary with the query form.
It's not so easy to find it in a SPARQL query without parsing it.

  • One can make a guess: look for the first match of ^\s*(select|ask|construct|describe)\b (case-insensitive).
  • Or could use an external command arq.qparse (part of JENA)

Look at this twisted test.rq: the first keyword CONSTRUCT is a false positive, and select is hidden at the end of a line:

# CONSTRUCT {is not the form of this query}                        
PREFIX owl: <http://www.w3.org/2002/07/owl#>                       
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> select  * {   
    values ?t {owl:Class owl:DatatypeProperty owl:ObjectProperty}  
        ?x a ?t                                                    
    filter(not exists {?x rdfs:isDefinedBy ?y})                    
}                                                                  

The regex will fail miserably, but qparse outputs the query in a normal form, where SELECT is easy to detect:

# qparse --query test.rq
PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT  *
WHERE
  { VALUES ?t { owl:Class owl:DatatypeProperty owl:ObjectProperty }
    ?x  a  ?t
    FILTER NOT EXISTS { ?x  rdfs:isDefinedBy  ?y }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant