Class: Dumper_Yin

Inherits:
Dumper show all
Defined in:
lib/Yinspire/Dumpers/Dumper_Yin.rb

Instance Method Summary collapse

Methods inherited from Dumper

#dump_entities

Constructor Details

#initialize(*args) ⇒ Dumper_Yin

Returns a new instance of Dumper_Yin.



6
7
8
9
# File 'lib/Yinspire/Dumpers/Dumper_Yin.rb', line 6

def initialize(*args)
  super
  @verbose = true
end

Instance Method Details

#dump(out) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/Yinspire/Dumpers/Dumper_Yin.rb', line 11

def dump(out)
  ekeys = @entities.keys.sort
  ekeys.each {|key|
    entity = @entities[key]
    out << "ENTITY " if @verbose
    out << to_yin_str(entity.id)
    out << " = #{to_yin_str(entity.entity_type)} "
    out << to_yin_str(entity.dump)
    out << "\n"
  }

  ekeys.each {|key|
    entity = @entities[key]
    targets = []; entity.each_connection {|t| targets << to_yin_str(t.id)}
    next if targets.empty?

    out << "CONNECT " if @verbose
    out << "#{to_yin_str(entity.id)} -> #{targets.join(', ')}\n"
  }

  ekeys.each {|key|
    entity = @entities[key]
    next unless entity.respond_to?(:stimuli_pq_to_a)
    stimuli = entity.stimuli_pq_to_a
    next if stimuli.empty?

    out << "\n"
    out << "STIMULATE " if @verbose
    out << "#{to_yin_str(entity.id)} ! {\n  "

    out << stimuli.to_enum(:each_slice, 2).
      map {|at, weight| 
        if weight == Infinity
          to_yin_str(at)
        else
          "#{to_yin_str(weight)}@#{to_yin_str(at)}"
        end
      }.to_enum(:each_slice, 5).map {|arr| arr.join(" ")}.join("\n  ")

    out << "\n}\n"
  }
end

#to_yin_str(obj) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/Yinspire/Dumpers/Dumper_Yin.rb', line 54

def to_yin_str(obj)
  case obj
  when String
    if obj =~ /^\w+$/
      obj
    else
      '"' + obj + '"'
    end
  when Symbol
    to_yin_str(obj.to_s)
  when Hash
    out = ""
    out << "{\n"
    obj.keys.sort_by {|k| k.to_s}.each do |k|
      out << "  " if @verbose
      out << "#{to_yin_str(k)} = #{to_yin_str(obj[k])}\n"
    end
    out << "}\n"
    out
  when TrueClass, FalseClass, Float, Fixnum, Bignum
    obj.to_s
  else
    raise "#{obj.class}"
  end
end