Module: Sinatra::SPARQL::Helpers

Defined in:
lib/sinatra/sparql.rb

Overview

Helper methods.

Instance Method Summary collapse

Instance Method Details

#dataset(**options) ⇒ RDF::Dataset

This either creates a merge repo, or uses the standard repository for performing the query, based on the parameters passed (default-graph-uri and named-graph-uri). Loads from the datasource, unless a graph named by the datasource URI already exists in the repository.

Returns:

  • (RDF::Dataset)

See Also:

  • Algebra::Operator::Dataset


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/sinatra/sparql.rb', line 112

def dataset(**options)
  logger = options.fetch(:logger, ::Logger.new(false))
  repo = settings.repository
  if %i(default-graph-uri named-graph-uri).any? {|k| options.key?(k)}        
    default_datasets = Array(options[:"default-graph-uri"]).map {|u| RDF::URI(u)}
    named_datasets = Array(options[:"named-graph-uri"]).map {|u| RDF::URI(u)}

    (default_datasets + named_datasets).each do |uri|
      load_opts = {logger: logger, graph_name: uri, base_uri: uri}
      unless repo.has_graph?(uri)
        logger.debug(options) {"=> load #{uri}"}
        repo.load(uri.to_s, **load_opts)
      end
    end

    # Create an aggregate based on queryable having just the bits we want
    aggregate = RDF::AggregateRepo.new(repo)
    named_datasets.each {|name| aggregate.named(name) if repo.has_graph?(name)}
    aggregate.default(*default_datasets.select {|name| repo.has_graph?(name)})
    aggregate
  end || settings.repository
end

#service_description(**options) ⇒ RDF::Graph

This is useful when a GET request is performed against a SPARQL endpoint and no query is performed. Provide a set of datasets, including a default dataset along with optional triple count, dump location, and description of the dataset.

The results are serialized using content negotiation. For text/html, authors should generate RDFa for the serivce description directly.

Parameters:

Options Hash (**options):

  • :repository (RDF::Enumerable)

    An enumerable, typically a type of RDF::Repository containing the dataset used for queries against the service.

  • :endpoint (RDF::URI, #to_s)

    URI of the service endpoint, defaults to “/sparql” in the current realm.

Returns:

  • (RDF::Graph)

See Also:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sinatra/sparql.rb', line 32

def service_description(**options)
  repository = options[:repository]

  g = RDF::Graph.new
  sd = RDF::URI("http://www.w3.org/ns/sparql-service-description#")
  void = RDF::URI("http://rdfs.org/ns/void#")

  node = RDF::Node.new
  g << [node, RDF.type, sd.join("#Service")]
  g << [node, sd.join("#endpoint"), RDF::URI(url(options.fetch(:endpoint, "/sparql")))]
  g << [node, sd.join("#supportedLanguage"), sd.join("#SPARQL10Query")]
  g << [node, sd.join("#supportedLanguage"), sd.join("#SPARQL11Query")]
  g << [node, sd.join("#supportedLanguage"), sd.join("#SPARQL11Update")]
  g << [node, sd.join("#supportedLanguage"), RDF::URI('http://www.w3.org/ns/rdf-star#SPARQLStarQuery')]
  g << [node, sd.join("#supportedLanguage"), RDF::URI('http://www.w3.org/ns/rdf-star#SPARQLStarUpdate')]

  # Input formats
  RDF::Reader.map(&:format).select(&:to_uri).each do |format|
    g << [node, sd.join("#inputFormat"), format.to_uri]
  end

  # Result formats, both RDF and SPARQL Results.
  %w(
    http://www.w3.org/ns/formats/SPARQL_Results_XML
    http://www.w3.org/ns/formats/SPARQL_Results_JSON
    http://www.w3.org/ns/formats/SPARQL_Results_CSV
    http://www.w3.org/ns/formats/SPARQL_Results_TSV
  ).each do |uri|
    g << [node, sd.join("#resultFormat"), uri]
  end

  RDF::Writer.map(&:format).select(&:to_uri).each do |format|
    g << [node, sd.join("#resultFormat"), format.to_uri]
  end

  # Features
  g << [node, sd.join("#feature"), sd.join("#DereferencesURIs")]
  #g << [node, sd.join("#feature"), sd.join("#BasicFederatedQuery")]

  # Datasets
  ds = RDF::Node.new
  g << [node, sd.join("#defaultDataset"), ds]
  g << [ds, RDF.type, sd.join("#Dataset")]

  # Contexts
  if repository.is_a?(RDF::Enumerable)
    graph_names = {}
    repository.each do |statement|
      graph_names[statement.graph_name] ||= 0
      graph_names[statement.graph_name] += 1
    end
    
    graph_names.each do |name, count|
      bn = RDF::Node.new
      if name
        # Add named graphs as namedGraphs
        g << [ds, sd.join("#namedGraph"), bn]
        g << [bn, RDF.type, sd.join("#NamedGraph")]
        g << [bn, sd.join("#name"), name]
        graph = RDF::Node.new
        g << [bn, sd.join("#graph"), graph]
        bn = graph
      else
        # Default graph
        g << [ds, sd.join("#defaultGraph"), bn]
        g << [bn, RDF.type, sd.join("#Graph")]
      end
      g << [bn, void.join("#triples"), count]
    end
  end
  g
end