stamina 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. data/CHANGELOG.md +22 -5
  2. data/LICENCE.md +2 -2
  3. data/bin/stamina +1 -7
  4. data/lib/stamina.rb +10 -19
  5. metadata +54 -333
  6. data/.gemtest +0 -0
  7. data/Gemfile +0 -2
  8. data/Gemfile.lock +0 -37
  9. data/Manifest.txt +0 -16
  10. data/README.md +0 -78
  11. data/Rakefile +0 -23
  12. data/example/adl/automaton.adl +0 -49
  13. data/example/adl/sample.adl +0 -53
  14. data/example/basic/characteristic_sample.adl +0 -32
  15. data/example/basic/target.adl +0 -9
  16. data/example/competition/31_test.adl +0 -1500
  17. data/example/competition/31_training.adl +0 -1759
  18. data/lib/stamina/abbadingo.rb +0 -2
  19. data/lib/stamina/abbadingo/random_dfa.rb +0 -48
  20. data/lib/stamina/abbadingo/random_sample.rb +0 -146
  21. data/lib/stamina/adl.rb +0 -298
  22. data/lib/stamina/automaton.rb +0 -1263
  23. data/lib/stamina/automaton/complete.rb +0 -36
  24. data/lib/stamina/automaton/equivalence.rb +0 -55
  25. data/lib/stamina/automaton/metrics.rb +0 -78
  26. data/lib/stamina/automaton/minimize.rb +0 -25
  27. data/lib/stamina/automaton/minimize/hopcroft.rb +0 -116
  28. data/lib/stamina/automaton/minimize/pitchies.rb +0 -64
  29. data/lib/stamina/automaton/strip.rb +0 -16
  30. data/lib/stamina/automaton/walking.rb +0 -363
  31. data/lib/stamina/classifier.rb +0 -52
  32. data/lib/stamina/command.rb +0 -45
  33. data/lib/stamina/command/abbadingo_dfa.rb +0 -81
  34. data/lib/stamina/command/abbadingo_samples.rb +0 -40
  35. data/lib/stamina/command/adl2dot.rb +0 -71
  36. data/lib/stamina/command/classify.rb +0 -48
  37. data/lib/stamina/command/help.rb +0 -27
  38. data/lib/stamina/command/infer.rb +0 -141
  39. data/lib/stamina/command/metrics.rb +0 -51
  40. data/lib/stamina/command/robustness.rb +0 -22
  41. data/lib/stamina/command/score.rb +0 -35
  42. data/lib/stamina/errors.rb +0 -23
  43. data/lib/stamina/ext/math.rb +0 -20
  44. data/lib/stamina/induction/blue_fringe.rb +0 -265
  45. data/lib/stamina/induction/commons.rb +0 -156
  46. data/lib/stamina/induction/rpni.rb +0 -186
  47. data/lib/stamina/induction/union_find.rb +0 -377
  48. data/lib/stamina/input_string.rb +0 -123
  49. data/lib/stamina/loader.rb +0 -1
  50. data/lib/stamina/markable.rb +0 -42
  51. data/lib/stamina/sample.rb +0 -267
  52. data/lib/stamina/scoring.rb +0 -213
  53. data/lib/stamina/utils.rb +0 -1
  54. data/lib/stamina/utils/decorate.rb +0 -81
  55. data/lib/stamina/version.rb +0 -14
  56. data/stamina.gemspec +0 -191
  57. data/stamina.noespec +0 -32
  58. data/tasks/debug_mail.rake +0 -78
  59. data/tasks/debug_mail.txt +0 -13
  60. data/tasks/gem.rake +0 -68
  61. data/tasks/spec_test.rake +0 -79
  62. data/tasks/unit_test.rake +0 -77
  63. data/tasks/yard.rake +0 -51
  64. data/test/stamina/abbadingo/random_dfa_test.rb +0 -16
  65. data/test/stamina/abbadingo/random_sample_test.rb +0 -78
  66. data/test/stamina/adl_test.rb +0 -516
  67. data/test/stamina/automaton/classifier_test.rb +0 -259
  68. data/test/stamina/automaton/complete_test.rb +0 -58
  69. data/test/stamina/automaton/equivalence_test.rb +0 -120
  70. data/test/stamina/automaton/metrics_test.rb +0 -36
  71. data/test/stamina/automaton/minimize/hopcroft_test.rb +0 -15
  72. data/test/stamina/automaton/minimize/minimize_test.rb +0 -55
  73. data/test/stamina/automaton/minimize/pitchies_test.rb +0 -15
  74. data/test/stamina/automaton/minimize/rice_edu_10.adl +0 -16
  75. data/test/stamina/automaton/minimize/rice_edu_10.min.adl +0 -13
  76. data/test/stamina/automaton/minimize/rice_edu_13.adl +0 -13
  77. data/test/stamina/automaton/minimize/rice_edu_13.min.adl +0 -7
  78. data/test/stamina/automaton/minimize/should_strip_1.adl +0 -8
  79. data/test/stamina/automaton/minimize/should_strip_1.min.adl +0 -6
  80. data/test/stamina/automaton/minimize/unknown_1.adl +0 -16
  81. data/test/stamina/automaton/minimize/unknown_1.min.adl +0 -12
  82. data/test/stamina/automaton/strip_test.rb +0 -36
  83. data/test/stamina/automaton/to_dot_test.rb +0 -64
  84. data/test/stamina/automaton/walking/dfa_delta_test.rb +0 -39
  85. data/test/stamina/automaton/walking_test.rb +0 -206
  86. data/test/stamina/automaton_additional_test.rb +0 -190
  87. data/test/stamina/automaton_test.rb +0 -1104
  88. data/test/stamina/exit.rb +0 -3
  89. data/test/stamina/induction/blue_fringe_test.rb +0 -83
  90. data/test/stamina/induction/induction_test.rb +0 -70
  91. data/test/stamina/induction/redblue_mergesamestatebug_expected.adl +0 -19
  92. data/test/stamina/induction/redblue_mergesamestatebug_pta.dot +0 -64
  93. data/test/stamina/induction/redblue_mergesamestatebug_sample.adl +0 -9
  94. data/test/stamina/induction/redblue_universal_expected.adl +0 -4
  95. data/test/stamina/induction/redblue_universal_sample.adl +0 -5
  96. data/test/stamina/induction/rpni_inria_expected.adl +0 -7
  97. data/test/stamina/induction/rpni_inria_sample.adl +0 -9
  98. data/test/stamina/induction/rpni_test.rb +0 -129
  99. data/test/stamina/induction/rpni_test_pta.dot +0 -22
  100. data/test/stamina/induction/rpni_universal_expected.adl +0 -4
  101. data/test/stamina/induction/rpni_universal_sample.adl +0 -4
  102. data/test/stamina/induction/union_find_test.rb +0 -124
  103. data/test/stamina/input_string_test.rb +0 -323
  104. data/test/stamina/markable_test.rb +0 -70
  105. data/test/stamina/randdfa.adl +0 -66
  106. data/test/stamina/sample.adl +0 -4
  107. data/test/stamina/sample_classify_test.rb +0 -149
  108. data/test/stamina/sample_test.rb +0 -290
  109. data/test/stamina/scoring_test.rb +0 -63
  110. data/test/stamina/small_dfa.dot +0 -16
  111. data/test/stamina/small_dfa.gif +0 -0
  112. data/test/stamina/small_nfa.dot +0 -18
  113. data/test/stamina/small_nfa.gif +0 -0
  114. data/test/stamina/stamina_test.rb +0 -80
  115. data/test/stamina/utils/decorate_test.rb +0 -65
  116. data/test/test_all.rb +0 -7
@@ -1,15 +0,0 @@
1
- require File.expand_path("../minimize_test", __FILE__)
2
- module Stamina
3
- class Automaton
4
- module Minimize
5
- class HopcroftTest < MinimizeTest
6
-
7
- def algo
8
- Hopcroft
9
- end
10
-
11
- end # class HopcroftTest
12
- end # module Minimize
13
- end # class Automaton
14
- end # module Stamina
15
-
@@ -1,55 +0,0 @@
1
- require 'test/unit'
2
- require 'stamina/stamina_test'
3
- module Stamina
4
- class Automaton
5
- module Minimize
6
- class MinimizeTest < StaminaTest
7
-
8
- # To be overriden
9
- def algo
10
- nil
11
- end
12
-
13
- def test_on_unknown_1
14
- return unless algo
15
- dfa = load_adl_automaton("unknown_1.adl", __FILE__)
16
- min = load_adl_automaton("unknown_1.min.adl", __FILE__)
17
- assert_equivalent(algo.execute(dfa), min)
18
- end
19
-
20
- # From slide 10 in http://www.clear.rice.edu/comp412/Lectures/L07DFAMin-1up.pdf
21
- def test_on_rice_edu_10
22
- return unless algo
23
- dfa = load_adl_automaton("rice_edu_10.adl", __FILE__)
24
- min = load_adl_automaton("rice_edu_10.min.adl", __FILE__)
25
- assert_equivalent(algo.execute(dfa), min)
26
- end
27
-
28
- # From slide 13 in http://www.clear.rice.edu/comp412/Lectures/L07DFAMin-1up.pdf
29
- def test_on_rice_edu_13
30
- return unless algo
31
- dfa = load_adl_automaton("rice_edu_13.adl", __FILE__)
32
- min = load_adl_automaton("rice_edu_13.min.adl", __FILE__)
33
- assert_equivalent(algo.execute(dfa), min)
34
- end
35
-
36
- def test_it_strips_when_needed
37
- return unless algo
38
- dfa = load_adl_automaton("should_strip_1.adl", __FILE__)
39
- min = load_adl_automaton("should_strip_1.min.adl", __FILE__)
40
- assert_equivalent(algo.execute(dfa), min)
41
- end
42
-
43
- def test_it_has_no_effect_on_already_minimal
44
- return unless algo
45
- dfa = load_adl_automaton("rice_edu_13.min.adl", __FILE__)
46
- min = algo.execute(dfa)
47
- assert_equal dfa.complete.state_count, min.complete.state_count
48
- assert_equivalent(min, dfa)
49
- end
50
-
51
- end # class MinimizeTest
52
- end # module Minimize
53
- end # class Automaton
54
- end # module Stamina
55
-
@@ -1,15 +0,0 @@
1
- require File.expand_path("../minimize_test", __FILE__)
2
- module Stamina
3
- class Automaton
4
- module Minimize
5
- class PitchiesTest < MinimizeTest
6
-
7
- def algo
8
- Pitchies
9
- end
10
-
11
- end # class PitchiesTest
12
- end # module Minimize
13
- end # class Automaton
14
- end # module Stamina
15
-
@@ -1,16 +0,0 @@
1
- 5 10
2
- 0 true false
3
- 1 false false
4
- 2 false false
5
- 3 false false
6
- 4 false true
7
- 0 1 a
8
- 0 2 b
9
- 1 1 a
10
- 1 3 b
11
- 2 1 a
12
- 2 2 b
13
- 3 1 a
14
- 3 4 b
15
- 4 1 a
16
- 4 2 b
@@ -1,13 +0,0 @@
1
- 4 8
2
- 0 true false
3
- 1 false false
4
- 2 false false
5
- 3 false true
6
- 0 1 a
7
- 0 0 b
8
- 1 1 a
9
- 1 2 b
10
- 2 1 a
11
- 2 3 b
12
- 3 1 a
13
- 3 0 b
@@ -1,13 +0,0 @@
1
- 4 7
2
- 0 true false
3
- 1 false true
4
- 2 false true
5
- 3 false true
6
- 0 1 a
7
- 1 2 b
8
- 1 3 c
9
- 2 2 b
10
- 2 3 c
11
- 3 2 b
12
- 3 3 c
13
-
@@ -1,7 +0,0 @@
1
- 2 3
2
- 0 true false
3
- 1 false true
4
- 0 1 a
5
- 1 1 b
6
- 1 1 c
7
-
@@ -1,8 +0,0 @@
1
- 3 3
2
- 0 true false
3
- 1 false true
4
- 2 false false
5
- 0 1 a
6
- 1 0 b
7
- 2 1 b
8
-
@@ -1,6 +0,0 @@
1
- 2 2
2
- 0 true false
3
- 1 false true
4
- 0 1 a
5
- 1 0 b
6
-
@@ -1,16 +0,0 @@
1
- 5 9
2
- 0 true false
3
- 1 false false
4
- 2 false false
5
- 3 false true
6
- 4 false true
7
- 0 1 a
8
- 0 1 b
9
- 0 1 c
10
- 0 2 d
11
- 0 2 f
12
- 1 3 f
13
- 1 3 g
14
- 2 4 f
15
- 2 4 g
16
-
@@ -1,12 +0,0 @@
1
- 3 7
2
- 0 true false
3
- 1 false false
4
- 2 false true
5
- 0 1 a
6
- 0 1 b
7
- 0 1 c
8
- 0 1 d
9
- 0 1 f
10
- 1 2 f
11
- 1 2 g
12
-
@@ -1,36 +0,0 @@
1
- require 'test/unit'
2
- require 'stamina/adl'
3
- require 'stamina/stamina_test'
4
- module Stamina
5
- class Automaton
6
- class StripTest < StaminaTest
7
-
8
- def test_on_all_reachable
9
- assert_equivalent @small_dfa, @small_dfa.strip
10
- end
11
-
12
- def test_with_unreachable_states
13
- dfa = Stamina::ADL.parse_automaton <<-EOF
14
- 3 3
15
- 0 true false
16
- 1 false true
17
- 2 false false
18
- 0 1 a
19
- 1 0 b
20
- 2 1 a
21
- EOF
22
- expected = Stamina::ADL.parse_automaton <<-EOF
23
- 2 2
24
- 0 true false
25
- 1 false true
26
- 0 1 a
27
- 1 0 b
28
- EOF
29
- assert_not_equivalent(expected, dfa)
30
- assert_equivalent(expected, dfa.strip)
31
- end
32
-
33
- end # class StripTest
34
- end # class Automaton
35
- end # module Stamina
36
-
@@ -1,64 +0,0 @@
1
- require 'test/unit'
2
- require 'stamina'
3
- module Stamina
4
- module Utils
5
- class DotTest < Test::Unit::TestCase
6
-
7
- def setup
8
- @automaton = Automaton.new do |a|
9
- add_state(:initial => true, :accepting => false)
10
- add_state(:initial => false, :accepting => true)
11
- add_state(:initial => false, :accepting => true, :error => true)
12
- connect(0, 1, 'a')
13
- connect(1, 0, 'b')
14
- connect(1, 2, 'a')
15
- connect(2, 2, nil)
16
- end
17
- end
18
-
19
- def test_attributes2dot
20
- attrs = {:label => 'hello'}
21
- assert_equal 'label="hello"', @automaton.send(:attributes2dot, attrs)
22
- attrs = {:label => 'hello', :color => 'red'}
23
- assert_equal 'color="red" label="hello"', @automaton.send(:attributes2dot, attrs)
24
- attrs = {:label => 'O"Neil'}
25
- assert_equal 'label="O\"Neil"', @automaton.send(:attributes2dot, attrs)
26
- end
27
-
28
- def test_automaton_to_dot_with_default_rewriter
29
- expected = <<-EOF
30
- #digraph G {
31
- # graph [rankdir="LR"];
32
- # 0 [color="black" fillcolor="green" shape="circle" style="filled"];
33
- # 1 [color="black" fillcolor="white" shape="doublecircle" style="filled"];
34
- # 2 [color="black" fillcolor="red" shape="doublecircle" style="filled"];
35
- # 0 -> 1 [label="a"];
36
- # 1 -> 0 [label="b"];
37
- # 1 -> 2 [label="a"];
38
- # 2 -> 2 [label=""];
39
- #}
40
- EOF
41
- expected = expected.gsub(/^\s+#/,'')
42
- assert_equal expected, @automaton.to_dot
43
- end
44
-
45
- def test_automaton_with_specific_rewriter
46
- expected = <<-EOF
47
- #digraph G {
48
- # graph [];
49
- # 0 [accepting="false" initial="true"];
50
- # 1 [accepting="true" initial="false"];
51
- # 2 [accepting="true" error="true" initial="false"];
52
- # 0 -> 1 [symbol="a"];
53
- # 1 -> 0 [symbol="b"];
54
- # 1 -> 2 [symbol="a"];
55
- # 2 -> 2 [symbol=""];
56
- #}
57
- EOF
58
- expected = expected.gsub(/^\s+#/,'')
59
- assert_equal expected, @automaton.to_dot {|elm,kind| elm.data}
60
- end
61
-
62
- end
63
- end
64
- end
@@ -1,39 +0,0 @@
1
- require 'test/unit'
2
- require 'stamina/stamina_test'
3
- module Stamina
4
- class Automaton
5
- # Tests Walking module on Automaton class
6
- class WalkingTestDfaDelta < StaminaTest
7
-
8
- DFA = Stamina::ADL::parse_automaton <<-EOF
9
- 2 3
10
- 0 true false
11
- 1 false true
12
- 0 1 a
13
- 1 0 b
14
- 1 1 a
15
- EOF
16
- S0 = DFA.ith_state(0)
17
- S1 = DFA.ith_state(1)
18
-
19
- def test_with_single_state_as_from
20
- assert_equal S1, DFA.dfa_delta(S0, 'a')
21
- assert_equal S1, DFA.dfa_delta(0, 'a')
22
- assert_equal S0, DFA.dfa_delta(1, 'b')
23
- end
24
-
25
- def test_with_single_array_as_from
26
- assert_equal [S1], DFA.dfa_delta([S0], 'a')
27
- assert_equal [S1], DFA.dfa_delta([0], 'a')
28
- assert_equal [S0], DFA.dfa_delta([1], 'b')
29
- end
30
-
31
- def test_with_multiple_array_as_from
32
- assert_equal [S1], DFA.dfa_delta([S0, S1], 'a')
33
- assert_equal [S1], DFA.dfa_delta([0, 1], 'a')
34
- assert_equal [S0], DFA.dfa_delta([0, 1], 'b')
35
- end
36
-
37
- end # class WalkingTestDfaDelta
38
- end # class Automaton
39
- end # module Stamina
@@ -1,206 +0,0 @@
1
- require 'test/unit'
2
- require 'stamina/stamina_test'
3
- module Stamina
4
- class Automaton
5
- # Tests Walking module on Automaton class
6
- class WalkingTest < StaminaTest
7
-
8
- # Tests Walking#step on examples
9
- def test_step_on_examples
10
- assert_equal([], @small_dfa.step(0, 'b'))
11
- assert_equal(@small_dfa.ith_states(1), @small_dfa.step(0, 'a'))
12
- assert_equal(@small_dfa.ith_states(1,3), @small_dfa.step([0,1], 'a').sort)
13
- assert_equal(@small_dfa.ith_states(1), @small_dfa.step([0,2], 'a').sort)
14
-
15
- assert_equal([], @small_nfa.step(0, 'b'))
16
- assert_equal(@small_nfa.ith_states(1), @small_nfa.step(0, 'a'))
17
- assert_equal([], @small_nfa.step(2, 'b'))
18
- assert_equal(@small_nfa.ith_states(2,3), @small_nfa.step(1, 'b').sort)
19
- assert_equal(@small_nfa.ith_states(0,1), @small_nfa.step([0,3], 'a').sort)
20
- end
21
-
22
- # Tests Walking#dfa_step on examples
23
- def test_step_on_examples
24
- assert_equal(nil, @small_dfa.dfa_step(0, 'b'))
25
- assert_equal(@small_dfa.ith_state(1), @small_dfa.dfa_step(0, 'a'))
26
- assert_equal(@small_dfa.ith_states(1), @small_dfa.dfa_step([0], 'a'))
27
- assert_equal(@small_dfa.ith_states(1,3), @small_dfa.dfa_step([0,1], 'a').sort)
28
- assert_equal(@small_dfa.ith_states(1), @small_dfa.dfa_step([0,2], 'a').sort)
29
- end
30
-
31
- # Tests Walking#delta on examples
32
- def test_delta_on_examples
33
- assert_equal([], @small_dfa.delta(0, 'b'))
34
- assert_equal(@small_dfa.ith_states(1), @small_dfa.delta(0, 'a'))
35
- assert_equal(@small_dfa.ith_states(1,3), @small_dfa.delta([0,1], 'a').sort)
36
- assert_equal(@small_dfa.ith_states(1), @small_dfa.delta([0,2], 'a').sort)
37
-
38
- assert_equal([], @small_nfa.delta(0, 'b'))
39
- assert_equal(@small_nfa.ith_states(1), @small_nfa.delta(0, 'a'))
40
- assert_equal(@small_nfa.ith_states(1,2,3), @small_nfa.delta(2, 'b').sort)
41
- assert_equal(@small_nfa.ith_states(1,2,3), @small_nfa.delta(1, 'b').sort)
42
- assert_equal(@small_nfa.ith_states(0,1), @small_nfa.delta([0,3], 'a').sort)
43
- end
44
-
45
- # Tests Walking#dfa_delta on examples
46
- def test_delta_on_examples
47
- assert_equal(nil, @small_dfa.dfa_delta(0, 'b'))
48
- assert_equal(@small_dfa.ith_state(1), @small_dfa.dfa_delta(0, 'a'))
49
- assert_equal(@small_dfa.ith_states(1,3), @small_dfa.dfa_delta([0,1], 'a').sort)
50
- assert_equal(@small_dfa.ith_states(1), @small_dfa.dfa_delta([0,2], 'a').sort)
51
- end
52
-
53
- # Tests Walking#split on examples
54
- def test_split_on_examples
55
- assert_equal([[], @small_dfa.ith_states(3), []], @small_dfa.split('?'))
56
- assert_equal([[], @small_dfa.ith_states(3), ['a']], @small_dfa.split('? a'))
57
- assert_equal([['b'], @small_dfa.ith_states(2), []], @small_dfa.split('? b'))
58
- assert_equal([['b'], @small_dfa.ith_states(2), ['a']], @small_dfa.split('? b a'))
59
- assert_equal([['b','c'], @small_dfa.ith_states(0), []], @small_dfa.split('? b c'))
60
-
61
- assert_equal([[], @small_nfa.ith_states(0,3), []], @small_nfa.split('?'))
62
- assert_equal([[], @small_nfa.ith_states(0,3), ['b']], @small_nfa.split('? b'))
63
- assert_equal([['a'], @small_nfa.ith_states(0,1), []], @small_nfa.split('? a',nil,true))
64
- assert_equal([['a'], @small_nfa.ith_states(0,1), ['c']], @small_nfa.split('? a c',nil,true))
65
- assert_equal([['a','b'], @small_nfa.ith_states(1,2,3), []], @small_nfa.split('? a b',nil,true))
66
- end
67
-
68
- # Tests Walking#dfa_split on examples
69
- def test_split_on_examples
70
- assert_equal([[], @small_dfa.ith_state(3), []], @small_dfa.dfa_split('?'))
71
- assert_equal([[], @small_dfa.ith_state(3), ['a']], @small_dfa.dfa_split('? a'))
72
- assert_equal([['b'], @small_dfa.ith_state(2), []], @small_dfa.dfa_split('? b'))
73
- assert_equal([['b'], @small_dfa.ith_state(2), ['a']], @small_dfa.dfa_split('? b a'))
74
- assert_equal([['b','c'], @small_dfa.ith_state(0), []], @small_dfa.dfa_split('? b c'))
75
- assert_equal([['b','c'], @small_dfa.ith_states(0), []], @small_dfa.dfa_split('? b c',[3]))
76
- end
77
-
78
- # Tests Walking#reached on examples
79
- def test_reached_on_examples
80
- assert_equal([], @small_dfa.reached('? a a'))
81
- assert_equal(@small_dfa.ith_states(2), @small_dfa.reached('? b'))
82
- assert_equal(@small_dfa.ith_states(1), @small_dfa.reached('? b c a c'))
83
- assert_equal(@small_dfa.ith_states(1), @small_dfa.reached('? a c', @small_dfa.ith_state(0)))
84
- assert_equal(@small_dfa.ith_states(1), @small_dfa.reached('? a c',0))
85
- assert_equal(@small_dfa.ith_states(1), @small_dfa.reached('? a',[0]))
86
- assert_equal(@small_dfa.ith_states(1,3), @small_dfa.reached('? a',[0,1]))
87
- assert_equal(@small_dfa.ith_states(2), @small_dfa.reached('? b',[3,1]))
88
- assert_equal(@small_dfa.ith_states(2), @small_dfa.reached('? b',[0,3,1]))
89
-
90
- assert_equal(@small_nfa.ith_states(0,3), @small_nfa.reached('?').sort)
91
- assert_equal(@small_nfa.ith_states(0,1), @small_nfa.reached('? a').sort)
92
- assert_equal(@small_nfa.ith_states(1,2,3), @small_nfa.reached('? a b').sort)
93
- end
94
-
95
- # Tests Walking#dfa_reached on examples
96
- def test_dfa_reached_on_examples
97
- assert_equal(nil, @small_dfa.dfa_reached('? a a'))
98
- assert_equal(@small_dfa.ith_state(2), @small_dfa.dfa_reached('? b'))
99
- assert_equal(@small_dfa.ith_state(1), @small_dfa.dfa_reached('? b c a c'))
100
- assert_equal(@small_dfa.ith_state(1), @small_dfa.dfa_reached('? a c', @small_dfa.ith_state(0)))
101
- assert_equal(@small_dfa.ith_state(1), @small_dfa.dfa_reached('? a c',0))
102
- assert_equal(@small_dfa.ith_states(1), @small_dfa.dfa_reached('? a',[0]))
103
- assert_equal(@small_dfa.ith_states(1,3), @small_dfa.dfa_reached('? a',[0,1]))
104
- assert_equal(@small_dfa.ith_states(2), @small_dfa.dfa_reached('? b',[3,1]))
105
- assert_equal(@small_dfa.ith_states(2), @small_dfa.dfa_reached('? b',[0,3,1]))
106
- end
107
-
108
- # Tests Walking#dfa_reached
109
- def test_dfa_reached_on_simple_deterministic_automaton
110
- s0, s1 = nil
111
- fa = Automaton.new do |fa|
112
- s0 = fa.add_state(:initial => true)
113
- s1 = fa.add_state
114
- fa.connect(s0, s1, 'a')
115
- fa.connect(s1, s0, 'b')
116
- end
117
- assert_equal(s0, fa.dfa_reached('? '))
118
- assert_equal(s1, fa.dfa_reached('? a'))
119
- assert_equal(s0, fa.dfa_reached('? a b'))
120
- assert_equal(s1, fa.dfa_reached('? a b a'))
121
- assert_equal(s0, fa.dfa_reached('? a b a b'))
122
- assert_nil(fa.dfa_reached('? a a'))
123
- assert_nil(fa.dfa_reached('? b'))
124
- assert_nil(fa.dfa_reached('? a b b'))
125
- end
126
-
127
- # Tests Walking#reached on a deterministic automaton
128
- def test_reached_on_simple_deterministic_automaton
129
- s0, s1 = nil
130
- fa = Automaton.new do |fa|
131
- s0 = fa.add_state(:initial => true)
132
- s1 = fa.add_state
133
- fa.connect(s0, s1, 'a')
134
- fa.connect(s1, s0, 'b')
135
- end
136
- assert_equal([s0], fa.reached('?'))
137
- assert_equal([s1], fa.reached('? a'))
138
- assert_equal([s0], fa.reached('? a b'))
139
- assert_equal([s1], fa.reached('? a b a'))
140
- assert_equal([s0], fa.reached('? a b a b'))
141
- assert_equal([], fa.reached('? a a'))
142
- assert_equal([], fa.reached('? b'))
143
- assert_equal([], fa.reached('? a b b'))
144
- end
145
-
146
- # Tests Walking#reached on a non-deterministic automaton.
147
- def test_reached_on_non_deterministic_automaton
148
- s0, s1, s2, s3, s4 = nil
149
- fa = Automaton.new do |fa|
150
- s0 = fa.add_state(:initial => true) #
151
- s1, s2, s3, s4 = fa.add_n_states(4) # s1 -b-> s3
152
- fa.connect(s0, s1, 'a') # a
153
- fa.connect(s0, s2, 'a') # s0
154
- fa.connect(s1, s3, 'b') # a
155
- fa.connect(s2, s4, 'c') # s2 -c-> s4
156
- end #
157
- assert_equal([], s2.delta('b'))
158
- assert_equal([s0], fa.reached('?'))
159
- assert_equal([], fa.reached('? c'))
160
- assert_equal([s1,s2], fa.reached('? a').sort)
161
- assert_equal([s3], fa.reached('? a b'))
162
- assert_equal([s4], fa.reached('? a c'))
163
- assert_equal([s1,s2], fa.reached('? a').sort)
164
-
165
- # add a looping b on s2
166
- fa.connect(s2, s2, 'b')
167
- assert_equal([s2,s3], fa.reached('? a b').sort)
168
-
169
- # add an epsilon from s2 to s1
170
- fa.connect(s2, s1, nil)
171
- assert_equal([s1,s2], fa.reached('? a').sort)
172
- assert_equal([s1,s2,s3], fa.reached('? a b').sort)
173
- end
174
-
175
- # Tests Walking#accepts? and Walking#rejects?
176
- def test_accepts_and_rejects
177
- fa = Automaton.new do
178
- add_state(:initial => true)
179
- add_state(:accepting => true)
180
- add_state(:error => true)
181
- add_state(:accepting => true, :error => true)
182
- connect(0,1,'a')
183
- connect(1,0,'b')
184
- connect(0,2,'b')
185
- connect(1,3,'a')
186
- end
187
- assert_equal(false, fa.accepts?("?"))
188
- assert_equal(true, fa.accepts?("? a"))
189
- assert_equal(false, fa.accepts?("? a b"))
190
- assert_equal(true, fa.accepts?("? a b a"))
191
- assert_equal(false, fa.accepts?("? z"), "not accepts? on no state")
192
- assert_equal(false, fa.accepts?("? b"), "not accepts? on non accepting error state")
193
- assert_equal(false, fa.accepts?("? a a"), "not accepts? on accepting error state")
194
-
195
- assert_equal(true, fa.rejects?("?"))
196
- assert_equal(false, fa.rejects?("? a"))
197
- assert_equal(true, fa.rejects?("? a b"))
198
- assert_equal(false, fa.rejects?("? a b a"))
199
- assert_equal(true, fa.rejects?("? z"), "rejects? on no state")
200
- assert_equal(true, fa.rejects?("? b"), "rejects? on non accepting error state")
201
- assert_equal(true, fa.rejects?("? a a"), "rejects? on accepting error state")
202
- end
203
-
204
- end # class WalkingTest
205
- end # class Automaton
206
- end # module Stamina