Class: RDF::Vocabulary

Inherits:
Object
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/rdf/vocabulary.rb,
lib/rdf/vocab/writer.rb

Overview

Vocabulary format specification. This can be used to generate a Ruby class definition from a loaded vocabulary.

Direct Known Subclasses

StrictVocabulary, XSD

Defined Under Namespace

Classes: Format, Term, Writer

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Vocabulary

Returns a new instance of Vocabulary

Parameters:



456
457
458
459
460
461
# File 'lib/rdf/vocabulary.rb', line 456

def initialize(uri)
  @uri = case uri
    when RDF::URI then uri.to_s
    else RDF::URI.parse(uri.to_s) ? uri.to_s : nil
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(property, *args, &block) ⇒ Object (protected)



506
507
508
509
510
511
512
513
514
515
# File 'lib/rdf/vocabulary.rb', line 506

def method_missing(property, *args, &block)
  property = self.class.camelize(property.to_s)
  if %w(to_ary).include?(property.to_s)
    super
  elsif args.empty?
    self[property]
  else
    super
  end
end

Class Method Details

.[](property) ⇒ RDF::URI

Returns the URI for the term property in this vocabulary.

Parameters:

Returns:



237
238
239
240
241
242
243
# File 'lib/rdf/vocabulary.rb', line 237

def [](property)
  if props.has_key?(property.to_sym)
    props[property.to_sym]
  else
    Term.intern([to_s, property.to_s].join(''), attributes: {vocab: self})
  end
end

.__prefix__Symbol

Returns a suggested CURIE/PName prefix for this vocabulary class.

Returns:

  • (Symbol)

Since:

  • 0.3.0



416
417
418
# File 'lib/rdf/vocabulary.rb', line 416

def __prefix__
  __name__.split('::').last.downcase.to_sym
end

.camelize(str) ⇒ Object (protected)



517
518
519
520
521
# File 'lib/rdf/vocabulary.rb', line 517

def self.camelize(str)
  str.split('_').inject([]) do |buffer, e| 
    buffer.push(buffer.empty? ? e : e.capitalize)
  end.join
end

.each {|klass| ... } ⇒ Enumerator

Enumerates known RDF vocabulary classes.

Yields:

  • (klass)

Yield Parameters:

  • klass (Class)

Returns:

  • (Enumerator)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rdf/vocabulary.rb', line 70

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::VOCABS.each { |v| require "rdf/vocab/#{v}" unless v == :rdf }
    @@subclasses.select(&:name).each(&block)
  else
    __properties__.each(&block)
  end
end

.each_statement {|| ... } ⇒ Object

Enumerate each statement constructed from the defined vocabulary terms

If a property value is known to be a URI, or expands to a URI, the object is a URI, otherwise, it will be a Literal.

Yields:

  • statement

Yield Parameters:



302
303
304
305
306
307
308
309
# File 'lib/rdf/vocabulary.rb', line 302

def each_statement(&block)
  props.each do |name, subject|
    subject.each_statement(&block)
  end

  # Also include the ontology, if it's not also a property
  @ontology.each_statement(&block) if @ontology && @ontology != self
end

.enum_for(method = :each_statement, *args) ⇒ RDF::Enumerable::Enumerator Also known as: to_enum

Return an enumerator over Statement defined for this vocabulary.

Returns:

See Also:

  • Object#enum_for


286
287
288
289
290
291
292
# File 'lib/rdf/vocabulary.rb', line 286

def enum_for(method = :each_statement, *args)
  # Ensure that enumerators are, themselves, queryable
  this = self
  Enumerable::Enumerator.new do |yielder|
    this.send(method, *args) {|*y| yielder << (y.length > 1 ? y : y.first)}
  end
end

.expand_pname(pname) ⇒ RDF::URI

Attempt to expand a Compact IRI/PName/QName using loaded vocabularies

Parameters:

  • pname (String, #to_s)

Returns:

Raises:

  • (KeyError)

    if pname suffix not found in identified vocabulary



189
190
191
192
193
194
195
196
197
198
# File 'lib/rdf/vocabulary.rb', line 189

def expand_pname(pname)
  prefix, suffix = pname.to_s.split(":", 2)
  if prefix == "rdf"
    RDF[suffix]
  elsif vocab = RDF::Vocabulary.each.detect {|v| v.__name__ && v.__prefix__ == prefix.to_sym}
    suffix.to_s.empty? ? vocab.to_uri : vocab[suffix]
  else
    (RDF::Vocabulary.find_term(pname) rescue nil) || RDF::URI(pname)
  end
end

.find(uri) ⇒ Vocabulary

Return the Vocabulary associated with a URI. Allows the trailing '/' or '#' to be excluded

Parameters:

Returns:



205
206
207
208
209
210
211
212
213
# File 'lib/rdf/vocabulary.rb', line 205

def find(uri)
  RDF::Vocabulary.detect do |v|
    if uri.length >= v.to_uri.length
      RDF::URI(uri).start_with?(v.to_uri)
    else
      v.to_uri.to_s.sub(%r([/#]$), '') == uri.to_s
    end
  end
end

.find_term(uri) ⇒ Vocabulary::Term

Return the Vocabulary term associated with a URI

Parameters:

Returns:



220
221
222
223
224
225
226
227
228
229
230
# File 'lib/rdf/vocabulary.rb', line 220

def find_term(uri)
  uri = RDF::URI(uri)
  return uri if uri.is_a?(Vocabulary::Term)
  if vocab = find(uri)
    if vocab.ontology == uri
      vocab.ontology
    else
      vocab[uri.to_s[vocab.to_uri.to_s.length..-1].to_s]
    end
  end
end

.from_graph(graph, url: nil, class_name: nil, extra: nil) ⇒ RDF::Vocabulary

Create a vocabulary from a graph or enumerable

Parameters:

  • graph (RDF::Enumerable)
  • url (URI, #to_s)
  • class_name (RDF::Vocabulary, String)

    The class_name associated with the vocabulary, used for creating the class name of the vocabulary. This will create a new class named with a top-level constant based on class_name. If given an existing vocabulary, it replaces the existing definitions for that vocabulary with those from the graph.

  • extra (Array<Symbol>, Hash{Symbol => Hash})

    Extra terms to add to the vocabulary. In the first form, it is an array of symbols, for which terms are created. In the second, it is a Hash mapping symbols to property attributes, as described in property.

Returns:



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
# File 'lib/rdf/vocabulary.rb', line 329

def from_graph(graph, url: nil, class_name: nil, extra: nil)
  vocab = case class_name
  when RDF::Vocabulary
    class_name.instance_variable_set(:@ontology, nil)
    class_name.instance_variable_set(:@properties, nil)
    class_name
  when String
    Object.const_set(class_name, Class.new(self.create(url)))
  else
    Class.new(self.create(url))
  end

  ont_url = url.to_s.sub(%r([/#]$), '')
  term_defs = {}
  graph.each do |statement|
    next unless statement.subject.uri?
    next unless statement.subject.start_with?(url) || statement.subject == ont_url
    name = statement.subject.to_s[url.to_s.length..-1].to_s
    term = (term_defs[name.to_sym] ||= {})
    term[:uri] = statement.subject if name.empty?

    key = case statement.predicate
    when RDF.type                                     then :type
    when RDF::RDFS.subClassOf                         then :subClassOf
    when RDF::RDFS.subPropertyOf                      then :subPropertyOf
    when RDF::RDFS.range                              then :range
    when RDF::RDFS.domain                             then :domain
    when RDF::RDFS.comment                            then :comment
    when RDF::RDFS.label                              then :label
    when RDF::URI("http://schema.org/inverseOf")      then :inverseOf
    when RDF::URI("http://schema.org/domainIncludes") then :domainIncludes
    when RDF::URI("http://schema.org/rangeIncludes")  then :rangeIncludes
    else                                              statement.predicate.pname.to_sym
    end

    value = if statement.object.uri?
      statement.object.pname
    elsif statement.object.literal? && (statement.object.language || :en).to_s =~ /^en-?/
      statement.object.to_s
    end

    (term[key] ||= []) << value if value
  end

  # Create extra terms
  term_defs = case extra
  when Array
    extra.inject({}) {|memo, s| memo[s.to_sym] = {label: s.to_s}; memo}.merge(term_defs)
  when Hash
    extra.merge(term_defs)
  else
    term_defs
  end

  term_defs.each do |term, attributes|
    if term == :"
      uri = attributes.delete(:uri)
      vocab.__ontology__ uri, attributes
    else
      vocab.__property__ term, attributes
    end
  end

  vocab
end

.imported_fromArray<RDF::Vocabulary>

List of vocabularies which import this vocabulary

Returns:



265
266
267
268
269
# File 'lib/rdf/vocabulary.rb', line 265

def imported_from
  @imported_from ||= begin
    RDF::Vocabulary.select {|v| v.__imports__.include?(self)}
  end
end

.importsArray<RDF::Vocabulary> Also known as: __imports__

Note:

the alias __imports__ guards against RDF::OWL.imports returning a term, rather than an array of vocabularies

List of vocabularies this vocabulary owl:imports

Returns:



250
251
252
253
254
255
256
257
258
259
# File 'lib/rdf/vocabulary.rb', line 250

def imports
  return [] unless self.ontology
  @imports ||= begin
    Array(self.ontology.attributes[:owl:imports"]).map do |pn|
      find(expand_pname(pn)) rescue nil
    end.compact
  rescue KeyError
    []
  end
end

.inspectString

Returns a developer-friendly representation of this vocabulary class.

Returns:

  • (String)


399
400
401
402
403
404
405
# File 'lib/rdf/vocabulary.rb', line 399

def inspect
  if self == Vocabulary
    self.to_s
  else
    sprintf("%s(%s)", superclass.to_s, to_s)
  end
end

.method_missing(property, *args, &block) ⇒ Object (protected)



431
432
433
434
435
436
437
438
439
440
# File 'lib/rdf/vocabulary.rb', line 431

def method_missing(property, *args, &block)
  property = RDF::Vocabulary.camelize(property.to_s)
  if %w(to_ary).include?(property.to_s)
    super
  elsif args.empty? && !to_s.empty?
    Term.intern([to_s, property.to_s].join(''), attributes: {vocab: self})
  else
    super
  end
end

.ontologyRDF::Vocabulary::Term .ontology(name, options) ⇒ RDF::Vocabulary::Term Also known as: __ontology__

Note:

If the ontology URI has the vocabulary namespace URI as a prefix, it may also be defined using #property or #term

Overloads:

  • .ontologyRDF::Vocabulary::Term

    Returns the ontology definition of the current vocabulary as a term.

  • .ontology(name, options) ⇒ RDF::Vocabulary::Term

    Defines the vocabulary ontology.

    Parameters:

    • uri (String, #to_s)

      The URI of the ontology.

    • options (Hash{Symbol => Object})

      Any other values are expected to be String which expands to a URI using built-in vocabulary prefixes. The value is a String or Array<String> which is interpreted according to the range of the associated property.

    Options Hash (options):

    • :label (String, Array<String>)

      Shortcut for rdfs:label, values are String interpreted as a Literal.

    • :comment (String, Array<String>)

      Shortcut for rdfs:comment, values are String interpreted as a Literal.

    • :type (String, Array<String>)

      Shortcut for rdf:type, values are String interpreted as a URI.

    Returns:



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/rdf/vocabulary.rb', line 158

def ontology(*args)
  case args.length
  when 0
    @ontology
  else
    uri, options = args
    options = {vocab: self}.merge(options || {})
    Term.cache.delete(uri.to_s.to_sym)  # Clear any previous entry
    @ontology = Term.intern(uri.to_s, attributes: options)

    # If the URI is the same as the vocabulary namespace, also define it as a term
    props[:"] ||= @ontology if self.to_s == uri.to_s

    @ontology
  end
end

.propertiesObject Also known as: __properties__

@return [ArrayRDF::URI] a list of properties in the current vocabulary



178
179
180
# File 'lib/rdf/vocabulary.rb', line 178

def properties
  props.values
end

.propertyRDF::Vocabulary::Term .property(name, options) ⇒ RDF::Vocabulary::Term Also known as: term, __property__

Overloads:

  • .propertyRDF::Vocabulary::Term

    Returns property in the current vocabulary

  • .property(name, options) ⇒ RDF::Vocabulary::Term

    Defines a new property or class in the vocabulary.

    Parameters:

    • name (String, #to_s)
    • options (Hash{Symbol => Object})

      Any other values are expected to be String which expands to a URI using built-in vocabulary prefixes. The value is a String or Array<String> which is interpreted according to the range of the associated property.

    Options Hash (options):

    • :label (String, Array<String>)

      Shortcut for rdfs:label, values are String interpreted as a Literal.

    • :comment (String, Array<String>)

      Shortcut for rdfs:comment, values are String interpreted as a Literal.

    • :subClassOf (String, Array<String>)

      Shortcut for rdfs:subClassOf, values are String interpreted as a URI.

    • :subPropertyOf (String, Array<String>)

      Shortcut for rdfs:subPropertyOf, values are String interpreted as a URI.

    • :domain (String, Array<String>)

      Shortcut for rdfs:domain, values are String interpreted as a URI.

    • :range (String, Array<String>)

      Shortcut for rdfs:range, values are String interpreted as a URI.

    • :type (String, Array<String>)

      Shortcut for rdf:type, values are String interpreted as a URI.

    Returns:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rdf/vocabulary.rb', line 112

def property(*args)
  case args.length
  when 0
    Term.intern("#{self}property", attributes: {label: "property", vocab: self})
  else
    name, options = args
    options = {label: name.to_s, vocab: self}.merge(options || {})
    uri_str = [to_s, name.to_s].join('')
    Term.cache.delete(uri_str.to_sym)  # Clear any previous entry
    prop = Term.intern(uri_str, attributes: options)
    props[name.to_sym] = prop

    # If name is empty, also treat it as the ontology
    @ontology ||= prop if name.to_s.empty?

    # Define an accessor, except for problematic properties
    (class << self; self; end).send(:define_method, name) { prop } unless %w(property hash).include?(name.to_s)
    prop
  end
end

.strict?Boolean

Is this a strict vocabulary, or a liberal vocabulary allowing arbitrary properties?

Returns:

  • (Boolean)


84
# File 'lib/rdf/vocabulary.rb', line 84

def strict?; false; end

.to_iriRDF::URI

Returns the base URI for this vocabulary class. For IRI compatibility

Returns:



280
281
282
# File 'lib/rdf/vocabulary.rb', line 280

def to_uri
  RDF::URI.intern(@@uris[self].to_s)
end

.to_sString

Returns a string representation of this vocabulary class.

Returns:

  • (String)


315
316
317
# File 'lib/rdf/vocabulary.rb', line 315

def to_s
  @@uris.has_key?(self) ? @@uris[self].to_s : super
end

.to_uriRDF::URI

Returns the base URI for this vocabulary class.

Returns:



275
276
277
# File 'lib/rdf/vocabulary.rb', line 275

def to_uri
  RDF::URI.intern(@@uris[self].to_s)
end

Instance Method Details

#[](property) ⇒ URI

Returns the URI for the term property in this vocabulary.

Parameters:

Returns:



468
469
470
# File 'lib/rdf/vocabulary.rb', line 468

def [](property)
  Term.intern([to_s, property.to_s].join(''), attributes: {vocab: self.class})
end

#inspectString

Returns a developer-friendly representation of this vocabulary.

Returns:

  • (String)


495
496
497
# File 'lib/rdf/vocabulary.rb', line 495

def inspect
  sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s)
end

#to_sString

Returns a string representation of this vocabulary.

Returns:

  • (String)


487
488
489
# File 'lib/rdf/vocabulary.rb', line 487

def to_s
  @uri.to_s
end

#to_uriURI Also known as: to_iri

Returns the base URI for this vocabulary.

Returns:



476
477
478
# File 'lib/rdf/vocabulary.rb', line 476

def to_uri
  RDF::URI.intern(to_s)
end