Class: RDF::AggregateRepo
- Inherits:
-
Repository
- Object
- Repository
- RDF::AggregateRepo
- Defined in:
- lib/rdf/aggregate_repo.rb
Overview
Allow graph names to reassigned with queryable
An aggregated RDF repository.
Aggregates the default and named graphs from one or more instances implementing RDF::Queryable. By default, the aggregate projects no default or named graphs, which must be added explicitly.
Adding the existing default graph (identified with the name ‘false`) adds the merge of all default graphs from the specified `queryable` instances.
Adding a named graph, adds the last graph found having that name from the specified ‘queryable` instances.
Updating a previously non-existing named graph, appends to the last source. Updating the default graph updates to the merge of the graphs.
Defined Under Namespace
Modules: VERSION
Instance Attribute Summary collapse
-
#defaults ⇒ Array<RDF::URI, false>
readonly
Names of the named graphs making up the default graph, or false, if it is made up from the merger of all default graphs.
-
#sources ⇒ Array<RDF::Queryable>
readonly
The set of aggregated ‘queryable` instances included in this aggregate.
Instance Method Summary collapse
-
#count ⇒ Integer
Returns the number of RDF statements in all constituent graphs.
-
#default(*names) ⇒ RDF::AggregateRepo
Set the default graph based on zero or more named graphs, or the merge of all default graphs if ‘false`.
-
#default_graph ⇒ RDF::Graph
Default graph of this aggregate, either a projection of the source default graph (if ‘false`), a particular named graph from the last source in which it appears, or a MergeGraph composed of the graphs which compose it.
-
#durable? ⇒ Boolean
Returns ‘true` all constituent graphs are durable.
-
#each {|statement| ... } ⇒ Enumerator
Enumerates each RDF statement in constituent graphs.
-
#each_context(&block) ⇒ void
Iterates the given block for each unique RDF context, other than the default context.
-
#each_graph(&block) ⇒ void
Iterate over each graph, in order, finding named graphs from the most recently added ‘source`.
-
#each_statement(&block) ⇒ void
Iterates the given block for each RDF statement.
-
#empty? ⇒ Boolean
Returns ‘true` if all constituent graphs are empty.
-
#has_context?(value) ⇒ Boolean
Returns ‘true` if any constituent grahp contains the given RDF context.
-
#has_statement?(statement) ⇒ Boolean
Returns ‘true` if any constituent graph contains the given RDF statement.
-
#initialize(queryable = [], options = {}) {|aggregation| ... } ⇒ AggregateRepo
constructor
Create a new aggregation instance.
-
#named(name) ⇒ RDF::AggregateRepo
Add a named graph projection.
-
#source(queryable) ⇒ RDF::AggregateRepo
(also: #add)
Add a queryable to the set of constituent queryable instances.
-
#writable? ⇒ false
Not writable.
Constructor Details
#initialize(queryable = [], options = {}) {|aggregation| ... } ⇒ AggregateRepo
Create a new aggregation instance.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/rdf/aggregate_repo.rb', line 60 def initialize(*queryable, &block) = queryable.last.is_a?(Hash) ? queryable.last.dup : {} @sources = queryable @defaults = [] @contexts = [] if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end |
Instance Attribute Details
#defaults ⇒ Array<RDF::URI, false> (readonly)
Names of the named graphs making up the default graph, or false, if it is made up from the merger of all default graphs
49 50 51 |
# File 'lib/rdf/aggregate_repo.rb', line 49 def defaults @defaults end |
#sources ⇒ Array<RDF::Queryable> (readonly)
The set of aggregated ‘queryable` instances included in this aggregate
41 42 43 |
# File 'lib/rdf/aggregate_repo.rb', line 41 def sources @sources end |
Instance Method Details
#count ⇒ Integer
Returns the number of RDF statements in all constituent graphs.
144 145 146 |
# File 'lib/rdf/aggregate_repo.rb', line 144 def count each_graph.to_a.reduce(0) {|memo, g| memo += g.count} end |
#default(*names) ⇒ RDF::AggregateRepo
Set the default graph based on zero or more named graphs, or the merge of all default graphs if ‘false`
93 94 95 96 97 98 99 |
# File 'lib/rdf/aggregate_repo.rb', line 93 def default(*names) if names.any? {|n| n == false} && names.length > 1 raise ArgumentError, "If using merge of default graphs, there can be only one" end @default_graph = nil @defaults = names end |
#default_graph ⇒ RDF::Graph
Default graph of this aggregate, either a projection of the source default graph (if ‘false`), a particular named graph from the last source in which it appears, or a MergeGraph composed of the graphs which compose it.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/rdf/aggregate_repo.rb', line 275 def default_graph @default_graph ||= begin case when sources.length == 0 || defaults.length == 0 RDF::Graph.new when defaults.length == 1 && sources.length == 1 RDF::Graph.new((defaults.first || nil), :data => sources.first) else # Otherwise, create a MergeGraph from the set of pairs of source and context RDF::MergeGraph.new(:name => nil) do |graph| if defaults == [false] graph.sources.each do |s| # Add default graph from each source source s, false end else defaults.each do |context| # add the named graph graph.source sources.reverse.detect {|s| s.has_context?(context)}, context end end end end end end |
#durable? ⇒ Boolean
Returns ‘true` all constituent graphs are durable.
126 127 128 |
# File 'lib/rdf/aggregate_repo.rb', line 126 def durable? sources.all?(&:durable?) end |
#each {|statement| ... } ⇒ Enumerator
Enumerates each RDF statement in constituent graphs
193 194 195 196 |
# File 'lib/rdf/aggregate_repo.rb', line 193 def each(&block) return to_enum unless block_given? each_graph {|g| g.each(&block)} end |
#each_context {|context| ... } ⇒ void #each_context ⇒ Enumerator
This method returns an undefined value.
Iterates the given block for each unique RDF context, other than the default context.
If no block was given, returns an enumerator.
The order in which values are yielded is undefined.
228 229 230 231 232 233 |
# File 'lib/rdf/aggregate_repo.rb', line 228 def each_context(&block) if block_given? @contexts.each(&block) end enum_context end |
#each_graph {|graph| ... } ⇒ void #each_graph ⇒ Enumerator
This method returns an undefined value.
Iterate over each graph, in order, finding named graphs from the most recently added ‘source`.
If no block was given, returns an enumerator.
The order in which graphs are yielded is undefined.
255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/rdf/aggregate_repo.rb', line 255 def each_graph(&block) if block_given? block.call(default_graph) # Send context from appropriate source each_context do |context| source = sources.reverse.detect {|s| s.has_context?(context)} block.call(RDF::Graph.new(context, :data => source)) end end enum_graph end |
#each_statement {|statement| ... } ⇒ void #each_statement ⇒ Enumerator
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.
178 179 180 181 182 183 184 |
# File 'lib/rdf/aggregate_repo.rb', line 178 def each_statement(&block) if block_given? # Invoke {#each} in the containing class: each(&block) end enum_statement end |
#empty? ⇒ Boolean
Returns ‘true` if all constituent graphs are empty.
135 136 137 |
# File 'lib/rdf/aggregate_repo.rb', line 135 def empty? count == 0 end |
#has_context?(value) ⇒ Boolean
Returns ‘true` if any constituent grahp contains the given RDF context.
205 206 207 |
# File 'lib/rdf/aggregate_repo.rb', line 205 def has_context?(value) @contexts.include?(value) end |
#has_statement?(statement) ⇒ Boolean
Returns ‘true` if any constituent graph contains the given RDF statement.
154 155 156 |
# File 'lib/rdf/aggregate_repo.rb', line 154 def has_statement?(statement) each_graph.to_a.any? {|g| g.has_statement?(statement)} end |
#named(name) ⇒ RDF::AggregateRepo
Add a named graph projection. Dynamically binds to the last ‘queryable` having a matching context.
107 108 109 110 111 |
# File 'lib/rdf/aggregate_repo.rb', line 107 def named(name) raise ArgumentError, "name must be an RDF::Resource: #{name.inspect}" unless name.is_a?(RDF::Resource) raise ArgumentError, "name does not exist in loaded sources" unless sources.any?{|s| s.has_context?(name)} @contexts << name end |
#source(queryable) ⇒ RDF::AggregateRepo Also known as: add
Add a queryable to the set of constituent queryable instances
80 81 82 83 84 |
# File 'lib/rdf/aggregate_repo.rb', line 80 def source(queryable) @sources << queryable @default_graph = nil self end |
#writable? ⇒ false
Not writable
119 |
# File 'lib/rdf/aggregate_repo.rb', line 119 def writable?; false; end |