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
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
21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/logicle/digraph.rb', line 21 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
17 18 19 |
# File 'lib/logicle/digraph.rb', line 17 def add_node(id, node_type) @nodes[id] = Node.new(id, node_type) end |
#evaluate ⇒ Object
33 34 35 |
# File 'lib/logicle/digraph.rb', line 33 def evaluate outputs.each_value { |output| output.state } end |
#inputs ⇒ Object
9 10 11 |
# File 'lib/logicle/digraph.rb', line 9 def inputs @nodes.select { |id, node| node.switch? } end |
#outputs ⇒ Object
13 14 15 |
# File 'lib/logicle/digraph.rb', line 13 def outputs @nodes.select { |id, node| node.bulb? } end |