Class: Dbd::Graph

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

Overview

The Graph stores the Facts and ProvenanceFacts 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).

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Fact::Collection

#by_subject, #initialize, #newest_time_stamp, #oldest_time_stamp

Methods included from Helpers::OrderedSetCollection

add_and_return_index, #each, #initialize, #last, #size

Class Method Details

.from_CSV(csv) ⇒ Graph

Import a graph from a CSV string.

Parameters:

  • csv (String) —

    a string that contains the CSV serialization

Returns:

  • (Graph) —

    the imported graph



47
48
49
50
51
52
53
54
# File 'lib/dbd/graph.rb', line 47

def self.from_CSV(csv)
  new.tap do |graph|
    CSV.new(csv).each do |row|
      # TODO validate the input formats (e.g. invalid uuid codes)
      graph << Fact.from_string_values(row)
    end
  end
end

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:



22
23
24
25
26
27
28
# File 'lib/dbd/graph.rb', line 22

def <<(fact_collection)
  fact_collection.each_recursively do |fact|
    enforce_strictly_monotonic_time(fact)
    super(fact)
  end
  self
end

#to_CSV ⇒ String

Export the graph to a CSV string

Returns:

  • (String) —

    comma separated string with double quoted cells



34
35
36
37
38
39
40
# File 'lib/dbd/graph.rb', line 34

def to_CSV
  CSV.generate(force_quotes: true) do |csv|
    @internal_collection.each do |fact|
      csv << fact.values
    end
  end.encode("utf-8")
end