Class: RDF::List
- Inherits:
-
Object
- Object
- RDF::List
- Includes:
- Enumerable, Resource
- Defined in:
- lib/rdf/model/list.rb
Overview
An RDF list.
Constant Summary collapse
Instance Attribute Summary collapse
-
#graph ⇒ Object
readonly
Returns the underlying graph storing the statements that constitute this list.
-
#subject ⇒ Object
readonly
Returns the subject term of this list.
Class Method Summary collapse
-
.[](*values) ⇒ RDF::List
Constructs a new list from the given ‘values`.
Instance Method Summary collapse
-
#&(other) ⇒ RDF::List
Returns the set intersection of this list and ‘other`.
-
#*(int_or_str) ⇒ RDF::List
Returns either a repeated list or a string concatenation of the elements in this list.
-
#+(other) ⇒ RDF::List
Returns the concatenation of this list and ‘other`.
-
#-(other) ⇒ RDF::List
Returns the difference between this list and ‘other`, removing any elements that appear in both lists.
-
#<<(value) ⇒ RDF::List
Appends an element to the tail of this list.
-
#<=>(other) ⇒ Integer
Compares this list to ‘other` for sorting purposes.
-
#[](index) ⇒ RDF::Term
Returns the element at ‘index`.
-
#at(index) ⇒ RDF::Term
(also: #nth)
Returns the element at ‘index`.
-
#each(&block) ⇒ Enumerator
Yields each element in this list.
-
#each_statement(&block) ⇒ Enumerator
Yields each statement constituting this list.
-
#each_subject(&block) ⇒ Enumerator
Yields each subject term constituting this list.
-
#eighth ⇒ RDF::Term
Returns the eighth element in this list.
-
#empty? ⇒ Boolean
Returns ‘true` if this list is empty.
-
#fetch(index, default = UNSET, &block) ⇒ RDF::Term
Returns the element at ‘index`.
-
#fifth ⇒ RDF::Term
Returns the fifth element in this list.
-
#first ⇒ RDF::Term
Returns the first element in this list.
-
#first_subject ⇒ RDF::Resource
Returns the first subject term constituting this list.
-
#fourth ⇒ RDF::Term
Returns the fourth element in this list.
-
#index(value) ⇒ Integer
Returns the index of the first element equal to ‘value`, or `nil` if no match was found.
-
#initialize(subject = nil, graph = nil, values = nil) {|list| ... } ⇒ List
constructor
Initializes a newly-constructed list.
-
#inspect ⇒ String
Returns a developer-friendly representation of this list.
-
#join(sep = $,) ⇒ String
Returns a string created by converting each element of this list into a string, separated by ‘sep`.
-
#last ⇒ RDF::Term
Returns the last element in this list.
-
#last_subject ⇒ RDF::Resource
Returns the last subject term constituting this list.
-
#length ⇒ Integer
(also: #size)
Returns the length of this list.
-
#ninth ⇒ RDF::Term
Returns the ninth element in this list.
-
#rest ⇒ RDF::List
Returns a list containing all but the first element of this list.
- #rest_subject ⇒ RDF::Resource
-
#reverse ⇒ RDF::List
Returns the elements in this list in reversed order.
-
#second ⇒ RDF::Term
Returns the second element in this list.
-
#seventh ⇒ RDF::Term
Returns the seventh element in this list.
-
#sixth ⇒ RDF::Term
Returns the sixth element in this list.
-
#slice(*args) ⇒ RDF::Term
Returns the element at ‘index`.
-
#sort(&block) ⇒ RDF::List
Returns the elements in this list in sorted order.
-
#sort_by(&block) ⇒ RDF::List
Returns the elements in this list in sorted order.
-
#tail ⇒ RDF::List
Returns a list containing the last element of this list.
-
#tenth ⇒ RDF::Term
Returns the tenth element in this list.
-
#third ⇒ RDF::Term
Returns the third element in this list.
-
#to_a ⇒ Array
Returns the elements in this list as an array.
-
#to_s ⇒ String
Returns a string representation of this list.
-
#to_set ⇒ Set
Returns the elements in this list as a set.
-
#uniq ⇒ RDF::List
Returns a new list with the duplicates in this list removed.
-
#valid? ⇒ Boolean
Validate the list ensuring that * rdf:rest values are all BNodes are nil * rdf:type, if it exists, is rdf:List * each subject has no properties other than single-valued rdf:first, rdf:rest other than for the first node in the list.
-
#|(other) ⇒ RDF::List
Returns the set union of this list and ‘other`.
Methods included from Resource
Methods included from Term
#==, #constant?, #eql?, #variable?
Methods included from Value
#graph?, #inspect!, #iri?, #literal?, #node?, #resource?, #statement?, #to_ntriples, #to_quad, #to_rdf, #type_error, #uri?, #variable?
Methods included from Enumerable
#contexts, #dump, #each_context, #each_graph, #each_object, #each_predicate, #each_quad, #each_triple, #enum_context, #enum_graph, #enum_object, #enum_predicate, #enum_quad, #enum_statement, #enum_subject, #enum_triple, #has_context?, #has_object?, #has_predicate?, #has_quad?, #has_statement?, #has_subject?, #has_triple?, #objects, #predicates, #quads, #statements, #subjects, #supports?, #to_hash, #triples
Methods included from Util::Aliasing::LateBound
Methods included from Countable
Constructor Details
#initialize(subject = nil, graph = nil, values = nil) {|list| ... } ⇒ List
Initializes a newly-constructed list.
40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rdf/model/list.rb', line 40 def initialize(subject = nil, graph = nil, values = nil, &block) @subject = subject || RDF::Node.new @graph = graph || RDF::Graph.new values.each { |value| self << value } unless values.nil? || values.empty? if block_given? case block.arity when 1 then block.call(self) else instance_eval(&block) end end end |
Instance Attribute Details
#graph ⇒ Object (readonly)
Returns the underlying graph storing the statements that constitute this list.
103 104 105 |
# File 'lib/rdf/model/list.rb', line 103 def graph @graph end |
#subject ⇒ Object (readonly)
Returns the subject term of this list.
96 97 98 |
# File 'lib/rdf/model/list.rb', line 96 def subject @subject end |
Class Method Details
.[](*values) ⇒ RDF::List
Constructs a new list from the given ‘values`.
The list will be identified by a new autogenerated blank node, and backed by an initially empty in-memory graph.
28 29 30 |
# File 'lib/rdf/model/list.rb', line 28 def self.[](*values) self.new(nil, nil, values) end |
Instance Method Details
#&(other) ⇒ RDF::List
Returns the set intersection of this list and ‘other`.
The resulting list contains the elements common to both lists, with no duplicates.
119 120 121 |
# File 'lib/rdf/model/list.rb', line 119 def &(other) RDF::List[*(to_a & other.to_a)] end |
#*(times) ⇒ RDF::List #*(sep) ⇒ RDF::List
Returns either a repeated list or a string concatenation of the elements in this list.
193 194 195 196 197 198 |
# File 'lib/rdf/model/list.rb', line 193 def *(int_or_str) case int_or_str when Integer then RDF::List[*(to_a * int_or_str)] else join(int_or_str.to_s) end end |
#+(other) ⇒ RDF::List
Returns the concatenation of this list and ‘other`.
150 151 152 |
# File 'lib/rdf/model/list.rb', line 150 def +(other) RDF::List[*(to_a + other.to_a)] end |
#-(other) ⇒ RDF::List
Returns the difference between this list and ‘other`, removing any elements that appear in both lists.
164 165 166 |
# File 'lib/rdf/model/list.rb', line 164 def -(other) RDF::List[*(to_a - other.to_a)] end |
#<<(value) ⇒ RDF::List
Appends an element to the tail of this list.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/rdf/model/list.rb', line 222 def <<(value) value = case value when nil then RDF.nil when RDF::Value then value when Array then RDF::List.new(nil, graph, value) else value end if empty? new_subject = subject else old_subject, new_subject = last_subject, RDF::Node.new graph.delete([old_subject, RDF.rest, RDF.nil]) graph.insert([old_subject, RDF.rest, new_subject]) end graph.insert([new_subject, RDF.type, RDF.List]) graph.insert([new_subject, RDF.first, value]) graph.insert([new_subject, RDF.rest, RDF.nil]) self end |
#<=>(other) ⇒ Integer
Compares this list to ‘other` for sorting purposes.
256 257 258 |
# File 'lib/rdf/model/list.rb', line 256 def <=>(other) to_a <=> other.to_a # TODO: optimize this end |
#[](index) ⇒ RDF::Term
Returns the element at ‘index`.
209 210 211 |
# File 'lib/rdf/model/list.rb', line 209 def [](index) at(index) end |
#at(index) ⇒ RDF::Term Also known as: nth
Returns the element at ‘index`.
369 370 371 372 373 374 |
# File 'lib/rdf/model/list.rb', line 369 def at(index) each.with_index do |v, i| return v if i == index end return nil end |
#each(&block) ⇒ Enumerator
Yields each element in this list.
588 589 590 591 592 593 594 595 596 |
# File 'lib/rdf/model/list.rb', line 588 def each(&block) return to_enum unless block_given? each_subject do |subject| if value = graph.first_object(:subject => subject, :predicate => RDF.first) block.call(value) # FIXME end end end |
#each_statement(&block) ⇒ Enumerator
Yields each statement constituting this list.
608 609 610 611 612 613 614 |
# File 'lib/rdf/model/list.rb', line 608 def each_statement(&block) return enum_statement unless block_given? each_subject do |subject| graph.query(:subject => subject, &block) end end |
#each_subject(&block) ⇒ Enumerator
Yields each subject term constituting this list.
565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'lib/rdf/model/list.rb', line 565 def each_subject(&block) return enum_subject unless block_given? subject = self.subject block.call(subject) loop do rest = graph.first_object(:subject => subject, :predicate => RDF.rest) break if rest.nil? || rest.eql?(RDF.nil) block.call(subject = rest) end end |
#eighth ⇒ RDF::Term
Returns the eighth element in this list.
462 463 464 |
# File 'lib/rdf/model/list.rb', line 462 def eighth at(7) end |
#empty? ⇒ Boolean
Returns ‘true` if this list is empty.
269 270 271 |
# File 'lib/rdf/model/list.rb', line 269 def empty? graph.query(:subject => subject, :predicate => RDF.first).empty? end |
#fetch(index, default = UNSET, &block) ⇒ RDF::Term
Returns the element at ‘index`.
349 350 351 352 353 354 355 356 357 358 359 |
# File 'lib/rdf/model/list.rb', line 349 def fetch(index, default = UNSET, &block) each.with_index do |v, i| return v if i == index end case when block_given? then block.call(index) when !default.eql?(UNSET) then default else raise IndexError, "index #{index} not in the list #{self.inspect}" end end |
#fifth ⇒ RDF::Term
Returns the fifth element in this list.
429 430 431 |
# File 'lib/rdf/model/list.rb', line 429 def fifth at(4) end |
#first ⇒ RDF::Term
Returns the first element in this list.
385 386 387 |
# File 'lib/rdf/model/list.rb', line 385 def first graph.first_object(:subject => first_subject, :predicate => RDF.first) end |
#first_subject ⇒ RDF::Resource
Returns the first subject term constituting this list.
This is equivalent to ‘subject`.
531 532 533 |
# File 'lib/rdf/model/list.rb', line 531 def first_subject subject end |
#fourth ⇒ RDF::Term
Returns the fourth element in this list.
418 419 420 |
# File 'lib/rdf/model/list.rb', line 418 def fourth at(3) end |
#index(value) ⇒ Integer
Returns the index of the first element equal to ‘value`, or `nil` if no match was found.
299 300 301 302 303 304 |
# File 'lib/rdf/model/list.rb', line 299 def index(value) each.with_index do |v, i| return i if v == value end return nil end |
#inspect ⇒ String
Returns a developer-friendly representation of this list.
722 723 724 725 726 727 728 729 |
# File 'lib/rdf/model/list.rb', line 722 def inspect if self.equal?(NIL) 'RDF::List::NIL' else #sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, subject.to_s) sprintf("#<%s:%#0x(%s)>", self.class.name, __id__, to_s) # FIXME end end |
#join(sep = $,) ⇒ String
Returns a string created by converting each element of this list into a string, separated by ‘sep`.
627 628 629 |
# File 'lib/rdf/model/list.rb', line 627 def join(sep = $,) map(&:to_s).join(sep) end |
#last ⇒ RDF::Term
Returns the last element in this list.
496 497 498 |
# File 'lib/rdf/model/list.rb', line 496 def last graph.first_object(:subject => last_subject, :predicate => RDF.first) end |
#last_subject ⇒ RDF::Resource
Returns the last subject term constituting this list.
551 552 553 |
# File 'lib/rdf/model/list.rb', line 551 def last_subject each_subject.to_a.last # TODO: optimize this end |
#length ⇒ Integer Also known as: size
Returns the length of this list.
282 283 284 |
# File 'lib/rdf/model/list.rb', line 282 def length each.count end |
#ninth ⇒ RDF::Term
Returns the ninth element in this list.
473 474 475 |
# File 'lib/rdf/model/list.rb', line 473 def ninth at(8) end |
#rest ⇒ RDF::List
Returns a list containing all but the first element of this list.
507 508 509 |
# File 'lib/rdf/model/list.rb', line 507 def rest (subject = rest_subject).eql?(RDF.nil) ? nil : self.class.new(subject, graph) end |
#rest_subject ⇒ RDF::Resource
540 541 542 |
# File 'lib/rdf/model/list.rb', line 540 def rest_subject graph.first_object(:subject => subject, :predicate => RDF.rest) end |
#reverse ⇒ RDF::List
Returns the elements in this list in reversed order.
639 640 641 |
# File 'lib/rdf/model/list.rb', line 639 def reverse RDF::List[*to_a.reverse] end |
#second ⇒ RDF::Term
Returns the second element in this list.
396 397 398 |
# File 'lib/rdf/model/list.rb', line 396 def second at(1) end |
#seventh ⇒ RDF::Term
Returns the seventh element in this list.
451 452 453 |
# File 'lib/rdf/model/list.rb', line 451 def seventh at(6) end |
#sixth ⇒ RDF::Term
Returns the sixth element in this list.
440 441 442 |
# File 'lib/rdf/model/list.rb', line 440 def sixth at(5) end |
#slice(*args) ⇒ RDF::Term
Returns the element at ‘index`.
314 315 316 317 318 319 320 321 |
# File 'lib/rdf/model/list.rb', line 314 def slice(*args) case argc = args.size when 2 then slice_with_start_and_length(*args) when 1 then (arg = args.first).is_a?(Range) ? slice_with_range(arg) : at(arg) when 0 then raise ArgumentError, "wrong number of arguments (0 for 1)" else raise ArgumentError, "wrong number of arguments (#{argc} for 2)" end end |
#sort(&block) ⇒ RDF::List
Returns the elements in this list in sorted order.
651 652 653 |
# File 'lib/rdf/model/list.rb', line 651 def sort(&block) RDF::List[*super] end |
#sort_by(&block) ⇒ RDF::List
Returns the elements in this list in sorted order.
663 664 665 |
# File 'lib/rdf/model/list.rb', line 663 def sort_by(&block) RDF::List[*super] end |
#tail ⇒ RDF::List
Returns a list containing the last element of this list.
518 519 520 |
# File 'lib/rdf/model/list.rb', line 518 def tail (subject = last_subject).eql?(RDF.nil) ? nil : self.class.new(subject, graph) end |
#tenth ⇒ RDF::Term
Returns the tenth element in this list.
484 485 486 |
# File 'lib/rdf/model/list.rb', line 484 def tenth at(9) end |
#third ⇒ RDF::Term
Returns the third element in this list.
407 408 409 |
# File 'lib/rdf/model/list.rb', line 407 def third at(2) end |
#to_a ⇒ Array
Returns the elements in this list as an array.
687 688 689 |
# File 'lib/rdf/model/list.rb', line 687 def to_a each.to_a end |
#to_s ⇒ String
Returns a string representation of this list.
711 712 713 |
# File 'lib/rdf/model/list.rb', line 711 def to_s 'RDF::List[' + join(', ') + ']' end |
#to_set ⇒ Set
Returns the elements in this list as a set.
698 699 700 701 |
# File 'lib/rdf/model/list.rb', line 698 def to_set require 'set' unless defined?(::Set) each.to_set end |
#uniq ⇒ RDF::List
Returns a new list with the duplicates in this list removed.
675 676 677 |
# File 'lib/rdf/model/list.rb', line 675 def uniq RDF::List[*to_a.uniq] end |
#valid? ⇒ Boolean
Validate the list ensuring that
-
rdf:rest values are all BNodes are nil
-
rdf:type, if it exists, is rdf:List
-
each subject has no properties other than single-valued rdf:first, rdf:rest other than for the first node in the list
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rdf/model/list.rb', line 66 def valid? li = subject while li != RDF.nil do rest = nil firsts = rests = 0 @graph.query(:subject => li) do |st| case st.predicate when RDF.type # Be tollerant about rdf:type entries, as some OWL vocabularies use it excessively when RDF.first firsts += 1 when RDF.rest rest = st.object return false unless rest.node? || rest == RDF.nil rests += 1 else # First node may have other properties return false unless li == subject end end return false unless firsts == 1 && rests == 1 li = rest end true end |