nfa2dfa 1.1.1 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/bin/nfa2dfa +3 -5
- data/lib/automaton.rb +264 -312
- data/lib/nfa2dfa.rb +6 -5
- data/lib/state.rb +72 -67
- data/lib/transition.rb +31 -24
- data/spec/automaton_spec.rb +139 -134
- data/spec/state_spec.rb +100 -101
- data/spec/transition_spec.rb +27 -28
- metadata +1 -1
data/spec/state_spec.rb
CHANGED
@@ -1,101 +1,100 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
require 'spec_helper'
|
3
|
-
require_relative '../lib/state.rb'
|
4
|
-
|
5
|
-
describe Nfa2Dfa::State do
|
6
|
-
before(:each) do
|
7
|
-
@end_state = State.new(
|
8
|
-
@end_state.to_starting_node
|
9
|
-
end
|
10
|
-
context
|
11
|
-
subject { State.new(
|
12
|
-
|
13
|
-
it
|
14
|
-
subject.id.should eq
|
15
|
-
subject.is_final.should eq false
|
16
|
-
end
|
17
|
-
|
18
|
-
it
|
19
|
-
expect { subject.transitions }.to raise_error NoMethodError
|
20
|
-
end
|
21
|
-
|
22
|
-
it
|
23
|
-
trans = Transition.new(subject,
|
24
|
-
transes = subject.add_transition(trans)
|
25
|
-
transes.size.should eq 1
|
26
|
-
transes[0].should eq trans
|
27
|
-
end
|
28
|
-
|
29
|
-
it
|
30
|
-
subject.clear_transitions.size.should eq 0
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
it
|
35
|
-
state_a = State.new(
|
36
|
-
state_a.is_final.should eq false
|
37
|
-
state_a.finalize
|
38
|
-
state_a.is_final.should eq true
|
39
|
-
state_a.finalize
|
40
|
-
state_a.is_final.should eq true
|
41
|
-
end
|
42
|
-
|
43
|
-
|
44
|
-
state_a
|
45
|
-
state_a.
|
46
|
-
state_a.
|
47
|
-
state_a.
|
48
|
-
state_a.
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
trans[
|
58
|
-
trans[
|
59
|
-
trans[
|
60
|
-
trans[
|
61
|
-
|
62
|
-
state_a.add_transition(trans[
|
63
|
-
state_a.add_transition(trans[
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
state_a.get_next(
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
trans[
|
88
|
-
trans[
|
89
|
-
trans[
|
90
|
-
trans[
|
91
|
-
|
92
|
-
subject { State.new(
|
93
|
-
it
|
94
|
-
subject.associate_transitions(trans)
|
95
|
-
subject.get_next(
|
96
|
-
subject.get_next(
|
97
|
-
subject.get_next(
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
101
|
-
|
1
|
+
require 'rspec'
|
2
|
+
require 'spec_helper'
|
3
|
+
require_relative '../lib/state.rb'
|
4
|
+
|
5
|
+
describe Nfa2Dfa::State do
|
6
|
+
before(:each) do
|
7
|
+
@end_state = State.new('end')
|
8
|
+
@end_state.to_starting_node
|
9
|
+
end
|
10
|
+
context 'should keep ' do
|
11
|
+
subject { State.new('state') }
|
12
|
+
|
13
|
+
it 'basic data' do
|
14
|
+
subject.id.should eq 'state'
|
15
|
+
subject.is_final.should eq false
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'transitions as private data' do
|
19
|
+
expect { subject.transitions }.to raise_error NoMethodError
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'added transitions' do
|
23
|
+
trans = Transition.new(subject, 'jump', @end_state)
|
24
|
+
transes = subject.add_transition(trans)
|
25
|
+
transes.size.should eq 1
|
26
|
+
transes[0].should eq trans
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'cleared transition array' do
|
30
|
+
subject.clear_transitions.size.should eq 0
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should be able to be finalized' do
|
35
|
+
state_a = State.new('a')
|
36
|
+
state_a.is_final.should eq false
|
37
|
+
state_a.finalize
|
38
|
+
state_a.is_final.should eq true
|
39
|
+
state_a.finalize
|
40
|
+
state_a.is_final.should eq true
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'should be able to be starting node' do
|
44
|
+
state_a = State.new('a')
|
45
|
+
state_a.is_starting.should eq false
|
46
|
+
state_a.to_starting_node
|
47
|
+
state_a.is_starting.should eq true
|
48
|
+
state_a.to_starting_node
|
49
|
+
state_a.is_starting.should eq true
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'gets correct next state' do
|
53
|
+
trans = []
|
54
|
+
state_a = State.new('a')
|
55
|
+
state_b = State.new('b')
|
56
|
+
state_c = State.new('c')
|
57
|
+
trans[0] = Transition.new(state_a, '0', state_b)
|
58
|
+
trans[1] = Transition.new(state_b, '1', state_a)
|
59
|
+
trans[2] = Transition.new(state_a, '0', state_a)
|
60
|
+
trans[3] = Transition.new(state_c, '1', state_a)
|
61
|
+
trans[4] = Transition.new(state_a, '0', state_c)
|
62
|
+
state_a.add_transition(trans[0])
|
63
|
+
state_a.add_transition(trans[2])
|
64
|
+
state_a.add_transition(trans[4])
|
65
|
+
state_b.add_transition(trans[1])
|
66
|
+
state_c.add_transition(trans[3])
|
67
|
+
|
68
|
+
it 'array' do
|
69
|
+
state_a.get_next('0').size.should eq 3
|
70
|
+
state_a.get_next('1').size.should eq 0
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'in first step' do
|
74
|
+
state_a.get_next('0')[0].should eq state_b
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'in second step' do
|
78
|
+
state_a.get_next('0')[0].get_next('1')[0].should eq state_a
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context 'while conversion from NFA to DFA' do
|
83
|
+
trans = Array.new
|
84
|
+
state_a = State.new('a')
|
85
|
+
state_b = State.new('b')
|
86
|
+
state_c = State.new('c')
|
87
|
+
trans[0] = Transition.new(state_a, '0', state_b)
|
88
|
+
trans[1] = Transition.new(state_b, '1', state_a)
|
89
|
+
trans[2] = Transition.new(state_a, '0', state_a)
|
90
|
+
trans[3] = Transition.new(state_c, '1', state_a)
|
91
|
+
trans[4] = Transition.new(state_a, '0', state_c)
|
92
|
+
subject { State.new('a,b') }
|
93
|
+
it 'should find correct transitions' do
|
94
|
+
subject.associate_transitions(trans)
|
95
|
+
subject.get_next('a').size.should eq 0
|
96
|
+
subject.get_next('1').size.should eq 1
|
97
|
+
subject.get_next('0').size.should eq 3
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
data/spec/transition_spec.rb
CHANGED
@@ -1,28 +1,27 @@
|
|
1
|
-
require 'rspec'
|
2
|
-
require 'spec_helper'
|
3
|
-
require_relative '../lib/transition.rb'
|
4
|
-
|
5
|
-
describe Nfa2Dfa::Transition do
|
6
|
-
context
|
7
|
-
state1 = State.new(
|
8
|
-
state2 = State.new(
|
9
|
-
state2.finalize
|
10
|
-
subject { Transition.new(state1,
|
11
|
-
|
12
|
-
it
|
13
|
-
subject.beginning_state.should eq state1
|
14
|
-
subject.ending_state.should eq state2
|
15
|
-
end
|
16
|
-
|
17
|
-
it
|
18
|
-
subject.alphabet.should eq
|
19
|
-
end
|
20
|
-
|
21
|
-
it
|
22
|
-
subject.beginning_state.class.should eq State
|
23
|
-
subject.ending_state.class.should eq State
|
24
|
-
subject.alphabet.class.should eq String
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
1
|
+
require 'rspec'
|
2
|
+
require 'spec_helper'
|
3
|
+
require_relative '../lib/transition.rb'
|
4
|
+
|
5
|
+
describe Nfa2Dfa::Transition do
|
6
|
+
context 'should keep ' do
|
7
|
+
state1 = State.new('1')
|
8
|
+
state2 = State.new('2')
|
9
|
+
state2.finalize
|
10
|
+
subject { Transition.new(state1, 'a', state2) }
|
11
|
+
|
12
|
+
it 'states' do
|
13
|
+
subject.beginning_state.should eq state1
|
14
|
+
subject.ending_state.should eq state2
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'transition alphabet' do
|
18
|
+
subject.alphabet.should eq 'a'
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'correct types' do
|
22
|
+
subject.beginning_state.class.should eq State
|
23
|
+
subject.ending_state.class.should eq State
|
24
|
+
subject.alphabet.class.should eq String
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|