automata 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in automata.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jico Baligod
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Automata
2
+
3
+ A sweet Ruby gem for creating and simulating deterministic/nondeterministic finite automata, push-down automata, and Turing machines.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'automata'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install automata
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/automata.gemspec ADDED
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/automata/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Jico Baligod"]
6
+ gem.email = ["jico@baligod.com"]
7
+ gem.description = %q{Create and simulate automaton.}
8
+ gem.summary = %q{This gem provides a number of classes to create and simulate Deterministic/Nondeterministic Finite Automata, Push-down Automata, and Turing Machines.}
9
+ gem.homepage = "http://github.com/jico/automata"
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "automata"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Automata::VERSION
17
+
18
+ gem.add_development_dependency "rake"
19
+ gem.add_development_dependency "rspec", "~>2.9.0"
20
+ end
@@ -0,0 +1,20 @@
1
+ states:
2
+ - q1
3
+ - q2
4
+ - q3
5
+ alphabet:
6
+ - '0'
7
+ - '1'
8
+ start: q1
9
+ accept:
10
+ - q3
11
+ transitions:
12
+ q1:
13
+ '0': q2
14
+ '1': q2
15
+ q2:
16
+ '0': q1
17
+ '1': q3
18
+ q3:
19
+ '0': q3
20
+ '1': q3
@@ -0,0 +1,49 @@
1
+ module Automata
2
+
3
+ ##
4
+ # Deterministic Finite Automata.
5
+ #
6
+ # Each state of a DFA must have exactly one transition for each transition
7
+ # defined at creation.
8
+ #
9
+ #--
10
+ # TODO: Check that each state is connected.
11
+ #
12
+ class DFA < StateDiagram
13
+
14
+ ##
15
+ # Verify that the initialized DFA is valid.
16
+ #
17
+ # * *Returns*:
18
+ # Whether or not the DFA is valid (boolean).
19
+ #
20
+ def is_valid?
21
+ @transitions.each do |key, val|
22
+ @alphabet.each { |a| return false unless @transitions[key].has_key? a }
23
+ end
24
+ true
25
+ end
26
+
27
+ ##
28
+ # Determines whether the DFA accepts the given string.
29
+ #
30
+ # * *Args*:
31
+ # - +string* -> The string to use as input for the DFA.
32
+ #
33
+ # * *Returns*:
34
+ # Whether or not the DFA accepts the string (boolean).
35
+ #
36
+ def accepts?(string)
37
+ head = @start
38
+ string.each_char do |symbol|
39
+ head = @transitions[head][symbol]
40
+ end
41
+ is_accept_state? head
42
+ end
43
+
44
+ def is_accept_state?(state)
45
+ @accept.include? state
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,32 @@
1
+ module Automata
2
+
3
+ ##
4
+ # A generic state diagram class represented as a 5-tuple.
5
+ #
6
+ #--
7
+ # TODO: Check for valid transitions against declared states and alphabet.
8
+ #
9
+ class StateDiagram
10
+ attr_accessor :states, :alphabet, :start, :accept, :transitions
11
+
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
28
+ end
29
+
30
+ end
31
+
32
+ end
@@ -0,0 +1,3 @@
1
+ module Automata
2
+ VERSION = "0.0.1"
3
+ end
data/lib/automata.rb ADDED
@@ -0,0 +1,8 @@
1
+ require "automata/version"
2
+ require 'yaml'
3
+ require "automata/state_diagram"
4
+ require "automata/dfa"
5
+
6
+ module Automata
7
+ # Your code goes here...
8
+ end
data/spec/dfa_spec.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'spec_helper'
2
+
3
+ describe Automata::DFA do
4
+ context "Initializing from a valid file" do
5
+ before do
6
+ @dfa = Automata::DFA.new
7
+ @dfa.init_from_file('examples/dfa_sample.yml')
8
+ end
9
+
10
+ it "should be valid" do
11
+ @dfa.is_valid?.should == true
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
+ end
@@ -0,0 +1,14 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper.rb"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+
8
+ require_relative "../lib/automata"
9
+
10
+ RSpec.configure do |config|
11
+ config.treat_symbols_as_metadata_keys_with_true_values = true
12
+ config.run_all_when_everything_filtered = true
13
+ config.filter_run :focus
14
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: automata
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jico Baligod
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70249174639400 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70249174639400
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70249174654560 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 2.9.0
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70249174654560
36
+ description: Create and simulate automaton.
37
+ email:
38
+ - jico@baligod.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - Gemfile
46
+ - LICENSE
47
+ - README.md
48
+ - Rakefile
49
+ - automata.gemspec
50
+ - examples/dfa_sample.yml
51
+ - lib/automata.rb
52
+ - lib/automata/dfa.rb
53
+ - lib/automata/state_diagram.rb
54
+ - lib/automata/version.rb
55
+ - spec/dfa_spec.rb
56
+ - spec/spec_helper.rb
57
+ homepage: http://github.com/jico/automata
58
+ licenses: []
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ segments:
70
+ - 0
71
+ hash: -2006038974877205235
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ segments:
79
+ - 0
80
+ hash: -2006038974877205235
81
+ requirements: []
82
+ rubyforge_project:
83
+ rubygems_version: 1.8.17
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: This gem provides a number of classes to create and simulate Deterministic/Nondeterministic
87
+ Finite Automata, Push-down Automata, and Turing Machines.
88
+ test_files:
89
+ - spec/dfa_spec.rb
90
+ - spec/spec_helper.rb