Class: RDF::Tabular::Metadata

Inherits:
Object
  • Object
show all
Includes:
Util::Logger
Defined in:
lib/rdf/tabular/metadata.rb

Direct Known Subclasses

Column, Datatype, Dialect, Schema, Table, TableGroup, Transformation

Defined Under Namespace

Classes: DebugContext

Constant Summary collapse

INHERITED_PROPERTIES =

Inheritect properties, valid for all types

{
  aboutUrl:           :uri_template,
  datatype:           :atomic,
  default:            :atomic,
  lang:               :atomic,
  null:               :atomic,
  ordered:            :atomic,
  propertyUrl:        :uri_template,
  required:           :atomic,
  separator:          :atomic,
  textDirection:      :atomic,
  valueUrl:           :uri_template,
}.freeze
INHERITED_DEFAULTS =
{
  default:            "".freeze,
  lang:               "und",
  null:               "".freeze,
  ordered:            false,
  required:           false,
  textDirection:      "ltr".freeze,
}.freeze
DATATYPES =

Valid datatypes

{
  anyAtomicType:      RDF::XSD.anyAtomicType,
  anyURI:             RDF::XSD.anyURI,
  base64Binary:       RDF::XSD.basee65Binary,
  boolean:            RDF::XSD.boolean,
  byte:               RDF::XSD.byte,
  date:               RDF::XSD.date,
  dateTime:           RDF::XSD.dateTime,
  dayTimeDuration:    RDF::XSD.dayTimeDuration,
  dateTimeStamp:      RDF::XSD.dateTimeStamp,
  decimal:            RDF::XSD.decimal,
  double:             RDF::XSD.double,
  duration:           RDF::XSD.duration,
  float:              RDF::XSD.float,
  ENTITY:             RDF::XSD.ENTITY,
  gDay:               RDF::XSD.gDay,
  gMonth:             RDF::XSD.gMonth,
  gMonthDay:          RDF::XSD.gMonthDay,
  gYear:              RDF::XSD.gYear,
  gYearMonth:         RDF::XSD.gYearMonth,
  hexBinary:          RDF::XSD.hexBinary,
  int:                RDF::XSD.int,
  integer:            RDF::XSD.integer,
  language:           RDF::XSD.language,
  long:               RDF::XSD.long,
  Name:               RDF::XSD.Name,
  NCName:             RDF::XSD.NCName,
  negativeInteger:    RDF::XSD.negativeInteger,
  NMTOKEN:            RDF::XSD.NMTOKEN,
  nonNegativeInteger: RDF::XSD.nonNegativeInteger,
  nonPositiveInteger: RDF::XSD.nonPositiveInteger,
  normalizedString:   RDF::XSD.normalizedString,
  NOTATION:           RDF::XSD.NOTATION,
  positiveInteger:    RDF::XSD.positiveInteger,
  QName:              RDF::XSD.Qname,
  short:              RDF::XSD.short,
  string:             RDF::XSD.string,
  time:               RDF::XSD.time,
  token:              RDF::XSD.token,
  unsignedByte:       RDF::XSD.unsignedByte,
  unsignedInt:        RDF::XSD.unsignedInt,
  unsignedLong:       RDF::XSD.unsignedLong,
  unsignedShort:      RDF::XSD.unsignedShort,
  yearMonthDuration:  RDF::XSD.yearMonthDuration,

  any:                RDF::XSD.anyAtomicType,
  binary:             RDF::XSD.base64Binary,
  datetime:           RDF::XSD.dateTime,
  html:               RDF.HTML,
  json:               RDF::Tabular::CSVW.JSON,
  number:             RDF::XSD.double,
  xml:                RDF.XMLLiteral,
}
NAME_SYNTAX =

A name is restricted according to the following RegExp.

Returns:

  • (RegExp)
%r(\A(?:_col|[a-zA-Z0-9]|%\h\h)([a-zA-Z0-9\._]|%\h\h)*\z)
LOCAL_CONTEXT =

Local version of the context

Returns:

  • (JSON::LD::Context)
::JSON::LD::Context.new.parse(File.expand_path("../../../../etc/csvw.jsonld", __FILE__)).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, **options) ⇒ Metadata

Create Metadata from IO, Hash or String

Parameters:

  • input (Metadata, Hash, #read)
  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :type (:TableGroup, :Table, :Transformation, :Schema, :Column, :Dialect)

    Type of schema, if not set, intuited from properties

  • context (JSON::LD::Context)

    Context used for this metadata. Taken from input if not provided

  • :base (RDF::URI)

    The Base URL to use when expanding the document. This overrides the value of ‘input` if it is a URL. If not specified and `input` is not an URL, the base URL defaults to the current document URL if in a browser context, or the empty string if there is no document context.

  • :decode_uri (Boolean)

    Decode %-encodings in the result of a URI Template operation.

  • :normalize (Boolean)

    normalize the object

  • :validate (Boolean)

    Strict metadata validation

Raises:



327
328
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
# File 'lib/rdf/tabular/metadata.rb', line 327

def initialize(input, **options)
  @options = options.dup

  # Parent of this Metadata, if any
  @parent = @options[:parent]

  # Get context from input
  # Optimize by using built-in version of context, and just extract @base, @lang
  opt_base = @options[:base]
  opt_base ||= input.base_uri if input.respond_to?(:base_uri)
  opt_base ||= input.filename if input.respond_to?(:filename)

  @context = case input['@context']
  when Array
    log_warn "Context missing required value 'http://www.w3.org/ns/csvw'" unless input['@context'].include?('http://www.w3.org/ns/csvw')
    c = LOCAL_CONTEXT.dup
    c.base = RDF::URI(opt_base)
    obj = input['@context'].detect {|e| e.is_a?(Hash)} || {}
    raise Error, "@context has object with properties other than @base and @language" unless (obj.keys.map(&:to_s) - %w(@base @language)).empty?
    c.parse(obj)
  when Hash
    log_warn "Context missing required value 'http://www.w3.org/ns/csvw'"
    c = LOCAL_CONTEXT.dup
    c.base = RDF::URI(opt_base)
    c.parse(input['@context'])
  when "http://www.w3.org/ns/csvw"
    LOCAL_CONTEXT.dup
    c = LOCAL_CONTEXT.dup
    c.base = RDF::URI(opt_base)
    c
  else
    if self.is_a?(TableGroup) || self.is_a?(Table) && !@parent
      log_warn "Context missing required value 'http://www.w3.org/ns/csvw'"
      LOCAL_CONTEXT.dup
      c = LOCAL_CONTEXT.dup
      c.base = RDF::URI(opt_base)
      c
    end
  end

  reason = @options.delete(:reason)

  @options[:base] = @context ? @context.base : RDF::URI(opt_base)

  if @context && @context.default_language && !BCP47.valid?(@context.default_language.to_s)
    log_warn "Context has invalid @language (#{@context.default_language.inspect}): expected valid BCP47 language tag"
    @context.default_language = nil
  end

  @filenames = Array(@options[:filenames]).map {|fn| RDF::URI(fn)} if @options[:filenames]
  @properties = self.class.const_get(:PROPERTIES)
  @required = self.class.const_get(:REQUIRED)

  @object = {}

  log_depth do
    # Input was parsed in .new
    # Metadata is object with symbolic keys
    input.each do |key, value|
      key = key.to_sym
      case key
      when :url
        # URL of CSV relative to metadata
        object[:url] = value
        @url = @options[:base].join(value)
        @options[:base] = @url if @context # Use as base for expanding IRIs
      when :@id
        # metadata identifier
        object[:@id] = if value.is_a?(String)
          value
        else
          log_warn "#{type} has invalid property '@id' (#{value.inspect}): expected a string"
          ""  # Default value
        end
        @id = @options[:base].join(object[:@id])
      else
        if @properties.has_key?(key) || INHERITED_PROPERTIES.has_key?(key)
          self.send("#{key}=".to_sym, value)
        else
          object[key] = value
        end
      end
    end
  end

  # Set type from @type, if present and not otherwise defined
  @type = object[:@type].to_sym if object[:@type]

  if options[:normalize]
    # If normalizing, also remove remaining @context
    self.normalize!
    @context = nil
    object.delete(:@context)
  end

  if reason
    log_debug("md#initialize") {reason}
    log_debug("md#initialize") {"filenames: #{filenames}"}
    log_debug("md#initialize") {"#{inspect}, parent: #{!@parent.nil?}, context: #{!@context.nil?}"} unless is_a?(Dialect)
  end
end

Instance Attribute Details

#filenamesArray<RDF::URI> (readonly)

Filename(s) (URI) of opened metadata, if any May be plural when merged

Returns:

  • (Array<RDF::URI>)

    filenames



129
130
131
# File 'lib/rdf/tabular/metadata.rb', line 129

def filenames
  @filenames
end

#idRDF::URI (readonly)

ID of this Metadata

Returns:

  • (RDF::URI)


116
117
118
# File 'lib/rdf/tabular/metadata.rb', line 116

def id
  @id
end

#objectHash<Symbol,Object>

Hash representation

Returns:

  • (Hash<Symbol,Object>)


26
27
28
# File 'lib/rdf/tabular/metadata.rb', line 26

def object
  @object
end

#parentMetadata (readonly)

Parent of this Metadata (TableGroup for Table, …)

Returns:



124
125
126
# File 'lib/rdf/tabular/metadata.rb', line 124

def parent
  @parent
end

#urlRDF::URI (readonly)

URL of related resource

Returns:

  • (RDF::URI)


120
121
122
# File 'lib/rdf/tabular/metadata.rb', line 120

def url
  @url
end

Class Method Details

.for_input(input, **options) ⇒ Metadata

Return metadata for a file, based on user-specified, linked, and site-wide location configuration from an input file

Parameters:

  • input (IO, StringIO)
  • options (Hash{Symbol => Object})

Options Hash (**options):

  • :metadata (Metadata, Hash, String, RDF::URI)

    user supplied metadata, merged on top of extracted metadata. If provided as a URL, Metadata is loade from that location

  • :base (RDF::URI)

    The Base URL to use when expanding the document. This overrides the value of ‘input` if it is a URL. If not specified and `input` is not an URL, the base URL defaults to the current document URL if in a browser context, or the empty string if there is no document context.

Returns:



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
# File 'lib/rdf/tabular/metadata.rb', line 178

def self.for_input(input, **options)
  base = options[:base]

  # Use user metadata, if provided
   = case options[:metadata]
  when Metadata then options[:metadata]
  when Hash
    Metadata.new(options[:metadata], **options.merge(reason: "load user metadata: #{options[:metadata].inspect}"))
  when String, RDF::URI
    Metadata.open(options[:metadata], **options.merge(filenames: options[:metadata], reason: "load user metadata: #{options[:metadata].inspect}"))
  end

  # Search for metadata until found

  # load link metadata, if available
  if ! && input.respond_to?(:links) && 
    link = input.links.find_link(%w(rel describedby))
    link_loc = RDF::URI(base).join(link.href).to_s
    md = Metadata.open(link_loc, **options.merge(filenames: link_loc, reason: "load linked metadata: #{link_loc}"))
    if md
      # Metadata must describe file to be useful
      if md.describes_file?(base)
         = md
      else
        log_warn("Found metadata at #{link_loc}, which does not describe #{base}, ignoring", **options)
      end
    end
  end

  locs = []
  # If we still don't have metadata, load the site-wide configuration file and use templates found there as locations
  if ! && base
    templates = site_wide_config(base)
    log_debug("for_input", **options) {"templates: #{templates.map(&:to_s).inspect}"}
    locs = templates.map do |template|
      t = Addressable::Template.new(template)
      mapped = t.expand(url: base).to_s
      mapped = RDF::URI.decode(mapped) if options[:decode_uri]
      RDF::URI(base).join(mapped)
    end
    log_debug("for_input", **options) {"locs: #{locs.map(&:to_s).inspect}"}

    locs.each do |loc|
       ||= begin
        md = Metadata.open(loc, **options.merge(filenames: loc, reason: "load found metadata: #{loc}"))
        # Metadata must describe file to be useful
        if md
          # Metadata must describe file to be useful
          if md.describes_file?(base)
            md
          else
            log_warn("Found metadata at #{loc}, which does not describe #{base}, ignoring", **options)
            nil
          end
        end
      rescue IOError
        log_debug("for_input", **options) {"failed to load found metadata #{loc}: #{$!}"}
        nil
      end
    end
  end

  # Return either the merge or user- and found-metadata, any of these, or an empty TableGroup
   = case
  when  then 
  when base     then TableGroup.new({"@context" => "http://www.w3.org/ns/csvw", tables: [{url: base}]}, **options)
  else               TableGroup.new({"@context" => "http://www.w3.org/ns/csvw", tables: [{url: nil}]}, **options)
  end

  # Make TableGroup, if not already
  .is_a?(TableGroup) ?  : .to_table_group
end

.new(input, **options) ⇒ Object



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
# File 'lib/rdf/tabular/metadata.rb', line 253

def self.new(input, **options)
  # Triveal case
  return input if input.is_a?(Metadata)

  object = case input
  when Hash then input
  when IO, StringIO then ::JSON.parse(input.read)
  else ::JSON.parse(input.to_s)
  end

  raise ::JSON::ParserError unless object.is_a?(Hash)

  unless options[:parent]
    # Add context, if not set (which it should be)
    object['@context'] ||= options.delete(:@context) || options[:context]
  end

  klass = case
    when !self.equal?(RDF::Tabular::Metadata)
      self # subclasses can be directly constructed without type dispatch
    else
      type =  options[:type].to_sym if options[:type]

      # Figure out type by @type
      type ||= object['@type'].to_sym if object['@type']

      # Otherwise, Figure out type by site
      object_keys = object.keys.map(&:to_s)
      type ||= case
      when %w(tables).any? {|k| object_keys.include?(k)} then :TableGroup
      when %w(dialect tableSchema transformations).any? {|k| object_keys.include?(k)} then :Table
      when %w(targetFormat scriptFormat source).any? {|k| object_keys.include?(k)} then :Template
      when %w(columns primaryKey foreignKeys rowTitles).any? {|k| object_keys.include?(k)} then :Schema
      when %w(name virtual).any? {|k| object_keys.include?(k)} then :Column
      when %w(commentPrefix delimiter doubleQuote encoding header headerRowCount).any? {|k| object_keys.include?(k)} then :Dialect
      when %w(lineTerminators quoteChar skipBlankRows skipColumns skipInitialSpace skipRows trim).any? {|k| object_keys.include?(k)} then :Dialect
      end

      case type.to_s.to_sym
      when :TableGroup, :"" then RDF::Tabular::TableGroup
      when :Table then RDF::Tabular::Table
      when :Template then RDF::Tabular::Transformation
      when :Schema then RDF::Tabular::Schema
      when :Column then RDF::Tabular::Column
      when :Dialect then RDF::Tabular::Dialect
      else
        raise Error, "Unknown metadata type: #{type.inspect}"
      end
    end

  md = klass.allocate
  md.send(:initialize, object, **options)
  md
rescue ::JSON::ParserError
  raise Error, "Expected input to be a JSON Object"
end

.open(path, **options) {|Metadata| ... } ⇒ Object

Attempt to retrieve the file at the specified path. If it is valid metadata, create a new Metadata object from it, otherwise, an empty Metadata object

Parameters:

  • path (String)
  • options (Hash{Symbol => Object})

    see ‘RDF::Util::File.open_file` in RDF.rb and new

Yields:

Raises:

  • (IOError)

    if file not found



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rdf/tabular/metadata.rb', line 139

def self.open(path, **options)
  options = options.merge(
    headers: {
      'Accept' => 'application/ld+json, application/json'
    }
  )
  path = "file:#{path}" if RDF::URI(path).relative?
  RDF::Util::File.open_file(path, **options) do |file|
    self.new(file, **options.merge(base: path, filenames: path))
  end
rescue Errno::ENOENT => e
  raise IOError, e.message
end

.site_wide_config(base) ⇒ Array<String>, false

Return the well-known configuration for a file, and remember using a weak-reference cache to avoid uncessary retreivles.

Parameters:

  • base (String)

    the URL used for finding the file

Returns:

  • (Array<String>, false)


156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rdf/tabular/metadata.rb', line 156

def self.site_wide_config(base)
  require 'rdf/util/cache' unless defined?(::RDF::Util::Cache)
  @cache ||= RDF::Util::Cache.new(-1)

  config_loc = RDF::URI(base).join(SITE_WIDE_CONFIG).to_s
  # Only load if we haven't tried before. Use `SITE_WIDE_DEFAULT` if not found
  if @cache[config_loc].nil?
    @cache[config_loc] = RDF::Util::File.open_file(config_loc) do |rd|
      rd.each_line.to_a
    end rescue SITE_WIDE_DEFAULT.split
  end
  @cache[config_loc]
end

Instance Method Details

#==(other) ⇒ Object



1095
1096
1097
# File 'lib/rdf/tabular/metadata.rb', line 1095

def ==(other)
  object == (other.is_a?(Hash) ? other : (other.respond_to?(:object) ? other.object : other))
end

#[](key) ⇒ Object

Proxy to @object



1092
# File 'lib/rdf/tabular/metadata.rb', line 1092

def [](key); object[key]; end

#[]=(key, value) ⇒ Object



1093
# File 'lib/rdf/tabular/metadata.rb', line 1093

def []=(key, value); object[key] = value; end

#baseRDF::URI

Base URL of metadata

Returns:

  • (RDF::URI)


559
# File 'lib/rdf/tabular/metadata.rb', line 559

def base; @options[:base]; end

#common_properties(subject, property, value) {|property, statement| ... } ⇒ Object #common_properties(subject, property, value) ⇒ String, ...

Return JSON-friendly or yield RDF for common properties

Overloads:

  • #common_properties(subject, property, value) {|property, statement| ... } ⇒ Object

    Yield RDF statements

    Parameters:

    • subject (RDF::Resource)
    • property (String)
    • value (String, Hash{String => Object}, Array<String, Hash{String => Object}>)

    Yields:

    • property, value

    Yield Parameters:

    • property (String)

      as a PName or URL

    • statement (RDF::Statement)
  • #common_properties(subject, property, value) ⇒ String, ...

    Return value with expanded values and node references flattened

    Returns:

    • (String, Hash{String => Object}, Array<String, Hash{String => Object}>)

      simply extracted from metadata



921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
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
978
979
# File 'lib/rdf/tabular/metadata.rb', line 921

def common_properties(subject, property, value, &block)
  if block_given?
    property = context.expand_iri(property.to_s, vocab: true) unless property.is_a?(RDF::URI)
    case value
    when Array
      value.each {|v| common_properties(subject, property, v, &block)}
    when Hash
      if value['@value']
        dt = RDF::URI(context.expand_iri(value['@type'], vocab: true)) if value['@type']
        lit = RDF::Literal(value['@value'], language: value['@language'], datatype: dt)
        block.call(RDF::Statement(subject, property, lit))
      else
        # value MUST be a node object, establish a new subject from `@id`
        s2 = value.has_key?('@id') ? context.expand_iri(value['@id']) : RDF::Node.new

        # Generate a triple
        block.call(RDF::Statement(subject, property, s2))

        # Generate types
        Array(value['@type']).each do |t|
          block.call(RDF::Statement(s2, RDF.type, context.expand_iri(t, vocab: true)))
        end

        # Generate triples for all other properties
        value.each do |prop, val|
          next if prop.to_s.start_with?('@')
          common_properties(s2, prop, val, &block)
        end
      end
    else
      # Value is a primitive JSON value
      lit = RDF::Literal(value)
      block.call(RDF::Statement(subject, property, RDF::Literal(value)))
    end
  else
    case value
    when Array
      value.map {|v| common_properties(subject, property, v)}
    when Hash
      if value['@value']
        value['@value']
      elsif value.keys == %w(@id) && value['@id']
        value['@id']
      else
        nv = {}
        value.each do |k, v|
          case k.to_s
          when '@id' then nv[k.to_s] = context.expand_iri(v['@id']).to_s
          when '@type' then nv[k.to_s] = v
          else nv[k.to_s] = common_properties(nil, k, v)
          end
        end
        nv
      end
    else
      value
    end
  end
end

#contextJSON::LD::Context

Context used for this metadata. Use parent’s if not defined on self.

Returns:

  • (JSON::LD::Context)


462
463
464
# File 'lib/rdf/tabular/metadata.rb', line 462

def context
  @context || (parent.context if parent)
end

#datatype=(value) ⇒ Dialect

Set new datatype

Returns:

Raises:

  • (Error)

    if datatype is not valid



539
540
541
542
543
544
545
546
547
548
549
550
551
# File 'lib/rdf/tabular/metadata.rb', line 539

def datatype=(value)
  val = case value
  when Hash then Datatype.new(value, **@options.merge(parent: self))
  else           Datatype.new({base: value}, **@options.merge(parent: self))
  end

  if val.valid? || value.is_a?(Hash)
    # Set it if it was specified as an object, which may cause validation errors later
    object[:datatype] = val
  else
    log_warn "#{type} has invalid property 'datatype': expected a built-in or an object"
  end
end

#describes_file?(url) ⇒ Boolean

Does this metadata describe the file (URL)?

Parameters:

  • url (RDF::URL)

Returns:

  • (Boolean)


990
991
992
993
994
995
996
997
# File 'lib/rdf/tabular/metadata.rb', line 990

def describes_file?(url)
  case self
  when TableGroup
    tables.any? {|t| t.url == url}
  else
    self.url == url
  end
end

#dialectDialect

Treat ‘dialect` similar to an inherited property, but merge together values from Table and TableGroup

Returns:



495
496
497
498
499
500
501
502
503
504
505
506
# File 'lib/rdf/tabular/metadata.rb', line 495

def dialect
  @dialect ||= case
  when object[:dialect] then object[:dialect]
  when parent then parent.dialect
  when is_a?(Table) || is_a?(TableGroup)
    d = Dialect.new({}, **@options.merge(parent: self, context: nil))
    self.dialect = d unless self.parent
    d
  else
    raise Error, "Can't access dialect from #{self.class} without a parent"
  end
end

#dialect=(value) ⇒ Dialect

Set new dialect

Returns:



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/rdf/tabular/metadata.rb', line 510

def dialect=(value)
  # Clear cached dialect information from children
  object.values.each do |v|
    case v
    when Metadata then v.object.delete(:dialect)
    when Array then v.each {|vv| vv.object.delete(:dialect) if vv.is_a?(Metadata)}
    end
  end

  # If provided, dialect provides hints to processors about how to parse the referenced file to create a tabular data model.
  @dialect = object[:dialect] = case value
  when String
    link = context.base.join(value).to_s
    md = Metadata.open(link, **@options.merge(parent: self, context: nil, normalize: true))
    md[:@id] ||= link
    md
  when Hash
    Dialect.new(value, **@options.merge(parent: self, context: nil))
  when Dialect
    value
  else
    log_warn "#{type} has invalid property 'dialect' (#{value.inspect}): expected a URL or object"
    nil
  end
end

#each(&block) ⇒ Object



1094
# File 'lib/rdf/tabular/metadata.rb', line 1094

def each(&block); object.each(&block); end

#each_row(input) {|Row| ... } ⇒ Object

Yield each data row from the input file

Parameters:

  • input (:read)

Yields:



854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/rdf/tabular/metadata.rb', line 854

def each_row(input)
  csv, number, skipped = nil, 0, 0
  path = input.base_uri.path rescue ""
  if path.end_with?('.html') || input.respond_to?(:content_type) && input.content_type == 'text/html'
    # Input is HTML; use fragment identfier to find table.
    fragment = RDF::URI(self.url).fragment rescue nil
    tab = begin
      # Extract with nokogiri
      require 'nokogiri' unless defined?(:Nokogiri)
      doc = Nokogiri::HTML.parse(input)
      doc.search("##{fragment}").first if fragment
    rescue LoadError
      # Extract with REXML
      # FIXME
    end

    raise Error, "Expected to find HTML table identified by fragment identifer ##{fragment}" unless tab

    # Use rows with <td> to create column data
    csv = []
    number = 0
    tab.xpath('.//tr').map do |row|
      number += 1 if row.xpath('th')
      data = row.xpath('td').map(&:content)
      csv << data unless data.empty?
    end
  else
    csv = ::CSV.new(input, **csv_options)
    # Skip skipRows and headerRowCount
    skipped = (dialect.skipRows.to_i + dialect.headerRowCount)
    (1..skipped).each {csv.shift}
  end
  csv.each do |data|
    # Check for embedded comments
    if dialect.commentPrefix && data.first.to_s.start_with?(dialect.commentPrefix)
      v = data.join(' ')[1..-1].strip
      unless v.empty?
        (self["rdfs:comment"] ||= []) << v
      end
      skipped += 1
      next
    elsif dialect.skipBlankRows && data.join("").strip.empty?
      skipped += 1
      next
    end
    number += 1
    row = Row.new(data, self, number, number + skipped, **@options)
    (self.object[:rows] ||= []) << row if @options[:validate] # Keep track of rows when validating
    yield(row)
  end
end

#has_annotations?Boolean

Does the Metadata have any common properties?

Returns:

  • (Boolean)


983
984
985
# File 'lib/rdf/tabular/metadata.rb', line 983

def has_annotations?
  object.keys.any? {|k| k.to_s.include?(':')}
end

#inspectObject



1087
1088
1089
# File 'lib/rdf/tabular/metadata.rb', line 1087

def inspect
  self.class.name + (respond_to?(:to_atd) ? to_atd : object).inspect
end

#normalize!self

Normalize object

Returns:

  • (self)

Raises:



1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
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
1148
1149
1150
1151
# File 'lib/rdf/tabular/metadata.rb', line 1104

def normalize!
  self.each do |key, value|
    self[key] = case @properties[key] || INHERITED_PROPERTIES[key]
    when ->(k) {key.to_s.include?(':') || key == :notes}
      normalize_jsonld(key, value)
    when ->(k) {key.to_s == '@context'}
      "http://www.w3.org/ns/csvw"
    when :array
      value = [value] unless value.is_a?(Array)
      value.map do |v|
        if v.is_a?(Metadata)
          v.normalize!
        elsif v.is_a?(Hash) && (ref = v["reference"]).is_a?(Hash)
          # SPEC SUGGESTION: special case for foreignKeys
          ref["resource"] = context.base.join(ref["resource"]).to_s if ref["resource"]
          ref["schemaReference"] = context.base.join(ref["schemaReference"]).to_s if ref["schemaReference"]
          v
        else
          v
        end
      end
    when :link
      context.base.join(value).to_s
    when :object
      case value
      when Metadata then value.normalize!
      when String
        # Load referenced JSON document
        # (This is done when objects are loaded in this implementation)
        raise "unexpected String value of property '#{key}': #{value}"
      else value
      end
    when :natural_language
      value.is_a?(Hash) ? value : {(context.default_language || 'und') => Array(value)}
    when :atomic
      case key
      when :minimum, :maximum, :minInclusive, :maxInclusive, :minExclusive, :maxExclusive
        # Convert to a typed literal based on `base`. This will be validated later
        RDF::Literal(value, datatype: DATATYPES[self.base.to_sym])
      else
        value
      end
    else
      value
    end
  end
  self
end

#normalize_jsonld(property, value) ⇒ String, ...

Normalize JSON-LD

Also, raise error if invalid JSON-LD dialect is detected

Parameters:

  • property (Symbol, String)
  • value (String, Hash{String => Object}, Array<String, Hash{String => Object}>)

Returns:

  • (String, Hash{String => Object}, Array<String, Hash{String => Object}>)


1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
# File 'lib/rdf/tabular/metadata.rb', line 1161

def normalize_jsonld(property, value)
  case value
  when Array
    value.map {|v| normalize_jsonld(property, v)}
  when String
    ev = {'@value' => value}
    ev['@language'] = context.default_language if context.default_language
    ev
  when Hash
    if value['@value']
      if !(value.keys.sort - %w(@value @type @language)).empty?
        log_error "Value object may not contain keys other than @value, @type, or @language: #{value.to_json}"
      elsif (value.keys.sort & %w(@language @type)) == %w(@language @type)
        log_error "Value object may not contain both @type and @language: #{value.to_json}"
      elsif value['@language'] && !BCP47.valid?(value['@language'].to_s)
        log_error "Value object with @language must use valid language: #{value.to_json}"
      elsif value['@type'] && (value['@type'].start_with?('_:') || !context.expand_iri(value['@type'], vocab: true).absolute?)
        log_error "Value object with @type must defined type: #{value.to_json}"
      end
      value
    else
      nv = {}
      value.each do |k, v|
        case k
        when "@id"
          nv[k] = context.expand_iri(v, documentRelative: true).to_s
          log_error "Invalid use of explicit BNode on @id" if nv[k].start_with?('_:')
        when "@type"
          Array(v).each do |vv|
            # Validate that all type values transform to absolute IRIs
            resource = context.expand_iri(vv, vocab: true)
            log_error "Invalid type #{vv} in JSON-LD context" unless resource.is_a?(RDF::URI) && resource.absolute?
          end
          nv[k] = v
        when /^(@|_:)/
          log_error "Invalid use of #{k} in JSON-LD content"
        else
          nv[k] = normalize_jsonld(k, v)
        end
      end
      nv
    end
  else
    value
  end
end

#tables=(value) ⇒ Object



466
467
468
# File 'lib/rdf/tabular/metadata.rb', line 466

def tables=(value)
  set_array_value(:tables, value, Table)
end

#tableSchema=(value) ⇒ Object

An object property that provides a schema description as described in section 3.8 Schemas, for all the tables in the group. This may be provided as an embedded object within the JSON metadata or as a URL reference to a separate JSON schema document when loading a remote schema, assign @id from it’s location if not already set



472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
# File 'lib/rdf/tabular/metadata.rb', line 472

def tableSchema=(value)
  object[:tableSchema] = case value
  when String
    link = context.base.join(value).to_s
    md = Schema.open(link, **@options.merge(parent: self, context: nil, normalize: true))
    md[:@id] ||= link
    md
  when Hash
    Schema.new(value, **@options.merge(parent: self, context: nil))
  when Schema
    value
  else
    log_warn "#{type} has invalid property 'tableSchema' (#{value.inspect}): expected a URL or object"
    Schema.new({}, **@options.merge(parent: self, context: nil))
  end
end

#to_json(args = nil) ⇒ Object



1098
# File 'lib/rdf/tabular/metadata.rb', line 1098

def to_json(args=nil); object.to_json(args); end

#transformations=(value) ⇒ Object



489
490
491
# File 'lib/rdf/tabular/metadata.rb', line 489

def transformations=(value)
  set_array_value(:transformations, value, Metadata)
end

#type:TableGroup, ...

Type of this Metadata

Returns:

  • (:TableGroup, :Table, :Template, :Schema, :Column)


555
# File 'lib/rdf/tabular/metadata.rb', line 555

def type; self.class.name.split('::').last.to_sym; end

#valid?Boolean

Do we have valid metadata?

Returns:

  • (Boolean)


563
564
565
566
# File 'lib/rdf/tabular/metadata.rb', line 563

def valid?
  validate # Possibly re-validate
  !log_statistics[:error]
end

#valid_natural_language_property?(value) { ... } ⇒ String?

Determine if a natural language property is valid

Parameters:

  • value (String, Array<String>, Hash{String => String})

Yields:

  • message error message

Returns:

  • (String, nil)


835
836
837
838
839
840
841
842
843
844
845
846
847
# File 'lib/rdf/tabular/metadata.rb', line 835

def valid_natural_language_property?(value)
  case value
  when String
  when Array
    "a valid natural language property" unless value.all? {|v| v.is_a?(String)}
  when Hash
    "a valid natural language property" if
      value.keys.any? {|k| k.to_s != "und" && !BCP47.valid?(k)} ||
      value.values.any? {|v| valid_natural_language_property?(v).is_a?(String)}
  else
    "a valid natural language property"
  end
end

#validateself

Validate metadata and content, raising an error containing all errors detected during validation

Returns:

  • (self)

Raises:

  • (Error)

    Raise error if metadata has any unexpected properties



576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
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
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# File 'lib/rdf/tabular/metadata.rb', line 576

def validate
  expected_props, required_props = @properties.keys, @required

  unless is_a?(Dialect) || is_a?(Transformation)
    expected_props = expected_props + INHERITED_PROPERTIES.keys
  end

  # It has only expected properties (exclude metadata)
  check_keys = object.keys - [:"@id", :"@context"]
  check_keys = check_keys.reject {|k| k.to_s.include?(':')} unless is_a?(Dialect)
  log_warn "#{type} has unexpected keys: #{(check_keys - expected_props).map(&:to_s)}" unless check_keys.all? {|k| expected_props.include?(k)}

  # It has required properties
  log_error "#{type} missing required keys: #{(required_props - check_keys).map(&:to_s)}"  unless (required_props & check_keys) == required_props

  self.normalize!

  # Every property is valid
  object.keys.each do |key|
    value = object[key]
    case key
    when :base
      log_error "#{type} has invalid base: #{value.inspect}" unless DATATYPES.keys.map(&:to_s).include?(value)
    when :columns
      value.each do |col|
        col.validate
        log_statistics.merge!(col.log_statistics)
      end
      column_names = value.map(&:name)
      log_error "#{type} has invalid property '#{key}': must have unique names: #{column_names.inspect}" unless column_names.uniq == column_names
    when :datatype, :dialect, :tables, :tableSchema, :transformations
      Array(value).each do |t|
        # Make sure value is of appropriate class
        if t.is_a?({datatype: Datatype, dialect: Dialect, tables: Table, tableSchema: Schema, transformations: Transformation}[key])
          t.validate
          log_statistics.merge!(t.log_statistics)
        else
          log_error "#{type} has invalid property '#{key}': unexpected value #{value.class.name}"
        end
      end
      log_error "#{type} has invalid property 'tables': must not be empty" if key == :tables && Array(value).empty?
    when :foreignKeys
      # An array of foreign key definitions that define how the values from specified columns within this table link to rows within this table or other tables. A foreign key definition is a JSON object with the properties:
      value.each do |fk|
        columnReference, reference = fk['columnReference'], fk['reference']
        log_error "#{type} has invalid property '#{key}': missing columnReference and reference" unless columnReference && reference
        log_error "#{type} has invalid property '#{key}': has extra entries #{fk.keys.inspect}" unless fk.keys.length == 2

        # Verify that columns exist in this schema
        log_error "#{type} has invalid property '#{key}': no columnReference found" unless Array(columnReference).length > 0
        Array(columnReference).each do |k|
          log_error "#{type} has invalid property '#{key}': columnReference not found #{k}" unless self.columns.any? {|c| c[:name] == k}
        end

        if reference.is_a?(Hash)
          log_error "#{type} has invalid property '#{key}': reference has extra entries #{reference.keys.inspect}" unless (reference.keys - %w(resource schemaReference columnReference)).empty?
          ref_cols = reference['columnReference']
          schema = if reference.has_key?('resource')
            if reference.has_key?('schemaReference')
              log_error "#{type} has invalid property '#{key}': reference has a schemaReference: #{reference.inspect}" 
            end
            # resource is the URL of a Table in the TableGroup
            ref = context.base.join(reference['resource']).to_s
            table = root.is_a?(TableGroup) && Array(root.tables).detect {|t| t.url == ref}
            log_error "#{type} has invalid property '#{key}': table referenced by #{ref} not found" unless table
            table.tableSchema if table
          elsif reference.has_key?('schemaReference')
            # resource is the @id of a Schema in the TableGroup
            ref = context.base.join(reference['schemaReference']).to_s
            tables = root.is_a?(TableGroup) ? root.tables.select {|t| t.tableSchema[:@id] == ref} : []
            case tables.length
            when 0
              log_error "#{type} has invalid property '#{key}': schema referenced by #{ref} not found"
              nil
            when 1
              tables.first.tableSchema
            else
              log_error "#{type} has invalid property '#{key}': multiple schemas found from #{ref}"
              nil
            end
          end

          if schema
            # ref_cols must exist in schema
            log_error "#{type} has invalid property '#{key}': no columnReference found" unless Array(ref_cols).length > 0
            Array(ref_cols).each do |k|
              log_error "#{type} has invalid property '#{key}': column reference not found #{k}" unless schema.columns.any? {|c| c[:name] == k}
            end
          end
        else
          log_error "#{type} has invalid property '#{key}': reference must be an object #{reference.inspect}"
        end
      end
    when :format
      case value
      when Hash
        # Object form only appropriate for numeric type
        unless %w(
          decimal integer long int short byte double float number
          nonNegativeInteger positiveInteger nonPositiveInteger negativeInteger
          unsignedLong unsignedInt unsignedShort unsignedByte
        ).include?(self.base)
          log_warn "#{type} has invalid property '#{key}': Object form only allowed on string or binary datatypes"
          object.delete(:format) # act as if not set
        end

        # Otherwise, if it exists, its a UAX35 number pattern
        begin
          parse_uax35_number(value["pattern"], nil, value.fetch('groupChar', ','), value.fetch('decimalChar', '.'))
        rescue ArgumentError => e
          log_warn "#{type} has invalid property '#{key}' pattern: #{e.message}"
          object[:format].delete("pattern") if object[:format] # act as if not set
        end
      else
        case self.base
        when 'boolean'
          unless value.split("|").length == 2
            log_warn "#{type} has invalid property '#{key}': annotation provides the true and false values expected, separated by '|'"
            object.delete(:format) # act as if not set
          end
        when :decimal, :integer, :long, :int, :short, :byte,
             :nonNegativeInteger, :positiveInteger,
             :unsignedLong, :unsignedInt, :unsignedShort, :unsignedByte,
             :nonPositiveInteger, :negativeInteger,
             :double, :float, :number
          begin
            parse_uax35_number(value, nil)
          rescue ArgumentError => e
            log_warn "#{type} has invalid property '#{key}': #{e.message}"
            object.delete(:format) # act as if not set
          end
        when 'date', 'dateTime', 'datetime', 'dateTimeStamp', 'time'
          # Parse and validate format
          begin
            parse_uax35_date(value, nil)
          rescue ArgumentError => e
            log_warn "#{type} has invalid property '#{key}': #{e.message}"
            object.delete(:format) # act as if not set
          end
        else
          # Otherwise, if it exists, its a regular expression
          begin
            Regexp.compile(value)
          rescue
            log_warn "#{type} has invalid property '#{key}': #{$!.message}"
            object.delete(:format) # act as if not set
          end
        end
      end
    when :length, :minLength, :maxLength
      # Applications must raise an error if both length and minLength are specified and length is less than minLength.
      # Similarly, applications must raise an error if both length and maxLength are specified and length is greater than maxLength.
      if object[:length]
        case key
        when :minLength
          log_error "#{type} has invalid property minLength': both length and minLength requires length be greater than or equal to minLength" if object[:length] < value
        when :maxLength
          log_error "#{type} has invalid property maxLength': both length and maxLength requires length be less than or equal to maxLength" if object[:length] > value
        end
      end

      # Applications must raise an error if minLength and maxLength are both specified and minLength is greater than maxLength.
      if key == :maxLength && object[:minLength]
        log_error "#{type} has invalid property #{key}': both minLength and maxLength requires minLength be less than or equal to maxLength" if object[:minLength] > value
      end

      # Applications must raise an error if length, maxLength, or minLength are specified and the base datatype is not string or one of its subtypes, or a binary type.
      unless %w(string normalizedString token language Name NMTOKEN hexBinary base64Binary binary).include?(self.base)
        log_error "#{type} has invalid property '#{key}': only allowed on string or binary datatypes"
      end
    when :minimum, :maximum, :minInclusive, :maxInclusive, :minExclusive, :maxExclusive
      case self.base
      when 'decimal', 'integer', 'long', 'int', 'short', 'byte', 'double', 'number', 'float',
           'nonNegativeInteger', 'positiveInteger', 'unsignedLong', 'unsignedInt', 'unsignedShort', 'unsignedByte',
           'nonPositiveInteger', 'negativeInteger', 'date', 'dateTime', 'datetime', 'dateTimeStamp', 'time',
           'duration', 'dayTimeDuration', 'yearMonthDuration'
        log_error "#{type} has invalid property '#{key}': #{value.to_ntriples} is not a valid #{self.base}" unless value.valid?

        case key
        when :minInclusive
          # Applications MUST raise an error if both minInclusive and minExclusive are specified
          log_error "#{type} cannot specify both minInclusive and minExclusive" if self.minExclusive

          # Applications MUST raise an error if both minInclusive and maxInclusive are specified and maxInclusive is less than minInclusive
          log_error "#{type} maxInclusive < minInclusive" if self.maxInclusive && self.maxInclusive < value

          # Applications MUST raise an error if both minInclusive and maxExclusive are specified and maxExclusive is less than or equal to minInclusive
          log_error "#{type} maxExclusive <= minInclusive" if self.maxExclusive && self.maxExclusive <= value
        when :maxInclusive
          # Applications MUST raise an error if both maxInclusive and maxExclusive are specified
          log_error "#{type} cannot specify both maInclusive and maxExclusive" if self.maxExclusive
        when :minExclusive
          # Applications MUST raise an error if both minExclusive and maxExclusive are specified and maxExclusive is less than minExclusive
          log_error "#{type} minExclusive < maxExclusive" if self.maxExclusive && self.maxExclusive < value

          # Applications MUST raise an error if both minExclusive and maxInclusive are specified and maxInclusive is less than or equal to minExclusive
          log_error "#{type} maxInclusive < minExclusive" if self.maxInclusive && self.maxInclusive <= value
        end
      else
        log_error "#{type} has invalid property '#{key}': only allowed on numeric, date/time or duration datatypes"
      end
    when :notes
      unless value.is_a?(Hash) || value.is_a?(Array)
        log_error "#{type} has invalid property '#{key}': #{value}, Object or Array"
      end
      begin
        normalize_jsonld(key, value)
      rescue Error => e
        log_error "#{type} has invalid content '#{key}': #{e.message}"
      end
    when :primaryKey, :rowTitles
      # A column reference property that holds either a single reference to a column description object or an array of references.
      "#{type} has invalid property '#{key}': no column references found" unless Array(value).length > 0
      Array(value).each do |k|
        unless self.columns.any? {|c| c[:name] == k}
          log_warn "#{type} has invalid property '#{key}': column reference not found #{k}"
          object.delete(key)
        end
      end
    when :@context
      # Skip these
    when :@id
      # Must not be a BNode
      if value.to_s.start_with?("_:")
        log_error "#{type} has invalid property '#{key}': #{value.inspect}, must not start with '_:'"
      end

      # Datatype @id MUST NOT be the URL of a built-in type
      if self.is_a?(Datatype) && DATATYPES.values.include?(value)
        log_error "#{type} has invalid property '#{key}': #{value.inspect}, must not be the URL of a built-in datatype"
      end
    when :@type
      # Must not be a BNode
      if value.to_s.start_with?("_:")
        log_error "#{type} has invalid property '@type': #{value.inspect}, must not start with '_:'"
      end
      case type
      when :Transformation
        log_error "#{type} has invalid property '@type': #{value.inspect}, expected #{type}" unless value.to_sym == :Template
      else
        log_error "#{type} has invalid property '@type': #{value.inspect}, expected #{type}" unless value.to_sym == type
      end
    when ->(k) {key.to_s.include?(':')}
      begin
        normalize_jsonld(key, value)
      rescue Error => e
        log_error "#{type} has invalid content '#{key}': #{e.message}"
      end
    end
  end

  self
end

#validate!Object

Raises:



568
569
570
# File 'lib/rdf/tabular/metadata.rb', line 568

def validate!
  raise Error, "Metadata error" unless valid?
end

#verify_compatible!(other) ⇒ Object

Verify that the metadata we’re using is compatible with embedded metadata

Parameters:

Raises:

  • (Error)

    if not compatible



1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
# File 'lib/rdf/tabular/metadata.rb', line 1002

def verify_compatible!(other)
  if self.is_a?(TableGroup)
    unless tables.any? {|t| t.url == other.url && t.verify_compatible!(other)}
      if @options[:validate]
        raise Error, "TableGroups must have Table with matching url #{tables.map(&:url).inspect} vs #{other.url.inspect}"
      else
        log_warn "TableGroups must have Table with matching url #{tables.map(&:url).inspect} vs #{other.url.inspect}"
      end
    end
  else
    # Tables must have the same url
    unless url == other.url
      if @options[:validate]
        raise Error, "Tables must have the same url: #{url.inspect} vs #{other.url.inspect}}"
      else
        log_warn "Tables must have the same url: #{url.inspect} vs #{other.url.inspect}}"
      end
    end

    # Each column description within B MUST match the corresponding column description in A for non-virtual columns
    non_virtual_columns = Array(tableSchema.columns).reject(&:virtual)
    object_columns = Array(other.tableSchema.columns)

    # Special case, if there is no header, then there are no column definitions, allow this as being compatible
    if non_virtual_columns.length != object_columns.length && !object_columns.empty?
      if @options[:validate]
        raise Error, "Columns must have the same number of non-virtual columns: #{non_virtual_columns.map(&:name).inspect} vs #{object_columns.map(&:name).inspect}"
      else
        log_warn "Columns must have the same number of non-virtual columns: #{non_virtual_columns.map(&:name).inspect} vs #{object_columns.map(&:name).inspect}"

        # If present, a virtual column MUST appear after all other non-virtual column definitions
        raise Error, "Virtual columns may not appear before non-virtual columns" unless Array(tableSchema.columns)[0..non_virtual_columns.length-1] == non_virtual_columns
        virtual_columns = Array(tableSchema.columns).select(&:virtual)
        while non_virtual_columns.length < object_columns.length
          non_virtual_columns << nil
        end

        # Create necessary column entries
        tableSchema.columns = non_virtual_columns + virtual_columns
      end
    end
    index = 0
    object_columns.all? do |cb|
      ca = non_virtual_columns[index] || Column.new({}, **@options)
      ta = ca.titles || {}
      tb = cb.titles || {}
      if !ca.object.has_key?(:name) && !cb.object.has_key?(:name) && ta.empty? && tb.empty?
        true
      elsif ca.object.has_key?(:name) && cb.object.has_key?(:name)
        raise Error, "Column #{index + 1} doesn't match on name: #{ca.name || 'no name'}, #{cb.name || 'no name'}" unless ca.name == cb.name
      elsif @options[:validate] || !ta.empty? && !tb.empty?
        # If validating, column compatibility requires strict match between titles
        titles_match = case
        when Array(ta['und']).any? {|t| tb.values.flatten.compact.include?(t)}
          true
        when Array(tb['und']).any? {|t| ta.values.flatten.compact.include?(t)}
          true
        when ta.any? {|lang, values| !(Array(tb[lang]) & Array(values)).empty?}
          # Match on title and language
          true
        else
          # Match if a language from ta is a prefix of a language from tb with matching titles
          ta.any? do |la, values|
            tb.keys.any? do |lb|
              (la.start_with?(lb) || lb.start_with?(la)) && !(Array(tb[lb]) & Array(values)).empty?
            end
          end
        end

        if titles_match
          true
        elsif !@options[:validate]
          # If not validating, columns don't match, but processing continues
          log_warn "Column #{index + 1} doesn't match on titles: #{Array(ta['und']).join(',').inspect} vs #{Array(tb['und']).join(',').inspect}"
          true
        else
          raise Error, "Column #{index + 1} doesn't match on titles: #{Array(ta['und']).join(',').inspect} vs #{Array(tb['und']).join(',').inspect}"
        end
      end
      index += 1
    end
  end
  true
end