Class: Cudd::Cube

Inherits:
Object
  • Object
show all
Defined in:
lib/cudd-rb/cube.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface, a012) ⇒ Cube

Returns a new instance of Cube.



6
7
8
9
# File 'lib/cudd-rb/cube.rb', line 6

def initialize(interface, a012)
  @interface = interface
  @a012      = a012.freeze
end

Instance Attribute Details

#interfaceObject (readonly)

Returns the value of attribute interface.



4
5
6
# File 'lib/cudd-rb/cube.rb', line 4

def interface
  @interface
end

Class Method Details

.new(interface, arg) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cudd-rb/cube.rb', line 11

def self.new(interface, arg)
  return arg if arg.is_a?(Cube)
  if arg.is_a?(BDD)
    super(interface, interface.bdd2cube(arg))
  else
    a012 = Array.new(interface.size, 2)
    enum = arg.is_a?(Hash) ? arg.each_pair : arg.each_with_index
    enum.each do |k,v|
      k,v = v,k unless arg.is_a?(Hash)
      index = [k, v].find{|x| x.is_a?(Cudd::BDD) }.index rescue k
      a012[index] = truth_to_012(v)
    end
    super(interface, a012)
  end
end

Instance Method Details

#==(other) ⇒ Object



31
32
33
34
# File 'lib/cudd-rb/cube.rb', line 31

def ==(other)
  return nil unless other.is_a?(Cube) && other.interface==interface
  to_a012 == other.to_a012
end

#hashObject



27
28
29
# File 'lib/cudd-rb/cube.rb', line 27

def hash
  @hash ||= to_a012.hash + 37*interface.hash
end

#to_a012Object



40
41
42
# File 'lib/cudd-rb/cube.rb', line 40

def to_a012
  @a012
end

#to_bddObject



44
45
46
# File 'lib/cudd-rb/cube.rb', line 44

def to_bdd
  interface.cube2bdd(to_a012)
end

#to_cubeObject



36
37
38
# File 'lib/cudd-rb/cube.rb', line 36

def to_cube
  self
end

#to_dnf(operators = {:not => :'!', :or => :'|', :and => :'&'}) ⇒ Object Also known as: to_s



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cudd-rb/cube.rb', line 61

def to_dnf(operators = {:not => :'!', :or => :'|', :and => :'&'})
  buf = ""
  to_a012.each_with_index do |val, index|
    next if val == 2
    name = interface.ith_var(index).var_name
    name = name.respond_to?(:to_dnf) ? name.to_dnf : name.to_s
    buf << " #{operators[:and]} " unless buf.empty?
    buf << "#{operators[:not]}("  if val==0
    buf << name
    buf << ")" if val==0
  end
  buf
end

#to_hashObject



52
53
54
55
56
57
58
59
# File 'lib/cudd-rb/cube.rb', line 52

def to_hash
  h = {}
  to_a012.each_with_index do |val,index|
    truth = Cube.arg012_to_truth(val)
    h[interface.ith_var(index)] = truth unless truth.nil?
  end
  h
end

#to_truthsObject



48
49
50
# File 'lib/cudd-rb/cube.rb', line 48

def to_truths
  to_a012.map{|x| Cube.arg012_to_truth(x)}
end