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::DC.creator
o = RDF::URI.new("http://ar.to/#self")
RDF::Statement(s, p, o)

Creating an RDF statement with a context

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

Creating an RDF statement from a Hash

RDF::Statement({
  :subject   => RDF::URI.new("http://rubygems.org/gems/rdf"),
  :predicate => RDF::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?, #node?, #resource?, #term?, #to_nquads, #to_ntriples, #to_rdf, #to_term, #type_error, #uri?, #validate!

Constructor Details

- (RDF::Statement) initialize(options = {}) - (RDF::Statement) initialize(subject, predicate, object, options = {})

Returns a new instance of Statement

Overloads:

  • - (RDF::Statement) initialize(options = {})

    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.

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

      Note, in RDF 1.1, a context MUST be an Resource.

  • - (RDF::Statement) initialize(subject, predicate, object, options = {})

    Options Hash (options):



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

def initialize(subject = nil, predicate = nil, object = nil, options = {})
  case subject
    when Hash
      @options   = subject.dup
      @subject   = @options.delete(:subject)
      @predicate = @options.delete(:predicate)
      @object    = @options.delete(:object)
    else
      @options   = !options.empty? ? options.dup : {}
      @subject   = subject
      @predicate = predicate
      @object    = object
  end
  @id      = @options.delete(:id) if @options.has_key?(:id)
  @context = @options.delete(:context)
  initialize!
end

Instance Attribute Details

- (RDF::Resource) context



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

def context
  @context
end

- (Object) id



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

def id
  @id
end

- (RDF::Term) object



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

def object
  @object
end

- (RDF::URI) predicate



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

def predicate
  @predicate
end

- (RDF::Resource) subject



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

def subject
  @subject
end

Instance Method Details

- (Boolean) ==(other)



221
222
223
# File 'lib/rdf/model/statement.rb', line 221

def ==(other)
  to_a == other.to_a
end

- (Boolean) ===(other)



228
229
230
231
232
233
234
# File 'lib/rdf/model/statement.rb', line 228

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

- (RDF::Term) [](index)



239
240
241
242
243
244
245
246
247
# File 'lib/rdf/model/statement.rb', line 239

def [](index)
  case index
    when 0 then self.subject
    when 1 then self.predicate
    when 2 then self.object
    when 3 then self.context
    else nil
  end
end

- (RDF::Term) []=(index, value)



253
254
255
256
257
258
259
260
261
# File 'lib/rdf/model/statement.rb', line 253

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.context   = value
    else nil
  end
end

- (Boolean) asserted?



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

def asserted?
  !quoted?
end

- (RDF::Statement) canonicalize

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

Since:

  • 1.0.8



298
299
300
301
302
# File 'lib/rdf/model/statement.rb', line 298

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

- (RDF::Statement) canonicalize!

Canonicalizes each unfrozen term in the statement

Raises:

  • (ArgumentError)

    if any element cannot be canonicalized.

Since:

  • 1.0.8



284
285
286
287
288
289
290
291
# File 'lib/rdf/model/statement.rb', line 284

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.context.canonicalize!    if has_context? && !self.context.frozen?
  self.validate!
  self
end

- (Boolean) eql?(other)



214
215
216
# File 'lib/rdf/model/statement.rb', line 214

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

- (Boolean) has_blank_nodes?

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



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

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

- (Boolean) has_context?



181
182
183
# File 'lib/rdf/model/statement.rb', line 181

def has_context?
  !!context
end

- (Boolean) has_graph?



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

def has_graph?
  has_context?
end

- (Boolean) has_object?



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

def has_object?
  !!object
end

- (Boolean) has_predicate?



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

def has_predicate?
  !!predicate
end

- (Boolean) has_subject?



187
188
189
# File 'lib/rdf/model/statement.rb', line 187

def has_subject?
  !!subject
end

- (Boolean) inferred?



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

def inferred?
  false
end

- (Boolean) invalid?



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

def invalid?
  !valid?
end

- (Boolean) quoted?



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

def quoted?
  false
end

- (RDF::Graph) reified(options = {})

Returns a graph containing this statement in reified form.



331
332
333
334
335
336
337
338
339
# File 'lib/rdf/model/statement.rb', line 331

def reified(options = {})
  RDF::Graph.new(options[:context]) 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

- (Boolean) statement?

Returns true to indicate that this value is a statement.



124
125
126
# File 'lib/rdf/model/statement.rb', line 124

def statement?
  true
end

- (Hash{Symbol => RDF::Term}) to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, context_key = :context)

Returns the terms of this statement as a Hash.



311
312
313
# File 'lib/rdf/model/statement.rb', line 311

def to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, context_key = :context)
  {subject_key => subject, predicate_key => predicate, object_key => object, context_key => context}
end

- (Array(RDF::Term)) to_quad



265
266
267
# File 'lib/rdf/model/statement.rb', line 265

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

- (String) to_s

Returns a string representation of this statement.



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

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

- (Array(RDF::Term)) to_triple Also known as: to_a, to_ary



271
272
273
# File 'lib/rdf/model/statement.rb', line 271

def to_triple
  [subject, predicate, object]
end

- (Boolean) valid?



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

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

- (Boolean) variable?

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



133
134
135
136
137
138
# File 'lib/rdf/model/statement.rb', line 133

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