Class: RDF::Query::HashPatternNormalizer

Inherits:
Object
  • Object
show all
Defined in:
lib/rdf/query/hash_pattern_normalizer.rb

Overview

An RDF query pattern normalizer.

Since:

  • 0.3.0

Defined Under Namespace

Classes: Counter

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ HashPatternNormalizer

Returns a new instance of HashPatternNormalizer

Parameters:

  • options (Hash{Symbol => Object})

    (Hash.new) any additional normalization options.

Options Hash (**options):

  • :anonymous_subject_format (String) — default: "__%s__"

    the string format for anonymous subjects.

Since:

  • 0.3.0



176
177
178
# File 'lib/rdf/query/hash_pattern_normalizer.rb', line 176

def initialize(**options)
  @options = options.dup
end

Instance Attribute Details

#optionsHash{Symbol => Object} (readonly)

The options for this hash pattern normalizer.

Returns:

  • (Hash{Symbol => Object})

Since:

  • 0.3.0



169
170
171
# File 'lib/rdf/query/hash_pattern_normalizer.rb', line 169

def options
  @options
end

Class Method Details

.normalize!(hash_pattern = {}, options = {}) ⇒ Hash{Symbol => Object}

Returns the normalization of the specified hash_pattern.

Parameters:

  • hash_pattern (Hash{Symbol => Object}) (defaults to: {})

    (Hash.new) the query pattern as a hash.

  • options (Hash{Symbol => Object}) (defaults to: {})

    (Hash.new) any additional normalization options.

Options Hash (options):

  • :anonymous_subject_format (String) — default: "__%s__"

    the string format for anonymous subjects.

Returns:

  • (Hash{Symbol => Object})

    the resulting query pattern as a normalized hash.

Raises:

  • (ArgumentError)

Since:

  • 0.3.0



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# 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

Instance Method Details

#normalize!(**hash_pattern) ⇒ Hash{Symbol => Object}

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

Parameters:

  • hash_pattern (Hash{Symbol => Object})

    the query pattern as a hash.

Returns:

  • (Hash{Symbol => Object})

    the resulting query pattern as a normalized hash.

Since:

  • 0.3.0



187
188
189
# File 'lib/rdf/query/hash_pattern_normalizer.rb', line 187

def normalize!(**hash_pattern)
  self.class.normalize!(hash_pattern, @options)
end