Add schema inspection queries and script.
SPARQL queries to list classes and properties, and src/schema.rb CLI to run them against any graph file. Includes bundler/setup for compatibility with Ruby 4.0 without bundle exec.
This commit is contained in:
parent
d2481d6e80
commit
cd85a66c46
3 changed files with 49 additions and 0 deletions
4
queries/list_classes.sparql
Normal file
4
queries/list_classes.sparql
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
SELECT DISTINCT ?class WHERE {
|
||||
?s a ?class .
|
||||
}
|
||||
ORDER BY ?class
|
||||
5
queries/list_properties.sparql
Normal file
5
queries/list_properties.sparql
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
SELECT DISTINCT ?property WHERE {
|
||||
?s ?property ?o .
|
||||
FILTER(?property != <http://www.w3.org/1999/02/22-rdf-syntax-ns#type>)
|
||||
}
|
||||
ORDER BY ?property
|
||||
40
src/schema.rb
Executable file
40
src/schema.rb
Executable file
|
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env ruby
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Usage: ruby src/schema.rb TYPE [--graph FILE]
|
||||
# TYPE: classes | properties
|
||||
# FILE: path to an RDF/Turtle file (default: last data/graph-*.ttl)
|
||||
|
||||
require 'bundler/setup'
|
||||
require 'rdf'
|
||||
require 'rdf/turtle'
|
||||
require 'sparql'
|
||||
|
||||
type = ARGV.shift
|
||||
if !%w[classes properties].include?(type)
|
||||
abort "Usage: ruby src/schema.rb TYPE [--graph FILE]\n TYPE: classes | properties"
|
||||
end
|
||||
|
||||
graph_file = nil
|
||||
if ARGV[0] == '--graph'
|
||||
ARGV.shift
|
||||
graph_file = ARGV.shift
|
||||
end
|
||||
|
||||
graph_file ||= Dir.glob(File.join('data', 'graph-*.ttl')).sort.last
|
||||
|
||||
abort "No graph file found." if graph_file.nil? || !File.exist?(graph_file)
|
||||
|
||||
query_file = File.join(__dir__, '..', 'queries', "list_#{type}.sparql")
|
||||
query = File.read(query_file)
|
||||
|
||||
$stderr.puts "Loading #{graph_file}..."
|
||||
graph = RDF::Graph.load(graph_file)
|
||||
$stderr.puts "Loaded #{graph.count} triples."
|
||||
|
||||
solutions = SPARQL.execute(query, graph)
|
||||
|
||||
key = type == 'classes' ? :class : :property
|
||||
solutions.each do |solution|
|
||||
puts solution[key].to_s
|
||||
end
|
||||
Loading…
Reference in a new issue