Class: RDF::Graph
- Inherits:
-
Object
- Object
- RDF::Graph
- Includes:
- Countable, Durable, Enumerable, Mutable, Queryable, Transactable, Value
- Defined in:
- lib/rdf/model/graph.rb
Overview
An RDF graph.
An Graph contains a unique set of Statement. It is based on an underlying data object, which may be specified when the graph is initialized, and will default to a Repository without support for named graphs otherwise.
Note that in RDF 1.1, graphs are not named, but are associated with a graph name in a Dataset, as a pair of <name, graph>. This class allows a name to be associated with a graph when it is a projection of an underlying Repository supporting graph_names.
Instance Attribute Summary collapse
-
#data ⇒ RDF::Queryable
Queryable backing this graph.
-
#graph_name ⇒ RDF::Resource
(also: #name)
Name of this graph, if it is part of an Repository.
-
#options ⇒ Hash{Symbol => Object}
readonly
Returns the options passed to this graph when it was constructed.
Class Method Summary collapse
-
.load(url, graph_name: nil, **options) {|graph| ... } ⇒ Graph
Creates a new ‘Graph` instance populated by the RDF data returned by dereferencing the given graph_name Resource.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Graph equivalence based on the contents of each graph being exactly the same.
-
#anonymous? ⇒ Boolean
Returns ‘true` if this graph has an anonymous graph, `false` otherwise.
-
#count ⇒ Integer
Returns the number of RDF statements in this graph.
-
#durable? ⇒ Boolean
A graph is durable if it’s underlying data model is durable.
-
#each {|statement| ... } ⇒ Enumerator
Enumerates each RDF statement in this graph.
- #each_graph ⇒ Object
-
#graph? ⇒ Boolean
Returns ‘true` to indicate that this is a graph.
-
#graph_names(unique: true) ⇒ Enumerator<RDF::Resource>
Returns all unique RDF names for this graph.
- #graphs ⇒ Object
-
#has_statement?(statement) ⇒ Boolean
Returns ‘true` if this graph contains the given RDF statement.
-
#initialize(graph_name: nil, data: nil, **options) {|graph| ... } ⇒ Graph
constructor
A new instance of Graph.
- #insert_statements(statements) ⇒ Object
-
#load!(*args) ⇒ void
(re)loads the graph from the specified location, or from the location associated with the graph name, if any.
-
#named? ⇒ Boolean
Returns ‘true` if this is a named graph.
- #project_graph(graph_name, &block) ⇒ Object
-
#to_s ⇒ String
Returns a string representation of this graph.
-
#to_uri ⇒ RDF::Resource
Returns the Resource representation of this graph.
-
#unnamed? ⇒ Boolean
Returns ‘true` if this is a unnamed graph.
Methods included from Transactable
Methods included from Mutable
#<<, #apply_changeset, #clear, #delete, #delete_insert, #immutable?, #insert, #load, #method_missing, #mutable?, #respond_to_missing?, #snapshot, #update
Methods included from Util::Aliasing::LateBound
Methods included from Writable
Methods included from Readable
Methods included from Queryable
#enum_for, #first, #first_literal, #first_object, #first_predicate, #first_subject, #first_value, #query
Methods included from Enumerable
#dump, #each_object, #each_predicate, #each_quad, #each_statement, #each_subject, #each_term, #each_triple, #enum_for, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_term, #enum_triple, #has_graph?, #has_object?, #has_predicate?, #has_quad?, #has_subject?, #has_term?, #has_triple?, #invalid?, #method_missing, #objects, #predicates, #quads, #respond_to_missing?, #statements, #subjects, #supports?, #terms, #to_a, #to_hash, #to_set, #triples, #valid?, #validate!
Methods included from Countable
Methods included from Durable
Methods included from Value
#canonicalize, #canonicalize!, #constant?, #inspect, #inspect!, #invalid?, #iri?, #list?, #literal?, #node?, #resource?, #statement?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #valid?, #validate!, #variable?
Constructor Details
#initialize(graph_name: nil, data: nil, **options) {|graph| ... } ⇒ Graph
Graph names are only useful when used as a projection on a ‘:data` which supports named graphs. Otherwise, there is no such thing as a named graph in RDF 1.1, a repository may have graphs which are named, but the name is not a property of the graph.
Returns a new instance of Graph.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/rdf/model/graph.rb', line 106 def initialize(graph_name: nil, data: nil, **, &block) @graph_name = case graph_name when nil then nil when RDF::Resource then graph_name else RDF::URI.new(graph_name) end = .dup @data = data || RDF::Repository.new(with_graph_name: false) raise ArgumentError, "Can't apply graph_name unless initialized with `data` supporting graph_names" if @graph_name && !@data.supports?(:graph_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::Mutable
Instance Attribute Details
#data ⇒ RDF::Queryable
Queryable backing this graph.
61 62 63 |
# File 'lib/rdf/model/graph.rb', line 61 def data @data end |
#graph_name ⇒ RDF::Resource Also known as: name
Name of this graph, if it is part of an Repository
52 53 54 |
# File 'lib/rdf/model/graph.rb', line 52 def graph_name @graph_name end |
#options ⇒ Hash{Symbol => Object} (readonly)
Returns the options passed to this graph when it was constructed.
45 46 47 |
# File 'lib/rdf/model/graph.rb', line 45 def end |
Class Method Details
.load(url, graph_name: nil, **options) {|graph| ... } ⇒ Graph
Creates a new ‘Graph` instance populated by the RDF data returned by dereferencing the given graph_name Resource.
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/rdf/model/graph.rb', line 76 def self.load(url, graph_name: nil, **, &block) self.new(graph_name: graph_name, **) do |graph| graph.load(url, graph_name: graph_name, **) if block_given? case block.arity when 1 then block.call(graph) else graph.instance_eval(&block) end end end end |
Instance Method Details
#==(other) ⇒ Boolean
Graph equivalence based on the contents of each graph being exactly the same. To determine if the have the same meaning, consider [rdf-isomorphic](rubygems.org/gems/rdf-isomorphic).
267 268 269 270 271 |
# File 'lib/rdf/model/graph.rb', line 267 def ==(other) other.is_a?(RDF::Graph) && graph_name == other.graph_name && statements.to_a == other.statements.to_a end |
#anonymous? ⇒ Boolean
The next release, graphs will not be named, this will return true
Returns ‘true` if this graph has an anonymous graph, `false` otherwise.
204 205 206 |
# File 'lib/rdf/model/graph.rb', line 204 def anonymous? graph_name.nil? ? false : graph_name.anonymous? end |
#count ⇒ Integer
Returns the number of RDF statements in this graph.
213 214 215 |
# File 'lib/rdf/model/graph.rb', line 213 def count @data.query(graph_name: graph_name || false).count end |
#durable? ⇒ Boolean
A graph is durable if it’s underlying data model is durable
171 172 173 |
# File 'lib/rdf/model/graph.rb', line 171 def durable? @data.durable? end |
#each {|statement| ... } ⇒ Enumerator
Enumerates each RDF statement in this graph.
238 239 240 241 242 243 244 245 246 |
# File 'lib/rdf/model/graph.rb', line 238 def each(&block) if @data.respond_to?(:query) @data.query(graph_name: graph_name || false, &block) elsif @data.respond_to?(:each) @data.each(&block) else @data.to_a.each(&block) end end |
#each_graph ⇒ Object
348 349 350 351 352 353 354 |
# File 'lib/rdf/model/graph.rb', line 348 def each_graph if block_given? yield self else enum_graph end end |
#graph? ⇒ Boolean
Returns ‘true` to indicate that this is a graph.
144 145 146 |
# File 'lib/rdf/model/graph.rb', line 144 def graph? true end |
#graph_names(unique: true) ⇒ Enumerator<RDF::Resource>
Returns all unique RDF names for this graph.
179 180 181 |
# File 'lib/rdf/model/graph.rb', line 179 def graph_names(unique: true) (named? ? [graph_name] : []).extend(RDF::Countable) end |
#graphs ⇒ Object
340 341 342 |
# File 'lib/rdf/model/graph.rb', line 340 def graphs Array(enum_graph) end |
#has_statement?(statement) ⇒ Boolean
Returns ‘true` if this graph contains the given RDF statement.
A statement is in a graph if the statement if it has the same triples without regard to graph_name.
225 226 227 228 229 |
# File 'lib/rdf/model/graph.rb', line 225 def has_statement?(statement) statement = statement.dup statement.graph_name = graph_name @data.has_statement?(statement) end |
#insert_statements(statements) ⇒ Object
294 295 296 297 298 299 300 301 302 303 304 |
# File 'lib/rdf/model/graph.rb', line 294 def insert_statements(statements) enum = Enumerable::Enumerator.new do |yielder| statements.send(method = statements.respond_to?(:each_statement) ? :each_statement : :each) do |s| s = s.dup s.graph_name = graph_name yielder << s end end @data.insert(enum) end |
#load!(*args) ⇒ void
This method returns an undefined value.
(re)loads the graph from the specified location, or from the location associated with the graph name, if any
131 132 133 134 135 136 137 138 |
# File 'lib/rdf/model/graph.rb', line 131 def load!(*args) case when args.empty? raise ArgumentError, "Can't reload graph without a graph_name" unless graph_name.is_a?(RDF::URI) load(graph_name.to_s, base_uri: graph_name) else super end end |
#named? ⇒ Boolean
The next release, graphs will not be named, this will return false
Returns ‘true` if this is a named graph.
153 154 155 |
# File 'lib/rdf/model/graph.rb', line 153 def named? !unnamed? end |
#project_graph(graph_name, &block) ⇒ Object
251 252 253 254 255 256 257 |
# File 'lib/rdf/model/graph.rb', line 251 def project_graph(graph_name, &block) if block_given? self.each(&block) if graph_name == self.graph_name else graph_name == self.graph_name ? self : RDF::Graph.new end end |
#to_s ⇒ String
Returns a string representation of this graph.
195 196 197 |
# File 'lib/rdf/model/graph.rb', line 195 def to_s named? ? graph_name.to_s : "default" end |
#to_uri ⇒ RDF::Resource
Returns the Resource representation of this graph.
187 188 189 |
# File 'lib/rdf/model/graph.rb', line 187 def to_uri graph_name end |
#unnamed? ⇒ Boolean
The next release, graphs will not be named, this will return true
Returns ‘true` if this is a unnamed graph.
162 163 164 |
# File 'lib/rdf/model/graph.rb', line 162 def unnamed? graph_name.nil? end |