class RDF::MergeGraph

A Merged graph.

Implements a merged graph, containing statements from one or more source graphs. This is done through lazy evaluation of the sources, so that a copy of each source isn’t required.

This class can also be used to change the context (graph name) of triples from the name used in the source.

@example Constructing a merge with arguments

aggregate = RDF::AggregateRepo.new(repo1, repo2)

@example Constructing an aggregate with closure

aggregate = RDF::MergeGraph.new do
  source graph1, context1
  source graph2, context2
  name false
end

@see www.w3.org/TR/rdf11-mt/#dfn-merge

Attributes

graph_name[R]

Name of this graph, used for setting the context on returned ‘Statements`.

@return [Array<RDF::URI, false>]

sources[R]

The set of aggregated ‘queryable` instances included in this aggregate

@return [Array<Array<(RDF::Queryable, RDF::Resource)>>]

Public Class Methods

new(graph_name: nil, name: nil, &block) click to toggle source

Create a new aggregation instance.

@param [RDF::Resource] graph_name @param [RDF::Resource] name alias for ‘graph_name` @yield merger @yieldparam [RDF::MergeGraph] self @yieldreturn [void] ignored

# File lib/rdf/aggregate_repo/merge_graph.rb, line 48
def initialize(graph_name: nil, name: nil, &block)
  @sources = []
  @graph_name = graph_name || name

  if block_given?
    case block.arity
    when 1 then block.call(self)
    else instance_eval(&block)
    end
  end
end

Public Instance Methods

add(queryable, graph_name)
Alias for: source
count() click to toggle source

@private @see RDF::Countable#count

# File lib/rdf/aggregate_repo/merge_graph.rb, line 136
def count
  each_statement.to_a.length
end
durable?() click to toggle source

@private @see RDF::Durable#durable?

# File lib/rdf/aggregate_repo/merge_graph.rb, line 122
def durable?
  sources.all? {|(source, ctx)| source.durable?}
end
each(&block) click to toggle source

@see RDF::Enumerable#each_statement

# File lib/rdf/aggregate_repo/merge_graph.rb, line 153
def each(&block)
  return enum_for(:each) unless block_given?

  # Add everything to a new graph for de-duplication
  tmp = RDF::Graph.new(graph_name: @graph_name, data: RDF::Repository.new)
  sources.each do |(source, gn)|
    tmp << RDF::Graph.new(graph_name: gn || nil, data: source)
  end
  tmp.each(&block)
end
each_graph() { |self| ... } click to toggle source

Iterate over each graph, in order, finding named graphs from the most recently added ‘source`.

@see RDF::Enumerable#each_graph

# File lib/rdf/aggregate_repo/merge_graph.rb, line 175
def each_graph(&block)
  if block_given?
    yield self
  end
  enum_graph
end
empty?() click to toggle source

@private @see RDF::Countable#empty?

# File lib/rdf/aggregate_repo/merge_graph.rb, line 129
def empty?
  count == 0
end
graph?() click to toggle source

Returns ‘true` to indicate that this is a graph.

@return [Boolean]

# File lib/rdf/aggregate_repo/merge_graph.rb, line 65
def graph?
  true
end
has_graph?(value) click to toggle source

@private @see RDF::Enumerable#has_graph?

# File lib/rdf/aggregate_repo/merge_graph.rb, line 167
def has_graph?(value)
  @graph_name == value
end
has_statement?(statement) click to toggle source

@private @see RDF::Enumerable#has_statement?

# File lib/rdf/aggregate_repo/merge_graph.rb, line 143
def has_statement?(statement)
  sources.any? do |(source, ctx)|
    statement = statement.dup
    statement.graph_name = ctx
    source.has_statement?(statement)
  end
end
name(name) click to toggle source

Set the graph_name for statements in this graph

@param [RDF::Resource, false] name @return [RDF::MergeGraph] self

# File lib/rdf/aggregate_repo/merge_graph.rb, line 112
def name(name)
  @graph_name = name
  self
end
named?() click to toggle source

Returns ‘true` if this is a named graph.

@return [Boolean] @note The next release, graphs will not be named, this will return false

# File lib/rdf/aggregate_repo/merge_graph.rb, line 74
def named?
  !unnamed?
end
source(queryable, graph_name) click to toggle source

Add a queryable to the set of constituent queryable instances

@param [RDF::Queryable] queryable @param [RDF::Resource] graph_name @return [RDF::MergeGraph] self

# File lib/rdf/aggregate_repo/merge_graph.rb, line 101
def source(queryable, graph_name)
  @sources << [queryable, graph_name]
  self
end
Also aliased as: add
unnamed?() click to toggle source

Returns ‘true` if this is a unnamed graph.

@return [Boolean] @note The next release, graphs will not be named, this will return true

# File lib/rdf/aggregate_repo/merge_graph.rb, line 83
def unnamed?
  @graph_name.nil?
end
writable?() click to toggle source

MergeGraph is writable if any source is writable. Updates go to the last writable source.

@return [Boolean]

# File lib/rdf/aggregate_repo/merge_graph.rb, line 91
def writable?
  sources.any? {|(source, ctx)| source.writable?}
end

Protected Instance Methods

query_pattern(pattern, **options, &block) click to toggle source

@private @see RDF::Queryable#query_pattern

# File lib/rdf/aggregate_repo/merge_graph.rb, line 187
def query_pattern(pattern, **options, &block)
  return enum_for(:query_pattern, pattern, **options) unless block_given?
  pattern = pattern.dup
  sources.each do |(source, gn)|
    pattern.graph_name = gn
    source.send(:query_pattern, pattern, **options, &block)
  end
end