Skip to content

Instantly share code, notes, and snippets.

@jpstroop
Created October 22, 2013 23:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpstroop/7109886 to your computer and use it in GitHub Desktop.
Save jpstroop/7109886 to your computer and use it in GitHub Desktop.
OWL to RDF::Vocabulary
require 'linkeddata'
include RDF
# args
@ns = 'http://library.ucsd.edu/ontology/dams/#'
@class_name = 'UCSD_DAMS'
@i = 'https://raw.github.com/ucsdlib/dams/master/ontology/dams.owl'
@o = 'ucsd_dams.rb'
#
# other types, say, from RDFS?
owl_objp = RDF::URI.new("http://www.w3.org/2002/07/owl#ObjectProperty")
owl_datap = RDF::URI.new("http://www.w3.org/2002/07/owl#DatatypeProperty")
def get_properties(type, ns, graph)
props = []
statements = graph.query([nil, RDF.type, type])
statements.select{ |s| s.subject.starts_with? ns }.each do |s|
props << s.subject
end
props
end
def get_properties_of_subject(subj, graph)
props = []
[ RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#range"),
RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#comment"),
RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#domain"),
RDF::URI.new("http://www.w3.org/2000/01/rdf-schema#subPropertyOf"),
RDF::URI.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type")
].each do |p|
graph.query([subj, p, nil]).each do |p|
props << p
end
end
props
end
def prefix(uri)
uri.to_s.sub!('http://www.w3.org/2000/01/rdf-schema#', 'rdfs:')
uri.to_s.sub!('http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'rdf:')
uri.to_s.sub!('http://www.w3.org/2002/07/owl#', 'owl:')
uri.to_s.sub!(@ns, '_:')
uri
end
graph = RDF::Graph.load(@i, format: :rdfxml)
object_properties = get_properties(owl_objp, @ns, graph)
data_properties = get_properties(owl_datap, @ns, graph)
rb = []
rb << "module RDF"
rb << " class #{@class_name} < Vocabulary(\"#{@ns}\")"
rb << " \# Source: #{@i} on #{Time.now.utc}"
object_properties.each do |op|
rb << " property :#{op.to_s.sub(@ns, "")}"
get_properties_of_subject(op, graph).each do |a|
rb << " \# #{prefix a.predicate.to_s} #{prefix a.object.to_s}"
end
end
rb << " end"
rb << "end"
rb << ""
File.open(@o, 'w') {|f| f.write(rb.join("\n")) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment