Module: RDF::Vocabulary::Term

Includes:
Resource
Defined in:
vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb

Overview

A Vocabulary Term is a Resource that can also act as an Enumerable to generate the RDF definition of vocabulary terms as defined within the vocabulary definition.

Terms include attributes where values a embedded resources, lists or other terms. This allows, for example, navigation of a concept heirarchy.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resource

#resource?

Methods included from Term

#<=>, #==, #aggregate?, #compatible?, #eql?, #escape, #evaluate, #ndvars, #term?, #to_base, #to_term, #vars

Methods included from SPARQL::Algebra::Expression

cast, #constant?, #evaluate, extension, extensions, for, #invalid?, #node?, open, #optimize, parse, register_extension, #to_sxp_bin, #validate!, #variable?

Methods included from RDF::Value

#anonymous?, #canonicalize, #canonicalize!, #constant?, #graph?, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!, #variable?

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Implement accessor to symbol attributes



1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 1121

def method_missing(method, *args, &block)
  case method
  when :comment, :notation, :note, :editorialNote, :definition
    attribute_value(method)
  when :label, :altLabel, :prefLabel
    # Defaults to URI fragment or path tail
    begin
      attribute_value(method)
    rescue KeyError
      to_s.split(/[\/\#]/).last
    end
  when :type, :subClassOf, :subPropertyOf, :domain, :range, :isDefinedBy,
       :allValuesFrom, :cardinality, :equivalentClass, :equivalentProperty,
       :intersectionOf, :inverseOf, :maxCardinality, :minCardinality,
       :onProperty, :someValuesFrom, :unionOf,
       :domainIncludes, :rangeIncludes,
       :broader, :exactMatch, :hasTopConcept, :inScheme, :member, :narrower, :related

    # Return value as an Array, unless it is a list
    case value = attribute_value(method)
    when Array, RDF::List then value
    else [value].compact
    end
  else
    super
  end
end

Instance Attribute Details

#allValuesFromArray<Term> (readonly)

owl:allValuesFrom accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#altLabelLiteral+ (readonly)

skos:altLabel accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#attributesObject (readonly)

Attributes of this vocabulary term, used for finding label and comment and to serialize the term back to RDF. @return [Hash=> Term, #to_s]



822
823
824
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 822

def attributes
  @attributes
end

#broaderArray<Term> (readonly)

skos:broader accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#cardinalityArray<Literal> (readonly)

owl:cardinality accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#commentLiteral+ (readonly)

rdfs:comment accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#definitionLiteral+ (readonly)

skos:definition accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#domainArray<Term> (readonly)

rdfs:domain accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#domainIncludesArray<Term> (readonly)

schema:domainIncludes accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 767

#editorialNoteLiteral+ (readonly)

skos:editorialNote accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#equivalentClassArray<Term> (readonly)

owl:equivalentClass accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#equivalentPropertyArray<Term> (readonly)

owl:equivalentProperty accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#exactMatchArray<Term> (readonly)

skos:exactMatch accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#hasTopConceptArray<Term> (readonly)

skos:hasTopConcept accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#inSchemeArray<Term> (readonly)

skos:inScheme accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#intersectionOfArray<Term> (readonly)

owl:intersectionOf accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#inverseOfArray<Term> (readonly)

owl:inverseOf accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#isDefinedByArray<Term> (readonly)

rdfs:isDefinedBy accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#labelLiteral (readonly)

rdfs:label accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#maxCardinalityArray<Literal> (readonly)

owl:maxCardinality accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#memberArray<Term> (readonly)

skos:member accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#minCardinalityArray<Literal> (readonly)

owl:minCardinality accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#narrowerArray<Term> (readonly)

skos:narrower accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#notationLiteral+ (readonly)

skos:notation accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#noteLiteral+ (readonly)

skos:note accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#onPropertyArray<Term> (readonly)

owl:onProperty accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#prefLabelLiteral (readonly)

skos:prefLabel accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#rangeArray<Term> (readonly)

rdfs:range accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#rangeIncludesArray<Term> (readonly)

schema:rangeIncludes accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 767

skos:related accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 774

#someValuesFromArray<Term> (readonly)

owl:someValuesFrom accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#subClassOfArray<Term> (readonly)

rdfs:subClassOf accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#subPropertyOfArray<Term> (readonly)

rdfs:subPropertyOf accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#typeArray<Term> (readonly)

rdf:type accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 708

#unionOfList<Term>, Array<Term> (readonly)

owl:unionOf accessor

Returns:



# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 733

#vocabRDF::Vocabulary (readonly)

Vocabulary of this term.

Returns:



818
819
820
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 818

def vocab
  @vocab
end

Class Method Details

.intern(str, *args) ⇒ RDF::URI

Returns an interned RDF::URI instance based on the given uri string.

The maximum number of cached interned URI references is given by the CACHE_SIZE constant. This value is unlimited by default, in which case an interned URI object will be purged only when the last strong reference to it is garbage collected (i.e., when its finalizer runs).

Excepting special memory-limited circumstances, it should always be safe and preferred to construct new URI references using RDF::URI.intern instead of RDF::URI.new, since if an interned object can't be returned for some reason, this method will fall back to returning a freshly-allocated one.

Parameters:

Returns:

  • (RDF::URI)

    an immutable, frozen URI object



876
877
878
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 876

def self.intern(str, *args)
  (URI.cache[(str = str.to_s).to_sym] ||= self.new(str, *args)).freeze
end

.new(uri, attributes: , **options) ⇒ Object .new(attributes: , **options) ⇒ Object

Overloads:



841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 841

def self.new(*args, vocab: nil, attributes: {}, **options)
  klass = if args.first.nil?
    RDF::Node
  elsif args.first.is_a?(Hash)
    args.unshift(nil)
    RDF::Node
  elsif args.first.to_s.start_with?("_:")
    args = args[1..-1].unshift($1)
    RDF::Node
  else RDF::URI
  end
  term = klass.allocate.extend(Term)
  term.send(:initialize, *args)
  term.instance_variable_set(:@vocab, vocab)
  term.instance_variable_set(:@attributes, attributes)
  term
end

Instance Method Details

#attribute_value(prop) ⇒ RDF::Value+

Values of an attributes as RDF::Value

Returns:



948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 948

def attribute_value(prop)
  values = attributes[prop]
  values = [values].compact unless values.is_a?(Array)
  prop_values = values.map do |value|
    v = value.is_a?(Symbol) ? value.to_s : value
    value = (RDF::Vocabulary.expand_pname(v) rescue nil) if v.is_a?(String) && v.include?(':')
    value = value.to_uri if value.respond_to?(:to_uri)
    unless value.is_a?(RDF::Value) && value.valid?
      # Use as most appropriate literal
      value = [
        RDF::Literal::Date,
        RDF::Literal::DateTime,
        RDF::Literal::Integer,
        RDF::Literal::Decimal,
        RDF::Literal::Double,
        RDF::Literal::Boolean,
        RDF::Literal
      ].inject(nil) do |m, klass|
        m || begin
          l = klass.new(v)
          l if l.valid?
        end
      end
    end

    value
  end

  prop_values.length <= 1 ? prop_values.first : prop_values
end

#class?Boolean

Is this a class term?

Returns:

  • (Boolean)


901
902
903
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 901

def class?
  Array(self.type).any? {|t| t.to_s.include?('Class')}
end

#datatype?Boolean

Is this a class term?

Returns:

  • (Boolean)


915
916
917
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 915

def datatype?
  Array(self.type).any? {|t| t.to_s.include?('Datatype')}
end

#domain_includesRDF::URI

Accessor for schema:domainIncludes

Returns:



1070
1071
1072
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 1070

def domain_includes
  domainIncludes
end

#dupRDF::URI

Returns a duplicate copy of self.

Returns:



884
885
886
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 884

def dup
  self.class.new((@value || @object).dup, attributes: attributes).extend(Term)
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:



986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 986

def each_statement
  attributes.keys.each do |p|
    values = attribute_value(p)
    values = [values].compact unless values.is_a?(Array)
    values.each do |value|
      begin
        prop = case p
        when :type
          RDF::RDFV[p]
        when :subClassOf, :subPropertyOf, :domain, :range, :isDefinedBy, :label, :comment
          RDF::RDFS[p]
        when :allValuesFrom, :cardinality, :equivalentClass, :equivalentProperty,
             :intersectionOf, :inverseOf, :maxCardinality, :minCardinality,
             :onProperty, :someValuesFrom, :unionOf
          RDF::OWL[p]
        when :domainIncludes, :rangeIncludes
          RDF::Vocabulary.find_term("http://schema.org/#{p}")
        when :broader, :definition, :exactMatch, :hasTopConcept, :inScheme,
             :member, :narrower, :related, :altLabel, :definition, :editorialNote,
             :notation, :note, :prefLabel
          RDF::Vocabulary.find_term("http://www.w3.org/2004/02/skos/core##{p}")
        else
          RDF::Vocabulary.expand_pname(p)
        end

        yield RDF::Statement(self, prop, value) if prop.is_a?(RDF::URI)

        # Enumerate over value statements, if enumerable
        if value.is_a?(RDF::Enumerable) || (value.is_a?(Term) && value.node?)
          value.each_statement {|s| yield s}
        end
      rescue KeyError
        # Skip things eroneously defined in the vocabulary
      end
    end
  end
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


1028
1029
1030
1031
1032
1033
1034
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 1028

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

#inspectString

Returns a String representation of the URI object's state.

Returns:

  • (String)

    The URI object's state, as a String.



1041
1042
1043
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 1041

def inspect
  sprintf("#<%s:%#0x ID:%s>", Term.to_s, self.object_id, self.to_s)
end

#other?Boolean

Is this neither a class, property or datatype term?

Returns:

  • (Boolean)


929
930
931
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 929

def other?
  Array(self.type).none? {|t| t.to_s =~ /(Class|Property|Datatype|Restriction)/}
end

#propertiesHash{Symbol => Array<RDF::Value>}

Enumerate attributes with values transformed into RDF::Value instances

Returns:



937
938
939
940
941
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 937

def properties
  attributes.keys.inject({}) do |memo, p|
    memo.merge(p => attribute_value(p))
  end
end

#property?Boolean

Is this a class term?

Returns:

  • (Boolean)


908
909
910
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 908

def property?
  Array(self.type).any? {|t| t.to_s.include?('Property')}
end

#range_includesRDF::URI

Accessor for schema:rangeIncludes

Returns:



1076
1077
1078
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 1076

def range_includes
  rangeIncludes
end

#respond_to?(method, include_all = false) ⇒ Boolean

Implement accessor to symbol attributes

Returns:

  • (Boolean)


1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 1046

def respond_to?(method, include_all = false)
  case method
  when :comment, :notation, :note, :editorialNote, :definition,
       :label, :altLabel, :prefLabel, :type, :isDefinedBy
    true
  when :subClassOf, :subPropertyOf,
       :domainIncludes, :rangeIncludes,
       :equivalentClass, :intersectionOf, :unionOf
    self.class?
  when :domain, :range, :equivalentProperty, :inverseOf
    self.property?
  when :allValuesFrom, :cardinality,
       :maxCardinality, :minCardinality,
       :onProperty, :someValuesFrom
    self.restriction?
  when :broader, :exactMatch, :hasTopConcept, :inScheme, :member, :narrower, :related
    @attributes.has_key?(method)
  else
    super
  end
end

#restriction?Boolean

Is this a Restriction term?

Returns:

  • (Boolean)


922
923
924
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 922

def restriction?
  Array(self.type).any? {|t| t.to_s.include?('Restriction')}
end

#to_ruby(indent: "") ⇒ String

Serialize back to a Ruby source initializer

Parameters:

Returns:



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 1083

def to_ruby(indent: "")
  "term(" +
  (self.uri? ? self.to_s.inspect + ",\n" : "\n") +
  "#{indent}  " +
  attributes.keys.sort.map do |k|
    values = attribute_value(k)
    values = [values].compact unless values.is_a?(Array)
    values = values.map do |value|
      if value.is_a?(Literal) && %w(: comment definition notation note editorialNote).include?(k.to_s)
        "%(#{value.to_s.gsub('(', '\(').gsub(')', '\)')}).freeze"
      elsif value.is_a?(RDF::URI)
        "#{value.pname.inspect}.freeze"
      elsif value.is_a?(RDF::Vocabulary::Term)
        value.to_ruby(indent: indent + "  ")
      elsif value.is_a?(RDF::Term)
        "#{value.to_s.inspect}.freeze"
      elsif value.is_a?(RDF::List)
        list_elements = value.map do |u|
          if u.uri?
            "#{u.pname.inspect}.freeze"
          elsif u.respond_to?(:to_ruby)
            u.to_ruby(indent: indent + "  ")
          else
            "#{u.to_s.inspect}.freeze"
          end
        end
        "list(#{list_elements.join(', ')})"
      else
        "#{value.inspect}.freeze"
      end
    end
    "#{k.to_s.include?(':') ? k.to_s.inspect : k}: " +
    (values.length == 1 ? values.first : ('[' + values.join(',') + ']'))
  end.join(",\n#{indent}  ") + "\n#{indent})"
  
end

#valid?Boolean

Determine if the URI is a valid according to RFC3987

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.9



893
894
895
896
# File 'vendor/bundler/ruby/2.5.0/bundler/gems/rdf-c7356a6367f8/lib/rdf/vocabulary.rb', line 893

def valid?
  # Validate relative to RFC3987
  node? || RDF::URI::IRI.match(to_s) || false
end