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.new(s, p, o)

Creating an RDF statement with a context

RDF::Statement.new(s, p, o, :context => uri)

Creating an RDF statement from a ‘Hash`

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

Direct Known Subclasses

Query::Pattern

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Value

#graph?, #inspect, #inspect!, #iri?, #literal?, #node?, #resource?, #to_ntriples, #to_rdf, #type_error, #uri?, #variable?

Constructor Details

#initialize(options = {}) ⇒ Statement #initialize(subject, predicate, object, options = {}) ⇒ Statement

Returns a new instance of Statement.

Overloads:

  • #initialize(options = {}) ⇒ Statement

    Parameters:

    • options (Hash{Symbol => Object}) (defaults to: {})

    Options Hash (options):

  • #initialize(subject, predicate, object, options = {}) ⇒ Statement

    Parameters:

    Options Hash (options):



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rdf/model/statement.rb', line 67

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

#contextRDF::Resource

Returns:



42
43
44
# File 'lib/rdf/model/statement.rb', line 42

def context
  @context
end

#idObject

Returns:

  • (Object)


39
40
41
# File 'lib/rdf/model/statement.rb', line 39

def id
  @id
end

#objectRDF::Term

Returns:



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

def object
  @object
end

#predicateRDF::URI

Returns:



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

def predicate
  @predicate
end

#subjectRDF::Resource

Returns:



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

def subject
  @subject
end

Class Method Details

.from(statement, options = {}) ⇒ Object

Since:

  • 0.2.2



27
28
29
30
31
32
33
34
35
36
# File 'lib/rdf/model/statement.rb', line 27

def self.from(statement, options = {})
  case statement
    when Array, Query::Pattern
      context = statement[3] == false ? nil : statement[3]
      self.new(statement[0], statement[1], statement[2], options.merge(:context => context))
    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

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • other (Object)

Returns:

  • (Boolean)


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

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

#===(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


193
194
195
196
197
198
199
# File 'lib/rdf/model/statement.rb', line 193

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

#[](index) ⇒ RDF::Term

Parameters:

  • index (Integer)

Returns:



204
205
206
207
208
209
210
211
212
# File 'lib/rdf/model/statement.rb', line 204

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

#[]=(index, value) ⇒ RDF::Term

Parameters:

Returns:



218
219
220
221
222
223
224
225
226
# File 'lib/rdf/model/statement.rb', line 218

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

#asserted?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/rdf/model/statement.rb', line 121

def asserted?
  !quoted?
end

#eql?(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


179
180
181
# File 'lib/rdf/model/statement.rb', line 179

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

#has_blank_nodes?Boolean

Returns ‘true` if the subject or object of this statement is a blank node.

Returns:

  • (Boolean)


172
173
174
# File 'lib/rdf/model/statement.rb', line 172

def has_blank_nodes?
  (has_object? && object.node?) || (has_subject? && subject.node?)
end

#has_context?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/rdf/model/statement.rb', line 145

def has_context?
  !!context
end

#has_graph?Boolean

Returns:

  • (Boolean)


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

def has_graph?
  has_context?
end

#has_object?Boolean

Returns:

  • (Boolean)


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

def has_object?
  !!object
end

#has_predicate?Boolean

Returns:

  • (Boolean)


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

def has_predicate?
  !!predicate
end

#has_subject?Boolean

Returns:

  • (Boolean)


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

def has_subject?
  !!subject
end

#inferred?Boolean

Returns:

  • (Boolean)


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

def inferred?
  false
end

#initialize!Object



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

def initialize!
  @context   = Node.intern(@context)   if @context.is_a?(Symbol)
  @subject   = Node.intern(@subject)   if @subject.is_a?(Symbol)
  @predicate = Node.intern(@predicate) if @predicate.is_a?(Symbol)
  @object    = case @object
    when nil    then nil
    when Symbol then Node.intern(@object)
    when Term   then @object
    else Literal.new(@object)
  end
end

#invalid?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/rdf/model/statement.rb', line 109

def invalid?
  !valid?
end

#quoted?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/rdf/model/statement.rb', line 127

def quoted?
  false
end

#reified(options = {}) ⇒ RDF::Graph

Returns a graph containing this statement in reified form.

Parameters:

  • options (Hash{Symbol => Object}) (defaults to: {})

Returns:

See Also:



286
287
288
289
290
291
292
293
294
# File 'lib/rdf/model/statement.rb', line 286

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

#statement?Boolean

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

Returns:

  • (Boolean)


103
104
105
# File 'lib/rdf/model/statement.rb', line 103

def statement?
  true
end

#to_hash(subject_key = :subject, predicate_key = :predicate, object_key = :object, context_key = :context) ⇒ 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:



250
251
252
# File 'lib/rdf/model/statement.rb', line 250

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

#to_quadArray(RDF::Term)

Returns:



230
231
232
# File 'lib/rdf/model/statement.rb', line 230

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

#to_sString

Returns a string representation of this statement.

Returns:

  • (String)


258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/rdf/model/statement.rb', line 258

def to_s
  StringIO.open do |buffer|
    buffer << case subject
      when RDF::Node    then subject.to_s
      when RDF::URI     then "<#{subject}>"
      else subject.inspect
    end
    buffer << " <#{predicate}> "
    buffer << case object
      when RDF::Literal then object.to_s
      when RDF::Node    then object.to_s
      when RDF::URI     then "<#{object}>"
      else object.inspect
    end
    buffer << case context
      when nil then " ."
      else " <#{context}> ."
    end
    buffer.string
  end
end

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

Returns:



236
237
238
# File 'lib/rdf/model/statement.rb', line 236

def to_triple
  [subject, predicate, object]
end

#valid?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/rdf/model/statement.rb', line 115

def valid?
  has_subject? && has_predicate? && has_object?
end