cudd-rb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. data/CHANGELOG.md +5 -0
  2. data/Gemfile +12 -0
  3. data/Gemfile.lock +26 -0
  4. data/LICENCE.md +22 -0
  5. data/Manifest.txt +15 -0
  6. data/README.md +7 -0
  7. data/Rakefile +11 -0
  8. data/cudd-rb.gemspec +188 -0
  9. data/cudd-rb.noespec +30 -0
  10. data/lib/cudd-rb/bdd.rb +68 -0
  11. data/lib/cudd-rb/cube.rb +94 -0
  12. data/lib/cudd-rb/errors.rb +4 -0
  13. data/lib/cudd-rb/interfaces/bdd.rb +334 -0
  14. data/lib/cudd-rb/interfaces/root.rb +35 -0
  15. data/lib/cudd-rb/loader.rb +1 -0
  16. data/lib/cudd-rb/manager.rb +78 -0
  17. data/lib/cudd-rb/version.rb +14 -0
  18. data/lib/cudd-rb/wrapper.rb +58 -0
  19. data/lib/cudd-rb.rb +46 -0
  20. data/spec/cube/test_equality.rb +22 -0
  21. data/spec/cube/test_hash.rb +34 -0
  22. data/spec/cube/test_new.rb +49 -0
  23. data/spec/cube/test_to_a012.rb +34 -0
  24. data/spec/cube/test_to_bdd.rb +42 -0
  25. data/spec/cube/test_to_cube.rb +18 -0
  26. data/spec/cube/test_to_dnf.rb +55 -0
  27. data/spec/cube/test_to_hash.rb +34 -0
  28. data/spec/cube/test_to_truths.rb +34 -0
  29. data/spec/cudd/test_manager.rb +35 -0
  30. data/spec/interfaces/bdd/test_and.rb +14 -0
  31. data/spec/interfaces/bdd/test_bdd2cube.rb +32 -0
  32. data/spec/interfaces/bdd/test_bdd2dnf.rb +39 -0
  33. data/spec/interfaces/bdd/test_cofactor.rb +44 -0
  34. data/spec/interfaces/bdd/test_cube.rb +61 -0
  35. data/spec/interfaces/bdd/test_cube2bdd.rb +22 -0
  36. data/spec/interfaces/bdd/test_each_cube.rb +79 -0
  37. data/spec/interfaces/bdd/test_eval.rb +76 -0
  38. data/spec/interfaces/bdd/test_exist.rb +24 -0
  39. data/spec/interfaces/bdd/test_forall.rb +24 -0
  40. data/spec/interfaces/bdd/test_is_complement.rb +26 -0
  41. data/spec/interfaces/bdd/test_ite.rb +18 -0
  42. data/spec/interfaces/bdd/test_ith_var.rb +34 -0
  43. data/spec/interfaces/bdd/test_ith_vars.rb +40 -0
  44. data/spec/interfaces/bdd/test_new_var.rb +36 -0
  45. data/spec/interfaces/bdd/test_new_vars.rb +45 -0
  46. data/spec/interfaces/bdd/test_not.rb +18 -0
  47. data/spec/interfaces/bdd/test_one.rb +26 -0
  48. data/spec/interfaces/bdd/test_one_cube.rb +35 -0
  49. data/spec/interfaces/bdd/test_or.rb +14 -0
  50. data/spec/interfaces/bdd/test_restrict.rb +26 -0
  51. data/spec/interfaces/bdd/test_satisfiable.rb +33 -0
  52. data/spec/interfaces/bdd/test_satisfied.rb +40 -0
  53. data/spec/interfaces/bdd/test_size.rb +33 -0
  54. data/spec/interfaces/bdd/test_support.rb +66 -0
  55. data/spec/interfaces/bdd/test_var_index.rb +26 -0
  56. data/spec/interfaces/bdd/test_var_name.rb +32 -0
  57. data/spec/interfaces/bdd/test_var_names.rb +25 -0
  58. data/spec/interfaces/bdd/test_zero.rb +26 -0
  59. data/spec/manager/test_interface.rb +49 -0
  60. data/spec/shared/a_bdd.rb +15 -0
  61. data/spec/spec_helper.rb +45 -0
  62. data/spec/test_cudd-rb.rb +8 -0
  63. data/spec/wrapper/test_assumptions.rb +28 -0
  64. data/tasks/gem.rake +73 -0
  65. data/tasks/spec_test.rake +71 -0
  66. data/tasks/unit_test.rake +76 -0
  67. data/tasks/yard.rake +51 -0
  68. metadata +218 -0
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'is_complement' do
4
+
5
+ context 'on ZERO' do
6
+ subject{ zero.is_complement? }
7
+ it{ should be_true }
8
+ end
9
+
10
+ context 'on ONE' do
11
+ subject{ one.is_complement? }
12
+ it{ should be_false }
13
+ end
14
+
15
+ context 'on x' do
16
+ subject{ x.is_complement? }
17
+ it{ should be_false }
18
+ end
19
+
20
+ context 'on !x' do
21
+ subject{ !x.is_complement? }
22
+ it{ should be_true }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'ite' do
4
+
5
+ subject{ bdd_interface.ite(x,y,z) }
6
+
7
+ it_behaves_like "a BDD"
8
+
9
+ it 'is equal (x & y) | (!x & z)' do
10
+ subject.should eq((x & y) | (!x & z))
11
+ end
12
+
13
+ it 'is equal to x.ite(y, z)' do
14
+ subject.should eq(x.ite(y,z))
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,34 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'ith_var' do
4
+
5
+ subject{ bdd_interface.ith_var(1) }
6
+
7
+ it_behaves_like "a BDD"
8
+
9
+ it 'has the correct index' do
10
+ subject.var_index.should eq(1)
11
+ end
12
+
13
+ it 'is not one' do
14
+ subject.should_not be_one
15
+ end
16
+
17
+ it 'is not zero' do
18
+ subject.should_not be_zero
19
+ end
20
+
21
+ it 'is equal to itself' do
22
+ subject.should eq(bdd_interface.ith_var(1))
23
+ end
24
+
25
+ it 'is not equal to another one' do
26
+ subject.should_not eq(bdd_interface.ith_var(0))
27
+ end
28
+
29
+ it 'is satisfiable and not a tautology' do
30
+ subject.should be_satisfiable
31
+ subject.should_not be_tautology
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'ith_vars' do
4
+
5
+ subject{ bdd_interface.ith_vars(*Array(arg)) }
6
+
7
+ context "with a single Integer" do
8
+ let(:arg){ 2 }
9
+
10
+ it 'returns [ z ]' do
11
+ subject.should eq([ z ])
12
+ end
13
+ end
14
+
15
+ context "with multiple Integers" do
16
+ let(:arg){ [ 0, 2 ] }
17
+
18
+ it 'returns [ x, z ]' do
19
+ subject.should eq([ x, z ])
20
+ end
21
+ end
22
+
23
+ context "with multiple Integers in an Array" do
24
+ let(:arg){ [[ 0, 2 ]] }
25
+
26
+ it 'returns [ x, z ]' do
27
+ subject.should eq([ x, z ])
28
+ end
29
+ end
30
+
31
+ context "with multiple Integers in a Range" do
32
+ let(:arg){ 0..1 }
33
+
34
+ it 'returns [ x, y ]' do
35
+ subject.should eq([ x, y ])
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'new_var' do
4
+
5
+ context 'without argument' do
6
+ subject{ bdd_interface.new_var }
7
+
8
+ it_behaves_like "a BDD"
9
+
10
+ it 'has the last index' do
11
+ next_index = bdd_interface.size
12
+ subject.var_index.should eq(next_index)
13
+ end
14
+
15
+ it 'has a default name' do
16
+ subject.var_name.should eq(:"v#{subject.var_index}")
17
+ end
18
+ end
19
+
20
+ context 'with a name as argument' do
21
+ subject{ bdd_interface.new_var(:foo) }
22
+
23
+ it_behaves_like "a BDD"
24
+
25
+ it 'has the last index' do
26
+ next_index = bdd_interface.size
27
+ subject.var_index.should eq(next_index)
28
+ end
29
+
30
+ it 'has the provided name' do
31
+ subject.var_name.should eq(:foo)
32
+ end
33
+ end
34
+
35
+ end
36
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'new_vars' do
4
+
5
+ context "with a number of variables" do
6
+ subject{ bdd_interface.new_vars(5) }
7
+
8
+ it 'returns an Array of 5 BDDs' do
9
+ subject.should be_a(Array)
10
+ subject.size.should eq(5)
11
+ subject.each{|bdd| bdd.should be_a(Cudd::BDD) }
12
+ end
13
+
14
+ it 'returns 5 variables of different indexes' do
15
+ start = bdd_interface.size
16
+ subject.map(&:index).should eq((start...start+5).to_a)
17
+ end
18
+ end
19
+
20
+ context 'with a single name' do
21
+ subject{ bdd_interface.new_vars(:foo) }
22
+
23
+ it 'returns an Array of 1 BDD with correct name' do
24
+ subject.map(&:var_name).should eq([:foo])
25
+ end
26
+ end
27
+
28
+ context 'with an array of names' do
29
+ subject{ bdd_interface.new_vars([:foo, :bar]) }
30
+
31
+ it 'returns an array of 2 BDDs with correct names' do
32
+ subject.map(&:var_name).should eq([:foo, :bar])
33
+ end
34
+ end
35
+
36
+ context 'with a varargs of names' do
37
+ subject{ bdd_interface.new_vars(:foo, :bar) }
38
+
39
+ it 'returns an array of 2 BDDs with correct names' do
40
+ subject.map(&:var_name).should eq([:foo, :bar])
41
+ end
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'not' do
4
+
5
+ subject{ bdd_interface.not(x) }
6
+
7
+ it_behaves_like "a BDD"
8
+
9
+ it 'yields its origin if negated' do
10
+ bdd_interface.not(subject).should eq(x)
11
+ end
12
+
13
+ it 'is equal to !x' do
14
+ subject.should eq(!x)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'one' do
4
+
5
+ subject{ one }
6
+
7
+ it_behaves_like "a BDD"
8
+
9
+ it 'is one' do
10
+ subject.should be_one
11
+ subject.true?.should eq(true)
12
+ subject.should be_tautology
13
+ end
14
+
15
+ it 'is not zero' do
16
+ subject.should_not be_zero
17
+ subject.should_not be_contradiction
18
+ subject.false?.should eq(false)
19
+ end
20
+
21
+ it 'is satisfiable' do
22
+ subject.should be_satisfiable
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ module Interfaces
4
+ describe BDD, "one_cube" do
5
+
6
+ subject{ formula.one_cube }
7
+
8
+ before do
9
+ formula.ref
10
+ subject.should be_a(Cube)
11
+ end
12
+
13
+ after do
14
+ formula.deref if formula
15
+ end
16
+
17
+ context 'on (x & y)' do
18
+ let(:formula){ x & y }
19
+
20
+ it 'returns an assignment x => true & y => true' do
21
+ subject.should eq(cube(x => true, y => true))
22
+ end
23
+ end
24
+
25
+ context 'on (x | y)' do
26
+ let(:formula){ x | y }
27
+
28
+ it 'returns a bdd satisfied by x => false, y => true' do
29
+ subject.should eq(cube(x => false, y => true))
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'or' do
4
+
5
+ subject{ bdd_interface.or(x,y) }
6
+
7
+ it_behaves_like "a BDD"
8
+
9
+ it 'is equal to (x | y)' do
10
+ subject.should eq(x | y)
11
+ end
12
+
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'restrict' do
4
+
5
+ subject{ f.restrict(g) }
6
+
7
+ context 'when g is a cube' do
8
+ let(:f){ x & y }
9
+ let(:g){ {x => true} }
10
+
11
+ it_behaves_like "a BDD"
12
+
13
+ it{ should eq(f.cofactor(g)) }
14
+ end
15
+
16
+ context 'when it is a complex bdd' do
17
+ let(:f){ (x & y) | z }
18
+ let(:g){ x.xor(y) }
19
+
20
+ it_behaves_like "a BDD"
21
+
22
+ it { should eq(z) }
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'satisfiable?' do
4
+
5
+ subject{ bdd.satisfiable? }
6
+
7
+ context 'on ZERO' do
8
+ let(:bdd){ zero }
9
+ it{ should be_false }
10
+ end
11
+
12
+ context 'on ONE' do
13
+ let(:bdd){ one }
14
+ it{ should be_true }
15
+ end
16
+
17
+ context 'on a variable' do
18
+ let(:bdd){ x }
19
+ it{ should be_true }
20
+ end
21
+
22
+ context 'on a satisfiable formula' do
23
+ let(:bdd){ x & y }
24
+ it{ should be_true }
25
+ end
26
+
27
+ context 'on a non satisfiable formula' do
28
+ let(:bdd){ x & !x }
29
+ it{ should be_false }
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'satisfied?' do
4
+
5
+ let(:assignment){ { x => true, y => false } }
6
+
7
+ subject{ bdd.satisfied?(assignment) }
8
+
9
+ context 'on ZERO' do
10
+ let(:bdd){ zero }
11
+ it{ should be_false }
12
+ end
13
+
14
+ context 'on ONE' do
15
+ let(:bdd){ one }
16
+ it{ should be_true }
17
+ end
18
+
19
+ context 'on the variable x' do
20
+ let(:bdd){ x }
21
+ it{ should be_true }
22
+ end
23
+
24
+ context 'on the variable y' do
25
+ let(:bdd){ y }
26
+ it{ should be_false }
27
+ end
28
+
29
+ context 'on a non satisfied formula' do
30
+ let(:bdd){ x & y }
31
+ it{ should be_false }
32
+ end
33
+
34
+ context 'on a satisfied formula' do
35
+ let(:bdd){ x | y }
36
+ it{ should be_true }
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'size' do
4
+
5
+ after do
6
+ interface.close if interface
7
+ end
8
+ subject{ interface.size }
9
+
10
+ context 'when built without size' do
11
+ let(:interface){ Cudd.manager.interface(:BDD) }
12
+
13
+ it 'starts at zero by default' do
14
+ subject.should eq(0)
15
+ end
16
+
17
+ it 'counts the number of declared variables' do
18
+ interface.new_var
19
+ interface.new_var
20
+ subject.should eq(2)
21
+ end
22
+ end
23
+
24
+ context 'when built with an initialize bdd size' do
25
+ let(:interface){ Cudd.manager(:numVars => 10).interface(:BDD) }
26
+
27
+ it 'starts at ten by default' do
28
+ subject.should eq(10)
29
+ end
30
+ end
31
+
32
+ end
33
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ module Interfaces
4
+ describe BDD, "support" do
5
+
6
+ subject{ formula.support }
7
+
8
+ before do
9
+ formula.ref
10
+ end
11
+
12
+ after do
13
+ formula.deref if formula
14
+ end
15
+
16
+ context "on ZERO" do
17
+ let(:formula){ zero }
18
+
19
+ it 'returns ONE' do
20
+ subject.should eq(one)
21
+ end
22
+ end
23
+
24
+ context "on ONE" do
25
+ let(:formula){ one }
26
+
27
+ it 'returns ONE' do
28
+ subject.should eq(one)
29
+ end
30
+ end
31
+
32
+ context "on x" do
33
+ let(:formula){ x }
34
+
35
+ it 'returns x' do
36
+ subject.should eq(x)
37
+ end
38
+ end
39
+
40
+ context "on !x" do
41
+ let(:formula){ !x }
42
+
43
+ it 'returns x' do
44
+ subject.should eq(x)
45
+ end
46
+ end
47
+
48
+ context 'on (x & y)' do
49
+ let(:formula){ x & y }
50
+
51
+ it 'returns x & y' do
52
+ subject.should eq(x & y)
53
+ end
54
+ end
55
+
56
+ context 'on (x | !y)' do
57
+ let(:formula){ x | !y }
58
+
59
+ it 'returns x & y' do
60
+ subject.should eq(x & y)
61
+ end
62
+ end
63
+
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'var_index' do
4
+
5
+ it 'returns nil on zero' do
6
+ zero.var_index.should be_nil
7
+ end
8
+
9
+ it 'returns nil on one' do
10
+ one.var_index.should be_nil
11
+ end
12
+
13
+ it 'returns 0 on x' do
14
+ x.var_index.should eq(0)
15
+ end
16
+
17
+ it 'returns 1 on y' do
18
+ y.var_index.should eq(1)
19
+ end
20
+
21
+ it 'returns 2 on z' do
22
+ z.var_index.should eq(2)
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,32 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'var_name' do
4
+
5
+ subject{ bdd_interface.var_name(bdd) }
6
+
7
+ context 'on zero' do
8
+ let(:bdd){ zero }
9
+
10
+ it{ should eq(:zero) }
11
+ end
12
+
13
+ context 'on one' do
14
+ let(:bdd){ one }
15
+
16
+ it{ should eq(:one) }
17
+ end
18
+
19
+ context 'on an unnamed variable' do
20
+ let(:bdd){ x }
21
+
22
+ it { should eq(:v0) }
23
+ end
24
+
25
+ context 'on an named variable' do
26
+ let(:bdd){ bdd_interface.new_var(:foo) }
27
+
28
+ it { should eq(:foo) }
29
+ end
30
+
31
+ end
32
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'var_names' do
4
+
5
+ subject{ bdd_interface.var_names }
6
+
7
+ context 'when no variables have been declared at all' do
8
+ it{ should eq([]) }
9
+ end
10
+
11
+ context 'when some variables have been declared but not named' do
12
+ before{ x; y }
13
+ it{ should eq([ :v0, :v1 ]) }
14
+ end
15
+
16
+ context 'when some variables have even been named' do
17
+ before{
18
+ x; y
19
+ bdd_interface.var_names = [ :x ]
20
+ }
21
+ it{ should eq([ :x, :v1 ]) }
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Interface::BDD, 'zero' do
4
+
5
+ subject{ zero }
6
+
7
+ it_behaves_like "a BDD"
8
+
9
+ it 'is zero' do
10
+ subject.should be_zero
11
+ subject.should be_contradiction
12
+ subject.false?.should eq(true)
13
+ end
14
+
15
+ it 'is not one' do
16
+ subject.should_not be_one
17
+ subject.true?.should eq(false)
18
+ subject.should_not be_tautology
19
+ end
20
+
21
+ it 'is not satisfiable' do
22
+ subject.should_not be_satisfiable
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ module Cudd
3
+ describe Manager, "#interface" do
4
+
5
+ it 'serves itself as Root interface' do
6
+ with_manager do |m|
7
+ i = m.interface(:Root)
8
+
9
+ i.should be_a(Manager)
10
+ i.should be_root_manager
11
+
12
+ i.should be_a(Interface::Root)
13
+ i.should eq(m)
14
+ end
15
+ end
16
+
17
+ it 'serves the BDD interface without error' do
18
+ with_manager do |m|
19
+ manager = m
20
+ i = m.interface(:BDD)
21
+
22
+ i.should be_a(Manager)
23
+ i.should_not be_root_manager
24
+
25
+ i.should be_a(Interface::Root)
26
+ i.should be_a(Interface::BDD)
27
+ end
28
+ end
29
+
30
+ it 'always serves the same Root' do
31
+ with_manager do |m|
32
+ root1 = m.interface(:Root)
33
+ bdd1 = m.interface(:BDD)
34
+ bdd1.interface(:Root).should eq(root1)
35
+ end
36
+ end
37
+
38
+ it 'always serves the same names interface' do
39
+ with_manager do |m|
40
+ bdd1 = m.interface(:BDD)
41
+ bdd2 = m.interface(:BDD)
42
+ bdd3 = bdd1.interface(:BDD)
43
+ bdd1.should eq(bdd2)
44
+ bdd2.should eq(bdd3)
45
+ end
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,15 @@
1
+ shared_examples_for "a BDD" do
2
+
3
+ it 'is a FFI::Pointer' do
4
+ subject.should be_a(FFI::Pointer)
5
+ end
6
+
7
+ it 'includes the Cudd::BDD module' do
8
+ subject.should be_a(Cudd::BDD)
9
+ end
10
+
11
+ it 'has a manager, which is a Cudd::Manager' do
12
+ subject.manager.should be_a(Cudd::Manager)
13
+ end
14
+
15
+ end