Class: Logicle::Digraph
- Inherits:
-
Object
- Object
- Logicle::Digraph
- Defined in:
- lib/logicle/digraph.rb
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#nodes ⇒ Object
readonly
Returns the value of attribute nodes.
Instance Method Summary collapse
- #add_edge(start_id, end_id) ⇒ Object
- #add_node(id, node_type) ⇒ Object
- #evaluate ⇒ Object
-
#initialize ⇒ Digraph
constructor
A new instance of Digraph.
- #inputs ⇒ Object
- #outputs ⇒ Object
Constructor Details
#initialize ⇒ Digraph
Returns a new instance of Digraph.
5 6 7 |
# File 'lib/logicle/digraph.rb', line 5 def initialize @nodes, @edges = {}, {} end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
3 4 5 |
# File 'lib/logicle/digraph.rb', line 3 def edges @edges end |
#nodes ⇒ Object (readonly)
Returns the value of attribute nodes.
3 4 5 |
# File 'lib/logicle/digraph.rb', line 3 def nodes @nodes end |
Instance Method Details
#add_edge(start_id, end_id) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/logicle/digraph.rb', line 27 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 |
#add_node(id, node_type) ⇒ Object
23 24 25 |
# File 'lib/logicle/digraph.rb', line 23 def add_node(id, node_type) @nodes[id] = Node.new(id, node_type) end |
#evaluate ⇒ Object
9 10 11 12 13 |
# File 'lib/logicle/digraph.rb', line 9 def evaluate outputs.each_value do |output| output.state end end |
#inputs ⇒ Object
15 16 17 |
# File 'lib/logicle/digraph.rb', line 15 def inputs @nodes.select { |id, node| node.switch? } end |
#outputs ⇒ Object
19 20 21 |
# File 'lib/logicle/digraph.rb', line 19 def outputs @nodes.select { |id, node| node.bulb? } end |