automata 0.0.7 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -25,6 +25,7 @@ Or install it yourself as:
25
25
  ### Creating a new machine
26
26
 
27
27
  Defining a new machine can be done in two ways:
28
+
28
29
  * From a structured YAML file.
29
30
  * Through class setter methods.
30
31
 
@@ -79,7 +80,7 @@ We can define each property of the DFA in a similar manner.
79
80
  Now that we've built a machine, we can pass it input and let it work its magic. Consider our DFA built using our `examples/dfa_sample.yml` file, which accepts all strings starting with _00_. Let's experiment with some input:
80
81
 
81
82
  # We can make sure it's a valid DFA
82
- >> dfa.is_valid?
83
+ >> dfa.valid?
83
84
  => true
84
85
  >> dfa.accepts? '001'
85
86
  => true
@@ -90,7 +91,7 @@ Awesomesauce.
90
91
 
91
92
  ## Special Characters
92
93
 
93
- * _&_ - Represents an ε-transition (epsilon/empty transition)
94
+ * _&_ - Represents an ε-transition (epsilon transition)
94
95
 
95
96
  ## Contributing
96
97
 
@@ -9,21 +9,21 @@ states:
9
9
  - C
10
10
  - D
11
11
  alphabet:
12
- - '0'
13
- - '1'
12
+ - 0
13
+ - 1
14
14
  start: A
15
15
  accept:
16
16
  - C
17
17
  transitions:
18
18
  A:
19
- '0': B
20
- '1': D
19
+ 0: B
20
+ 1: D
21
21
  B:
22
- '0': C
23
- '1': D
22
+ 0: C
23
+ 1: D
24
24
  C:
25
- '0': C
26
- '1': C
25
+ 0: C
26
+ 1: C
27
27
  D:
28
- '0': D
29
- '1': D
28
+ 0: D
29
+ 1: D
data/examples/nfa_2.yml CHANGED
@@ -11,21 +11,21 @@ states:
11
11
  - C
12
12
  - D
13
13
  alphabet:
14
- - '0'
15
- - '1'
14
+ - 0
15
+ - 1
16
16
  start: B
17
17
  accept:
18
18
  - A
19
19
  - D
20
20
  transitions:
21
21
  A:
22
- '1': A
22
+ 1: A
23
23
  B:
24
24
  '&':
25
25
  - A
26
26
  - C
27
27
  C:
28
- '0':
28
+ 0:
29
29
  - C
30
30
  - D
31
- '1': C
31
+ 1: C
data/lib/automata.rb CHANGED
@@ -5,5 +5,17 @@ require "automata/dfa"
5
5
  require "automata/nfa"
6
6
 
7
7
  module Automata
8
- # Your code goes here...
8
+
9
9
  end
10
+
11
+ class Hash
12
+ # Transforms all keys of a hash to strings.
13
+ #
14
+ # @param [Hash] the Hash whose keys to convert.
15
+ # @return [Hash] the new Hash with strings as keys.
16
+ def self.keys_to_strings(obj)
17
+ return obj unless obj.kind_of? Hash
18
+ obj = obj.inject({}){|h,(k,v)| h[k.to_s] = Hash.keys_to_strings(v); h}
19
+ return obj
20
+ end
21
+ end
data/lib/automata/dfa.rb CHANGED
@@ -11,9 +11,11 @@ module Automata
11
11
  # Iterate through each states to verify the graph
12
12
  # is not disjoint.
13
13
  @transitions.each do |key, val|
14
- @alphabet.each { |a| return false unless @transitions[key].has_key? a }
14
+ @alphabet.each do |a|
15
+ return false unless @transitions[key].has_key? a.to_s
16
+ end
15
17
  end
16
- true
18
+ return true
17
19
  end
18
20
 
19
21
  # Determines whether the DFA accepts a given string.
@@ -21,10 +23,8 @@ module Automata
21
23
  # @param [String] input the string to use as input for the DFA.
22
24
  # @return [Boolean] whether or not the DFA accepts the string.
23
25
  def accepts?(input)
24
- head = @start
25
- input.each_char do |symbol|
26
- head = @transitions[head][symbol]
27
- end
26
+ head = @start.to_s
27
+ input.each_char { |symbol| head = @transitions[head][symbol] }
28
28
  is_accept_state? head
29
29
  end
30
30
 
@@ -33,7 +33,7 @@ module Automata
33
33
  # @param [String] state the state label to check.
34
34
  # @return [Boolean] whether or not the state is an accept state.
35
35
  def is_accept_state?(state)
36
- @accept.include? state
36
+ @accept.include? state.to_s
37
37
  end
38
38
  end
39
39
  end
@@ -28,6 +28,7 @@ module Automata
28
28
  @start = yaml['start'] || params[:start]
29
29
  @accept = yaml['accept'] || params[:accept]
30
30
  @transitions = yaml['transitions'] || params[:transitions]
31
+ @transitions = Hash.keys_to_strings(@transitions)
31
32
  end
32
33
  end
33
34
  end
@@ -1,3 +1,3 @@
1
1
  module Automata
2
- VERSION = "0.0.7"
2
+ VERSION = "0.1.0"
3
3
  end
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.7
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-25 00:00:00.000000000 Z
12
+ date: 2012-04-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70318108152500 !ruby/object:Gem::Requirement
16
+ requirement: &70286071406040 !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: *70318108152500
24
+ version_requirements: *70286071406040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70318108151900 !ruby/object:Gem::Requirement
27
+ requirement: &70286071405420 !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: *70318108151900
35
+ version_requirements: *70286071405420
36
36
  description: Create and simulate automaton.
37
37
  email:
38
38
  - jico@baligod.com
@@ -72,7 +72,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
72
  version: '0'
73
73
  segments:
74
74
  - 0
75
- hash: 149328891026543090
75
+ hash: 3744273630478255702
76
76
  required_rubygems_version: !ruby/object:Gem::Requirement
77
77
  none: false
78
78
  requirements:
@@ -81,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
81
81
  version: '0'
82
82
  segments:
83
83
  - 0
84
- hash: 149328891026543090
84
+ hash: 3744273630478255702
85
85
  requirements: []
86
86
  rubyforge_project:
87
87
  rubygems_version: 1.8.17