automata 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/automata/dfa.rb CHANGED
@@ -17,7 +17,7 @@ module Automata
17
17
  # * *Returns*:
18
18
  # Whether or not the DFA is valid (boolean).
19
19
  #
20
- def is_valid?
20
+ def valid?
21
21
  @transitions.each do |key, val|
22
22
  @alphabet.each { |a| return false unless @transitions[key].has_key? a }
23
23
  end
@@ -10,21 +10,13 @@ module Automata
10
10
  attr_accessor :states, :alphabet, :start, :accept, :transitions
11
11
 
12
12
  def initialize(params={})
13
- @states = params[:states]
14
- @alphabet = params[:alphabet]
15
- @start = params[:start]
16
- @accept = params[:accept]
17
- @transitions = params[:transitions]
18
- end
19
-
20
- def init_from_file(filename)
21
- yaml = YAML::load_file(filename)
22
- @states = yaml['states']
23
- @alphabet = yaml['alphabet']
24
- @start = yaml['start']
25
- @accept = yaml['accept']
26
- @transitions = yaml['transitions']
27
- yaml
13
+ yaml = {}
14
+ yaml = YAML::load_file(params[:file]) if params.has_key? :file
15
+ @states = yaml['states'] || params[:states]
16
+ @alphabet = yaml['alphabet'] || params[:alphabet]
17
+ @start = yaml['start'] || params[:start]
18
+ @accept = yaml['accept'] || params[:accept]
19
+ @transitions = yaml['transitions'] || params[:transitions]
28
20
  end
29
21
 
30
22
  end
@@ -1,3 +1,3 @@
1
1
  module Automata
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/spec/dfa_spec.rb CHANGED
@@ -1,14 +1,75 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Automata::DFA do
4
+
4
5
  context "Initializing from a valid file" do
5
6
  before do
6
- @dfa = Automata::DFA.new
7
- @dfa.init_from_file('examples/dfa_sample.yml')
7
+ @dfa = Automata::DFA.new(file: 'examples/dfa_sample.yml')
8
8
  end
9
9
 
10
10
  it "should be valid" do
11
- @dfa.is_valid?.should == true
11
+ @dfa.should be_valid
12
+ end
13
+
14
+ it "should accept '01'" do
15
+ @dfa.accepts?('01').should == true
16
+ end
17
+
18
+ it "should accept '001101'" do
19
+ @dfa.accepts?('001101').should == true
20
+ end
21
+
22
+ it "should not accept the empty string" do
23
+ @dfa.accepts?('').should == false
24
+ end
25
+
26
+ it "should not accept '10'" do
27
+ @dfa.accepts?('10').should == false
28
+ end
29
+ end
30
+
31
+ context "Initializing an empty DFA" do
32
+ before do
33
+ @dfa = Automata::DFA.new
34
+ end
35
+
36
+ it "should be created successfully" do
37
+ @dfa.should be_an_instance_of Automata::DFA
38
+ end
39
+ end
40
+
41
+ context "Initializing a DFA by params" do
42
+ before do
43
+ states = ['q1','q2','q3']
44
+ alphabet = ['0','1']
45
+ start = 'q1'
46
+ accept = ['q3']
47
+ transitions = {
48
+ 'q1' => {
49
+ '0' => 'q2',
50
+ '1' => 'q2'
51
+ },
52
+ 'q2' => {
53
+ '0' => 'q1',
54
+ '1' => 'q3'
55
+ },
56
+ 'q3' => {
57
+ '0' => 'q3',
58
+ '1' => 'q3'
59
+ }
60
+ }
61
+ params = {
62
+ states: states,
63
+ alphabet: alphabet,
64
+ start: start,
65
+ accept: accept,
66
+ transitions: transitions
67
+ }
68
+ @dfa = Automata::DFA.new(params)
69
+ end
70
+
71
+ it "should create a valid DFA" do
72
+ @dfa.should be_valid
12
73
  end
13
74
 
14
75
  it "should accept '01'" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: automata
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-04-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70249174639400 !ruby/object:Gem::Requirement
16
+ requirement: &70316676891620 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70249174639400
24
+ version_requirements: *70316676891620
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70249174654560 !ruby/object:Gem::Requirement
27
+ requirement: &70316676891100 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: 2.9.0
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70249174654560
35
+ version_requirements: *70316676891100
36
36
  description: Create and simulate automaton.
37
37
  email:
38
38
  - jico@baligod.com
@@ -68,7 +68,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
68
68
  version: '0'
69
69
  segments:
70
70
  - 0
71
- hash: -2006038974877205235
71
+ hash: -1178858470182539386
72
72
  required_rubygems_version: !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
@@ -77,7 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  version: '0'
78
78
  segments:
79
79
  - 0
80
- hash: -2006038974877205235
80
+ hash: -1178858470182539386
81
81
  requirements: []
82
82
  rubyforge_project:
83
83
  rubygems_version: 1.8.17