Module: RDF::Enumerable

Extended by:
Util::Aliasing::LateBound
Includes:
Enumerable, Countable
Included in:
Graph, List, Query::Solution, Reader, Repository
Defined in:
lib/rdf/mixin/enumerable.rb

Overview

An RDF statement enumeration mixin.

Classes that include this module must implement an #each method that yields RDF statements.

Examples:

Checking whether any statements exist

enumerable.empty?

Checking how many statements exist

enumerable.count

Checking whether a specific statement exists

enumerable.has_statement?(RDF::Statement(subject, predicate, object))
enumerable.has_triple?([subject, predicate, object])
enumerable.has_quad?([subject, predicate, object, context])

Checking whether a specific value exists

enumerable.has_subject?(RDF::URI("http://rdf.rubyforge.org/"))
enumerable.has_predicate?(RDF::DC.creator)
enumerable.has_object?(RDF::Literal("Hello!", :language => :en))
enumerable.has_context?(RDF::URI("http://rubyforge.org/"))

Enumerating all statements

enumerable.each_statement do |statement|
  puts statement.inspect
end

Enumerating all statements in the form of triples

enumerable.each_triple do |subject, predicate, object|
  puts [subject, predicate, object].inspect
end

Enumerating all statements in the form of quads

enumerable.each_quad do |subject, predicate, object, context|
  puts [subject, predicate, object, context].inspect
end

Enumerating all terms

enumerable.each_subject   { |term| puts term.inspect }
enumerable.each_predicate { |term| puts term.inspect }
enumerable.each_object    { |term| puts term.inspect }
enumerable.each_context   { |term| puts term.inspect }

Obtaining all statements

enumerable.statements  #=> [RDF::Statement(subject1, predicate1, object1), ...]
enumerable.triples     #=> [[subject1, predicate1, object1], ...]
enumerable.quads       #=> [[subject1, predicate1, object1, context1], ...]

Obtaining all unique values

enumerable.subjects    #=> [subject1, subject2, subject3, ...]
enumerable.predicates  #=> [predicate1, predicate2, predicate3, ...]
enumerable.objects     #=> [object1, object2, object3, ...]
enumerable.contexts    #=> [context1, context2, context3, ...]

See Also:

Instance Method Summary (collapse)

Methods included from Util::Aliasing::LateBound

alias_method

Methods included from Countable

#count, #empty?

Instance Method Details

- (Enumerator<RDF::Resource>) contexts(options = {})

Returns all unique RDF contexts.

Parameters:

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

Options Hash (options):

  • (Boolean) :unique — default: true

Returns:

See Also:



455
456
457
458
459
460
461
# File 'lib/rdf/mixin/enumerable.rb', line 455

def contexts(options = {})
  if options[:unique] == false
    enum_statement.map(&:context).to_enum # TODO: optimize
  else
    enum_context
  end
end

- (String) dump(*args)

Returns a serialized string representation of self.

Before calling this method you may need to explicitly require a serialization plugin for the specified format.

Examples:

Serializing into N-Triples format

require 'rdf/ntriples'
ntriples = enumerable.dump(:ntriples)

Parameters:

  • (Array<Object>) args

    if the last argument is a hash, it is passed as keyword options to Writer.dump.

Returns:

  • (String)

See Also:

Since:

  • 0.2.0



615
616
617
618
# File 'lib/rdf/mixin/enumerable.rb', line 615

def dump(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  RDF::Writer.for(*args).dump(self, nil, options)
end

- each_context {|context| ... } - (Enumerator) each_context

This method returns an undefined value.

Iterates the given block for each unique RDF context.

If no block was given, returns an enumerator.

The order in which values are yielded is undefined.

Overloads:

  • - each_context {|context| ... }

    This method returns an undefined value.

    Yields:

    • (context)

      each context term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_context

    Returns:

See Also:



491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/rdf/mixin/enumerable.rb', line 491

def each_context(&block)
  if block_given?
    values = {}
    each_statement do |statement|
      value = statement.context
      unless value.nil? || values.include?(value)
        values[value] = true
        block.call(value)
      end
    end
  end
  enum_context
end

- each_graph {|graph| ... } - (Enumerator) each_graph

This method returns an undefined value.

Iterates the given block for each RDF graph in self.

If no block was given, returns an enumerator.

The order in which graphs are yielded is undefined.

Overloads:

  • - each_graph {|graph| ... }

    This method returns an undefined value.

    Yields:

    • (graph)

      each graph

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_graph

    Returns:

See Also:

Since:

  • 0.1.9



535
536
537
538
539
540
541
542
543
# File 'lib/rdf/mixin/enumerable.rb', line 535

def each_graph(&block)
  if block_given?
    block.call(RDF::Graph.new(nil, :data => self))
    each_context do |context|
      block.call(RDF::Graph.new(context, :data => self))
    end
  end
  enum_graph
end

- each_object {|object| ... } - (Enumerator) each_object

This method returns an undefined value.

Iterates the given block for each unique RDF object term.

If no block was given, returns an enumerator.

The order in which values are yielded is undefined.

Overloads:

  • - each_object {|object| ... }

    This method returns an undefined value.

    Yields:

    • (object)

      each object term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_object

    Returns:

See Also:



423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'lib/rdf/mixin/enumerable.rb', line 423

def each_object(&block) # FIXME: deduplication
  if block_given?
    values = {}
    each_statement do |statement|
      value = statement.object
      unless value.nil? || values.include?(value)
        values[value] = true
        block.call(value)
      end
    end
  end
  enum_object
end

- each_predicate {|predicate| ... } - (Enumerator) each_predicate

This method returns an undefined value.

Iterates the given block for each unique RDF predicate term.

If no block was given, returns an enumerator.

The order in which values are yielded is undefined.

Overloads:

  • - each_predicate {|predicate| ... }

    This method returns an undefined value.

    Yields:

    • (predicate)

      each predicate term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_predicate

    Returns:

See Also:



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/rdf/mixin/enumerable.rb', line 355

def each_predicate(&block)
  if block_given?
    values = {}
    each_statement do |statement|
      value = statement.predicate
      unless value.nil? || values.include?(value.to_s)
        values[value.to_s] = true
        block.call(value)
      end
    end
  end
  enum_predicate
end

- each_quad {|subject, predicate, object, context| ... } - (Enumerator) each_quad

This method returns an undefined value.

Iterates the given block for each RDF quad.

If no block was given, returns an enumerator.

The order in which quads are yielded is undefined.

Overloads:

  • - each_quad {|subject, predicate, object, context| ... }

    This method returns an undefined value.

    Yields:

    • (subject, predicate, object, context)

      each quad

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_quad

    Returns:

See Also:



224
225
226
227
228
229
230
231
# File 'lib/rdf/mixin/enumerable.rb', line 224

def each_quad(&block)
  if block_given?
    each_statement do |statement|
      block.call(*statement.to_quad)
    end
  end
  enum_quad
end

- each_statement {|statement| ... } - (Enumerator) each_statement

This method returns an undefined value.

Iterates the given block for each RDF statement.

If no block was given, returns an enumerator.

The order in which statements are yielded is undefined.

Overloads:

  • - each_statement {|statement| ... }

    This method returns an undefined value.

    Yields:

    • (statement)

      each statement

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_statement

    Returns:

See Also:



104
105
106
107
108
109
110
# File 'lib/rdf/mixin/enumerable.rb', line 104

def each_statement(&block)
  if block_given?
    # Invoke {#each} in the containing class:
    each(&block)
  end
  enum_statement
end

- each_subject {|subject| ... } - (Enumerator) each_subject

This method returns an undefined value.

Iterates the given block for each unique RDF subject term.

If no block was given, returns an enumerator.

The order in which values are yielded is undefined.

Overloads:

  • - each_subject {|subject| ... }

    This method returns an undefined value.

    Yields:

    • (subject)

      each subject term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_subject

    Returns:

See Also:



287
288
289
290
291
292
293
294
295
296
297
298
299
# File 'lib/rdf/mixin/enumerable.rb', line 287

def each_subject(&block)
  if block_given?
    values = {}
    each_statement do |statement|
      value = statement.subject
      unless value.nil? || values.include?(value.to_s)
        values[value.to_s] = true
        block.call(value)
      end
    end
  end
  enum_subject
end

- each_triple {|subject, predicate, object| ... } - (Enumerator) each_triple

This method returns an undefined value.

Iterates the given block for each RDF triple.

If no block was given, returns an enumerator.

The order in which triples are yielded is undefined.

Overloads:

  • - each_triple {|subject, predicate, object| ... }

    This method returns an undefined value.

    Yields:

    • (subject, predicate, object)

      each triple

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • - (Enumerator) each_triple

    Returns:

See Also:



163
164
165
166
167
168
169
170
# File 'lib/rdf/mixin/enumerable.rb', line 163

def each_triple(&block)
  if block_given?
    each_statement do |statement|
      block.call(*statement.to_triple)
    end
  end
  enum_triple
end

- (Enumerator) enum_context Also known as: enum_contexts

Returns an enumerator for #each_context.

Returns:

See Also:



510
511
512
# File 'lib/rdf/mixin/enumerable.rb', line 510

def enum_context
  enum_for(:each_context)
end

- (Enumerator) enum_graph Also known as: enum_graphs

Returns an enumerator for #each_graph.

Returns:

See Also:

Since:

  • 0.1.9



551
552
553
# File 'lib/rdf/mixin/enumerable.rb', line 551

def enum_graph
  enum_for(:each_graph)
end

- (Enumerator) enum_object Also known as: enum_objects

Returns an enumerator for #each_object.

Returns:

See Also:



442
443
444
# File 'lib/rdf/mixin/enumerable.rb', line 442

def enum_object
  enum_for(:each_object)
end

- (Enumerator) enum_predicate Also known as: enum_predicates

Returns an enumerator for #each_predicate.

Returns:

See Also:



374
375
376
# File 'lib/rdf/mixin/enumerable.rb', line 374

def enum_predicate
  enum_for(:each_predicate)
end

- (Enumerator) enum_quad Also known as: enum_quads

Returns an enumerator for #each_quad.

Returns:

See Also:



238
239
240
# File 'lib/rdf/mixin/enumerable.rb', line 238

def enum_quad
  enum_for(:each_quad)
end

- (Enumerator) enum_statement Also known as: enum_statements

Returns an enumerator for #each_statement.

Returns:

See Also:



117
118
119
# File 'lib/rdf/mixin/enumerable.rb', line 117

def enum_statement
  enum_for(:each_statement).extend(RDF::Queryable, RDF::Enumerable)
end

- (Enumerator) enum_subject Also known as: enum_subjects

Returns an enumerator for #each_subject.

Returns:

See Also:



306
307
308
# File 'lib/rdf/mixin/enumerable.rb', line 306

def enum_subject
  enum_for(:each_subject)
end

- (Enumerator) enum_triple Also known as: enum_triples

Returns an enumerator for #each_triple.

Returns:

See Also:



177
178
179
# File 'lib/rdf/mixin/enumerable.rb', line 177

def enum_triple
  enum_for(:each_triple)
end

- (Boolean) has_context?(value)

Returns true if self contains the given RDF context.

Parameters:

Returns:

  • (Boolean)


468
469
470
# File 'lib/rdf/mixin/enumerable.rb', line 468

def has_context?(value)
  enum_context.include?(value)
end

- (Boolean) has_object?(value)

Returns true if self contains the given RDF object term.

Parameters:

Returns:

  • (Boolean)


400
401
402
# File 'lib/rdf/mixin/enumerable.rb', line 400

def has_object?(value)
  enum_object.include?(value)
end

- (Boolean) has_predicate?(value)

Returns true if self contains the given RDF predicate term.

Parameters:

Returns:

  • (Boolean)


332
333
334
# File 'lib/rdf/mixin/enumerable.rb', line 332

def has_predicate?(value)
  enum_predicate.include?(value)
end

- (Boolean) has_quad?(quad)

Returns true if self contains the given RDF quad.

Parameters:

Returns:

  • (Boolean)


198
199
200
# File 'lib/rdf/mixin/enumerable.rb', line 198

def has_quad?(quad)
  enum_quad.include?(quad)
end

- (Boolean) has_statement?(statement) Also known as: include?

Returns true if self contains the given RDF statement.

Parameters:

Returns:

  • (Boolean)


80
81
82
# File 'lib/rdf/mixin/enumerable.rb', line 80

def has_statement?(statement)
  !enum_statement.find { |s| s.eql?(statement) }.nil?
end

- (Boolean) has_subject?(value)

Returns true if self contains the given RDF subject term.

Parameters:

Returns:

  • (Boolean)


264
265
266
# File 'lib/rdf/mixin/enumerable.rb', line 264

def has_subject?(value)
  enum_subject.include?(value)
end

- (Boolean) has_triple?(triple)

Returns true if self contains the given RDF triple.

Parameters:

Returns:

  • (Boolean)


138
139
140
# File 'lib/rdf/mixin/enumerable.rb', line 138

def has_triple?(triple)
  enum_triple.include?(triple)
end

- (Enumerator<RDF::Term>) objects(options = {})

Returns all unique RDF object terms.

Parameters:

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

Options Hash (options):

  • (Boolean) :unique — default: true

Returns:

See Also:



387
388
389
390
391
392
393
# File 'lib/rdf/mixin/enumerable.rb', line 387

def objects(options = {})
  if options[:unique] == false
    enum_statement.map(&:object).to_enum # TODO: optimize
  else
    enum_object
  end
end

- (Enumerator<RDF::URI>) predicates(options = {})

Returns all unique RDF predicate terms.

Parameters:

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

Options Hash (options):

  • (Boolean) :unique — default: true

Returns:

See Also:



319
320
321
322
323
324
325
# File 'lib/rdf/mixin/enumerable.rb', line 319

def predicates(options = {})
  if options[:unique] == false
    enum_statement.map(&:predicate).to_enum # TODO: optimize
  else
    enum_predicate
  end
end

- (Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term, RDF::Resource)>) quads(options = {})

Returns all RDF quads.

Parameters:

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

Returns:

See Also:



189
190
191
# File 'lib/rdf/mixin/enumerable.rb', line 189

def quads(options = {})
  enum_statement.map(&:to_quad).to_enum # TODO: optimize
end

- (Enumerator<RDF::Statement>) statements(options = {})

Returns all RDF statements.

Parameters:

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

Returns:

See Also:



71
72
73
# File 'lib/rdf/mixin/enumerable.rb', line 71

def statements(options = {})
  enum_statement
end

- (Enumerator<RDF::Resource>) subjects(options = {})

Returns all unique RDF subject terms.

Parameters:

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

Options Hash (options):

  • (Boolean) :unique — default: true

Returns:

See Also:



251
252
253
254
255
256
257
# File 'lib/rdf/mixin/enumerable.rb', line 251

def subjects(options = {})
  if options[:unique] == false
    enum_statement.map(&:subject).to_enum # TODO: optimize
  else
    enum_subject
  end
end

- (Array) to_a

Returns all RDF statements in self as an array.

Mixes in RDF::Enumerable into the returned object.

Returns:

  • (Array)

Since:

  • 0.2.0



563
564
565
# File 'lib/rdf/mixin/enumerable.rb', line 563

def to_a
  super.extend(RDF::Enumerable)
end

- (Hash) to_hash

Returns all RDF object terms indexed by their subject and predicate terms.

The return value is a Hash instance that has the structure: {subject => {predicate => [*objects]}}.

Returns:

  • (Hash)


587
588
589
590
591
592
593
594
595
596
597
# File 'lib/rdf/mixin/enumerable.rb', line 587

def to_hash
  result = {}
  each_statement do |statement|
    next if statement.invalid? # skip any incomplete statements

    result[statement.subject] ||= {}
    values = (result[statement.subject][statement.predicate] ||= [])
    values << statement.object unless values.include?(statement.object)
  end
  result
end

- (Set) to_set

Returns all RDF statements in self as a set.

Mixes in RDF::Enumerable into the returned object.

Returns:

  • (Set)

Since:

  • 0.2.0



574
575
576
577
# File 'lib/rdf/mixin/enumerable.rb', line 574

def to_set
  require 'set' unless defined?(::Set)
  super.extend(RDF::Enumerable)
end

- (Enumerator<Array(RDF::Resource, RDF::URI, RDF::Term)>) triples(options = {})

Returns all RDF triples.

Parameters:

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

Returns:

See Also:



129
130
131
# File 'lib/rdf/mixin/enumerable.rb', line 129

def triples(options = {})
  enum_statement.map(&:to_triple).to_enum # TODO: optimize
end