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?, #enum_for

Instance Method Details

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

Returns all unique RDF contexts.

Parameters:

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

Options Hash (options):

  • :unique (Boolean) — default: true

Returns:

See Also:



469
470
471
472
473
474
475
# File 'lib/rdf/mixin/enumerable.rb', line 469

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

#dump(*args) ⇒ String

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:

  • args (Array<Object>)

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

Returns:

  • (String)

Raises:

See Also:

Since:

  • 0.2.0



630
631
632
633
634
635
# File 'lib/rdf/mixin/enumerable.rb', line 630

def dump(*args)
  options = args.last.is_a?(Hash) ? args.pop : {}
  writer = RDF::Writer.for(*args)
  raise RDF::WriterError, "No writer found using #{args.inspect}" unless writer
  writer.dump(self, nil, options)
end

#each_context {|context| ... } ⇒ void #each_contextEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (context)

      each context term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_contextEnumerator

    Returns:

See Also:



505
506
507
508
509
510
511
512
513
514
515
516
517
# File 'lib/rdf/mixin/enumerable.rb', line 505

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| ... } ⇒ void #each_graphEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (graph)

      each graph

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_graphEnumerator

    Returns:

See Also:

Since:

  • 0.1.9



549
550
551
552
553
554
555
556
557
# File 'lib/rdf/mixin/enumerable.rb', line 549

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| ... } ⇒ void #each_objectEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (object)

      each object term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_objectEnumerator

    Returns:

See Also:



437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/rdf/mixin/enumerable.rb', line 437

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| ... } ⇒ void #each_predicateEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (predicate)

      each predicate term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_predicateEnumerator

    Returns:

See Also:



369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/rdf/mixin/enumerable.rb', line 369

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| ... } ⇒ void #each_quadEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (subject, predicate, object, context)

      each quad

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_quadEnumerator

    Returns:

See Also:



238
239
240
241
242
243
244
245
# File 'lib/rdf/mixin/enumerable.rb', line 238

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

#each_statement {|statement| ... } ⇒ void #each_statementEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (statement)

      each statement

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_statementEnumerator

    Returns:

See Also:



118
119
120
121
122
123
124
# File 'lib/rdf/mixin/enumerable.rb', line 118

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

#each_subject {|subject| ... } ⇒ void #each_subjectEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (subject)

      each subject term

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_subjectEnumerator

    Returns:

See Also:



301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/rdf/mixin/enumerable.rb', line 301

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| ... } ⇒ void #each_tripleEnumerator

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| ... } ⇒ void

    This method returns an undefined value.

    Yields:

    • (subject, predicate, object)

      each triple

    Yield Parameters:

    Yield Returns:

    • (void)

      ignored

  • #each_tripleEnumerator

    Returns:

See Also:



177
178
179
180
181
182
183
184
# File 'lib/rdf/mixin/enumerable.rb', line 177

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

#enum_contextEnumerator Also known as: enum_contexts

Returns an enumerator for #each_context.

Returns:

See Also:



524
525
526
# File 'lib/rdf/mixin/enumerable.rb', line 524

def enum_context
  enum_for(:each_context)
end

#enum_graphEnumerator Also known as: enum_graphs

Returns an enumerator for #each_graph.

Returns:

See Also:

Since:

  • 0.1.9



565
566
567
# File 'lib/rdf/mixin/enumerable.rb', line 565

def enum_graph
  enum_for(:each_graph)
end

#enum_objectEnumerator Also known as: enum_objects

Returns an enumerator for #each_object.

Returns:

See Also:



456
457
458
# File 'lib/rdf/mixin/enumerable.rb', line 456

def enum_object
  enum_for(:each_object)
end

#enum_predicateEnumerator Also known as: enum_predicates

Returns an enumerator for #each_predicate.

Returns:

See Also:



388
389
390
# File 'lib/rdf/mixin/enumerable.rb', line 388

def enum_predicate
  enum_for(:each_predicate)
end

#enum_quadEnumerator Also known as: enum_quads

Returns an enumerator for #each_quad.

Returns:

See Also:



252
253
254
# File 'lib/rdf/mixin/enumerable.rb', line 252

def enum_quad
  enum_for(:each_quad)
end

#enum_statementEnumerator Also known as: enum_statements

Returns an enumerator for #each_statement.

Returns:

See Also:



131
132
133
# File 'lib/rdf/mixin/enumerable.rb', line 131

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

#enum_subjectEnumerator Also known as: enum_subjects

Returns an enumerator for #each_subject.

Returns:

See Also:



320
321
322
# File 'lib/rdf/mixin/enumerable.rb', line 320

def enum_subject
  enum_for(:each_subject)
end

#enum_tripleEnumerator Also known as: enum_triples

Returns an enumerator for #each_triple.

Returns:

See Also:



191
192
193
# File 'lib/rdf/mixin/enumerable.rb', line 191

def enum_triple
  enum_for(:each_triple)
end

#has_context?(value) ⇒ Boolean

Returns ‘true` if `self` contains the given RDF context.

Parameters:

Returns:

  • (Boolean)


482
483
484
# File 'lib/rdf/mixin/enumerable.rb', line 482

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

#has_object?(value) ⇒ Boolean

Returns ‘true` if `self` contains the given RDF object term.

Parameters:

Returns:

  • (Boolean)


414
415
416
# File 'lib/rdf/mixin/enumerable.rb', line 414

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

#has_predicate?(value) ⇒ Boolean

Returns ‘true` if `self` contains the given RDF predicate term.

Parameters:

Returns:

  • (Boolean)


346
347
348
# File 'lib/rdf/mixin/enumerable.rb', line 346

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

#has_quad?(quad) ⇒ Boolean

Returns ‘true` if `self` contains the given RDF quad.

Parameters:

Returns:

  • (Boolean)


212
213
214
# File 'lib/rdf/mixin/enumerable.rb', line 212

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

#has_statement?(statement) ⇒ Boolean Also known as: include?

Returns ‘true` if `self` contains the given RDF statement.

Parameters:

Returns:

  • (Boolean)


94
95
96
# File 'lib/rdf/mixin/enumerable.rb', line 94

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

#has_subject?(value) ⇒ Boolean

Returns ‘true` if `self` contains the given RDF subject term.

Parameters:

Returns:

  • (Boolean)


278
279
280
# File 'lib/rdf/mixin/enumerable.rb', line 278

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

#has_triple?(triple) ⇒ Boolean

Returns ‘true` if `self` contains the given RDF triple.

Parameters:

Returns:

  • (Boolean)


152
153
154
# File 'lib/rdf/mixin/enumerable.rb', line 152

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

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

Returns all unique RDF object terms.

Parameters:

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

Options Hash (options):

  • :unique (Boolean) — default: true

Returns:

See Also:



401
402
403
404
405
406
407
# File 'lib/rdf/mixin/enumerable.rb', line 401

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

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

Returns all unique RDF predicate terms.

Parameters:

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

Options Hash (options):

  • :unique (Boolean) — default: true

Returns:

See Also:



333
334
335
336
337
338
339
# File 'lib/rdf/mixin/enumerable.rb', line 333

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

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

Returns all RDF quads.

Parameters:

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

Returns:

See Also:



203
204
205
# File 'lib/rdf/mixin/enumerable.rb', line 203

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

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

Returns all RDF statements.

Parameters:

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

Returns:

See Also:



85
86
87
# File 'lib/rdf/mixin/enumerable.rb', line 85

def statements(options = {})
  enum_statement
end

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

Returns all unique RDF subject terms.

Parameters:

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

Options Hash (options):

  • :unique (Boolean) — default: true

Returns:

See Also:



265
266
267
268
269
270
271
# File 'lib/rdf/mixin/enumerable.rb', line 265

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

#supports?(feature) ⇒ Boolean

Returns ‘true` if this repository supports the given `feature`.

Supported features include:

* `:context` supports statements with a context, allowing multiple contexts
* `:inferrence` supports RDFS inferrence of queryable contents.

Parameters:

  • feature (Symbol, #to_sym)

Returns:

  • (Boolean)

Since:

  • 0.3.5



74
75
76
# File 'lib/rdf/mixin/enumerable.rb', line 74

def supports?(feature)
  false
end

#to_aArray

Returns all RDF statements in ‘self` as an array.

Mixes in ‘RDF::Enumerable` into the returned object.

Returns:

  • (Array)

Since:

  • 0.2.0



577
578
579
# File 'lib/rdf/mixin/enumerable.rb', line 577

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

#to_hashHash

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

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

Returns:

  • (Hash)


601
602
603
604
605
606
607
608
609
610
611
# File 'lib/rdf/mixin/enumerable.rb', line 601

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

#to_setSet

Returns all RDF statements in ‘self` as a set.

Mixes in ‘RDF::Enumerable` into the returned object.

Returns:

  • (Set)

Since:

  • 0.2.0



588
589
590
591
# File 'lib/rdf/mixin/enumerable.rb', line 588

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

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

Returns all RDF triples.

Parameters:

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

Returns:

See Also:



143
144
145
# File 'lib/rdf/mixin/enumerable.rb', line 143

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