Class: RDF::Statement

Inherits:
Object
  • Object
show all
Includes:
Value
Defined in:
lib/rdf/model/statement.rb

Overview

An RDF statement.

Examples:

Creating an RDF statement

s = RDF::URI.new("http://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("http://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

Instance Attribute 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

    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

    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).



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/rdf/model/statement.rb', line 84

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#see #to_h (protected)

Returns the terms of this statement as a Hash.



426
427
428
429
430
431
432
433
434
435
436
437
# File 'lib/rdf/model/statement.rb', line 426

def method_missing(meth, *args)
  case meth
  when :to_hash
    warn "[DEPRECATION] RDF::Statement#to_hash is deprecated, use RDF::Statement#to_h instead.\n" +
         "This is due to the introduction of keyword arugments that attempt to turn the last argument into a hash using #to_hash.\n" +
         "This can be avoided by explicitly passing an options hash as the last argument.\n" +
         "Called from #{Gem.location_of_caller.join(':')}"
    self.to_h
  else
    super
  end
end

Instance Attribute Details

#graph_nameRDF::Resource



49
50
51
# File 'lib/rdf/model/statement.rb', line 49

def graph_name
  @graph_name
end

#idObject



46
47
48
# File 'lib/rdf/model/statement.rb', line 46

def id
  @id
end

#objectRDF::Term



58
59
60
# File 'lib/rdf/model/statement.rb', line 58

def object
  @object
end

#predicateRDF::URI



55
56
57
# File 'lib/rdf/model/statement.rb', line 55

def predicate
  @predicate
end

#subjectRDF::Resource



52
53
54
# File 'lib/rdf/model/statement.rb', line 52

def subject
  @subject
end

Instance Method Details

#==(other) ⇒ Boolean

Checks statement equality as a triple.



262
263
264
265
# File 'lib/rdf/model/statement.rb', line 262

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)

See Also:



294
295
296
297
298
299
300
# File 'lib/rdf/model/statement.rb', line 294

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



305
306
307
308
309
310
311
312
313
# File 'lib/rdf/model/statement.rb', line 305

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



319
320
321
322
323
324
325
326
327
# File 'lib/rdf/model/statement.rb', line 319

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



163
164
165
# File 'lib/rdf/model/statement.rb', line 163

def asserted?
  !quoted?
end

#canonicalizeRDF::Statement

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

Since:

  • 1.0.8



370
371
372
373
374
# File 'lib/rdf/model/statement.rb', line 370

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

#canonicalize!RDF::Statement

Canonicalizes each unfrozen term in the statement

Raises:

  • (ArgumentError)

    if any element cannot be canonicalized.

Since:

  • 1.0.8



355
356
357
358
359
360
361
362
363
# File 'lib/rdf/model/statement.rb', line 355

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.

Since:

  • 3.0



193
194
195
# File 'lib/rdf/model/statement.rb', line 193

def complete?
  !incomplete?
end

#eql?(other) ⇒ Boolean

Checks statement equality as a quad.



242
243
244
# File 'lib/rdf/model/statement.rb', line 242

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?



199
200
201
# File 'lib/rdf/model/statement.rb', line 199

def has_graph?
  !!graph_name
end

#has_object?Boolean



218
219
220
# File 'lib/rdf/model/statement.rb', line 218

def has_object?
  !!object
end

#has_predicate?Boolean



212
213
214
# File 'lib/rdf/model/statement.rb', line 212

def has_predicate?
  !!predicate
end

#has_subject?Boolean



206
207
208
# File 'lib/rdf/model/statement.rb', line 206

def has_subject?
  !!subject
end

#hashObject

Generates a Integer hash value as a quad.



248
249
250
# File 'lib/rdf/model/statement.rb', line 248

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.

Since:

  • 3.0



184
185
186
# File 'lib/rdf/model/statement.rb', line 184

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

#inferred?Boolean



175
176
177
# File 'lib/rdf/model/statement.rb', line 175

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

#invalid?Boolean



148
149
150
# File 'lib/rdf/model/statement.rb', line 148

def invalid?
  !valid?
end

#node?Boolean Also known as: has_blank_nodes?

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

Since:

  • 2.0



227
228
229
# File 'lib/rdf/model/statement.rb', line 227

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

#quoted?Boolean



169
170
171
# File 'lib/rdf/model/statement.rb', line 169

def quoted?
  false
end

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

Returns a graph containing this statement in reified form.



408
409
410
411
412
413
414
415
416
# File 'lib/rdf/model/statement.rb', line 408

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.



130
131
132
# File 'lib/rdf/model/statement.rb', line 130

def statement?
  true
end

#to_aryObject

Deprecated.

use #to_a or #to_triple instead

See Also:



345
346
347
# File 'lib/rdf/model/statement.rb', line 345

def to_ary
  to_triple
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.



383
384
385
# File 'lib/rdf/model/statement.rb', line 383

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)



331
332
333
# File 'lib/rdf/model/statement.rb', line 331

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

#to_sString

Returns a string representation of this statement.



391
392
393
394
395
# File 'lib/rdf/model/statement.rb', line 391

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_tripleArray(RDF::Term) Also known as: to_a



337
338
339
# File 'lib/rdf/model/statement.rb', line 337

def to_triple
  [subject, predicate, object]
end

#valid?Boolean



154
155
156
157
158
159
# File 'lib/rdf/model/statement.rb', line 154

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

#variable?Boolean

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



139
140
141
142
143
144
# File 'lib/rdf/model/statement.rb', line 139

def variable?
  !(has_subject?    && subject.resource? &&
    has_predicate?  && predicate.resource? &&
    has_object?     && (object.resource? || object.literal?) &&
    (has_graph?     ? graph_name.resource? : true))
end