sparqlite 0.1.0 → 0.2.0
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 +4 -4
- data/README.md +73 -0
- data/exe/sparqlite +54 -11
- data/lib/sparqlite/endpoints.rb +50 -0
- data/lib/sparqlite/reasoner.rb +102 -0
- data/lib/sparqlite/shell.rb +44 -6
- data/lib/sparqlite/version.rb +1 -1
- data/lib/sparqlite.rb +2 -0
- metadata +32 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 06c02e7ed8ddbfb46a22a2d38858049b9c002e3e2751e4d5f5bdb2d45c4f3259
|
|
4
|
+
data.tar.gz: a0865c10af2d60e774b10104c1a1bcdc5f151791374a35b155b1dad04184e626
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fc5c0d669827a1c61e8c4f86ff49498f0ab6a52cf304d0dc07a53a98663364dd12d4d3dcb23063538609bedfbfd236525dffc0215cc27efca4ee2c2109e437be
|
|
7
|
+
data.tar.gz: 5ad2d0a0560ea06be97fd6b50a3a8b82c830f5b2844a3e7e618d1f9452c1c0ee35a73db8e0afd4b46a01759deb94cb6012e6545d3bfc1893f1f45d7d6818f612
|
data/README.md
CHANGED
|
@@ -40,6 +40,79 @@ Several files are loaded into one graph and queried together, for example
|
|
|
40
40
|
same prefix differently, the first file given wins. A `FILE` may also be an
|
|
41
41
|
`http://` or `https://` URL of an RDF file.
|
|
42
42
|
|
|
43
|
+
### Remote endpoints
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
sparqlite -e ENDPOINT
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
With `-e` (`--endpoint`) queries are sent to a SPARQL endpoint instead of
|
|
50
|
+
being run against local files. `ENDPOINT` is a URL, or one of the built-in
|
|
51
|
+
names `wikidata` and `dbpedia`:
|
|
52
|
+
|
|
53
|
+
```
|
|
54
|
+
$ sparq -e wikidata
|
|
55
|
+
sparql> SELECT ?cat ?catLabel WHERE {
|
|
56
|
+
...> ?cat wdt:P31 wd:Q146 .
|
|
57
|
+
...> SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
|
|
58
|
+
...> } LIMIT 2;
|
|
59
|
+
cat catLabel
|
|
60
|
+
---------- --------
|
|
61
|
+
wd:Q498787 Muezza
|
|
62
|
+
wd:Q677525 Orangey
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
For the built-in names, the prefixes the service predefines (`wd:`, `wdt:`,
|
|
66
|
+
`dbo:`, `dbr:`, ...) are known, so they are completed and used in results.
|
|
67
|
+
A prefix sparqlite does not know is passed through for the endpoint to
|
|
68
|
+
resolve. Files and `-e` cannot be combined.
|
|
69
|
+
|
|
70
|
+
### Reasoning
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
sparqlite -r FILE...
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
With `-r` (`--reason`) the triples that follow from the schema in the files
|
|
77
|
+
are added before the prompt starts, so queries see them like any other data:
|
|
78
|
+
|
|
79
|
+
```
|
|
80
|
+
$ cat family.ttl
|
|
81
|
+
@prefix : <http://example.org/> .
|
|
82
|
+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
|
|
83
|
+
|
|
84
|
+
:Man rdfs:subClassOf :Person .
|
|
85
|
+
:hasFather rdfs:subPropertyOf :hasParent .
|
|
86
|
+
|
|
87
|
+
:John a :Man .
|
|
88
|
+
:Caroline :hasFather :John .
|
|
89
|
+
|
|
90
|
+
$ sparq -r family.ttl
|
|
91
|
+
2 triples inferred
|
|
92
|
+
sparql> SELECT ?x WHERE { ?x a :Person };
|
|
93
|
+
x
|
|
94
|
+
-----
|
|
95
|
+
:John
|
|
96
|
+
sparql> SELECT ?parent WHERE { :Caroline :hasParent ?parent };
|
|
97
|
+
parent
|
|
98
|
+
------
|
|
99
|
+
:John
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Reasoning is done by [rdf-reasoner](https://github.com/ruby-rdf/rdf-reasoner)
|
|
103
|
+
and covers `rdfs:subClassOf`, `rdfs:subPropertyOf`, `rdfs:domain`,
|
|
104
|
+
`rdfs:range`, `owl:equivalentClass` and `owl:equivalentProperty`. Other OWL
|
|
105
|
+
constructs (`owl:inverseOf`, transitive properties, restrictions, ...) are
|
|
106
|
+
not interpreted. The schema may be in the same file as the data or in a
|
|
107
|
+
separate one given alongside it. Terms of the common vocabularies (`foaf:`,
|
|
108
|
+
`schema:`, ...) bring their published definitions with them, so an instance
|
|
109
|
+
of a subclass of `foaf:Person` is also a `foaf:Agent`; statements the files
|
|
110
|
+
make about those vocabularies' own terms are not used. Triples about the
|
|
111
|
+
RDF, RDFS and OWL vocabularies themselves (`?x a rdfs:Resource`,
|
|
112
|
+
`?c a rdfs:Class`) are not added. `-r` cannot be combined with `-e`.
|
|
113
|
+
|
|
114
|
+
### At the prompt
|
|
115
|
+
|
|
43
116
|
- End a statement with `;`. A statement may span several lines.
|
|
44
117
|
- Press Tab to complete keywords, `?variables` already used in the statement,
|
|
45
118
|
and prefixed names found in the data.
|
data/exe/sparqlite
CHANGED
|
@@ -1,21 +1,63 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
|
|
3
|
+
require 'optparse'
|
|
3
4
|
require 'sparqlite'
|
|
4
5
|
|
|
5
6
|
# Also run as "sparq", so messages use the name it was started with.
|
|
6
7
|
program = File.basename($PROGRAM_NAME)
|
|
7
|
-
|
|
8
|
+
endpoint = nil
|
|
9
|
+
reason = false
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
11
|
+
parser = OptionParser.new do |opts|
|
|
12
|
+
opts.program_name = program
|
|
13
|
+
opts.version = Sparqlite::VERSION
|
|
14
|
+
opts.banner = "Usage: #{program} [-r] FILE...\n #{program} -e ENDPOINT"
|
|
15
|
+
opts.separator ''
|
|
16
|
+
opts.separator 'Open one or more RDF files, or a remote SPARQL endpoint, and start an'
|
|
17
|
+
opts.separator 'interactive SPARQL prompt.'
|
|
18
|
+
opts.separator 'End a statement with ";". Type ".exit" or press Ctrl-D to quit.'
|
|
19
|
+
opts.separator ''
|
|
20
|
+
opts.on('-e', '--endpoint ENDPOINT',
|
|
21
|
+
"Query a SPARQL endpoint: a URL, or one of #{Sparqlite::ENDPOINTS.keys.join(', ')}") do |value|
|
|
22
|
+
endpoint = value
|
|
23
|
+
end
|
|
24
|
+
opts.on('-r', '--reason',
|
|
25
|
+
'Add the triples entailed by the RDFS/OWL schema in the files') do
|
|
26
|
+
reason = true
|
|
27
|
+
end
|
|
28
|
+
opts.on('-h', '--help', 'Show this help') do
|
|
29
|
+
puts opts
|
|
30
|
+
exit
|
|
31
|
+
end
|
|
32
|
+
opts.on('-v', '--version', 'Show the version') do
|
|
33
|
+
puts Sparqlite::VERSION
|
|
34
|
+
exit
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
begin
|
|
39
|
+
parser.parse!
|
|
40
|
+
rescue OptionParser::ParseError => e
|
|
41
|
+
warn "#{program}: #{e.message}"
|
|
42
|
+
exit 1
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
if endpoint
|
|
46
|
+
unless ARGV.empty?
|
|
47
|
+
warn "#{program}: files cannot be combined with --endpoint"
|
|
48
|
+
exit 1
|
|
49
|
+
end
|
|
50
|
+
if reason
|
|
51
|
+
warn "#{program}: --reason cannot be combined with --endpoint"
|
|
52
|
+
exit 1
|
|
53
|
+
end
|
|
54
|
+
unless Sparqlite::ENDPOINTS.key?(endpoint) || endpoint.match?(%r{\Ahttps?://})
|
|
55
|
+
warn "#{program}: unknown endpoint: #{endpoint} (give a URL, or one of #{Sparqlite::ENDPOINTS.keys.join(', ')})"
|
|
56
|
+
exit 1
|
|
57
|
+
end
|
|
58
|
+
elsif ARGV.empty?
|
|
59
|
+
warn parser.banner
|
|
12
60
|
exit 1
|
|
13
|
-
when '-h', '--help'
|
|
14
|
-
puts usage
|
|
15
|
-
puts 'Open one or more RDF files and start an interactive SPARQL prompt.'
|
|
16
|
-
puts 'End a statement with ";". Type ".exit" or press Ctrl-D to quit.'
|
|
17
|
-
when '-v', '--version'
|
|
18
|
-
puts Sparqlite::VERSION
|
|
19
61
|
else
|
|
20
62
|
# A URL is fetched by the reader, so only local paths are checked here.
|
|
21
63
|
missing = ARGV.reject { |path| path.match?(%r{\Ahttps?://}) || File.file?(path) }
|
|
@@ -23,5 +65,6 @@ else
|
|
|
23
65
|
missing.each { |path| warn "#{program}: no such file: #{path}" }
|
|
24
66
|
exit 1
|
|
25
67
|
end
|
|
26
|
-
Sparqlite::Shell.new(ARGV).start
|
|
27
68
|
end
|
|
69
|
+
|
|
70
|
+
Sparqlite::Shell.new(ARGV, endpoint: endpoint, reason: reason).start
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
module Sparqlite
|
|
2
|
+
# Public endpoints that can be named instead of giving a URL. Their
|
|
3
|
+
# prefixes are the ones the service itself predefines, so queries and
|
|
4
|
+
# results read the same as on the service's own query page.
|
|
5
|
+
ENDPOINTS = {
|
|
6
|
+
'wikidata' => {
|
|
7
|
+
url: 'https://query.wikidata.org/sparql',
|
|
8
|
+
prefixes: {
|
|
9
|
+
'wd' => 'http://www.wikidata.org/entity/',
|
|
10
|
+
'wds' => 'http://www.wikidata.org/entity/statement/',
|
|
11
|
+
'wdv' => 'http://www.wikidata.org/value/',
|
|
12
|
+
'wdt' => 'http://www.wikidata.org/prop/direct/',
|
|
13
|
+
'wdtn' => 'http://www.wikidata.org/prop/direct-normalized/',
|
|
14
|
+
'wdref' => 'http://www.wikidata.org/reference/',
|
|
15
|
+
'wdno' => 'http://www.wikidata.org/prop/novalue/',
|
|
16
|
+
'wdata' => 'http://www.wikidata.org/wiki/Special:EntityData/',
|
|
17
|
+
'p' => 'http://www.wikidata.org/prop/',
|
|
18
|
+
'ps' => 'http://www.wikidata.org/prop/statement/',
|
|
19
|
+
'psv' => 'http://www.wikidata.org/prop/statement/value/',
|
|
20
|
+
'psn' => 'http://www.wikidata.org/prop/statement/value-normalized/',
|
|
21
|
+
'pq' => 'http://www.wikidata.org/prop/qualifier/',
|
|
22
|
+
'pqv' => 'http://www.wikidata.org/prop/qualifier/value/',
|
|
23
|
+
'pqn' => 'http://www.wikidata.org/prop/qualifier/value-normalized/',
|
|
24
|
+
'pr' => 'http://www.wikidata.org/prop/reference/',
|
|
25
|
+
'prv' => 'http://www.wikidata.org/prop/reference/value/',
|
|
26
|
+
'prn' => 'http://www.wikidata.org/prop/reference/value-normalized/',
|
|
27
|
+
'wikibase' => 'http://wikiba.se/ontology#',
|
|
28
|
+
'bd' => 'http://www.bigdata.com/rdf#',
|
|
29
|
+
'hint' => 'http://www.bigdata.com/queryHints#',
|
|
30
|
+
'mwapi' => 'https://www.mediawiki.org/ontology#API/',
|
|
31
|
+
'schema' => 'http://schema.org/',
|
|
32
|
+
'geo' => 'http://www.opengis.net/ont/geosparql#',
|
|
33
|
+
'prov' => 'http://www.w3.org/ns/prov#',
|
|
34
|
+
'ontolex' => 'http://www.w3.org/ns/lemon/ontolex#',
|
|
35
|
+
'dct' => 'http://purl.org/dc/terms/'
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
'dbpedia' => {
|
|
39
|
+
url: 'https://dbpedia.org/sparql',
|
|
40
|
+
prefixes: {
|
|
41
|
+
'dbo' => 'http://dbpedia.org/ontology/',
|
|
42
|
+
'dbr' => 'http://dbpedia.org/resource/',
|
|
43
|
+
'dbp' => 'http://dbpedia.org/property/',
|
|
44
|
+
'dbc' => 'http://dbpedia.org/resource/Category:',
|
|
45
|
+
'dbt' => 'http://dbpedia.org/resource/Template:',
|
|
46
|
+
'yago' => 'http://dbpedia.org/class/yago/'
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}.freeze
|
|
50
|
+
end
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
require 'rdf'
|
|
2
|
+
|
|
3
|
+
module Sparqlite
|
|
4
|
+
# Adds the triples entailed by the schema statements found in the data
|
|
5
|
+
# itself, using rdf-reasoner.
|
|
6
|
+
module Reasoner
|
|
7
|
+
CLASS = RDF::RDFS.Class
|
|
8
|
+
PROPERTY = RDF.Property
|
|
9
|
+
|
|
10
|
+
# The schema predicates rdf-reasoner has rules for, each with the type
|
|
11
|
+
# it implies for its subject and for its object.
|
|
12
|
+
SCHEMA = {
|
|
13
|
+
RDF::RDFS.subClassOf => [CLASS, CLASS],
|
|
14
|
+
RDF::RDFS.subPropertyOf => [PROPERTY, PROPERTY],
|
|
15
|
+
RDF::RDFS.domain => [PROPERTY, CLASS],
|
|
16
|
+
RDF::RDFS.range => [PROPERTY, CLASS],
|
|
17
|
+
RDF::OWL.equivalentClass => [CLASS, CLASS],
|
|
18
|
+
RDF::OWL.equivalentProperty => [PROPERTY, PROPERTY]
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
# The rules that apply to an "rdf:type" statement, and to any other one.
|
|
22
|
+
TYPE_RULES = %i[subClassOf equivalentClass].freeze
|
|
23
|
+
PROPERTY_RULES = %i[subPropertyOf domain range equivalentProperty].freeze
|
|
24
|
+
|
|
25
|
+
# The vocabularies that describe schemas themselves.
|
|
26
|
+
META = [RDF, RDF::RDFS, RDF::OWL].map { |vocabulary| vocabulary.to_uri.to_s }.freeze
|
|
27
|
+
|
|
28
|
+
# Holds the vocabularies built from the data. rdf-reasoner only finds
|
|
29
|
+
# vocabulary classes that have a name, hence the constants.
|
|
30
|
+
module Vocabularies; end
|
|
31
|
+
|
|
32
|
+
module_function
|
|
33
|
+
|
|
34
|
+
# Adds the entailed triples to the graph and returns how many were added.
|
|
35
|
+
def entail!(graph)
|
|
36
|
+
# Loaded on demand: it extends the core RDF classes, and is not needed
|
|
37
|
+
# unless reasoning was asked for.
|
|
38
|
+
require 'rdf/reasoner'
|
|
39
|
+
RDF::Reasoner.apply(:rdfs, :owl)
|
|
40
|
+
|
|
41
|
+
register(schema_of(graph))
|
|
42
|
+
before = graph.count
|
|
43
|
+
# A rule has a single statement as its premise, so only the statements
|
|
44
|
+
# added in the last round can lead to new ones.
|
|
45
|
+
fresh = graph.to_a
|
|
46
|
+
until fresh.empty?
|
|
47
|
+
fresh = fresh.flat_map { |statement| entailed(statement) }.reject { |statement| graph.include?(statement) }
|
|
48
|
+
graph.insert(*fresh) unless fresh.empty?
|
|
49
|
+
end
|
|
50
|
+
graph.count - before
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# The statements that follow from one statement. Reasoning about the
|
|
54
|
+
# RDF, RDFS and OWL vocabularies themselves is left out: it would type
|
|
55
|
+
# everything as rdfs:Resource and every class as rdfs:Class, and go on
|
|
56
|
+
# from there through whatever the known vocabularies say about those.
|
|
57
|
+
def entailed(statement)
|
|
58
|
+
typing = statement.predicate == RDF.type
|
|
59
|
+
return [] if meta?(typing ? statement.object : statement.predicate)
|
|
60
|
+
|
|
61
|
+
found = []
|
|
62
|
+
(typing ? TYPE_RULES : PROPERTY_RULES).each { |rule| statement.entail(rule) { |entailed| found << entailed } }
|
|
63
|
+
found.reject { |entailed| entailed.predicate == RDF.type && meta?(entailed.object) }
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def meta?(term)
|
|
67
|
+
term.uri? && META.any? { |namespace| term.to_s.start_with?(namespace) }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# The schema statements of the graph. rdf-reasoner applies its rules
|
|
71
|
+
# only to terms typed as a class or a property, which data often leaves
|
|
72
|
+
# implicit (":Man rdfs:subClassOf :Person" alone), so the types the
|
|
73
|
+
# schema predicates imply are spelled out.
|
|
74
|
+
def schema_of(graph)
|
|
75
|
+
schema = RDF::Graph.new
|
|
76
|
+
graph.each_statement do |statement|
|
|
77
|
+
types = SCHEMA[statement.predicate] or next
|
|
78
|
+
schema << statement
|
|
79
|
+
[statement.subject, statement.object].zip(types) do |term, type|
|
|
80
|
+
schema << [term, RDF.type, type] if term.uri?
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
schema
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# rdf-reasoner looks terms up in the registered vocabularies, not in the
|
|
87
|
+
# graph, so each namespace the schema describes becomes a vocabulary.
|
|
88
|
+
# A namespace of a vocabulary that is already known (rdfs:, foaf:, ...)
|
|
89
|
+
# keeps its built-in definition.
|
|
90
|
+
def register(schema)
|
|
91
|
+
namespaces = schema.subjects.grep(RDF::URI).map { |term| term.to_s[%r{\A.*[/#:]}] }.compact.uniq
|
|
92
|
+
# The first registered vocabulary whose IRI starts a term claims it,
|
|
93
|
+
# so a namespace nested in another one has to come first.
|
|
94
|
+
namespaces.sort_by { |namespace| -namespace.length }.each do |namespace|
|
|
95
|
+
next if RDF::Vocabulary.find("#{namespace}_")
|
|
96
|
+
|
|
97
|
+
name = "V#{Vocabularies.constants.length}"
|
|
98
|
+
Vocabularies.const_set(name, RDF::Vocabulary.from_graph(schema, url: namespace))
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|
|
102
|
+
end
|
data/lib/sparqlite/shell.rb
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
require 'rdf/turtle'
|
|
2
2
|
require 'rdf/vocab'
|
|
3
3
|
require 'sparql'
|
|
4
|
+
require 'sparql/client'
|
|
4
5
|
require 'reline'
|
|
5
6
|
|
|
6
7
|
module Sparqlite
|
|
7
|
-
# An interactive SPARQL prompt over
|
|
8
|
+
# An interactive SPARQL prompt over local RDF files or a remote endpoint.
|
|
8
9
|
class Shell
|
|
9
10
|
# Always available, so common vocabularies (foaf:, schema:, skos:, ...)
|
|
10
11
|
# work without a declaration. Prefixes declared in the data file take
|
|
@@ -56,10 +57,23 @@ module Sparqlite
|
|
|
56
57
|
HISTORY_FILE = File.expand_path('~/.sparql_history')
|
|
57
58
|
HISTORY_MAX = 1000
|
|
58
59
|
|
|
60
|
+
QUERY_FORMS = %w[SELECT ASK CONSTRUCT DESCRIBE].freeze
|
|
61
|
+
|
|
62
|
+
# Error bodies from an endpoint can be very long.
|
|
63
|
+
ERROR_LINES = 5
|
|
64
|
+
|
|
59
65
|
# All files are loaded into one graph. Blank nodes stay separate per
|
|
60
|
-
# file even when two files use the same label.
|
|
61
|
-
|
|
66
|
+
# file even when two files use the same label. With an endpoint (a URL
|
|
67
|
+
# or a name from ENDPOINTS), queries are sent there instead. With
|
|
68
|
+
# reason, the triples entailed by the files' own schema are added.
|
|
69
|
+
def initialize(paths, endpoint: nil, reason: false)
|
|
62
70
|
@prefixes = {}
|
|
71
|
+
if endpoint
|
|
72
|
+
known = ENDPOINTS[endpoint]
|
|
73
|
+
@prefixes = known[:prefixes].dup if known
|
|
74
|
+
@client = SPARQL::Client.new(known ? known[:url] : endpoint,
|
|
75
|
+
headers: { 'User-Agent' => "sparqlite/#{VERSION}" })
|
|
76
|
+
end
|
|
63
77
|
@graph = RDF::Graph.new
|
|
64
78
|
Array(paths).each do |path|
|
|
65
79
|
RDF::Reader.open(path) do |reader|
|
|
@@ -69,6 +83,8 @@ module Sparqlite
|
|
|
69
83
|
reader.prefixes.each { |name, ns| @prefixes[name.to_s] ||= ns.to_s }
|
|
70
84
|
end
|
|
71
85
|
end
|
|
86
|
+
# Reported on stderr so that piped results stay clean.
|
|
87
|
+
warn "#{Reasoner.entail!(@graph)} triples inferred" if reason
|
|
72
88
|
# The files' prefixes come first so they win, both by name and when
|
|
73
89
|
# two prefixes share a namespace.
|
|
74
90
|
@prefixes = @prefixes.merge(DEFAULT_PREFIXES) { |_, from_file, _| from_file }
|
|
@@ -202,18 +218,40 @@ module Sparqlite
|
|
|
202
218
|
used, = query_prefixes(query)
|
|
203
219
|
# The engine silently matches nothing for an undeclared prefix, which
|
|
204
220
|
# looks like missing data, so reject it here.
|
|
205
|
-
|
|
221
|
+
# An endpoint may predefine prefixes of its own, so there the check
|
|
222
|
+
# is left to the server.
|
|
223
|
+
unknown = @client ? [] : used - @prefixes.keys
|
|
206
224
|
return puts("unknown prefix: #{unknown.map { |name| "#{name}:" }.join(', ')}") unless unknown.empty?
|
|
207
225
|
|
|
208
226
|
# Declare only what the query uses, on the query's first line so the
|
|
209
227
|
# line numbers in error messages match what was typed.
|
|
210
|
-
header = used.map { |name| "PREFIX #{name}: <#{@prefixes[name]}> " }.join
|
|
211
|
-
|
|
228
|
+
header = (used & @prefixes.keys).map { |name| "PREFIX #{name}: <#{@prefixes[name]}> " }.join
|
|
229
|
+
text = "#{header}#{query}"
|
|
230
|
+
print_result(@client ? @client.query(text, **accept(query)) : SPARQL.execute(text, @graph))
|
|
231
|
+
rescue SPARQL::Client::ClientError, SPARQL::Client::ServerError => e
|
|
232
|
+
puts endpoint_error(e.message)
|
|
212
233
|
rescue StandardError, SPARQL::MalformedQuery => e
|
|
213
234
|
# MalformedQuery does not inherit from StandardError.
|
|
214
235
|
puts e.message
|
|
215
236
|
end
|
|
216
237
|
|
|
238
|
+
# Ask for triples when the query builds a graph. Left to itself the
|
|
239
|
+
# client accepts anything, and some servers then answer a CONSTRUCT
|
|
240
|
+
# with a table of solutions.
|
|
241
|
+
def accept(query)
|
|
242
|
+
form = query.scan(TOKEN).map(&:last).compact.map(&:upcase).find { |w| QUERY_FORMS.include?(w) }
|
|
243
|
+
%w[CONSTRUCT DESCRIBE].include?(form) ? { content_type: 'application/n-triples' } : {}
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
# The client appends the whole query to the response body, and servers
|
|
247
|
+
# often echo it and add a stack trace; show only what the server said.
|
|
248
|
+
def endpoint_error(message)
|
|
249
|
+
lines = message.sub(/ Processing query .*\z/m, '').lines.map(&:rstrip)
|
|
250
|
+
.reject { |l| l.empty? || l.match?(/\A\s+(at |\.\.\. \d+ more)|\ASPARQL-QUERY: /) }
|
|
251
|
+
lines = lines.first(ERROR_LINES) + ['...'] if lines.length > ERROR_LINES
|
|
252
|
+
lines.join("\n")
|
|
253
|
+
end
|
|
254
|
+
|
|
217
255
|
# Every IRI in the data that can be written as a prefixed name.
|
|
218
256
|
def graph_terms
|
|
219
257
|
@graph.each_statement.flat_map(&:to_a).grep(RDF::URI).uniq
|
data/lib/sparqlite/version.rb
CHANGED
data/lib/sparqlite.rb
CHANGED
metadata
CHANGED
|
@@ -1,15 +1,29 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: sparqlite
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shoji Tokunaga
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-11-
|
|
11
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rdf-reasoner
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '0.9'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '0.9'
|
|
13
27
|
- !ruby/object:Gem::Dependency
|
|
14
28
|
name: rdf-turtle
|
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -66,6 +80,20 @@ dependencies:
|
|
|
66
80
|
- - "~>"
|
|
67
81
|
- !ruby/object:Gem::Version
|
|
68
82
|
version: '3.3'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: sparql-client
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '3.3'
|
|
90
|
+
type: :runtime
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '3.3'
|
|
69
97
|
description: |
|
|
70
98
|
sparqlite opens a Turtle file and drops you into a SPARQL prompt with no
|
|
71
99
|
server and no configuration. Queries are syntax-highlighted as you type,
|
|
@@ -82,6 +110,8 @@ files:
|
|
|
82
110
|
- exe/sparq
|
|
83
111
|
- exe/sparqlite
|
|
84
112
|
- lib/sparqlite.rb
|
|
113
|
+
- lib/sparqlite/endpoints.rb
|
|
114
|
+
- lib/sparqlite/reasoner.rb
|
|
85
115
|
- lib/sparqlite/shell.rb
|
|
86
116
|
- lib/sparqlite/version.rb
|
|
87
117
|
homepage: https://github.com/sean2121/sparqlite
|