Class: RDF::Statement

Inherits:
Object show all
Includes:
Value
Defined in:
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/extensions.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/rdf-reasoner-dc33ae391715/lib/rdf/reasoner/extensions.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/json-ld-94ee7791d19f/lib/json/ld/extensions.rb,
vendor/bundler/ruby/2.6.0/bundler/gems/sparql-2d2905435bf8/lib/sparql/algebra/extensions.rb

Overview

An RDF statement.

Examples:

Creating an RDF statement

s = RDF::URI.new("https://rubygems.org/gems/rdf")
p = RDF::Vocab::DC.creator
o = RDF::URI.new("http://ar.to/#self")
RDF::Statement(s, p, o)

Creating an RDF statement with a graph_name

uri = RDF::URI("http://example/")
RDF::Statement(s, p, o, graph_name: uri)

Creating an RDF statement from a Hash

RDF::Statement({
  subject:   RDF::URI.new("https://rubygems.org/gems/rdf"),
  predicate: RDF::Vocab::DC.creator,
  object:    RDF::URI.new("http://ar.to/#self"),
})

Creating an RDF statement with interned nodes

RDF::Statement(:s, p, :o)

Creating an RDF statement with a string

RDF::Statement(s, p, "o")

Direct Known Subclasses

Query::Pattern

Constant Summary collapse

@@entailments =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Value

#anonymous?, #constant?, #graph?, #inspect, #inspect!, #iri?, #list?, #literal?, #resource?, #start_with?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!

Constructor Details

#initialize(**options) ⇒ RDF::Statement #initialize(subject, predicate, object, **options) ⇒ RDF::Statement

Returns a new instance of Statement

Overloads:

  • #initialize(**options) ⇒ RDF::Statement

    Parameters:

    Options Hash (**options):

    • :subject (RDF::Term) — default: nil

      A symbol is converted to an interned Node.

    • :predicate (RDF::URI) — default: nil
    • :object (RDF::Resource) — default: nil

      if not a Resource, it is coerced to Literal or Node depending on if it is a symbol or something other than a Term.

    • :graph_name (RDF::Term) — default: nil

      Note, in RDF 1.1, a graph name MUST be an Resource.

    • :inferred (Boolean)

      used as a marker to record that this statement was inferred based on semantic relationships (T-Box).

  • #initialize(subject, predicate, object, **options) ⇒ RDF::Statement

    Parameters:

    Options Hash (**options):

    • :graph_name (RDF::Term) — default: nil

      Note, in RDF 1.1, a graph name MUST be an Resource.

    • :inferred (Boolean)

      used as a marker to record that this statement was inferred based on semantic relationships (T-Box).



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 87

def initialize(subject = nil, predicate = nil, object = nil, options = {})
  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   = !options.empty? ? Hash[options] : {}
    @subject   = subject
    @predicate = predicate
    @object    = object
  end
  @id          = @options.delete(:id) if @options.has_key?(:id)
  @graph_name  = @options.delete(:graph_name)
  initialize!
end

Instance Attribute Details

#graph_nameRDF::Resource

Returns:



49
50
51
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 49

def graph_name
  @graph_name
end

#idObject

Returns:



46
47
48
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 46

def id
  @id
end

#objectRDF::Term

Returns:



58
59
60
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 58

def object
  @object
end

#optionsHash{Symbol => Object}

Returns:



61
62
63
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 61

def options
  @options
end

#predicateRDF::URI

Returns:



55
56
57
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 55

def predicate
  @predicate
end

#subjectRDF::Resource

Returns:



52
53
54
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/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

Parameters:



123
124
125
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-reasoner-dc33ae391715/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.

Parameters:

Returns:

  • (Boolean)

See Also:



265
266
267
268
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 265

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.

Examples:

statement = RDF::Statement.new(RDF::URI('s'), RDF::URI('p'), RDF::URI('o'))
pattern   = RDF::Statement.new(RDF::URI('s'), RDF::URI('p'), RDF::Query::Variable.new)

# true
statement === statement
pattern   === statement
RDF::Statement.new(nil, nil, nil) === statement

# false
statement === pattern
statement === RDF::Statement.new(nil, nil, nil)

Parameters:

Returns:

  • (Boolean)

See Also:



297
298
299
300
301
302
303
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 297

def ===(other)
  return false if has_object?    && !object.eql?(other.object)
  return false if has_predicate? && !predicate.eql?(other.predicate)
  return false if has_subject?   && !subject.eql?(other.subject)
  return false if has_graph?     && !graph_name.eql?(other.graph_name)
  return true
end

#[](index) ⇒ RDF::Term

Parameters:

Returns:



308
309
310
311
312
313
314
315
316
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 308

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

Parameters:

Returns:



322
323
324
325
326
327
328
329
330
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 322

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

Returns:

  • (Boolean)


166
167
168
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 166

def asserted?
  !quoted?
end

#canonicalizeRDF::Statement

Returns a version of the statement with each position in canonical form

Returns:

  • (RDF::Statement)

    self or nil if statement cannot be canonicalized

Since:

  • 1.0.8



366
367
368
369
370
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 366

def canonicalize
  self.dup.canonicalize!
rescue ArgumentError
  nil
end

#canonicalize!RDF::Statement

Canonicalizes each unfrozen term in the statement

Returns:

Raises:

  • (ArgumentError)

    if any element cannot be canonicalized.

Since:

  • 1.0.8



351
352
353
354
355
356
357
358
359
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 351

def canonicalize!
  self.subject.canonicalize!    if has_subject? && !self.subject.frozen?
  self.predicate.canonicalize!  if has_predicate? && !self.predicate.frozen?
  self.object.canonicalize!     if has_object? && !self.object.frozen?
  self.graph_name.canonicalize! if has_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.

Returns:

  • (Boolean)

Since:

  • 3.0



196
197
198
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 196

def complete?
  !incomplete?
end

#entail(method) {|term| ... } ⇒ Array<Term>

Perform an entailment on this term.

Parameters:

  • method (Symbol)

    A registered entailment method

Yields:

  • term

Yield Parameters:

Returns:



135
136
137
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-reasoner-dc33ae391715/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.

Parameters:

Returns:

  • (Boolean)

See Also:



245
246
247
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 245

def eql?(other)
  other.is_a?(Statement) && self == other && (self.graph_name || false) == (other.graph_name || false)
end

#has_graph?Boolean Also known as: has_name?

Returns:

  • (Boolean)


202
203
204
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 202

def has_graph?
  !!graph_name
end

#has_object?Boolean

Returns:

  • (Boolean)


221
222
223
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 221

def has_object?
  !!object
end

#has_predicate?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 215

def has_predicate?
  !!predicate
end

#has_subject?Boolean

Returns:

  • (Boolean)


209
210
211
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 209

def has_subject?
  !!subject
end

#hashObject

Generates a Integer hash value as a quad.



251
252
253
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 251

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.

Returns:

  • (Boolean)

Since:

  • 3.0



187
188
189
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 187

def incomplete?
  to_triple.any?(&:nil?)
end

#inferred?Boolean

Returns:

  • (Boolean)


178
179
180
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 178

def inferred?
  !!@options[:inferred]
end

#invalid?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 151

def invalid?
  !valid?
end

#node?Boolean Also known as: has_blank_nodes?

Returns true if any resource of this statement is a blank node.

Returns:

  • (Boolean)

Since:

  • 2.0



230
231
232
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 230

def node?
  to_quad.compact.any?(&:node?)
end

#quoted?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 172

def quoted?
  false
end

#reified(subject: nil, id: nil, graph_name: nil) ⇒ RDF::Graph

Returns a graph containing this statement in reified form.

Parameters:

  • subject (RDF::Term)

    (nil) Subject of reification.

  • id (RDF::Term)

    (nil) Node identifier, when subject is anonymous

  • graph_name (RDF::Term)

    (nil) Note, in RDF 1.1, a graph name MUST be an Resource.

Returns:

See Also:



404
405
406
407
408
409
410
411
412
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 404

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

Returns true to indicate that this value is a statement.

Returns:

  • (Boolean)


133
134
135
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 133

def statement?
  true
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.

Parameters:

  • subject_key (Symbol) (defaults to: :subject)
  • predicate_key (Symbol) (defaults to: :predicate)
  • object_key (Symbol) (defaults to: :object)

Returns:



379
380
381
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 379

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_quadArray(RDF::Term)

Returns:



334
335
336
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 334

def to_quad
  [subject, predicate, object, graph_name]
end

#to_sString

Returns a string representation of this statement.

Returns:



387
388
389
390
391
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 387

def to_s
  (graph_name ? to_quad : to_triple).map do |term|
    term.respond_to?(:to_base) ? term.to_base : term.inspect
  end.join(" ") + " ."
end

#to_sxpString

Returns an S-Expression (SXP) representation

Returns:



41
42
43
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/extensions.rb', line 41

def to_sxp
  to_sxp_bin.to_sxp
end

#to_sxp_binArray

Transform Statement Pattern into an SXP

Returns:



33
34
35
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/extensions.rb', line 33

def to_sxp_bin
  [(variable? ? :pattern : :triple), subject, predicate, object, graph_name].compact
end

#to_tripleArray(RDF::Term) Also known as: to_a

Returns:



340
341
342
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 340

def to_triple
  [subject, predicate, object]
end

#valid?Boolean

Returns:

  • (Boolean)


157
158
159
160
161
162
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-7506695c9b4f/lib/rdf/model/statement.rb', line 157

def valid?
  has_subject?    && subject.resource? && subject.valid? &&
  has_predicate?  && predicate.uri? && predicate.valid? &&
  has_object?     && object.term? && object.valid? &&
  (has_graph?      ? (graph_name.resource? && graph_name.valid?) : true)
end

#valid_extended?Boolean

Validate extended RDF

Returns:

  • (Boolean)


13
14
15
16
17
18
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/json-ld-94ee7791d19f/lib/json/ld/extensions.rb', line 13

def valid_extended?
  has_subject?    && subject.resource? && subject.valid_extended? &&
  has_predicate?  && predicate.resource? && predicate.valid_extended? &&
  has_object?     && object.term? && object.valid_extended? &&
  (has_graph?      ? (graph_name.resource? && graph_name.valid_extended?) : true)
end

#variable?Boolean

Returns true if any element of the statement is not a URI, Node or Literal.

Returns:

  • (Boolean)


27
28
29
# File 'vendor/bundler/ruby/2.6.0/bundler/gems/rdf-n3-015ce184efe3/lib/rdf/n3/extensions.rb', line 27

def variable?
  to_a.any? {|term| !term.is_a?(RDF::Term) || term.variable?} || graph_name && graph_name.variable?
end