sparql 1.1.8 → 1.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5d72aeb5d5db6d7d428b89437f714b0c210e863f
4
- data.tar.gz: daccd43bd940b353f85b9bfe9e4d6bf8915463b7
3
+ metadata.gz: c1f482dd9f82cbd685ddf363d85fce672b73ffa2
4
+ data.tar.gz: 42dad47f92ec19e8620b4fe9056fd56393e4e4ad
5
5
  SHA512:
6
- metadata.gz: 22242763a859e2ab8c5c704f67e5fcfe4e36868ef3591c8ff8aaaceb0edffe5a4e0cda204d77b558df270c161f85d4bbbc01b8c5af1043a7e3aed93b64fd7bbc
7
- data.tar.gz: 266accb58158ef89a63705165409ede9d92d3112d1c7d465cc213bca276c91cf83f23ebf8eaf318079cc9b06b9906761eb9e04e165db4668a7f0bcb2023ebc4f
6
+ metadata.gz: 73afb94c540c89133b1321c7075de3e77e7cba8a36e21282e673caa058e3cbe4bdff247b0ac02530ba4fd55e059dd1c480a151d74e0c2415445496d710131615
7
+ data.tar.gz: 8a7377e7d6a9ecda3c16e821b62751ec84557c3dbdbac26e0e9bf3f9f99bf2aee9720f044f21b9ab5bb7d4b86c27b8b6a12328c0916e83fb578af6bb3d5e8d60
data/README.md CHANGED
@@ -171,16 +171,19 @@ a full set of RDF formats.
171
171
 
172
172
  ### Command line processing
173
173
 
174
- sparql --default-graph etc/doap.ttl etc/from_default.rq
175
- sparql -e "SELECT * FROM <etc/doap.ttl> WHERE { ?s ?p ?o }"
174
+ sparql execute --dataset etc/doap.ttl etc/from_default.rq
175
+ sparql execute -e "SELECT * FROM <etc/doap.ttl> WHERE { ?s ?p ?o }"
176
176
 
177
177
  # Generate SPARQL Algebra Expression (SSE) format
178
- sparql --to-sse etc/input.rq
179
- sparql --to-sse -e "SELECT * WHERE { ?s ?p ?o }"
178
+ sparql parse etc/input.rq
179
+ sparql parse -e "SELECT * WHERE { ?s ?p ?o }"
180
180
 
181
181
  # Run query using SSE input
182
- sparql --default-graph etc/doap.ttl --sse etc/input.sse
183
- sparql --sse -e "(dataset (<etc/doap.ttl>) (bgp (triple ?s ?p ?o))))"
182
+ sparql execute --dataset etc/doap.ttl --sse etc/input.sse
183
+ sparql execute --sse -e "(dataset (<etc/doap.ttl>) (bgp (triple ?s ?p ?o))))"
184
+
185
+ # Run a local SPARQL server using a dataset
186
+ sparql server etc/doap.ttl
184
187
 
185
188
  ### Adding SPARQL content negotiation to a Rails 3.x application
186
189
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.1.8
1
+ 1.1.9
data/bin/sparql CHANGED
@@ -4,17 +4,26 @@ $:.unshift(File.expand_path(File.join(File.dirname(__FILE__), "..", 'lib')))
4
4
  require 'sparql'
5
5
  begin
6
6
  require 'linkeddata'
7
- rescue LoadError => e
7
+ rescue LoadError
8
8
  require 'rdf/ntriples'
9
9
  end
10
10
  require 'getoptlong'
11
11
 
12
+ def display_results(res, options)
13
+ puts res.inspect if options[:verbose]
14
+ puts case res
15
+ when RDF::Graph then res.dump(:ttl, base_uri: query.base_uri, prefixes: query.prefixes, standard_prefixes: true)
16
+ when RDF::Literal then res.inspect
17
+ else res.map {|s| s.bindings.map {|k,v| "#{k}: #{v}"}}.join("\n")
18
+ end
19
+ end
20
+
12
21
  def run(input, options = {})
13
22
  if options[:debug]
14
- puts "input graph:\n#{options[:graph].dump(:ttl, standard_prefixes: true)}\n" if options[:graph]
23
+ puts "input graph:\n#{options[:dataset].dump(:trig, standard_prefixes: true)}\n" if options[:dataset]
15
24
  puts "query:\n#{input}\n"
16
25
  end
17
- options[:graph] ||= RDF::Repository.new
26
+ options[:dataset] ||= RDF::Repository.new
18
27
 
19
28
  if options[:verbose]
20
29
  puts ("\nSPARQL:\n" + input)
@@ -30,67 +39,115 @@ def run(input, options = {})
30
39
  puts ("\nSSE:\n" + query.to_sse) if options[:debug] || options[:to_sse]
31
40
 
32
41
  unless options[:to_sse]
33
- res = query.execute(options[:graph], debug: options[:debug])
34
- puts res.inspect if options[:verbose]
35
- puts case res
36
- when RDF::Graph then res.dump(:ttl, base_uri: query.base_uri, prefixes: query.prefixes, standard_prefixes: true)
37
- when RDF::Literal then res.inspect
38
- else res.map {|s| s.bindings.map {|k,v| "#{k}: #{v}"}}.join("\n")
42
+ res = query.execute(options[:dataset], debug: options[:debug])
43
+ display_results(res, options)
44
+ end
45
+ end
46
+
47
+ def server(options)
48
+ require 'sinatra/sparql'
49
+ repository = options.fetch(:dataset, RDF::Repository.new)
50
+
51
+ app = Sinatra.new do
52
+ register Sinatra::SPARQL
53
+ set :repository, repository
54
+
55
+ get '/' do
56
+ if params["query"]
57
+ query = params["query"].to_s.match(/^http:/) ? RDF::Util::File.open_file(params["query"]) : ::URI.decode(params["query"].to_s)
58
+ SPARQL.execute(query, settings.repository)
59
+ else
60
+ settings.sparql_options.replace(standard_prefixes: true)
61
+ settings.sparql_options.merge!(:prefixes => {
62
+ ssd: "http://www.w3.org/ns/sparql-service-description#",
63
+ void: "http://rdfs.org/ns/void#"
64
+ })
65
+ service_description(repo: settings.repository, endpoint: url)
66
+ end
67
+ end
68
+
69
+ post '/' do
70
+ SPARQL.execute(params['query'], settings.repository)
39
71
  end
40
72
  end
73
+ Rack::Server.start(app: app)
74
+ rescue LoadError
75
+ $stderr.puts "Running SPARQL server requires Rack to be in environment: #{$!.message}"
76
+ end
77
+
78
+ def usage
79
+ puts "Usage: #{$0} execute [options] query-file Execute a query against the specified dataset"
80
+ puts " #{$0} parse [options] query-file Parse a query into SPARQL S-Expressions (SSE)"
81
+ puts " #{$0} query [options] end-point query-file Run the query against a remote end-point"
82
+ puts " #{$0} server [options] dataset-file Start a server initialized from the specified dataset"
83
+ puts "Options:"
84
+ puts " --execute,-e: Use option argument as the SPARQL input if no query-file given"
85
+ puts " --dataset: File containing RDF graph or dataset"
86
+ puts " --debug: Display detailed debug output"
87
+ puts " --port,-p Port on which to run server; defaults to 9292"
88
+ puts " --sse: Query input is in SSE format"
89
+ puts " --debug: Display detailed debug output"
90
+ puts " --update: Process query as a SPARQL Update"
91
+ puts " --verbose: Display details of processing"
92
+ puts " --help,-?: This message"
93
+ exit(0)
41
94
  end
42
95
 
96
+ cmd, input = ARGV.shift, nil
97
+
43
98
  opts = GetoptLong.new(
99
+ ["--dataset", GetoptLong::REQUIRED_ARGUMENT],
44
100
  ["--debug", GetoptLong::NO_ARGUMENT],
45
- ["--dump", GetoptLong::NO_ARGUMENT],
101
+ ["--port", "-p", GetoptLong::REQUIRED_ARGUMENT],
46
102
  ["--verbose", GetoptLong::NO_ARGUMENT],
47
103
  ["--sse", GetoptLong::NO_ARGUMENT],
48
- ["--to-sse", GetoptLong::NO_ARGUMENT],
104
+ ["--update", GetoptLong::NO_ARGUMENT],
49
105
  ["--execute", "-e", GetoptLong::REQUIRED_ARGUMENT],
50
- ["--default-graph", "-g", GetoptLong::REQUIRED_ARGUMENT],
51
106
  ["--help", "-?", GetoptLong::NO_ARGUMENT]
52
107
  )
53
108
 
54
109
  options = {
55
- graph: RDF::Repository.new,
110
+ dataset: RDF::Repository.new,
56
111
  }
57
112
 
58
- input = nil
59
-
60
113
  opts.each do |opt, arg|
61
114
  case opt
115
+ when '--dataset' then options[:dataset].load(arg)
62
116
  when '--execute' then input = arg
63
- when "--default-graph" then options[:graph] = RDF::Graph.load(arg)
64
- when '--dump' then $dump = true
117
+ when '--port' then options[:port] = arg.to_i
65
118
  when '--sse' then options[:sse] = true
66
- when '--to-sse' then options[:to_sse] = true
67
119
  when '--debug' then options[:debug] = true
68
120
  when '--update' then options[:update] = true
69
121
  when '--verbose' then options[:verbose] = true
70
- when "--help"
71
- puts "Usage: #{$0} [options] file-or-uri ..."
72
- puts "Options:"
73
- puts " --execute,-e: Use option argument as the SPARQL input if no files are given"
74
- puts " --default-graph: Load default graph"
75
- puts " --dump: Dump raw output, otherwise serialize to SSE"
76
- puts " --debug: Display detailed debug output"
77
- puts " --sse: Input is in SSE format"
78
- puts " --to-sse: Generate SSE instead of running query"
79
- puts " --debug: Display detailed debug output"
80
- puts " --update: Process query as a SPARQL Update"
81
- puts " --verbose: Display details of processing"
82
- puts " --help,-?: This message"
83
- exit(0)
122
+ when "--help" then usage
84
123
  end
85
124
  end
86
125
 
87
- if ARGV.empty?
88
- s = input ? input : $stdin.read
89
- run(s, options)
90
- else
91
- ARGV.each do |test_file|
92
- puts "parse #{test_file}"
93
- run(RDF::Util::File.open_file(test_file).read, options.merge(base_uri: RDF::URI(test_file)))
126
+ unless %w(execute query parse server help).include?(cmd)
127
+ $stderr.puts "Unrecognized command #{cmd}"
128
+ usage
129
+ end
130
+
131
+ case cmd
132
+ when 'execute', 'parse'
133
+ options[:to_sse] = true if cmd == 'parse'
134
+ input ||= ARGV.empty? ? $stdin.read : RDF::Util::File.open_file(ARGV.first).read
135
+ run(input, options)
136
+ when 'query'
137
+ endpoint = ARGV.shift
138
+ unless endpoint
139
+ $stderr.puts "Expected SPARQL endpoint URL"
140
+ usage
141
+ end
142
+ input ||= ARGV.empty? ? $stdin.read : RDF::Util::File.open_file(ARGV.first).read
143
+ SPARQL::Client.new(endpoint) do |client|
144
+ res = client.query(input)
145
+ display_results(res, options)
146
+ end
147
+ when 'server'
148
+ if data_file = ARGV.shift
149
+ options[:dataset] = RDF::Repository.load(data_file)
94
150
  end
151
+ server(options)
152
+ else usage
95
153
  end
96
- puts
@@ -584,23 +584,29 @@ module SPARQL; module Algebra
584
584
  end
585
585
 
586
586
  ##
587
- # Iterate via depth-first recursive descent over operands, yielding each operator
587
+ # Enumerate via depth-first recursive descent over operands, yielding each operator
588
588
  # @yield operator
589
589
  # @yieldparam [Object] operator
590
- def descendants(&block)
591
- operands.each do |operand|
592
- case operand
593
- when Operator
594
- operand.descendants(&block)
595
- when Array
596
- operand.each do |op|
597
- op.descendants(&block) if op.is_a?(Operator)
598
- block.call(op)
590
+ # @return [Enumerator]
591
+ def each_descendant(&block)
592
+ if block_given?
593
+ operands.each do |operand|
594
+ case operand
595
+ when Array
596
+ operand.each do |op|
597
+ op.each_descendant(&block) if op.respond_to?(:each_descendant)
598
+ block.call(op)
599
+ end
600
+ else
601
+ operand.each_descendant(&block) if operand.respond_to?(:each_descendant)
599
602
  end
603
+ block.call(operand)
600
604
  end
601
- block.call(operand)
602
605
  end
606
+ enum_for(:each_descendant)
603
607
  end
608
+ alias_method :descendants, :each_descendant
609
+ alias_method :each, :each_descendant
604
610
 
605
611
  ##
606
612
  # Parent expression, if any
@@ -37,7 +37,6 @@ module SPARQL; module Algebra
37
37
  operands.shift if silent
38
38
 
39
39
  iri = operands.first
40
- #require 'byebug'; byebug
41
40
  raise ArgumentError, "clear expected a single IRI" if operands.length != 1 || !iri.is_a?(RDF::URI)
42
41
  if queryable.has_context?(iri)
43
42
  raise IOError, "create operation graph #{iri.to_ntriples} exists" unless silent
@@ -46,7 +46,7 @@ module SPARQL; module Algebra
46
46
  end
47
47
 
48
48
  # Set name for RDF::Graph descendants having no context to the name variable
49
- descendants do |op|
49
+ each_descendant do |op|
50
50
  case op
51
51
  when RDF::Query, RDF::Query::Pattern
52
52
  unless op.context
@@ -120,6 +120,5 @@ module SPARQL; module Algebra
120
120
  def each_solution(&block)
121
121
  solutions.each(&block)
122
122
  end
123
- alias_method :each, :each_solution
124
123
  end # Query
125
124
  end; end # SPARQL::Algebra
@@ -552,6 +552,7 @@ module SPARQL::Grammar
552
552
  end
553
553
  production(:QuadData) do |input, data, callback|
554
554
  # Transform using statements instead of patterns, and verify there are no variables
555
+ raise Error, "QuadData empty" unless data[:pattern]
555
556
  raise Error, "QuadData contains variable operands: #{data[:pattern].to_sse}" if data[:pattern].first.variable?
556
557
  self.nd_var_gen = "0"
557
558
  input[:pattern] = data[:pattern]
@@ -881,8 +882,6 @@ module SPARQL::Grammar
881
882
 
882
883
  # [85] VerbSimple ::= Var
883
884
  production(:VerbSimple) do |input, data, callback|
884
- #require 'byebug'; byebug
885
- #data.values.each {|v| add_prod_datum(:Verb, v)}
886
885
  input[:Verb] = data.values.flatten.first
887
886
  end
888
887
 
@@ -1691,17 +1690,14 @@ module SPARQL::Grammar
1691
1690
  # Create URIs
1692
1691
  def iri(value)
1693
1692
  # If we have a base URI, use that when constructing a new URI
1694
- iri = if base_uri
1695
- u = base_uri.join(value.to_s)
1696
- u.lexical = "<#{value}>" unless u.to_s == value.to_s || resolve_iris?
1693
+ value = RDF::URI(value)
1694
+ if base_uri && value.relative?
1695
+ u = base_uri.join(value)
1696
+ u.lexical = "<#{value}>" unless resolve_iris?
1697
1697
  u
1698
1698
  else
1699
- RDF::URI(value)
1699
+ value
1700
1700
  end
1701
-
1702
- #iri.validate! if validate? && iri.respond_to?(:validate)
1703
- #iri = RDF::URI.intern(iri) if intern?
1704
- iri
1705
1701
  end
1706
1702
 
1707
1703
  def ns(prefix, suffix)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sparql
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.8
4
+ version: 1.1.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gregg Kellogg
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2015-08-27 00:00:00.000000000 Z
12
+ date: 2015-09-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rdf