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)

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, #type_error, #uri?, #validate!

Constructor Details

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

A new instance of Statement

Overloads:

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

    Parameters:

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

    Options Hash (options):

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

    Parameters:

    Options Hash (options):



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

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

Returns:



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

def context
  @context
end

- (Object) id

Returns:

  • (Object)


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

def id
  @id
end

- (RDF::Term) object

Returns:



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

def object
  @object
end

- (RDF::URI) predicate

Returns:



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

def predicate
  @predicate
end

- (RDF::Resource) subject

Returns:



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

def subject
  @subject
end

Instance Method Details

- (Boolean) ==(other)

Parameters:

  • other (Object)

Returns:

  • (Boolean)


202
203
204
# File 'lib/rdf/model/statement.rb', line 202

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

- (Boolean) ===(other)

Parameters:

Returns:

  • (Boolean)


209
210
211
212
213
214
215
# File 'lib/rdf/model/statement.rb', line 209

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)

Parameters:

  • index (Integer)

Returns:



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

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)

Parameters:

Returns:



234
235
236
237
238
239
240
241
242
# File 'lib/rdf/model/statement.rb', line 234

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?

Returns:

  • (Boolean)


137
138
139
# File 'lib/rdf/model/statement.rb', line 137

def asserted?
  !quoted?
end

- (Boolean) eql?(other)

Parameters:

Returns:

  • (Boolean)


195
196
197
# File 'lib/rdf/model/statement.rb', line 195

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

- (Boolean) has_blank_nodes?

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

Returns:

  • (Boolean)


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

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

- (Boolean) has_context?

Returns:

  • (Boolean)


161
162
163
# File 'lib/rdf/model/statement.rb', line 161

def has_context?
  !!context
end

- (Boolean) has_graph?

Returns:

  • (Boolean)


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

def has_graph?
  has_context?
end

- (Boolean) has_object?

Returns:

  • (Boolean)


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

def has_object?
  !!object
end

- (Boolean) has_predicate?

Returns:

  • (Boolean)


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

def has_predicate?
  !!predicate
end

- (Boolean) has_subject?

Returns:

  • (Boolean)


167
168
169
# File 'lib/rdf/model/statement.rb', line 167

def has_subject?
  !!subject
end

- (Boolean) inferred?

Returns:

  • (Boolean)


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

def inferred?
  false
end

- (Boolean) invalid?

Returns:

  • (Boolean)


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

def invalid?
  !valid?
end

- (Boolean) quoted?

Returns:

  • (Boolean)


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

def quoted?
  false
end

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

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

- (Boolean) statement?

Returns true to indicate that this value is a statement.

Returns:

  • (Boolean)


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

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.

Parameters:

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

Returns:



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

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

Returns:



246
247
248
# File 'lib/rdf/model/statement.rb', line 246

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

- (String) to_s

Returns a string representation of this statement.

Returns:

  • (String)


274
275
276
277
278
# File 'lib/rdf/model/statement.rb', line 274

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

Returns:



252
253
254
# File 'lib/rdf/model/statement.rb', line 252

def to_triple
  [subject, predicate, object]
end

- (Boolean) valid?

Returns:

  • (Boolean)


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

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

- (Boolean) variable?

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

Returns:

  • (Boolean)


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

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