Sha256: 542cffe310f8f0f18898985b8e717a2ad51ebee49e1c4fff6103206631762209

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

require 'rdf'

module RDF
  class List
    ##
    # Validate the list ensuring that
    # * rdf:rest values are all BNodes are nil
    # * rdf:type, if it exists, is rdf:List
    # * each subject has no properties other than single-valued rdf:first, rdf:rest
    #   other than for the first node in the list
    # @return [Boolean]
    def valid?
      li = subject
      while li != RDF.nil do
        rest = nil
        firsts = rests = 0
        @graph.query(:subject => li) do |st|
          case st.predicate
          when RDF.type
            # Be tollerant about rdf:type entries, as some OWL vocabularies use it excessively
          when RDF.first
            firsts += 1
          when RDF.rest
            rest = st.object
            return false unless rest.node? || rest == RDF.nil
            rests += 1
          else
            # First node may have other properties
            return false unless li == subject
          end
        end
        return false unless firsts == 1 && rests == 1
        li = rest
      end
      true
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rdf-turtle-0.1.0 lib/rdf/turtle/patches.rb
rdf-turtle-0.0.5 lib/rdf/turtle/patches.rb
rdf-turtle-0.0.4 lib/rdf/turtle/patches.rb
rdf-turtle-0.0.3 lib/rdf/turtle/patches.rb
rdf-turtle-0.0.2 lib/rdf/turtle/patches.rb