Class: Nodepile::PileOrganizer

Inherits:
Object
  • Object
show all
Defined in:
lib/nodepile/pile_organizer.rb

Overview

Container class for managing a Nodepile. A nodepile consists of a set of entities including nodes, edges, and rules. It includes methods for enumerating the various items in the collection, filtering, and deducing the existence of implied edges using rules.

Defined Under Namespace

Classes: ERecStack, RuleCache, SourceData

Instance Method Summary collapse

Constructor Details

#initializePileOrganizer

Returns a new instance of PileOrganizer.



20
21
22
23
24
25
26
27
28
# File 'lib/nodepile/pile_organizer.rb', line 20

def initialize()
    @nodes = Hash.new{|h,k| h[k] = ERecStack.new}
    @edges = Hash.new{|h,k| h[k] = ERecStack.new}
    @rules = Array.new  # not subject to overlaying with themselves
    @pragmas = Nodepile::Pragmas.new
    @sources = Hash.new{|h,k| h[k] = SourceData.new(k,0)} # insert a dummy source for unspecifie
    @last_source_name = nil
    @dirty = true
end

Instance Method Details

#<<(entity_record) ⇒ Object

Alias for #append()

Parameters:



92
# File 'lib/nodepile/pile_organizer.rb', line 92

def <<(entity_record) = append(entity_record)

#append(kaa) ⇒ self

If a source name is not specified, then the source is assumed to be the last source that was used to append. If no sequence number is provided then the sequence_number is assumed to be one more than the highest sequence number that was specified. If callers are manually specifying sequence numbers for a source, they should do so consistently to avoid repeats.

Parameters:

Returns:

  • (self)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nodepile/pile_organizer.rb', line 38

def append(kaa)
    @last_source_name = kaa.source || @last_source_name
    source_data = @sources[@last_source_name]
    # note that the way things work below deliberately "overlays" items
    # when a matching key is encountered.  Rule recalculation is deferred
    case kaa['@type']
        when :node
            @nodes[kaa['@key']] << kaa
        when :edge
            @edges[kaa['@key']] << kaa
        when :rule
            @rules << RuleCache.new(kaa)
        when :pragma
            @pragmas.parse(kaa['_id'])
        else
            raise "Unhandled entity entity type #{kaa['@type'].inspect}"
    end #case
    return self
end

#edge_countObject



60
# File 'lib/nodepile/pile_organizer.rb', line 60

def edge_count() = @edges.length

#edge_recordsObject

Provide summarized records in order



76
77
78
79
80
# File 'lib/nodepile/pile_organizer.rb', line 76

def edge_records
    return enum_for(:edge_records) unless block_given?
    _update_rule_impacts
    @edges.each_value{|erstack| yield(erstack.summary) }
end

#entity_record(key) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/nodepile/pile_organizer.rb', line 63

def entity_record(key)
    _update_rule_impacts
    case key
        when String
            return @nodes[key]
        when Array
            return @edges[key]
        else
            raise "Unrecognized key structure/type"
    end
end

#load_from_file(tabular_filepath) ⇒ Object

Loads the given file (on top of anything already stored in this object)



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/nodepile/pile_organizer.rb', line 95

def load_from_file(tabular_filepath)
    source = Nodepile::TabularRecordSource.new(tabular_filepath,format: :guess)
    specs = nil
    loaded_entity_count = 0
    rec_src_meta = {'path' => tabular_filepath,'rec_num' => nil}
     = Hash.new
    source.each{|(rec,rec_num)|
        rec_src_meta['rec_num'] = rec_num
        if specs.nil? #first row is header
            specs = Nodepile::InputColumnSpecs.new(rec,metadata_key_prefix: '@')
        else
            begin
                specs.parse(rec,source: tabular_filepath,
                            ref_num: rec_num,
                            metadata: ,
                           ){|keyed_array_accessor|
                    append(keyed_array_accessor)
                    loaded_entity_count += 1
                   }
            rescue Nodepile::InputColumnSpecs::InvalidRecordError => err 
                # re-raise but add info about the record number that triggered the error
                err.rec_num = rec_num
                err.file_path = tabular_filepath
                raise   # re-raise
            end
        end #if
       }
    return loaded_entity_count
end

#node_countObject



58
# File 'lib/nodepile/pile_organizer.rb', line 58

def node_count() = @nodes.length

#node_recordsObject

Provide the summarized records



83
84
85
86
87
# File 'lib/nodepile/pile_organizer.rb', line 83

def node_records
    return enum_for(:node_records) unless block_given?
    _update_rule_impacts
    @nodes.each_value{|erstack| yield(erstack.summary) }
end

#pragmasObject



61
# File 'lib/nodepile/pile_organizer.rb', line 61

def pragmas() = @pragmas

#rule_countObject



59
# File 'lib/nodepile/pile_organizer.rb', line 59

def rule_count() = @rules.length