Class: RDF::Vocabulary::Term

Inherits:
URI show all
Defined in:
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb,
vendor/bundler/ruby/2.4.0/bundler/gems/rdf-reasoner-17c965b5c3d4/lib/rdf/reasoner/extensions.rb

Overview

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

Constant Summary

@@entailments =
{}

Constants inherited from URI

URI::CACHE_SIZE, URI::GEN_DELIMS, URI::HOST_FROM_AUTHORITY_RE, URI::IAUTHORITY, URI::IFRAGMENT, URI::IHIER_PART, URI::IHOST, URI::IPATH_ABEMPTY, URI::IPATH_ABSOLUTE, URI::IPATH_EMPTY, URI::IPATH_NOSCHEME, URI::IPATH_ROOTLESS, URI::IPCHAR, URI::IPRIVATE, URI::IP_literal, URI::IQUERY, URI::IREG_NAME, URI::IRELATIVE_PART, URI::IRELATIVE_REF, URI::IRI, URI::IRI_PARTS, URI::ISEGMENT, URI::ISEGMENT_NZ, URI::ISEGMENT_NZ_NC, URI::IUNRESERVED, URI::IUSERINFO, URI::NON_HIER_SCHEMES, URI::PCT_ENCODED, URI::PORT, URI::PORT_FROM_AUTHORITY_RE, URI::PORT_MAPPING, URI::RDS_2A, URI::RDS_2B1, URI::RDS_2B2, URI::RDS_2C1, URI::RDS_2C2, URI::RDS_2D, URI::RDS_2E, URI::RESERVED, URI::SCHEME, URI::SUB_DELIMS, URI::UCSCHAR, URI::UNRESERVED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from URI

#+, #/, #==, #===, #=~, #absolute?, #authority, #authority=, #canonicalize, #canonicalize!, #end_with?, #eql?, #fragment, #fragment=, #has_parent?, #hash, #hier?, #host, #host=, intern, #join, #length, normalize_path, #normalized_authority, #normalized_fragment, #normalized_host, #normalized_password, #normalized_path, #normalized_port, #normalized_query, #normalized_scheme, #normalized_user, #normalized_userinfo, #object, #parent, #parse, parse, #password, #password=, #path, #path=, #pname, #port, #port=, #qname, #query, #query=, #query_values, #query_values=, #relative?, #relativize, #request_uri, #root, #root?, #scheme, #scheme=, #to_str, #to_uri, #uri?, #url?, #urn?, #user, #user=, #userinfo, #userinfo=, #validate!, #value

Methods included from Resource

new, #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?, new, #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?

Constructor Details

#URI(uri, **options) ⇒ Term #URI(**options) ⇒ Term

Returns a new instance of Term

Overloads:

  • #URI(uri, **options) ⇒ Term

    Parameters:

    Options Hash (**options):

    • :validate (Boolean) — default: false
    • :canonicalize (Boolean) — default: false
  • #URI(**options) ⇒ Term

    Parameters:

    Options Hash (**options):

    • options (Boolean)

      :validate (false)

    • options (Boolean)

      :canonicalize (false)

    • :vocab (Vocabulary)

      The RDF::Vocabulary associated with this term.

    • :scheme (String, #to_s)

      The scheme component.

    • :user (String, #to_s)

      The user component.

    • :password (String, #to_s)

      The password component.

    • :userinfo (String, #to_s)

      The u optionsserinfo component. If this is supplied, the user and password compo optionsnents must be omitted.

    • :host (String, #to_s)

      The host component.

    • :port (String, #to_s)

      The port component.

    • :authority (String, #to_s)

      The a optionsuthority component. If this is supplied, the user, password, useri optionsnfo, host, and port components must be omitted.

    • :path (String, #to_s)

      The path component.

    • :query (String, #to_s)

      The query component.

    • :fragment (String, #to_s)

      The fragment component.



596
597
598
599
600
601
602
603
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 596

def initialize(*args, attributes:, **options)
  @attributes = attributes
  if RUBY_ENGINE == "rbx"
    super(*args, **options)
  else
    super
  end
end

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



767
768
769
770
771
772
773
774
775
776
777
778
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 767

def method_missing(method, *args, &block)
  case method
  when :comment
    @attributes.fetch(method, "")
  when :label
    @attributes.fetch(method, to_s.split(/[\/\#]/).last)
  when :type, :subClassOf, :subPropertyOf, :domain, :range, :inverseOf, :domainIncludes, :rangeIncludes
    Array(@attributes[method]).map {|v| RDF::Vocabulary.expand_pname(v)}
  else
    super
  end
end

Instance Attribute Details

#attributesHash{Symbol,Resource => Term, #to_s}

Attributes of this vocabulary term, used for finding label and comment and to serialize the term back to RDF.

Returns:



564
565
566
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

def attributes
  @attributes
end

Class Method Details

.add_entailment(method, proc) ⇒ Object

Add an entailment method. The method accepts no arguments, and returns or yields an array of values associated with the particular entailment method

Parameters:



13
14
15
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-reasoner-17c965b5c3d4/lib/rdf/reasoner/extensions.rb', line 13

def add_entailment(method, proc)
  @@entailments[method] = proc
end

Instance Method Details

#class?Boolean

Is this a class term?

Returns:

  • (Boolean)


632
633
634
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 632

def class?
  !!(self.type.to_s =~ /Class/)
end

#commentString

rdfs:comment accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#datatype?Boolean

Is this a class term?

Returns:

  • (Boolean)


646
647
648
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 646

def datatype?
  !!(self.type.to_s =~ /Datatype/)
end

#domainRDF::URI

rdfs:domain accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#domain_compatible?(resource, queryable, options = {}) ⇒ Boolean

Determine if the domain of a property term is consistent with the specified resource in queryable.

Parameters:

Options Hash (options):

Returns:

  • (Boolean)


37
38
39
40
41
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-reasoner-17c965b5c3d4/lib/rdf/reasoner/extensions.rb', line 37

def domain_compatible?(resource, queryable, options = {})
  %w(owl rdfs schema).map {|r| "domain_compatible_#{r}?".to_sym}.all? do |meth|
    !self.respond_to?(meth) || self.send(meth, resource, queryable, options)
  end
end

#domain_includesRDF::URI

Accessor for schema:domainIncludes

Returns:



755
756
757
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 755

def domain_includes
  Array(@attributes[:domainIncludes]).map  {|v| RDF::Vocabulary.expand_pname(v)}
end

#domainIncludesRDF::URI

schema:domainIncludes accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#dupRDF::URI

Returns a duplicate copy of self.

Returns:



615
616
617
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 615

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



664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 664

def each_statement
  attributes.reject {|p| p == :vocab}.each do |prop, values|
    Array(values).each do |value|
      begin
        case prop
        when :type
          prop = RDF.type
          value = RDF::Vocabulary.expand_pname(value)
        when :subClassOf
          prop = RDFS.subClassOf
          value = RDF::Vocabulary.expand_pname(value)
        when :subPropertyOf
          prop = RDFS.subPropertyOf
          value = RDF::Vocabulary.expand_pname(value)
        when :domain
          prop = RDFS.domain
          value = RDF::Vocabulary.expand_pname(value)
        when :range
          prop = RDFS.range
          value = RDF::Vocabulary.expand_pname(value)
        when :inverseOf
          prop = RDF::URI("http://schema.org/inverseOf")
          value = RDF::Vocabulary.expand_pname(value)
        when :domainIncludes
          prop = RDF::URI("http://schema.org/domainIncludes")
          value = RDF::Vocabulary.expand_pname(value)
        when :rangeIncludes
          prop = RDF::URI("http://schema.org/rangeIncludes")
          value = RDF::Vocabulary.expand_pname(value)
        when :label
          prop = RDF::RDFS.label
        when :comment
          prop = RDF::RDFS.comment
        else
          prop = RDF::Vocabulary.expand_pname(prop.to_s)
          next unless prop

          v = value.to_s
          value = RDF::Vocabulary.expand_pname(v)
          unless 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 |memo, klass|
              l = klass.new(v)
              memo || (l if l.valid?)
            end
          end
        end
        yield RDF::Statement(self, prop, value)
      rescue KeyError
        # Skip things eroneously defined in the vocabulary
      end
    end
  end
end

#entail(method) {|term| ... } ⇒ Array<Term>

Perform an entailment on this term.

Parameters:

  • method (Symbol)

    A registered entailment method

Yields:

  • term

Yield Parameters:

Returns:



25
26
27
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-reasoner-17c965b5c3d4/lib/rdf/reasoner/extensions.rb', line 25

def entail(method, &block)
  self.send(@@entailments.fetch(method), &block)
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


731
732
733
734
735
736
737
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 731

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.



744
745
746
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 744

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

#inverseOfRDF::URI

owl:inverseOf accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#labelString

rdfs:label accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#other?Boolean

Is this neither a class, property or datatype term?

Returns:

  • (Boolean)


653
654
655
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 653

def other?
  !!(self.type.to_s !~ /(Class|Property|Datatype)/)
end

#property?Boolean

Is this a class term?

Returns:

  • (Boolean)


639
640
641
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 639

def property?
  !!(self.type.to_s =~ /Property/)
end

#rangeRDF::URI

rdfs:range accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#range_compatible?(resource, queryable, options = {}) ⇒ Boolean

Determine if the range of a property term is consistent with the specified resource in queryable.

Specific entailment regimes should insert themselves before this to apply the appropriate semantic condition

Parameters:

Options Hash (options):

Returns:

  • (Boolean)


53
54
55
56
57
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-reasoner-17c965b5c3d4/lib/rdf/reasoner/extensions.rb', line 53

def range_compatible?(resource, queryable, options = {})
  %w(owl rdfs schema).map {|r| "range_compatible_#{r}?".to_sym}.all? do |meth|
    !self.respond_to?(meth) || self.send(meth, resource, queryable, options)
  end
end

#range_includesRDF::URI

Accessor for schema:rangeIncludes

Returns:



761
762
763
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 761

def range_includes
  Array(@attributes[:rangeIncludes]).map  {|v| RDF::Vocabulary.expand_pname(v)}
end

#rangeIncludesRDF::URI

schema:rangeIncludes accoessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

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

Implement accessor to symbol attributes

Returns:

  • (Boolean)


749
750
751
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 749

def respond_to?(method, include_all = false)
  @attributes.has_key?(method) || super
end

#subClassOfRDF::URI

rdfs:subClassOf accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#subPropertyOfRDF::URI

rdfs:subPropertyOf accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#typeRDF::URI

rdf:type accessor

Returns:



564
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 564

attr_accessor :attributes

#valid?Boolean

Determine if the URI is a valid according to RFC3987

Returns:

  • (Boolean)

    true or false

Since:

  • 0.3.9



624
625
626
627
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 624

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

#vocabRDF::Vocabulary

Vocabulary of this term.

Returns:



609
# File 'vendor/bundler/ruby/2.4.0/bundler/gems/rdf-da525ad24ff7/lib/rdf/vocabulary.rb', line 609

def vocab; @attributes.fetch(:vocab); end