class RDF::Query::HashPatternNormalizer

An RDF query pattern normalizer.

Attributes

options[R]

The options for this hash pattern normalizer.

@return [Hash{Symbol => Object}]

Public Class Methods

new(options = {}) click to toggle source

@param [Hash{Symbol => Object}] options (Hash.new)

any additional normalization options.

@option options [String] :anonymous_subject_format (“__%s__”)

the string format for anonymous subjects.
# File lib/rdf/query/hash_pattern_normalizer.rb, line 176
def initialize(options = {})
  @options = options.dup
end
normalize!(hash_pattern = {}, options = {}) click to toggle source

Returns the normalization of the specified `hash_pattern`.

@param [Hash{Symbol => Object}] hash_pattern (Hash.new)

the query pattern as a hash.

@param [Hash{Symbol => Object}] options (Hash.new)

any additional normalization options.

@option options [String] :anonymous_subject_format (“__%s__”)

the string format for anonymous subjects.

@return [Hash{Symbol => Object}]

the resulting query pattern as a normalized hash.
# File lib/rdf/query/hash_pattern_normalizer.rb, line 88
def normalize!(hash_pattern = {}, options = {})
  raise ArgumentError, "invalid hash pattern: #{hash_pattern.inspect}" unless hash_pattern.is_a?(Hash)
  
  counter = RDF::Query::HashPatternNormalizer::Counter.new
  
  anonymous_subject_format = (options[:anonymous_subject_format] || '__%s__').to_s
  
  hash_pattern.inject({}) { |acc, pair|
    subject, predicate_to_object = pair
    
    ensure_absence_of_duplicate_subjects!(acc, subject)
    normalized_predicate_to_object = normalize_hash!(predicate_to_object, acc, counter, anonymous_subject_format)
    ensure_absence_of_duplicate_subjects!(acc, subject)
    
    acc[subject] = normalized_predicate_to_object
    acc
  }
end

Private Class Methods

ensure_absence_of_duplicate_subjects!(acc, subject) click to toggle source

@private

# File lib/rdf/query/hash_pattern_normalizer.rb, line 111
def ensure_absence_of_duplicate_subjects!(acc, subject)
  raise "duplicate subject #{subject.inspect} in normalized hash pattern: #{acc.inspect}" if acc.key?(subject)
  
  return
end
normalize_array!(array, *args) click to toggle source

@private

# File lib/rdf/query/hash_pattern_normalizer.rb, line 119
def normalize_array!(array, *args)
  raise ArgumentError, "invalid array pattern: #{array.inspect}" unless array.is_a?(Array)
  
  array.collect { |object| 
    normalize_object!(object, *args)
  }
end
normalize_hash!(hash, *args) click to toggle source

@private

# File lib/rdf/query/hash_pattern_normalizer.rb, line 129
def normalize_hash!(hash, *args)
  raise ArgumentError, "invalid hash pattern: #{hash.inspect}" unless hash.is_a?(Hash)
  
  hash.inject({}) { |acc, pair|
    acc[pair.first] = normalize_object!(pair.last, *args)
    acc
  }
end
normalize_object!(object, *args) click to toggle source

@private

# File lib/rdf/query/hash_pattern_normalizer.rb, line 140
def normalize_object!(object, *args)
  case object
    when Array then normalize_array!(object, *args)
    when Hash  then replace_hash_with_anonymous_subject!(object, *args)
               else object
  end
end
replace_hash_with_anonymous_subject!(hash, acc, counter, anonymous_subject_format) click to toggle source

@private

# File lib/rdf/query/hash_pattern_normalizer.rb, line 150
def replace_hash_with_anonymous_subject!(hash, acc, counter, anonymous_subject_format)
  raise ArgumentError, "invalid hash pattern: #{hash.inspect}" unless hash.is_a?(Hash)
  
  subject = (anonymous_subject_format % counter.increment!).to_sym
  
  ensure_absence_of_duplicate_subjects!(acc, subject)
  normalized_hash = normalize_hash!(hash, acc, counter, anonymous_subject_format)
  ensure_absence_of_duplicate_subjects!(acc, subject)
  
  acc[subject] = normalized_hash

  subject
end

Public Instance Methods

normalize!(hash_pattern = {}) click to toggle source

Equivalent to calling `self.class.normalize!(hash_pattern, self.options)`.

@param [Hash{Symbol => Object}] hash_pattern

the query pattern as a hash.

@return [Hash{Symbol => Object}]

the resulting query pattern as a normalized hash.
# File lib/rdf/query/hash_pattern_normalizer.rb, line 187
def normalize!(hash_pattern = {})
  self.class.normalize!(hash_pattern, @options)
end