Class: RDF::Statement
- Includes:
- Resource
- Defined in:
- vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/extensions.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/refinements.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/rdf-reasoner-e77a80426b61/lib/rdf/reasoner/extensions.rb,
vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/extensions.rb
Overview
Refinements on RDF::Statement
Direct Known Subclasses
Constant Summary collapse
- @@entailments =
{}
Constants included from SPARQL::Algebra::Expression
SPARQL::Algebra::Expression::PATTERN_PARENTS
Constants included from Util::Logger
Instance Attribute Summary collapse
- #graph_name ⇒ RDF::Resource
- #id ⇒ Object
- #object ⇒ RDF::Term
- #options ⇒ Hash{Symbol => Object}
- #predicate ⇒ RDF::URI
- #subject ⇒ RDF::Resource
Class Method Summary collapse
-
.add_entailment(method, proc) ⇒ Object
Add an entailment method.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks statement equality as a triple.
-
#===(other) ⇒ Boolean
Checks statement equality with patterns.
- #[](index) ⇒ RDF::Term
- #[]=(index, value) ⇒ RDF::Term
- #asserted? ⇒ Boolean
-
#canonicalize ⇒ RDF::Statement
Returns a version of the statement with each position in canonical form.
-
#canonicalize! ⇒ RDF::Statement
Canonicalizes each unfrozen term in the statement.
-
#complete? ⇒ Boolean
Determines if the statement is complete, vs.
-
#dup ⇒ RDF::Statement
New statement with duplicated components (other than blank nodes).
-
#embedded? ⇒ Boolean
Returns
true
if any element of the statement is, itself, a statement. -
#entail(method) {|term| ... } ⇒ Array<Term>
Perform an entailment on this term.
-
#eql?(other) ⇒ Boolean
Checks statement equality as a quad.
-
#evaluate(bindings, formulae:, **options) ⇒ RDF::Statement
As a statement is constant, this returns itself.
- #executable? ⇒ Boolean
- #graph?(*args) ⇒ Object (also: #name?, #has_graph?, #has_name?)
-
#hash ⇒ Object
Generates a Integer hash value as a quad.
-
#incomplete? ⇒ Boolean
Determines if the statement is incomplete, vs.
- #inferred? ⇒ Boolean
-
#initialize(subject = nil, predicate = nil, object = nil, options = {}) ⇒ Statement
constructor
A new instance of Statement.
-
#invalid? ⇒ Boolean
Refines
invalid?
to allow literal subjects and BNode predicates. -
#node? ⇒ Boolean
(also: #has_blank_nodes?)
Returns
true
if any resource of this statement is a blank node or has an embedded statement including a blank node. - #object? ⇒ Boolean (also: #has_object?)
-
#optimize(**options) ⇒ RDF::Statement
A duplicate of this Statement.
- #predicate? ⇒ Boolean (also: #has_predicate?)
-
#quoted? ⇒ Boolean
deprecated
Deprecated.
Quoted triples are now deprecated
-
#reified(subject: nil, id: nil, graph_name: nil) ⇒ RDF::Graph
Returns a graph containing this statement in reified form.
- #statement?(*args) ⇒ Object
- #subject? ⇒ Boolean (also: #has_subject?)
-
#terms ⇒ Array(RDF::Term)
Returns an array of all the non-nil non-statement terms.
-
#to_h(subject_key = :subject, predicate_key = :predicate, object_key = :object, graph_key = :graph_name) ⇒ Hash{Symbol => RDF::Term}
Returns the terms of this statement as a
Hash
. - #to_quad ⇒ Array(RDF::Term)
-
#to_s ⇒ String
Returns a string representation of this statement.
-
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this term.
-
#to_sxp(prefixes: nil, base_uri: nil) ⇒ String
Returns an S-Expression (SXP) representation.
-
#to_sxp_bin ⇒ Array
Transform Statement Pattern into an SXP.
- #to_triple ⇒ Array(RDF::Term) (also: #to_a)
- #tripleTerm? ⇒ Boolean
-
#valid? ⇒ Boolean
Refines
valid?
to allow literal subjects and BNode predicates. -
#valid_extended? ⇒ Boolean
Validate extended RDF.
-
#validate! ⇒ RDF::Value
Refines
validate!
to allow literal subjects and BNode predicates. -
#variable?(*args) ⇒ Object
URI, Node or Literal.
Methods included from Resource
Methods included from Term
#<=>, #aggregate?, #as_datetime, #as_number, #compatible?, #escape, #ndvars, #sameTerm?, #term?, #to_base, #to_term, #vars
Methods included from SPARQL::Algebra::Expression
cast, #constant?, extension, extension?, extensions, for, new, open, #optimize!, parse, register_extension
Methods included from Util::Logger
#log_debug, #log_depth, #log_error, #log_fatal, #log_info, #log_recover, #log_recovering?, #log_statistics, #log_warn, #logger
Methods included from Value
#anonymous?, #constant?, #formula?, #inspect, #inspect!, #iri?, #list?, #literal?, #resource?, #start_with?, #term?, #to_ndvar, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?
Constructor Details
#initialize(**options) ⇒ RDF::Statement #initialize(subject, predicate, object, **options) ⇒ RDF::Statement
Returns a new instance of Statement.
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 91 def initialize(subject = nil, predicate = nil, object = nil, = {}) if subject.is_a?(Hash) @options = Hash[subject] # faster subject.dup @subject = @options.delete(:subject) @predicate = @options.delete(:predicate) @object = @options.delete(:object) else @options = !.empty? ? Hash[] : {} @subject = subject @predicate = predicate @object = object end @id = @options.delete(:id) if @options.key?(:id) @graph_name = @options.delete(:graph_name) initialize! end |
Instance Attribute Details
#graph_name ⇒ RDF::Resource
49 50 51 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 49 def graph_name @graph_name end |
#id ⇒ Object
46 47 48 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 46 def id @id end |
#object ⇒ RDF::Term
58 59 60 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 58 def object @object end |
#options ⇒ Hash{Symbol => Object}
61 62 63 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 61 def @options end |
#predicate ⇒ RDF::URI
55 56 57 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 55 def predicate @predicate end |
#subject ⇒ RDF::Resource
52 53 54 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 52 def subject @subject end |
Class Method Details
.add_entailment(method, proc) ⇒ Object
Add an entailment method. The method accepts no arguments, and returns or yields an array of values associated with the particular entailment method
123 124 125 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-reasoner-e77a80426b61/lib/rdf/reasoner/extensions.rb', line 123 def add_entailment(method, proc) @@entailments[method] = proc end |
Instance Method Details
#==(other) ⇒ Boolean
Checks statement equality as a triple.
335 336 337 338 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 335 def ==(other) to_a == Array(other) && !(other.is_a?(RDF::Value) && other.list?) end |
#===(other) ⇒ Boolean
Checks statement equality with patterns.
Uses #eql?
to compare each of #subject
, #predicate
, #object
, and
#graph_name
to those of other
. Any statement part which is not
present in self
is ignored.
367 368 369 370 371 372 373 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 367 def ===(other) return false if object? && !object.eql?(other.object) return false if predicate? && !predicate.eql?(other.predicate) return false if subject? && !subject.eql?(other.subject) return false if graph? && !graph_name.eql?(other.graph_name) return true end |
#[](index) ⇒ RDF::Term
378 379 380 381 382 383 384 385 386 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 378 def [](index) case index when 0 then self.subject when 1 then self.predicate when 2 then self.object when 3 then self.graph_name else nil end end |
#[]=(index, value) ⇒ RDF::Term
392 393 394 395 396 397 398 399 400 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 392 def []=(index, value) case index when 0 then self.subject = value when 1 then self.predicate = value when 2 then self.object = value when 3 then self.graph_name = value else nil end end |
#asserted? ⇒ Boolean
210 211 212 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 210 def asserted? !quoted? end |
#canonicalize ⇒ RDF::Statement
Returns a version of the statement with each position in canonical form
443 444 445 446 447 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 443 def canonicalize self.dup.canonicalize! rescue ArgumentError nil end |
#canonicalize! ⇒ RDF::Statement
Canonicalizes each unfrozen term in the statement.
428 429 430 431 432 433 434 435 436 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 428 def canonicalize! self.subject.canonicalize! if subject? && !self.subject.frozen? self.predicate.canonicalize! if predicate? && !self.predicate.frozen? self.object.canonicalize! if object? && !self.object.frozen? self.graph_name.canonicalize! if graph? && !self.graph_name.frozen? self.validate! @hash = nil self end |
#complete? ⇒ Boolean
Determines if the statement is complete, vs. invalid. A complete statement is one in which none of subject
, predicate
, or object
, are nil.
247 248 249 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 247 def complete? !incomplete? end |
#dup ⇒ RDF::Statement
New statement with duplicated components (other than blank nodes)
452 453 454 455 456 457 458 459 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 452 def dup = Hash[@options] [:subject] = subject.is_a?(RDF::Node) ? subject : subject.dup [:predicate] = predicate.dup [:object] = object.is_a?(RDF::Node) ? object : object.dup [:graph_name] = graph_name.is_a?(RDF::Node) ? graph_name : graph_name.dup if graph_name RDF::Statement.new() end |
#embedded? ⇒ Boolean
Returns true
if any element of the statement is, itself, a statement.
Note: Nomenclature is evolving, alternatives could include #complex?
and #nested?
189 190 191 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 189 def subject && subject.statement? || object && object.statement? end |
#entail(method) {|term| ... } ⇒ Array<Term>
Perform an entailment on this term.
135 136 137 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-reasoner-e77a80426b61/lib/rdf/reasoner/extensions.rb', line 135 def entail(method, &block) self.send(@@entailments.fetch(method), &block) end |
#eql?(other) ⇒ Boolean
Checks statement equality as a quad.
315 316 317 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 315 def eql?(other) other.is_a?(Statement) && self.to_a.eql?(other.to_a) && (self.graph_name || false) == (other.graph_name || false) end |
#evaluate(bindings, formulae:, **options) ⇒ RDF::Statement
As a statement is constant, this returns itself.
25 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/refinements.rb', line 25 def evaluate(bindings, formulae:, **); end |
#executable? ⇒ Boolean
396 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/extensions.rb', line 396 def executable?; false; end |
#graph? ⇒ Boolean #graph?(name) ⇒ Boolean Also known as: name?, has_graph?, has_name?
262 263 264 265 266 267 268 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 262 def graph?(*args) case args.length when 0 then !!graph_name when 1 then graph_name == args.first else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") end end |
#hash ⇒ Object
Generates a Integer hash value as a quad.
321 322 323 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 321 def hash @hash ||= to_quad.hash end |
#incomplete? ⇒ Boolean
Determines if the statement is incomplete, vs. invalid. An incomplete statement is one in which any of subject
, predicate
, or object
, are nil.
238 239 240 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 238 def incomplete? to_triple.any?(&:nil?) end |
#inferred? ⇒ Boolean
229 230 231 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 229 def inferred? !!@options[:inferred] end |
#invalid? ⇒ Boolean
Refines invalid?
to allow literal subjects and BNode predicates.
195 196 197 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 195 def invalid? !valid? end |
#node? ⇒ Boolean Also known as: has_blank_nodes?
Returns true
if any resource of this statement is a blank node
or has an embedded statement including a blank node.
300 301 302 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 300 def node? to_quad.compact.any?(&:node?) end |
#object? ⇒ Boolean Also known as: has_object?
289 290 291 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 289 def object? !!object end |
#optimize(**options) ⇒ RDF::Statement
A duplicate of this Statement.
392 393 394 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/extensions.rb', line 392 def optimize(**) self.dup end |
#predicate? ⇒ Boolean Also known as: has_predicate?
282 283 284 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 282 def predicate? !!predicate end |
#quoted? ⇒ Boolean
Quoted triples are now deprecated
223 224 225 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 223 def quoted? !!@options[:quoted] end |
#reified(subject: nil, id: nil, graph_name: nil) ⇒ RDF::Graph
Returns a graph containing this statement in reified form.
499 500 501 502 503 504 505 506 507 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 499 def reified(subject: nil, id: nil, graph_name: nil) RDF::Graph.new(graph_name: graph_name) do |graph| subject = subject || RDF::Node.new(id) graph << [subject, RDF.type, RDF[:Statement]] graph << [subject, RDF.subject, self.subject] graph << [subject, RDF.predicate, self.predicate] graph << [subject, RDF.object, self.object] end end |
#statement? ⇒ Boolean #statement?(statement) ⇒ Boolean
152 153 154 155 156 157 158 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 152 def statement?(*args) case args.length when 0 then true when 1 then self == args.first || subject.statement?(*args) || object.statement?(*args) else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") end end |
#subject? ⇒ Boolean Also known as: has_subject?
275 276 277 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 275 def subject? !!subject end |
#terms ⇒ Array(RDF::Term)
Returns an array of all the non-nil non-statement terms.
418 419 420 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 418 def terms to_quad.map {|t| t.respond_to?(:terms) ? t.terms : t}.flatten.compact end |
#to_h(subject_key = :subject, predicate_key = :predicate, object_key = :object, graph_key = :graph_name) ⇒ Hash{Symbol => RDF::Term}
Returns the terms of this statement as a Hash
.
468 469 470 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 468 def to_h(subject_key = :subject, predicate_key = :predicate, object_key = :object, graph_key = :graph_name) {subject_key => subject, predicate_key => predicate, object_key => object, graph_key => graph_name} end |
#to_quad ⇒ Array(RDF::Term)
404 405 406 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 404 def to_quad [subject, predicate, object, graph_name] end |
#to_s ⇒ String
Returns a string representation of this statement.
476 477 478 479 480 481 482 483 484 485 486 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 476 def to_s (graph_name ? to_quad : to_triple).map do |term| if term.is_a?(Statement) "<<(#{term.to_s[0..-3]})>>" elsif term.respond_to?(:to_base) term.to_base else term.inspect end end.join(" ") + " ." end |
#to_sparql(**options) ⇒ String
Returns a partial SPARQL grammar for this term.
382 383 384 385 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/extensions.rb', line 382 def to_sparql(**) str = to_triple.map {|term| term.to_sparql(**)}.join(" ") quoted? ? ('<<' + str + '>>') : str end |
#to_sxp(prefixes: nil, base_uri: nil) ⇒ String
Returns an S-Expression (SXP) representation
373 374 375 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/extensions.rb', line 373 def to_sxp(prefixes: nil, base_uri: nil) to_sxp_bin.to_sxp(prefixes: prefixes, base_uri: base_uri) end |
#to_sxp_bin ⇒ Array
Transform Statement Pattern into an SXP
357 358 359 360 361 362 363 364 365 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/sparql-36baa432eb7f/lib/sparql/algebra/extensions.rb', line 357 def to_sxp_bin [ (has_graph? ? :quad : (quoted? ? :qtriple : :triple)), (:inferred if inferred?), subject, predicate, object, graph_name ].compact.map(&:to_sxp_bin) end |
#to_triple ⇒ Array(RDF::Term) Also known as: to_a
410 411 412 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 410 def to_triple [subject, predicate, object] end |
#tripleTerm? ⇒ Boolean
216 217 218 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 216 def tripleTerm? !!@options[:tripleTerm] end |
#valid? ⇒ Boolean
Refines valid?
to allow literal subjects and BNode predicates.
201 202 203 204 205 206 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 201 def valid? subject? && subject.resource? && subject.valid? && predicate? && predicate.uri? && predicate.valid? && object? && object.term? && object.valid? && (graph? ? (graph_name.resource? && graph_name.valid?) : true) end |
#valid_extended? ⇒ Boolean
Validate extended RDF
13 14 15 16 17 18 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/json-ld-f1de87658826/lib/json/ld/extensions.rb', line 13 def valid_extended? subject? && subject.resource? && subject.valid_extended? && predicate? && predicate.resource? && predicate.valid_extended? && object? && object.term? && object.valid_extended? && (graph? ? (graph_name.resource? && graph_name.valid_extended?) : true) end |
#validate! ⇒ RDF::Value
Refines validate!
to allow literal subjects and BNode predicates.
14 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-n3-a6ef81a7e1ce/lib/rdf/n3/refinements.rb', line 14 def validate!; end |
#variable? ⇒ Object #variable?(variables) ⇒ Boolean
URI, Node or Literal.
@return [Boolean]
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'vendor/bundler/ruby/3.3.0/bundler/gems/rdf-cfdeb1f47544/lib/rdf/model/statement.rb', line 171 def variable?(*args) case args.length when 0 !(subject? && subject.constant? && predicate? && predicate.constant? && object? && object.constant? && (graph? ? graph_name.constant? : true)) when 1 to_quad.any? {|t| t.respond_to?(:variable?) && t.variable?(*args)} else raise ArgumentError("wrong number of arguments (given #{args.length}, expected 0 or 1)") end end |