Sha256: a481dbd9b35b39f38c20fc3e1c2ee803fdbdfe6babb5a45231162b56d2441c09

Contents?: true

Size: 1.11 KB

Versions: 1

Compression:

Stored size: 1.11 KB

Contents

module Logicle
  class Digraph
    attr_reader :nodes, :edges

    def initialize
      @nodes, @edges = {}, {}
    end

    def inputs
      @nodes.select { |id, node| node.switch? }
    end

    def outputs
      @nodes.select { |id, node| node.bulb? }
    end

    def add_node(id, node_type)
      @nodes[id] = Node.new(id, node_type)
    end

    def add_edge(start_id, end_id)
      start_node, end_node = @nodes[start_id], @nodes[end_id]

      if start_node && end_node 
        end_node.append_input(start_node)
        @edges[start_id] = end_id
        true
      else
        raise_unknown_nodes(start_id => start_node, end_id => end_node)
      end
    end

    def evaluate
      outputs.each_value { |output| output.state }
    end


    private
    def raise_unknown_nodes(node_map={})
      bad_node_ids = node_map.select { |id, node| node.nil? }.keys 

      case bad_node_ids.count
      when 0
        return
      when 1
        raise UnknownNodeError, "Invalid node id: #{ bad_node_ids[0] }"
      else
        raise UnknownNodeError, "Invalid node ids: #{ bad_node_ids.join(", ") }"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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