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



178
179
180
# File 'lib/rdf/query/hash_pattern_normalizer.rb', line 178

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



171
172
173
# File 'lib/rdf/query/hash_pattern_normalizer.rb', line 171

def options
  @options
end

Class Method Details

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

Returns the normalization of the specified ‘hash_pattern`.

Returns the resulting query pattern as a normalized hash.

Parameters:

  • hash_pattern (Hash{Symbol => Object})

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

  • **options (Hash{Symbol => Object})

    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



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rdf/query/hash_pattern_normalizer.rb', line 89

def normalize!(*args)
  hash_pattern = args.shift
  options = args.shift || {}
  anonymous_subject_format = options.fetch(:anonymous_subject_format, '__%s__')
  raise ArgumentError, "invalid hash pattern: #{hash_pattern.inspect}" unless hash_pattern.is_a?(Hash)
  
  counter = RDF::Query::HashPatternNormalizer::Counter.new
  
  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



189
190
191
# File 'lib/rdf/query/hash_pattern_normalizer.rb', line 189

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