Sha256: 444962ce135b566b79a835ac7e3e6caf206b4df1e749edf8c7858e6f1d7286b6

Contents?: true

Size: 869 Bytes

Versions: 1

Compression:

Stored size: 869 Bytes

Contents

module Logicle
  class TgfWriter
    def initialize(filename, circuit)
      @output_file = File.open(filename, "w")
      @circuit = circuit
    end

    def write
      @circuit.nodes.each_value { |node| write_node(node) }

      write_separator

      @circuit.edges.each { |start, finish| write_edge(start, finish) }
    end

    private
    def write_node(node)
      id = node.id
      label = format_label(node)

      @output_file.puts "#{ id } #{ label }"
    end

    def format_label(node)
      type = case node.type
      when :on, :off 
        "SWITCH"
      else 
        node.type.to_s.upcase
      end
      state = node.state ? "ON" : "OFF"

      "#{ type } (#{ state })"
    end

    def write_separator
      @output_file.puts "#"
    end

    def write_edge(start_id, end_id)
      @output_file.puts "#{ start_id } #{ end_id }"
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
logicle-0.1.1 lib/logicle/tgf_writer.rb