Class: LogicTools::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/logic_tools/logicfunction.rb

Overview

Represents a logic function.

A logic function is described as a set of logic trees (LogicTools::Node) associated to variables (LogicTools::Variable) repreenting each one one-bit output. The inputs are represented by the variables contained by the variable nodes (LogicTools::NodeVar) of the trees.

Functions can be composed together through their variables.

Instance Method Summary collapse

Constructor Details

#initializeFunction

Creates a new empty function.



24
25
26
# File 'lib/logic_tools/logicfunction.rb', line 24

def initialize
    @assigns = {}
end

Instance Method Details

#add(output, tree) ⇒ Object Also known as: []=

Adds an output variable associated with a tree for computing it.



37
38
39
40
41
42
# File 'lib/logic_tools/logicfunction.rb', line 37

def add(output,tree)
    unless tree.is_a?(Node)
        raise "Invalid class for a logic tree: #{tree.class}"
    end
    @assigns[Variable.get(output)] = tree
end

#cloneObject Also known as: dup

duplicates the function.



29
30
31
32
33
# File 'lib/logic_tools/logicfunction.rb', line 29

def clone # :nodoc:
    result = Function.new
    @assigns.each { |output,tree| result.add(output,tree.clone) }
    return result
end

#each(&blk) ⇒ Object

Iterates over the assignments of the functions.



52
53
54
55
56
57
58
# File 'lib/logic_tools/logicfunction.rb', line 52

def each(&blk)
    # No block given? Return an enumerator.
    return to_enum(:each) unless block_given?

    # Block given? Apply it.
    @assigns.each(&blk)
end

#each_output(&blk) ⇒ Object

Iterates over the output variables of the function.



61
62
63
64
65
66
67
# File 'lib/logic_tools/logicfunction.rb', line 61

def each_output(&blk)
    # No block given? Return an enumerator.
    return to_enum(:each_output) unless block_given?

    # Block given? Apply it.
    @assigns.each_key(&blk)
end

#each_tree(&blk) ⇒ Object

Iterates over the trees of the function.



79
80
81
82
83
84
85
# File 'lib/logic_tools/logicfunction.rb', line 79

def each_tree(&blk)
    # No block given? Return an enumerator.
    return to_enum(:each_tree) unless block_given?

    # Block given? Apply it.
    @assigns.each_value(&blk)
end

#get(output) ⇒ Object Also known as: []

Gets the tree corresponding to an output variable.



46
47
48
# File 'lib/logic_tools/logicfunction.rb', line 46

def get(output)
    return @assigns[Variable.get(variable)]
end

#get_variablesObject

Gets a array containing the variables of the function sorted by name.



70
71
72
73
74
75
76
# File 'lib/logic_tools/logicfunction.rb', line 70

def get_variables
    result = @assigns.each_value.reduce([]) do |ar,tree|
        ar.concat(tree.get_variables)
    end
    result.uniq!.sort!
    return result
end

#to_sObject

Convert to a string.



88
89
90
91
92
93
94
95
96
97
# File 'lib/logic_tools/logicfunction.rb', line 88

def to_s # :nodoc:
    result = @assigns.each.reduce("[") do |str,assign|
        str << assign[0].to_s
        str << "="
        str << assign[1].to_s
        str << ","
    end
    result[-1] = "]"
    return result
end