Class: Dbd::Graph

Inherits:
Object
  • Object
show all
Includes:
Fact::Collection
Defined in:
lib/dbd/graph.rb

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

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.

Parameters:

  • fact_collection (#time_stamp, #time_stamp=, #each)

    a recursive collection of facts

Returns:



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

#contextsArray

Return an array of contexts for graph

Returns:

  • (Array)

    array with all contexts



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

Parameters:

  • csv (IO Stream)

    an IO Stream that contains the CSV serialization

Returns:

  • (Graph)

    the imported graph



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

Parameters:

  • filename (String)

    the filename of the unsorted CSV file

Returns:

  • (Graph)

    the imported graph



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

#resourcesArray

Return an array of resources for graph

Returns:

  • (Array)

    array with all resources



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_CSVString

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”.

Returns:

  • (String)

    comma separated string with double quoted cells



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”.

Parameters:

  • filename (String)

    the filename to stream the CSV to



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