class LogicTools::Generator

Class used for genrating logic expression.

Public Class Methods

new(*variables) click to toggle source

Creates a new generator for logic expressions on the boolean space based on a variables set.

# File lib/logic_tools/logicgenerator.rb, line 15
def initialize(*variables)
    @variables = variables.map {|var| var.to_s }
    @random = Random.new(0)   # The default seed is fixed to 0.
    @max = 2**@variables.size # The max number of cube per random cover.
    @rate= Rational(1,3)      # The rate of "-" in a cube.
end

Public Instance Methods

each_1cover() { |make_1cover(table)| ... } click to toggle source

Iterates over all the possible cover made of 1-cubes.

NOTE: this iteration can be huge!

# File lib/logic_tools/logicgenerator.rb, line 233
def each_1cover
    # No block given? Return an enumerator.
    return to_enum(:each_1cover) unless block_given?
    
    # Block given? Apply it on each bit.
    # Iterate on each possible truth table.
    ( 2 ** (2 ** @variables.size) ).times do |table|
        # Create the expression and apply the block on it.
        yield(make_1cover(table))
    end
end
each_std_conj() { |make_std_conj(table)| ... } click to toggle source

Iterates over all the possible standard conjunctive forms.

NOTE: this iteration can be huge!

# File lib/logic_tools/logicgenerator.rb, line 179
def each_std_conj
    # No block given? Return an enumerator.
    return to_enum(:each_std_conj) unless block_given?
    
    # Block given? Apply it on each bit.
    # Iterate on each possible truth table.
    ( 2 ** (2 ** @variables.size) ).times do |table|
        # Create the expression and apply the block on it.
        yield(make_std_conj(table))
    end
end
each_variable(&blk) click to toggle source

Iterates over the variables of the generator.

Returns an enumberator if no block is given

# File lib/logic_tools/logicgenerator.rb, line 55
def each_variable(&blk)
    # No block given? Return an enumerator
    return to_enum(:each_variable) unless block_given?
    # Block given? Apply it.
    @variables.each(&blk)
end
make_1cover(table) click to toggle source

Create a 1-cube cover from its values in a truth table.

# File lib/logic_tools/logicgenerator.rb, line 213
def make_1cover(table)
    # Convert the +table+ to a bit string if necessary.
    unless table.is_a?(String) then
        table = table.to_s(2).rjust(2 ** @variables.size,"0")
    end
    # Generate the cover.
    cover = Cover.new(*@variables)
    # Fill it with the required 1-cubes.
    table.each_char.with_index do |val,i|
        if (val == "1") then
            cover << make_cube(i)
        end
    end
    # Returns the cover.
    return cover
end
make_cube(row) click to toggle source

Creates a cube from binary row.

# File lib/logic_tools/logicgenerator.rb, line 204
def make_cube(row)
    # Convert the +row+ to a bit string if necessary.
    unless (row.is_a?(String))
        row = row.to_s(2).rjust(@variables.size,"0")
    end
    return Cube.new(row)
end
make_minterm(row) click to toggle source

Creates a minterm from the binary value of a truth table's row.

# File lib/logic_tools/logicgenerator.rb, line 142
def make_minterm(row)
    # Convert the +row+ to a bit string if necessary.
    unless (row.is_a?(String))
        row = row.to_s(2).rjust(@variables.size,"0")
    end
    # Create the minterm from the bit string: an AND where
    # each term is variable if the corresponding bit is "1"
    # and the opposite if the corresponding bit is "0".
    return NodeAnd.new(*row.each_char.with_index.map do |bit,j|
        var = NodeVar.new(Variable.get(@variables[j]))
        bit == "1" ? var : NodeNot.new(var)
    end )
end
make_std_conj(table) click to toggle source

Create a standard conjunctive form from its values in a truth table.

# File lib/logic_tools/logicgenerator.rb, line 158
def make_std_conj(table)
    # Convert the +table+ to a bit string if necessary.
    unless table.is_a?(String) then
        table = table.to_s(2).rjust(2 ** @variables.size,"0")
    end
    # Generate the terms from it.
    terms = []
    table.each_char.with_index do |val,i|
        if (val == "1") then
            terms << make_minterm(i)
        end
    end
    # If no term, return a NodeFalse
    return NodeFalse.new if terms.empty?
    # Generate and return the resulting sum.
    return NodeOr.new(*terms)
end
max() click to toggle source

Gets the maximum number of cubes for a cover.

# File lib/logic_tools/logicgenerator.rb, line 33
def max
    return @max
end
max=(max) click to toggle source

Sets the maximum number of cubes for a cover.

# File lib/logic_tools/logicgenerator.rb, line 38
def max=(max)
    @max = max.to_i
end
random_1cover() click to toggle source

Creates a random cover made of 1-cubes.

# File lib/logic_tools/logicgenerator.rb, line 251
def random_1cover
    return make_1cover(random_column)
end
random_1cube() click to toggle source

Creates a random 1-cube.

# File lib/logic_tools/logicgenerator.rb, line 246
def random_1cube
    return make_cube(random_2row)
end
random_2row()
Alias for: random_row
random_3row() click to toggle source

Creates a random 3-states row.

# File lib/logic_tools/logicgenerator.rb, line 129
def random_3row
    result = "-" * @variables.size
    @variables.size.times do |i| 
        value = @random.rand
        if value > @rate then
            result[i] = value <= @rate + (1-@rate)/2 ? "0" : "1"
        end
    end
    return result
end
random_column() click to toggle source

Creates a random truth table column value.

# File lib/logic_tools/logicgenerator.rb, line 118
def random_column
    return @random.rand(0..(2**(2**(@variables.size))-1))
end
random_cover() click to toggle source

Creates a random cover.

# File lib/logic_tools/logicgenerator.rb, line 261
def random_cover
    # Create the new cover.
    cover = Cover.new(*@variables)
    # Fill it with a random number of random cubes.
    volume = 0
    @random.rand(0..(@max-1)).times do
        cube = make_cube(random_3row)
        # volume += 2 ** cube.each_bit.count { |b| b == "-" }
        # break if volume >= 2 ** @variables.size
        cover << cube
    end
    # Return it.
    return cover
end
random_cube() click to toggle source

Creates a random cube.

# File lib/logic_tools/logicgenerator.rb, line 256
def random_cube
    return make_cube(random_3row)
end
random_expression() click to toggle source

Creates a random logic expression.

# File lib/logic_tools/logicgenerator.rb, line 64
def random_expression
    expression = ""
    pre = :"(" # The previous element type: start of expression
               #    is equivalent to a opened parenthesis.
    par = 0    # The number of opened parenthesis
    @random.rand(0..(@max-1)).times do
        choice = @random.rand(@variables.size+4)
        # print "par=#{par} pre=#{pre}\n"
        case choice
        when 0 then
            expression << "("
            par += 1
            pre = :"("
        when 1 then
            if ( par > 0 and ( pre==:v or pre==:")") )
                expression << ")" 
                par -= 1
                pre = :")"
            end
        when 3 then
            if ( pre != :"(" and pre != :+ and pre != :~ )
                expression << "+" 
                pre = :+
            end
        when 4 then
            expression << "~"
            pre = :~
        else
            var = @variables[choice-4]
            if var.size > 1
                expression << "{" + var + "}"
            else
                expression << var
            end
            pre = :v
        end
        # print "expression = #{expression}\n"
    end
    # Remove the last invalid character.
    while ["~","(","+"].include?(expression[-1]) do
        par -= 1 if expression[-1] == "("
        expression.chop!
    end
    # Close the remaining opened parenthesis.
    while par > 0 do
        par -=1
        expression << ")"
    end
    # Return the resulting expression.
    return expression
end
random_minterm() click to toggle source

Creates a random minterm.

# File lib/logic_tools/logicgenerator.rb, line 192
def random_minterm
    return make_minterm(random_row)
end
random_row() click to toggle source

Creates a random truth table row value.

# File lib/logic_tools/logicgenerator.rb, line 123
def random_row
    return @random.rand(0..(2**(@variables.size)-1))
end
Also aliased as: random_2row
random_std_conj() click to toggle source

Creates a random standard conjunctive from.

# File lib/logic_tools/logicgenerator.rb, line 197
def random_std_conj
    return make_std_conj(random_column)
end
rate() click to toggle source

Gets the rate of “-” in a cube.

# File lib/logic_tools/logicgenerator.rb, line 43
def rate
    return @rate
end
rate=(rate) click to toggle source

Sets the rate of “-” in a cube.

# File lib/logic_tools/logicgenerator.rb, line 48
def rate=(rate)
    @rate = rate
end
seed() click to toggle source

Gets the seed.

# File lib/logic_tools/logicgenerator.rb, line 23
def seed
    @random.seed
end
seed=(value) click to toggle source

Sets the seed to value.

# File lib/logic_tools/logicgenerator.rb, line 28
def seed=(value)
    @random = Random.new(value)
end