Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rephrase in terms of Cubes, for which a class is implemented.
- Loading branch information
Showing
14 changed files
with
371 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
module Cudd | ||
class Cube | ||
|
||
attr_reader :interface | ||
|
||
def initialize(interface, a012) | ||
@interface = interface | ||
@a012 = a012.freeze | ||
end | ||
|
||
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 | ||
|
||
def hash | ||
@hash ||= to_a012.hash + 37*interface.hash | ||
end | ||
|
||
def ==(other) | ||
return nil unless other.is_a?(Cube) && other.interface==interface | ||
to_a012 == other.to_a012 | ||
end | ||
|
||
def to_cube | ||
self | ||
end | ||
|
||
def to_a012 | ||
@a012 | ||
end | ||
|
||
def to_bdd | ||
interface.cube2bdd(to_a012) | ||
end | ||
|
||
def to_truths | ||
to_a012.map{|x| Cube.arg012_to_truth(x)} | ||
end | ||
|
||
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 | ||
|
||
private | ||
|
||
def self.arg012_to_truth(arg012) | ||
arg012 <= 1 ? (arg012 == 1) : nil | ||
end | ||
|
||
def self.truth_to_012(truth) | ||
case truth | ||
when Cudd::BDD then truth.is_complement? ? 0 : 1 | ||
when Integer then (truth >= 0 and truth <= 1) ? truth : 2 | ||
when TrueClass then 1 | ||
when FalseClass then 0 | ||
when NilClass then 2 | ||
else | ||
raise ArgumentError, "Invalid truth value #{truth}" | ||
end | ||
end | ||
|
||
end # class Cube | ||
end # module Cudd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
module Cudd | ||
describe Cube, '==' do | ||
|
||
subject{ cube1 == cube2 } | ||
|
||
before{ x; y; z } | ||
|
||
context 'on equal cubes' do | ||
let(:cube1){ cube(x => true, y => false) } | ||
let(:cube2){ cube(x => true, y => false) } | ||
it{ should be_true } | ||
end | ||
|
||
context 'on non equal cubes' do | ||
let(:cube1){ cube(x => true, y => false) } | ||
let(:cube2){ cube(x => false) } | ||
it{ should be_false } | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper' | ||
module Cudd | ||
describe Cube, 'to_truths' do | ||
|
||
subject{ c.to_truths } | ||
|
||
before{ x; y; z } | ||
|
||
context 'on x' do | ||
let(:c){ cube(x => true) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq([true, nil, nil]) | ||
end | ||
end | ||
|
||
context 'on !x' do | ||
let(:c){ cube(x => false) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq([false, nil, nil]) | ||
end | ||
end | ||
|
||
context 'on x & y' do | ||
let(:c){ cube(x => true, y => true) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq([true, true, nil]) | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require 'spec_helper' | ||
module Cudd | ||
describe Cube, 'new' do | ||
|
||
subject{ Cube.new(bdd_interface, arg) } | ||
|
||
before{ | ||
x; y; z | ||
subject.should be_a(Cube) | ||
} | ||
|
||
shared_examples_for "A cube for x=1,y=0,z=2" do | ||
it 'has [1,0,2] has to_a012' do | ||
subject.to_a012.should eq([1, 0, 2]) | ||
end | ||
end | ||
|
||
context 'when an array of 012' do | ||
let(:arg){ [ 1, 0, 2 ] } | ||
it_behaves_like "A cube for x=1,y=0,z=2" | ||
end | ||
|
||
context 'when an incomplete array of 012' do | ||
let(:arg){ [ 1, 0 ] } | ||
it_behaves_like "A cube for x=1,y=0,z=2" | ||
end | ||
|
||
context 'when truth values' do | ||
let(:arg){ [ true, false, nil ] } | ||
it_behaves_like "A cube for x=1,y=0,z=2" | ||
end | ||
|
||
context 'when an array of BDD variables' do | ||
let(:arg){ [!y, x] } | ||
it_behaves_like "A cube for x=1,y=0,z=2" | ||
end | ||
|
||
context 'when a Hash' do | ||
let(:arg){ {x => 1, y => false} } | ||
it_behaves_like "A cube for x=1,y=0,z=2" | ||
end | ||
|
||
context 'when a BDD' do | ||
let(:arg){ x & !y } | ||
it_behaves_like "A cube for x=1,y=0,z=2" | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper' | ||
module Cudd | ||
describe Cube, 'to_a012' do | ||
|
||
subject{ c.to_a012 } | ||
|
||
before{ x; y; z } | ||
|
||
context 'on x' do | ||
let(:c){ cube(x => true) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq([1, 2, 2]) | ||
end | ||
end | ||
|
||
context 'on !x' do | ||
let(:c){ cube(x => false) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq([0, 2, 2]) | ||
end | ||
end | ||
|
||
context 'on x & y' do | ||
let(:c){ cube(x => true, y => true) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq([1, 1, 2]) | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'spec_helper' | ||
module Cudd | ||
describe Cube, 'to_bdd' do | ||
|
||
subject{ c.to_bdd } | ||
|
||
before{ x; y; z } | ||
|
||
context 'on x' do | ||
let(:c){ cube(x => true) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq(x) | ||
end | ||
end | ||
|
||
context 'on !x' do | ||
let(:c){ cube(x => false) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq(!x) | ||
end | ||
end | ||
|
||
context 'on x & y' do | ||
let(:c){ cube(x => true, y => true) } | ||
|
||
it 'returns the expected bdd' do | ||
subject.should eq(x & y) | ||
end | ||
end | ||
|
||
end | ||
end |
Oops, something went wrong.