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

TurtleSerializer baseUrl and prefix mappings effect #86

Open
koslambrou opened this issue May 7, 2020 · 8 comments
Open

TurtleSerializer baseUrl and prefix mappings effect #86

koslambrou opened this issue May 7, 2020 · 8 comments

Comments

@koslambrou
Copy link
Contributor

Say I create a RDF graph.

I'm trying to save the graph in a file and I want to set the baseUrl and prefix mappings.
However, the following won't set the baseUrl and will only put the default prefix mappings (not the ones I specified).

main :: IO ()
main = do
  let prefixMappings = ...
  let graph = ...
  let baseUri = Just $ "http://localhost:8080/resource"
  withFile "..." WriteMode (\h -> hWriteRdf (TurtleSerializer baseUri prefixMappings) h graph)

Seems like the only way to set the baseUrl and prefix mapping is through mkRdf or addPrefixMappings. If that's the case, what is the point of TurtleSerializer parameters ?

@koslambrou
Copy link
Contributor Author

Actually, the 2nd parameter of TurtleSerializer correctly adds the specified prefixes in the saved file, but it also adds the defaults prefixes from Data.RDF.Namespace.standard_ns_mappings

@robstewart57
Copy link
Owner

Actually, the 2nd parameter of TurtleSerializer correctly adds the specified prefixes in the saved file, but it also adds the defaults prefixes from Data.RDF.Namespace.standard_ns_mappings

@koslambrou could you put together a small, compilable, example that demonstrates this? Thanks.

@koslambrou
Copy link
Contributor Author

koslambrou commented May 7, 2020

Here:

import Data.RDF (RDF)
import qualified Data.RDF as RDF
import qualified Data.RDF.Namespace as RDF
import qualified Data.Map as Map

test :: IO ()
test = do
  let emptyRdf = RDF.empty :: RDF RDF.TList
  let mappings = RDF.PrefixMappings $ Map.fromList [ ("mo", "http://purl.org/ontology/mo/") ]
  let baseUrl = "http://example.org/resource"
  RDF.writeRdf (TurtleSerializer Nothing mappings) emptyRdf

Expected output:

@base <http://example.com/resource> .
@prefix mo: <http://purl.org/ontology/mo/> .

Actual output:

@prefix dc: <http://purl.org/dc/elements/1.1/> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix ex: <http://www.example.org/> .
@prefix ex2: <http://www2.example.org/> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
@prefix mo: <http://purl.org/ontology/mo/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .

@koslambrou
Copy link
Contributor Author

The actual output in my last comment is not on the latest commit.

Here's the actual output:

@prefix mo: <http://purl.org/ontology/mo/> .

There's just the @base missing.

@robstewart57
Copy link
Owner

Because in your code:

let baseUrl = "http://example.org/resource"
RDF.writeRdf (TurtleSerializer Nothing mappings) emptyRdf

You're not using baseUrl in the TurtleSerializer.

@koslambrou
Copy link
Contributor Author

Oh right!

run :: RIO App ()
run = do
  let emptyRdf = RDF.empty :: RDF RDF.TList
  let mappings = RDF.PrefixMappings $ Map.fromList [ ("mo", "http://purl.org/ontology/mo/") ]
  let baseUrl = Just "http://example.org/resource"
  liftIO $ RDF.writeRdf (TurtleSerializer baseUrl mappings) emptyRdf

The actual output is still:

@prefix mo: <http://purl.org/ontology/mo/> .

@robstewart57
Copy link
Owner

Ah, thanks.

The optional argument representing the base URL in the Turtle parser is to use for resolving relative URLs in the document. This may be overridden in the document itself using the @base directive.

However in the Turtle serialisation, the optional base URL is used when serialising all URI nodes in the outputted RDF, i.e. normalising prefix:foo to be http://example.com/foo if the prefix namespace is http://example.com. That is, I'm not sure there's a need to include the @base in the serialised namespaces since no URIs in the outputted RDF are relative, they are all absolute.

@koslambrou
Copy link
Contributor Author

The following Turtle output contains a relative URI, no?

@base <http://example.com/resource> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

</Movie> a rdfs:Class .

Isn't /Movie considered a relative URI since it should be resolved to http://example.com/resource/Movie with the base URI ?

Also, I don't understand the use case behind the optional base URL in the Turtle serialisation. Why would we want the convert a single prefix (e.g. prefix:foo) to absolute form (e.g. http://example.com/foo). Hope I make sense.

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

2 participants