Class: PgVerify::Model::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/pg-verify/model/simulation/trace.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, states, loop_index: -1)) ⇒ Trace

Returns a new instance of Trace.



10
11
12
13
# File 'lib/pg-verify/model/simulation/trace.rb', line 10

def initialize(model, states, loop_index: -1)
    @model, @states = model, states
    @loop_index = loop_index
end

Instance Attribute Details

#loop_indexObject

Returns the value of attribute loop_index.



8
9
10
# File 'lib/pg-verify/model/simulation/trace.rb', line 8

def loop_index
  @loop_index
end

#modelObject

Returns the value of attribute model.



6
7
8
# File 'lib/pg-verify/model/simulation/trace.rb', line 6

def model
  @model
end

#statesObject

Returns the value of attribute states.



7
8
9
# File 'lib/pg-verify/model/simulation/trace.rb', line 7

def states
  @states
end

Instance Method Details

#to_s(include_steps: true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pg-verify/model/simulation/trace.rb', line 15

def to_s(include_steps: true)
    return "No states in trace" if @states.empty?
    # Get all variables (TODO: Bring into sensible order)
    vars = @states.first.keys
    state_vars = @model.state_variables()

    parts = vars.map { |var| 
        var_string = state_vars.varname?(var) ? var.to_s.c_state.c_bold : var.to_s.c_string
        var_string + "\n" + @states.each_with_index.map{ |state, index| value_str(var, state[var], index) }.join("\n")
    }
    str = "Step".c_num.c_bold + "\n" + (0...@states.length).map { |i| "#{i + 1}" } .join("\n")
    str = "" unless include_steps
    parts.each { |part| str = str.line_combine(part, separator: "  ") }

    unless @loop_index.nil? || @loop_index < 0
        loop_str = (0...@states.length).map { |i| i == @loop_index + 1 ? "<- loop".c_blue : "" } .join("\n")
        str = str.line_combine(loop_str, separator: "  ")
        str += "\n" + "Loop starts at #{@loop_index + 1}".c_sidenote
    end
    
    return str
end

#value_str(key, value, index) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/pg-verify/model/simulation/trace.rb', line 38

def value_str(key, value, index)
    return value.c_green if [ "On", "Yes", "Active" ].include?(value)
    return value.c_red if [ "Off", "No", "Idle" ].include?(value)


    settings_color = Settings.trace.colors.find { |key, val| File.fnmatch?(key.to_s, value) }
    settings_color = settings_color[1] unless settings_color.blank?
    return value.send(:"c_#{settings_color}") unless settings_color.blank?

    return "#{value}" 

end