class RDF::Statement

An RDF statement.

@example 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)

@example Creating an RDF statement with a #graph_name

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

@example 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"),
})

@example Creating an RDF statement with interned nodes

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

@example Creating an RDF statement with a string

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

Attributes

graph_name[RW]

@return [RDF::Resource]

id[RW]

@return [Object]

object[RW]

@return [RDF::Term]

predicate[RW]

@return [RDF::URI]

subject[RW]

@return [RDF::Resource]

Public Class Methods

from(statement, options = {}) click to toggle source

@private @since 0.2.2

# File lib/rdf/model/statement.rb, line 34
def self.from(statement, options = {})
  case statement
    when Array, Query::Pattern
      graph_name = statement[3] == false ? nil : statement[3]
      self.new(statement[0], statement[1], statement[2], options.merge(graph_name: graph_name))
    when Statement then statement
    when Hash      then self.new(options.merge(statement))
    else raise ArgumentError, "expected RDF::Statement, Hash, or Array, but got #{statement.inspect}"
  end
end
new(subject = nil, predicate = nil, object = nil, options = {}) click to toggle source

@overload initialize(options = {})

@param  [Hash{Symbol => Object}] options
@option options [RDF::Term]  :subject   (nil)
  A symbol is converted to an interned {Node}.
@option options [RDF::URI]       :predicate (nil)
@option options [RDF::Resource]      :object    (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}.
@option options [RDF::Term]  :graph_name   (nil)
  Note, in RDF 1.1, a graph name MUST be an {Resource}.
@return [RDF::Statement]

@overload initialize(subject, predicate, object, options = {})

@param  [RDF::Term]          subject
  A symbol is converted to an interned {Node}.
@param  [RDF::URI]               predicate
@param  [RDF::Resource]              object
  if not a {Resource}, it is coerced to {Literal} or {Node} depending on if it is a symbol or something other than a {Term}.
@param  [Hash{Symbol => Object}] options
@option options [RDF::Term]  :graph_name   (nil)
  Note, in RDF 1.1, a graph name MUST be an {Resource}.
@return [RDF::Statement]
# File lib/rdf/model/statement.rb, line 82
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

Public Instance Methods

==(other) click to toggle source

Checks statement equality as a triple.

@param [Object] other @return [Boolean]

@see RDF::URI#== @see RDF::Node#== @see RDF::Literal#== @see RDF::Query::Variable#==

# File lib/rdf/model/statement.rb, line 254
def ==(other)
  to_a == Array(other) &&
    !(other.is_a?(RDF::Value) && other.list?)
end
===(other) click to toggle source

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.

@example

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)

@param [Statement] other @return [Boolean]

@see RDF::URI#eql? @see RDF::Node#eql? @see RDF::Literal#eql? @see RDF::Query::Variable#eql?

# File lib/rdf/model/statement.rb, line 286
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) click to toggle source

@param [Integer] index @return [RDF::Term]

# File lib/rdf/model/statement.rb, line 297
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) click to toggle source

@param [Integer] index @param [RDF::Term] value @return [RDF::Term]

# File lib/rdf/model/statement.rb, line 311
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?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 161
def asserted?
  !quoted?
end
canonicalize() click to toggle source

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

@return [RDF::Statement] `self` or nil if statement cannot be canonicalized @since 1.0.8

# File lib/rdf/model/statement.rb, line 356
def canonicalize
  self.dup.canonicalize!
rescue ArgumentError
  nil
end
canonicalize!() click to toggle source

Canonicalizes each unfrozen term in the statement

@return [RDF::Statement] `self` @since 1.0.8 @raise [ArgumentError] if any element cannot be canonicalized.

# File lib/rdf/model/statement.rb, line 342
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!
  self
end
complete?() click to toggle source

Determines if the statement is complete, vs. invalid. A complete statement is one in which none of `subject`, `predicate`, or `object`, are nil.

@return [Boolean] @since 3.0

# File lib/rdf/model/statement.rb, line 191
def complete?
  !incomplete?
end
eql?(other) click to toggle source

Checks statement equality as a quad.

@param [Statement] other @return [Boolean]

@see RDF::URI#== @see RDF::Node#== @see RDF::Literal#== @see RDF::Query::Variable#==

# File lib/rdf/model/statement.rb, line 240
def eql?(other)
  other.is_a?(Statement) && self == other && (self.graph_name || false) == (other.graph_name || false)
end
has_blank_nodes?()
Alias for: node?
has_graph?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 197
def has_graph?
  !!graph_name
end
Also aliased as: has_name?
has_name?()
Alias for: has_graph?
has_object?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 216
def has_object?
  !!object
end
has_predicate?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 210
def has_predicate?
  !!predicate
end
has_subject?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 204
def has_subject?
  !!subject
end
incomplete?() click to toggle source

Determines if the statement is incomplete, vs. invalid. An incomplete statement is one in which any of `subject`, `predicate`, or `object`, are nil.

@return [Boolean] @since 3.0

# File lib/rdf/model/statement.rb, line 182
def incomplete?
  to_triple.any?(&:nil?)
end
inferred?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 173
def inferred?
  false
end
initialize!() click to toggle source

@private

# File lib/rdf/model/statement.rb, line 101
def initialize!
  @graph_name   = Node.intern(@graph_name)   if @graph_name.is_a?(Symbol)
  @subject   = if @subject.is_a?(Value)
    @subject.to_term
  elsif @subject.is_a?(Symbol)
    Node.intern(@subject)
  elsif @subject.nil?
    nil
  else
    raise ArgumentError, "expected subject to be nil or a term, was #{@subject.inspect}"
  end
  @predicate = Node.intern(@predicate) if @predicate.is_a?(Symbol)
  @object    = if @object.is_a?(Value)
    @object.to_term
  elsif @object.is_a?(Symbol)
    Node.intern(@object)
  elsif @object.nil?
    nil
  else
    Literal.new(@object)
  end
end
invalid?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 146
def invalid?
  !valid?
end
node?() click to toggle source

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

@return [Boolean] @since 2.0

# File lib/rdf/model/statement.rb, line 225
def node?
  to_quad.compact.any?(&:node?)
end
Also aliased as: has_blank_nodes?
quoted?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 167
def quoted?
  false
end
reified(options = {}) click to toggle source

Returns a graph containing this statement in reified form.

@param [Hash{Symbol => Object}] options @return [RDF::Graph] @see www.w3.org/TR/rdf-primer/#reification

# File lib/rdf/model/statement.rb, line 389
def reified(options = {})
  RDF::Graph.new(graph_name: options[:graph_name]) do |graph|
    subject = options[:subject] || RDF::Node.new(options[: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?() click to toggle source

Returns `true` to indicate that this value is a statement.

@return [Boolean]

# File lib/rdf/model/statement.rb, line 128
def statement?
  true
end
to_a()
Alias for: to_triple
to_ary()
Alias for: to_triple
to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, graph_key = :graph_name) click to toggle source

Returns the terms of this statement as a `Hash`.

@param [Symbol] subject_key @param [Symbol] predicate_key @param [Symbol] object_key @return [Hash{Symbol => RDF::Term}]

# File lib/rdf/model/statement.rb, line 369
def to_hash(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() click to toggle source

@return [Array(RDF::Term)]

# File lib/rdf/model/statement.rb, line 323
def to_quad
  [subject, predicate, object, graph_name]
end
to_s() click to toggle source

Returns a string representation of this statement.

@return [String]

# File lib/rdf/model/statement.rb, line 377
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_triple() click to toggle source

@return [Array(RDF::Term)]

# File lib/rdf/model/statement.rb, line 329
def to_triple
  [subject, predicate, object]
end
Also aliased as: to_a, to_ary
valid?() click to toggle source

@return [Boolean]

# File lib/rdf/model/statement.rb, line 152
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?() click to toggle source

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

@return [Boolean]

# File lib/rdf/model/statement.rb, line 137
def variable?
  !(has_subject?    && subject.resource? &&
    has_predicate?  && predicate.resource? &&
    has_object?     && (object.resource? || object.literal?) &&
    (has_graph?     ? graph_name.resource? : true ))
end