Class: RDF::MergeGraph

Inherits:
Object show all
Includes:
Countable, Enumerable, Queryable, Value
Defined in:
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb

Overview

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.

Examples:

Constructing a merge with arguments

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

Constructing an aggregate with closure

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

See Also:

Instance Attribute Summary collapse

Attributes included from Enumerable

#existentials, #universals

Instance Method Summary collapse

Methods included from Queryable

#concise_bounded_description, #first, #first_literal, #first_object, #first_predicate, #first_subject, #first_value, #lint, #query, #query_execute, #query_without_sparql, #to_sparql

Methods included from Enumerable

add_entailment, #canonicalize, #canonicalize!, #dump, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #entail, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #graph_names, #invalid?, #method_missing, #object?, #objects, #predicate?, #predicates, #project_graph, #quad?, #quads, #respond_to_missing?, #statement?, #statements, #subject?, #subjects, #supports?, #term?, #terms, #to_a, #to_h, #to_set, #triple?, #triples, #valid?, #validate!

Methods included from Util::Aliasing::LateBound

#alias_method

Methods included from Isomorphic

#bijection_to, #isomorphic_with?

Methods included from Value

#anonymous?, #canonicalize, #canonicalize!, #constant?, #formula?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #start_with?, #statement?, #term?, #to_ndvar, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?

Constructor Details

#initialize(graph_name: nil, name: nil) {|self| ... } ⇒ MergeGraph

Create a new aggregation instance.

Parameters:

Yields:

  • merger

Yield Parameters:

Yield Returns:

  • (void)

    ignored



48
49
50
51
52
53
54
55
56
57
58
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class RDF::Enumerable

Instance Attribute Details

#graph_nameArray<RDF::URI, false> (readonly)

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

Returns:



38
39
40
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 38

def graph_name
  @graph_name
end

#sourcesArray<Array<(RDF::Queryable, RDF::Resource)>> (readonly)

The set of aggregated queryable instances included in this aggregate



32
33
34
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 32

def sources
  @sources
end

Instance Method Details

#each(&block) ⇒ Object



153
154
155
156
157
158
159
160
161
162
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/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(&block) ⇒ Object

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



175
176
177
178
179
180
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 175

def each_graph(&block)
  if block_given?
    yield self
  end
  enum_graph
end

#graph?Boolean

Returns true to indicate that this is a graph.

Returns:

  • (Boolean)


65
66
67
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 65

def graph?
  true
end

#name(name) ⇒ RDF::MergeGraph

Set the graph_name for statements in this graph

Parameters:

Returns:



112
113
114
115
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 112

def name(name)
  @graph_name = name
  self
end

#named?Boolean

Note:

The next release, graphs will not be named, this will return false

Returns true if this is a named graph.

Returns:

  • (Boolean)


74
75
76
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 74

def named?
  !unnamed?
end

#source(queryable, graph_name) ⇒ RDF::MergeGraph Also known as: add

Add a queryable to the set of constituent queryable instances

Parameters:

Returns:



101
102
103
104
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 101

def source(queryable, graph_name)
  @sources << [queryable, graph_name]
  self
end

#unnamed?Boolean

Note:

The next release, graphs will not be named, this will return true

Returns true if this is a unnamed graph.

Returns:

  • (Boolean)


83
84
85
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 83

def unnamed?
  @graph_name.nil?
end

#writable?Boolean

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

Returns:

  • (Boolean)


91
92
93
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-aggregate-repo-cb93fb3485bc/lib/rdf/aggregate_repo/merge_graph.rb', line 91

def writable?
  sources.any? {|(source, ctx)| source.writable?}
end