Class: Logicle::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/logicle/node.rb

Constant Summary collapse

LOGIC_OPERATIONS =
{
  switch: Proc.new { |args| nil },
  bulb:   Proc.new { |args| args[0].state },
  on:     Proc.new { |args| true },
  off:    Proc.new { |args| false },
  not:    Proc.new { |args| !args[0].state },
  and:    Proc.new { |args| args[0].state & args[1].state },
  or:     Proc.new { |args| args[0].state | args[1].state },
  nand:   Proc.new { |args| !args[0].state | !args[1].state },
  nor:    Proc.new { |args| !args[0].state & !args[1].state },
  xor:    Proc.new { |args| args[0].state ^ args[1].state },
  xnor:   Proc.new { |args| !(args[0].state ^ args[1].state) }
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, type) ⇒ Node

Returns a new instance of Node.



19
20
21
22
23
# File 'lib/logicle/node.rb', line 19

def initialize(id, type)
  @id = id
  @type = validate_type(type)
  @inputs = []
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



17
18
19
# File 'lib/logicle/node.rb', line 17

def id
  @id
end

#inputsObject (readonly)

Returns the value of attribute inputs.



17
18
19
# File 'lib/logicle/node.rb', line 17

def inputs
  @inputs
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'lib/logicle/node.rb', line 17

def type
  @type
end

Instance Method Details

#append_inputs(*nodes) ⇒ Object Also known as: append_input



37
38
39
# File 'lib/logicle/node.rb', line 37

def append_inputs(*nodes)
  @inputs += nodes
end

#bulb?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/logicle/node.rb', line 25

def bulb?
  @type == :bulb
end

#clear_inputsObject



42
43
44
45
# File 'lib/logicle/node.rb', line 42

def clear_inputs
  @inputs = []
  @state = nil
end

#stateObject



47
48
49
# File 'lib/logicle/node.rb', line 47

def state
  @state ||= evaluate
end

#switch?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/logicle/node.rb', line 29

def switch?
  @type == :switch
end