Class: RDF::Statement
- Inherits:
-
Object
show all
- Includes:
- Value
- Defined in:
- lib/rdf/model/statement.rb
Overview
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#==(other) ⇒ Boolean
-
#===(other) ⇒ Boolean
-
#[](index) ⇒ RDF::Term
-
#[]=(index, value) ⇒ RDF::Term
-
#asserted? ⇒ Boolean
-
#canonicalize ⇒ RDF::Statement
Returns a version of the statement with each position in canonical form.
-
#canonicalize! ⇒ RDF::Statement
Canonicalizes each unfrozen term in the statement.
-
#complete? ⇒ Boolean
Determines if the statement is complete, vs.
-
#eql?(other) ⇒ Boolean
-
#has_graph? ⇒ Boolean
(also: #has_name?)
-
#has_object? ⇒ Boolean
-
#has_predicate? ⇒ Boolean
-
#has_subject? ⇒ Boolean
-
#incomplete? ⇒ Boolean
Determines if the statement is incomplete, vs.
-
#inferred? ⇒ Boolean
-
#initialize(subject = nil, predicate = nil, object = nil, options = {}) ⇒ Statement
constructor
A new instance of Statement.
-
#initialize! ⇒ Object
-
#invalid? ⇒ Boolean
-
#node? ⇒ Boolean
(also: #has_blank_nodes?)
Returns ‘true` if any resource of this statement is a blank node.
-
#quoted? ⇒ Boolean
-
#reified(options = {}) ⇒ RDF::Graph
Returns a graph containing this statement in reified form.
-
#statement? ⇒ Boolean
Returns ‘true` to indicate that this value is a statement.
-
#to_hash(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`.
-
#to_quad ⇒ Array(RDF::Term)
-
#to_s ⇒ String
Returns a string representation of this statement.
-
#to_triple ⇒ Array(RDF::Term)
(also: #to_a, #to_ary)
-
#valid? ⇒ Boolean
-
#variable? ⇒ Boolean
Returns ‘true` if any element of the statement is not a URI, Node or Literal.
Methods included from Value
#anonymous?, #constant?, #graph?, #inspect, #inspect!, #iri?, #list?, #literal?, #resource?, #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.
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# 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]
@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
|
Instance Attribute Details
49
50
51
|
# File 'lib/rdf/model/statement.rb', line 49
def graph_name
@graph_name
end
|
#id ⇒ Object
46
47
48
|
# File 'lib/rdf/model/statement.rb', line 46
def id
@id
end
|
58
59
60
|
# File 'lib/rdf/model/statement.rb', line 58
def object
@object
end
|
55
56
57
|
# File 'lib/rdf/model/statement.rb', line 55
def predicate
@predicate
end
|
52
53
54
|
# File 'lib/rdf/model/statement.rb', line 52
def subject
@subject
end
|
Class Method Details
.from(statement, options = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
|
# 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
|
Instance Method Details
#==(other) ⇒ Boolean
240
241
242
|
# File 'lib/rdf/model/statement.rb', line 240
def ==(other)
to_a == Array(other)
end
|
#===(other) ⇒ Boolean
247
248
249
250
251
252
253
|
# File 'lib/rdf/model/statement.rb', line 247
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
|
258
259
260
261
262
263
264
265
266
|
# File 'lib/rdf/model/statement.rb', line 258
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
272
273
274
275
276
277
278
279
280
|
# File 'lib/rdf/model/statement.rb', line 272
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
161
162
163
|
# File 'lib/rdf/model/statement.rb', line 161
def asserted?
!quoted?
end
|
Returns a version of the statement with each position in canonical form
317
318
319
320
321
|
# File 'lib/rdf/model/statement.rb', line 317
def canonicalize
self.dup.canonicalize!
rescue ArgumentError
nil
end
|
Canonicalizes each unfrozen term in the statement
303
304
305
306
307
308
309
310
|
# File 'lib/rdf/model/statement.rb', line 303
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? ⇒ Boolean
Determines if the statement is complete, vs. invalid. A complete statement is one in which none of ‘subject`, `predicate`, or `object`, are nil.
191
192
193
|
# File 'lib/rdf/model/statement.rb', line 191
def complete?
!incomplete?
end
|
#eql?(other) ⇒ Boolean
233
234
235
|
# File 'lib/rdf/model/statement.rb', line 233
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?
197
198
199
|
# File 'lib/rdf/model/statement.rb', line 197
def has_graph?
!!graph_name
end
|
#has_object? ⇒ Boolean
216
217
218
|
# File 'lib/rdf/model/statement.rb', line 216
def has_object?
!!object
end
|
#has_predicate? ⇒ Boolean
210
211
212
|
# File 'lib/rdf/model/statement.rb', line 210
def has_predicate?
!!predicate
end
|
#has_subject? ⇒ Boolean
204
205
206
|
# File 'lib/rdf/model/statement.rb', line 204
def has_subject?
!!subject
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.
182
183
184
|
# File 'lib/rdf/model/statement.rb', line 182
def incomplete?
to_triple.any?(&:nil?)
end
|
#inferred? ⇒ Boolean
173
174
175
|
# File 'lib/rdf/model/statement.rb', line 173
def inferred?
false
end
|
#initialize! ⇒ Object
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# 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? ⇒ Boolean
146
147
148
|
# File 'lib/rdf/model/statement.rb', line 146
def invalid?
!valid?
end
|
#node? ⇒ Boolean
Also known as:
has_blank_nodes?
Returns ‘true` if any resource of this statement is a blank node.
225
226
227
|
# File 'lib/rdf/model/statement.rb', line 225
def node?
to_quad.compact.any?(&:node?)
end
|
#quoted? ⇒ Boolean
167
168
169
|
# File 'lib/rdf/model/statement.rb', line 167
def quoted?
false
end
|
#reified(options = {}) ⇒ RDF::Graph
Returns a graph containing this statement in reified form.
350
351
352
353
354
355
356
357
358
|
# File 'lib/rdf/model/statement.rb', line 350
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? ⇒ Boolean
Returns ‘true` to indicate that this value is a statement.
128
129
130
|
# File 'lib/rdf/model/statement.rb', line 128
def statement?
true
end
|
#to_hash(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`.
330
331
332
|
# File 'lib/rdf/model/statement.rb', line 330
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
|
284
285
286
|
# File 'lib/rdf/model/statement.rb', line 284
def to_quad
[subject, predicate, object, graph_name]
end
|
#to_s ⇒ String
Returns a string representation of this statement.
338
339
340
341
342
|
# File 'lib/rdf/model/statement.rb', line 338
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 ⇒ Array(RDF::Term)
Also known as:
to_a, to_ary
290
291
292
|
# File 'lib/rdf/model/statement.rb', line 290
def to_triple
[subject, predicate, object]
end
|
#valid? ⇒ Boolean
152
153
154
155
156
157
|
# 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? ⇒ Boolean
Returns ‘true` if any element of the statement is not a URI, Node or Literal.
137
138
139
140
141
142
|
# 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
|