Class: RDF::Vocabulary
- Inherits:
-
Object
- Object
- RDF::Vocabulary
- Defined in:
- lib/rdf/vocab/extensions.rb
Defined Under Namespace
Modules: VocabFormatExtensions
Class Method Summary collapse
-
._orig_each {|klass| ... } ⇒ Enumerator
Enumerates known RDF vocabulary classes.
- ._orig_from_sym ⇒ Object
- .each(&block) ⇒ Object
-
.from_sym(sym) ⇒ RDF::Vocabulary
Return the vocabulary based on it’s class_name symbol.
-
.limit_vocabs(*vocabs) ⇒ Array<RDF::Vocabulary>
Limits iteration over vocabularies to just those selected.
-
.to_html(graph: nil, prefixes: nil, jsonld: nil, template: nil) ⇒ String
Generate HTML+RDFa representation, specific to vocabularies.
-
.to_jsonld(graph: nil, prefixes: nil) ⇒ String
Generate JSON-LD representation, specific to vocabularies.
-
.to_ttl(graph: nil, prefixes: nil) ⇒ String
Generate Turtle representation, specific to vocabularies.
-
.value_to_html(property, value, tag) ⇒ Object
Create HTML for values (Helper method, needs to be public).
-
.vocab_map ⇒ Hash{Symbol => Hash{Symbol => String}}
A hash of all vocabularies by prefix showing relevant URI and associated vocabulary Class Name.
Class Method Details
._orig_each {|klass| ... } ⇒ Enumerator
Enumerates known RDF vocabulary classes.
17 |
# File 'lib/rdf/vocab/extensions.rb', line 17 alias_method :_orig_each, :each |
._orig_from_sym ⇒ Object
43 |
# File 'lib/rdf/vocab/extensions.rb', line 43 alias_method :_orig_from_sym, :from_sym |
.each(&block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rdf/vocab/extensions.rb', line 18 def each(&block) if self.equal?(Vocabulary) # This is needed since all vocabulary classes are defined using # Ruby's autoloading facility, meaning that `@@subclasses` will be # empty until each subclass has been touched or require'd. RDF::Vocab::VOCABS.each do |n, params| clsname = params[:class_name].to_sym RDF::Vocab.const_get(clsname) # Forces class to load end unless instance_variable_defined?(:@classes_loaded) && @classes_loaded @classes_loaded = true end _orig_each(&block) end |
.from_sym(sym) ⇒ RDF::Vocabulary
Return the vocabulary based on it’s class_name symbol
49 50 51 52 |
# File 'lib/rdf/vocab/extensions.rb', line 49 def from_sym(sym) RDF::Vocab.const_defined?(sym.to_sym) ? RDF::Vocab.const_get(sym.to_sym) : _orig_from_sym(sym) end |
.limit_vocabs(*vocabs) ⇒ Array<RDF::Vocabulary>
Limits iteration over vocabularies to just those selected
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rdf/vocab/extensions.rb', line 73 def limit_vocabs(*vocabs) @vocabs = if Array(vocabs).empty? nil else @classes_loaded = true vocabs.map do |vocab| if vocab == :rdf || vocab == :rdfv RDF::RDFV elsif vocab.is_a?(Symbol) && RDF::Vocab::VOCABS.key?(vocab) RDF::Vocab.const_get(RDF::Vocab::VOCABS[vocab][:class_name].to_sym) else vocab end end.compact end end |
.to_html(graph: nil, prefixes: nil, jsonld: nil, template: nil) ⇒ String
Generate HTML+RDFa representation, specific to vocabularies. This uses generated JSON-LD and a Haml template.
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
# File 'lib/rdf/vocab/extensions.rb', line 329 def to_html(graph: nil, prefixes: nil, jsonld: nil, template: nil) # Find namespaces used in the vocabulary graph = RDF::Graph.new {|g| each_statement {|s| g << s}} if graph.nil? || graph.empty? # Get JSON as an object json = case jsonld when String then ::JSON.parse(File.read jsonld) when Hash then jsonld else ::JSON.parse(to_jsonld(graph: graph, prefixes: prefixes)) end raise "Expected JSON-LD data within the '@graph' key" unless json.has_key?('@graph') template ||= File.("../../../../etc/template.erb", __FILE__) prefixes = vocab_prefixes(graph).merge(prefixes || {}) prefixes[:owl] = RDF::OWL.to_uri.to_s # Make sure ontology is typed json['@graph']['@type'] ||= ['owl:Ontology'] jld_context = ::JSON::LD::Context.new.parse([json['@context'], json['@graph']['@context']]) # Expand the JSON-LD to normalize accesses = ::JSON::LD::API.(json).first .delete('@reverse') # Re-compact keys = .inject({}) do |memo, (k, v)| term = RDF::Vocabulary.find_term(k) k = term.pname if term memo.merge(k => v) end # Normalize label accessors ['rdfs:label'] ||= %w(dc:title dc11:title skos:prefLabel).inject(nil) do |memo, key| memo || [key] end || [{'@value' => json['@graph']['@id']}] %w(rdfs_classes rdfs_properties rdfs_datatypes rdfs_instances).each do |section| next unless json['@graph'][section] json['@graph'][section].each do |node| node['rdfs:label'] ||= %w(dc:title dc11:title skos:prefLabel).inject do |memo, key| memo || node[key] end || [{'@value' => node['@id']}] end end # Expand each part separately, as well. %w(rdfs_classes rdfs_properties rdfs_datatypes rdfs_instances).each do |section| next unless json['@graph'][section] = ::JSON::LD::API.(json['@graph'][section], expandContext: jld_context) # Re-compact keys [section] = .map do |node| node.inject({}) do |memo, (k, v)| term = RDF::Vocabulary.find_term(k) k = term.pname if term memo.merge(k => v) end end end # Template invoked with expanded JSON-LD with outer object including `rdfs_classes`, `rdfs_properties`, and `rdf_instances` sections. case template when /.haml$/ require 'haml' haml = if Haml::VERSION >= "6" Haml::Template.new {File.read(template)} else Haml::Engine.new(File.read(template)) end haml.render(self, ont: , context: json['@context'], prefixes: prefixes) when /.erb$/ require 'erubis' eruby = Erubis::FastEruby.new(File.read(template)) eruby.evaluate(binding: self, ont: , context: json['@context'], prefixes: prefixes) else raise "Unknown template type #{template}. Should have '.erb' or '.haml' extension" end end |
.to_jsonld(graph: nil, prefixes: nil) ⇒ String
Generate JSON-LD representation, specific to vocabularies
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/rdf/vocab/extensions.rb', line 176 def to_jsonld(graph: nil, prefixes: nil) require 'json/ld' context = {} rdfs_context = ::JSON.parse %({ "dc:title": {"@container": "@language"}, "dc:description": {"@container": "@language"}, "dc:date": {"@type": "xsd:date"}, "rdfs:comment": {"@container": "@language"}, "rdfs:domain": {"@type": "@vocab"}, "rdfs:label": {"@container": "@language"}, "rdfs:range": {"@type": "@vocab"}, "rdfs:seeAlso": {"@type": "@id"}, "rdfs:subClassOf": {"@type": "@vocab"}, "rdfs:subPropertyOf": {"@type": "@vocab"}, "schema:domainIncludes": {"@type": "@vocab"}, "schema:rangeIncludes": {"@type": "@vocab"}, "owl:equivalentClass": {"@type": "@vocab"}, "owl:equivalentProperty": {"@type": "@vocab"}, "owl:oneOf": {"@container": "@list", "@type": "@vocab"}, "owl:imports": {"@type": "@id"}, "owl:versionInfo": {"@type": "@id"}, "owl:inverseOf": {"@type": "@vocab"}, "owl:unionOf": {"@type": "@vocab", "@container": "@list"}, "rdfs_classes": {"@reverse": "rdfs:isDefinedBy", "@type": "@id"}, "rdfs_properties": {"@reverse": "rdfs:isDefinedBy", "@type": "@id"}, "rdfs_datatypes": {"@reverse": "rdfs:isDefinedBy", "@type": "@id"}, "rdfs_instances": {"@reverse": "rdfs:isDefinedBy", "@type": "@id"} }) rdfs_classes, rdfs_properties, rdfs_datatypes, rdfs_instances = [], [], [], [], [] ontology = { "@context" => rdfs_context, "@id" => to_uri.to_s } # Find namespaces used in the vocabulary graph = RDF::Graph.new {|g| each_statement {|s| g << s}} if graph.nil? || graph.empty? prefixes = vocab_prefixes(graph).merge(prefixes || {}) prefixes.each do |pfx, uri| context[pfx.to_s] = uri.to_s unless pfx.to_s.empty? end # Determine the category for each subject in the vocabulary graph cats = subject_categories(graph) # Generate term definitions from graph subjects cats.values.flatten.each do |term| next unless Array(term.qname).length == 2 context[term.qname.last.to_s] = term.to_uri.to_s end # Parse the two contexts so we know what terms are in scope jld_context = ::JSON::LD::Context.new.parse([context, rdfs_context]) { ont: { heading: "# #{__name__.split('::').last} Vocabulary definition\n", bucket: ontology, }, classes: { heading: "# Class definitions\n", bucket: rdfs_classes, rev_prop: "rdfs_classes" }, properties: { heading: "# Property definitions\n", bucket: rdfs_properties, rev_prop: "rdfs_properties" }, datatypes: { heading: "# Datatype definitions\n", bucket: rdfs_datatypes, rev_prop: "rdfs_datatypes" }, other: { heading: "# Other definitions\n", bucket: rdfs_instances, rev_prop: "rdfs_instances" } }.each do |key, hash| next unless cats[key] cats[key].each do |subject| node = {"@id" => subject.pname} po = {} # Group predicates with their values graph.query({subject: subject}) do |statement| # Sanity check this, as these are set to an empty string if not defined. next if [RDF::RDFS.label, RDF::RDFS.comment].include?(statement.predicate) && statement.object.to_s.empty? po[statement.predicate] ||= [] po[statement.predicate] << statement.object end next if po.empty? node['@type'] = po.delete(RDF.type).map {|t| jld_context.compact_iri(t, vocab: true)} po.each do |predicate, objects| term = jld_context.compact_iri(predicate, vocab: true) node[term] = if jld_context.container(term).include?('@language') lang_map = objects.inject({}) do |memo, o| raise "Language-mapped term #{term} with non plain-literal #{o.inspect}" unless o.literal? && o.plain? memo.merge(o.language.to_s => o.value) end # Don't use language map if there's only one entry with no language lang_map = lang_map[""] if lang_map.keys == [""] [lang_map] else objects.map do |o| = jld_context.(term, o) jld_context.compact_value(term, ) end end end node.each do |property, values| case values.length when 0 then node.delete(property) when 1 then node[property] = values.first end end # Either set bucket from node, or append node to bucket if hash[:bucket].is_a?(Hash) hash[:bucket].merge!(node) else ontology[hash[:rev_prop]] ||= hash[:bucket] hash[:bucket] << node end end end # Serialize result { "@context" => context, "@graph" => ontology }.to_json(::JSON::LD::JSON_STATE) rescue LoadError # No JSON-LD serialization unless gem loaded end |
.to_ttl(graph: nil, prefixes: nil) ⇒ String
Generate Turtle representation, specific to vocabularies
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/rdf/vocab/extensions.rb', line 96 def to_ttl(graph: nil, prefixes: nil) require 'rdf/turtle' output = [] # Find namespaces used in the vocabulary graph = RDF::Graph.new {|g| each_statement {|s| g << s}} if graph.nil? || graph.empty? prefixes = vocab_prefixes(graph).merge(prefixes || {}) pfx_width = prefixes.keys.map(&:to_s).map(&:length).max prefixes.each do |pfx, uri| output << "@prefix %*s: <%s> .\n" % [pfx_width, pfx, uri] end # Determine the category for each subject in the vocabulary graph cats = subject_categories(graph) writer = RDF::Turtle::Writer.new(StringIO.new, prefixes: prefixes) { ont: { heading: "# #{__name__.split('::').last} Vocabulary definition\n" }, classes: { heading: "# Class definitions\n" }, properties: { heading: "# Property definitions\n" }, datatypes: { heading: "# Datatype definitions\n" }, other: { heading: "# Other definitions\n" } }.each do |key, hash| next unless cats[key] output << "\n\n#{hash[:heading]}" cats[key].each do |subject| po = {} # Group predicates with their values graph.query({subject: subject}) do |statement| # Sanity check this, as these are set to an empty string if not defined. next if [RDF::RDFS.label, RDF::RDFS.comment].include?(statement.predicate) && statement.object.to_s.empty? po[statement.predicate] ||= [] po[statement.predicate] << statement.object end next if po.empty? po_list = [] unless (types = po.delete(RDF.type)).empty? po_list << 'a ' + types.map {|o| writer.format_term(o)}.join(", ") end # Serialize other predicate/objects po.each do |predicate, objects| resource = predicate.qname ? predicate.pname : "<#{predicate}>" po_list << resource + ' ' + objects.map {|o| writer.format_term(o)}.join(", ") end # Output statements for this subject subj = subject.qname ? subject.pname : "<#{subject}>" output << "#{subj} " + po_list.join(";\n ") + "\n .\n" end end output.join("") rescue LoadError # No Turtle serialization unless gem loaded end |
.value_to_html(property, value, tag) ⇒ Object
Create HTML for values (Helper method, needs to be public)
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 |
# File 'lib/rdf/vocab/extensions.rb', line 412 def value_to_html(property, value, tag) value.map do |v| %(<#{tag} property="#{property}") + if v['@value'] (v['@language'] ? %( lang="#{v['@language']}") : "") + (v['@type'] ? %( datatype="#{RDF::URI(v['@type']).pname}") : "") + %(>#{v['@value']}) elsif v['@id'] %( resource="#{RDF::URI(v['@id']).pname}">#{RDF::URI(v['@id']).pname}) else raise "Unknown value type: #{v.inspect}, #{property}" end + %(</#{tag}>) end.join("\n") end |
.vocab_map ⇒ Hash{Symbol => Hash{Symbol => String}}
A hash of all vocabularies by prefix showing relevant URI and associated vocabulary Class Name
alias_method :_orig_vocab_map, :vocab_map
38 39 40 41 |
# File 'lib/rdf/vocab/extensions.rb', line 38 def vocab_map @vocab_map ||= RDF::VOCABS.transform_values(&:freeze).merge( RDF::Vocab::VOCABS.transform_values(&:freeze)) end |