Class: Dbd::Graph
Overview
The Graph stores the Facts and Contexts in an in-memory collection structure.
On the other hand, it acts as an “interface” that can be re-implemented by other persistence mechanisms (duck typing).
Instance Method Summary collapse
-
#<<(fact_collection) ⇒ Graph
Add a Fact, Resource or other recursive collection of facts.
-
#contexts ⇒ Array
Return an array of contexts for graph.
-
#from_CSV(csv) ⇒ Graph
Import a graph from a sorted CSV IO stream.
-
#from_unsorted_CSV_file(filename) ⇒ Graph
Import a graph from an unsorted CSV file (by filename).
-
#resources ⇒ Array
Return an array of resources for graph.
-
#to_CSV ⇒ String
Export the graph to a CSV string.
-
#to_CSV_file(filename) ⇒ Object
Export the graph to a CSV file.
Methods included from Fact::Collection
#by_subject, #context_subjects, #initialize, #newest_time_stamp, #oldest_time_stamp, #resource_subjects
Methods included from Helpers::OrderedSetCollection
add_and_return_index, #each, #freeze, #initialize, #last, #size
Instance Method Details
#<<(fact_collection) ⇒ Graph
Add a Fact, Resource or other recursive collection of facts.
Side effect: this will set the time_stamp of the facts.
23 24 25 26 27 28 29 |
# File 'lib/dbd/graph.rb', line 23 def <<(fact_collection) fact_collection.each_recursively do |fact| enforce_strictly_monotonic_time(fact) super(fact) end self end |
#contexts ⇒ Array
Return an array of contexts for graph
102 103 104 105 106 |
# File 'lib/dbd/graph.rb', line 102 def contexts @context_indices_by_subject.map do |subject, fact_indices| Context.new(subject: subject) << facts_from_indices(fact_indices) end end |
#from_CSV(csv) ⇒ Graph
Import a graph from a sorted CSV IO stream
time_stamps need to be strictly monotonic increasing
Tokens “backslash n” in the CSV fields will be unescaped to newlines. Tokens “double backslash” in the CSV fields will be unescaped to single backslash
67 68 69 70 71 72 |
# File 'lib/dbd/graph.rb', line 67 def from_CSV(csv) CSV.new(csv).each do |row| self << Fact.factory.from_string_values(row, validate: true) end self end |
#from_unsorted_CSV_file(filename) ⇒ Graph
Import a graph from an unsorted CSV file (by filename)
time_stamps need to be unique (but can be random order)
Tokens “backslash n” in the CSV fields will be unescaped to newlines. Tokens “double backslash” in the CSV fields will be unescaped to single backslash
84 85 86 |
# File 'lib/dbd/graph.rb', line 84 def from_unsorted_CSV_file(filename) on_sorted_file(filename) { |sorted_file| from_CSV(sorted_file) } end |
#resources ⇒ Array
Return an array of resources for graph
92 93 94 95 96 |
# File 'lib/dbd/graph.rb', line 92 def resources @resource_indices_by_subject.map do |subject, fact_indices| Resource.new(subject: subject) << facts_from_indices(fact_indices) end end |
#to_CSV ⇒ String
Export the graph to a CSV string
Newlines in the fields are escaped to “backslash n”. Backslashes in the field are escape to “double backslash”.
38 39 40 41 42 |
# File 'lib/dbd/graph.rb', line 38 def to_CSV CSV.generate(csv_defaults) do |csv| push_facts(csv) end end |
#to_CSV_file(filename) ⇒ Object
Export the graph to a CSV file
Newlines in the fields are escaped to “backslash n”. Backslashes in the field are escape to “double backslash”.
51 52 53 54 55 |
# File 'lib/dbd/graph.rb', line 51 def to_CSV_file(filename) CSV.open(filename, 'w', csv_defaults) do |csv| push_facts(csv) end end |