moory 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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 75925ec5480670be1d13c23a9fa4d27832928abbba286a942a0bf8c92ed3ad4f
4
+ data.tar.gz: 5122d6a48f082ede41fbf06a271e74e87fad0e3b40422e205f34818b23308e90
5
+ SHA512:
6
+ metadata.gz: d02f7cbad65475e7c6161df499496dc4e81142ee67006bb0b037f56aaf5dd6ef4af51da2673d777ba60092f319eb77177ed03995479163743b1644e13c4d7a5a
7
+ data.tar.gz: 2a1b2982658c16da3b5bdae03626bad859a88e45c3ff4469cac244f8083e027a616e3f749b8ae65fc95fc98629ca45ccb6a88bb3cb57af8c8f621cbebf3f98fd
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in moory.gemspec
6
+ gemspec
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ moory (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.7.0)
12
+ rspec-core (~> 3.7.0)
13
+ rspec-expectations (~> 3.7.0)
14
+ rspec-mocks (~> 3.7.0)
15
+ rspec-core (3.7.1)
16
+ rspec-support (~> 3.7.0)
17
+ rspec-expectations (3.7.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.7.0)
20
+ rspec-mocks (3.7.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.7.0)
23
+ rspec-support (3.7.1)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.16)
30
+ moory!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.16.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Adam W. Grant
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # Moory
2
+ Welcome to the Moory gem!
3
+
4
+ You can use this gem to create various kinds of finite machines using a simple specification language.
5
+
6
+ Until I can get some tutorial material together, please take a look at the examples directory. Therein you'll find a Finite Acceptor, a Decoder (Mealy machine), and something useless but (hopefully) illuminating. Reading the spec files might also help.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'moory'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install moory
23
+
24
+ ## Usage
25
+
26
+ Please see the examples directory, for now.
27
+
28
+ ## Development
29
+
30
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
31
+
32
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+
34
+ ## Contributing
35
+
36
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/moory.
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "moory"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,20 @@
1
+ require 'moory'
2
+
3
+ ab_star = Moory::Acceptor.create(
4
+ initial: '0',
5
+ transitions: %q{
6
+ 0 : a : 1
7
+ 0 : b : 2
8
+ 1 : a : 2
9
+ 1 : b : 1
10
+ 2 : a : 2
11
+ 2 : b : 2
12
+ },
13
+ final: %w{ 1 }
14
+ )
15
+
16
+
17
+ ab_star.accepts?(string: "abb")
18
+ # => true
19
+ ab_star.accepts?(string:"abb", in_state: '1')
20
+ # => false
@@ -0,0 +1,34 @@
1
+ require 'moory'
2
+
3
+ silly = Moory::Interpreter.new do |i|
4
+ i.load """
5
+ 0 : a / u / foo : 1
6
+ 0 : b / v / foo : 2
7
+ 1 : a / w / bar : 1
8
+ 1 : b / x / bar : 2
9
+ 2 : a / y / : 1
10
+ 2 : b / z : 2
11
+ """
12
+ end
13
+
14
+ # Tell the interpreter where we want to start
15
+ silly.state = '0'
16
+
17
+ # Define some handlers for the effectors named in the
18
+ # loaded specification.
19
+ silly.effectors = {
20
+ 'foo' => proc { |msg| pp "#{msg} says: fooby fooby foo" },
21
+ 'bar' => proc { |msg| pp "#{msg} says: barby barby bar" },
22
+ }
23
+
24
+ # Tell the interpreter the name of the effector we want to use
25
+ # if a transition has output, but no effector.
26
+ silly.fallback_effector = 'bar'
27
+
28
+
29
+ # Send the interpreter some messages: putm means "PUT Message"
30
+ silly.putm('b') # outputs: "v says: fooby fooby foo"
31
+ silly.putm('b') # outputs: "z says: barby barby bar"
32
+ silly.putm('a') # outputs: "y says: barby barby bar"
33
+ silly.putm('a') # outputs: "w says: barby barby bar"
34
+
@@ -0,0 +1,21 @@
1
+ require 'moory'
2
+ require 'stringio'
3
+
4
+ ostream = StringIO.new
5
+
6
+ mealy = Moory::Decoder.create(
7
+ initial: '0',
8
+ transitions: %q{
9
+ 0 : a / a : 1
10
+ 0 : b / b : 2
11
+ 1 : a : 1
12
+ 1 : b / b : 2
13
+ 2 : a / a : 1
14
+ 2 : b : 2
15
+ },
16
+ ostream: ostream
17
+ )
18
+
19
+ mealy.decode("abbbbbbaaaabaabbba")
20
+
21
+ ostream.string # => "abababa"
@@ -0,0 +1,9 @@
1
+ require "moory/version"
2
+ require "moory/interpreter"
3
+ require "moory/parser"
4
+ require "moory/acceptor"
5
+ require "moory/decoder"
6
+
7
+ module Moory
8
+ # Your code goes here...
9
+ end
@@ -0,0 +1,17 @@
1
+ module Moory
2
+ module Acceptor
3
+ def Acceptor.create(specification)
4
+ Moory::Interpreter.new do |i|
5
+ i.load(specification[:transitions])
6
+
7
+ define_singleton_method(:accepts?) do |string:, in_state: nil|
8
+ i.state = in_state ? in_state : specification[:initial]
9
+
10
+ string.each_char { |c| putm(c) }
11
+
12
+ specification[:final].include?(i.state)
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,20 @@
1
+ module Moory
2
+ module Decoder
3
+ def Decoder.create(config)
4
+ Interpreter.new do |i|
5
+ i.load(config[:transitions])
6
+
7
+ define_singleton_method(:decode) do |string|
8
+ i.state = config[:initial]
9
+ i.fallback_effector = config
10
+ .fetch(:ostream, $stdout)
11
+ .method(:write)
12
+
13
+ string.each_char do |c|
14
+ i.putm(c)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,100 @@
1
+ require 'set'
2
+
3
+ module Moory
4
+ class Interpreter
5
+ attr_accessor :graph, :effectors, :state
6
+ attr_reader :fallback_effector, :default_proc
7
+
8
+ SKIP = proc {}
9
+ WARN = proc { |msg| warn "Did not understand: #{msg}" }
10
+
11
+ def initialize(graph: {}, effectors: {}, default_proc: SKIP, &block)
12
+ @graph = graph
13
+ @effectors = effectors
14
+ @default_proc = default_proc
15
+
16
+ instance_eval &block if block_given?
17
+ end
18
+
19
+ def load(source)
20
+ p = Moory::Parser.new
21
+ @graph.merge!(p.analyse(source))
22
+ end
23
+
24
+ def fallback_effector=(obj)
25
+ candidate = obj.kind_of?(String) ? effectors[obj] : obj
26
+
27
+ @fallback_effector = candidate.respond_to?(:call) ? candidate : nil
28
+ end
29
+
30
+ def default_proc=(obj)
31
+ @default_proc = obj.respond_to?(:call) ? obj : nil
32
+ end
33
+
34
+ def putm(msg)
35
+ warn """
36
+ #{self} received #{msg} before being assigned a state.
37
+ The message will be passed to the default_proc.
38
+ """ unless state
39
+
40
+ understand?(msg) ? respond(msg) : bad_call(msg)
41
+ end
42
+
43
+ def understand?(msg)
44
+ receptors.include?(msg)
45
+ end
46
+
47
+ def receptors
48
+ graph.fetch(state,{}).keys.to_set
49
+ end
50
+
51
+ def states
52
+ @states ||= graph.keys.to_set
53
+ end
54
+
55
+ def alphabet
56
+ @alphabet ||= Set.new(
57
+ graph.each_value.collect { |m| m.keys }.flatten
58
+ )
59
+ end
60
+
61
+ private
62
+
63
+ def respond(msg)
64
+ dispatch(msg)
65
+ resettle(msg)
66
+ end
67
+
68
+ def dispatch(msg)
69
+ _effector, _output = effector(msg), output(msg)
70
+
71
+ if _effector.respond_to?(:call)
72
+ _effector.arity == 0 ?
73
+ _effector.call :
74
+ _effector.call(_output)
75
+ end
76
+ end
77
+
78
+ def resettle(msg)
79
+ @state = graph[state][msg][:state]
80
+ end
81
+
82
+ def effector(msg)
83
+ candidate = graph[state][msg][:effector]
84
+
85
+ if candidate.kind_of?(String)
86
+ effectors[candidate]
87
+ else
88
+ candidate || fallback_effector
89
+ end
90
+ end
91
+
92
+ def output(msg)
93
+ graph[state][msg][:output]
94
+ end
95
+
96
+ def bad_call(msg)
97
+ default_proc.call(msg) if default_proc
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,105 @@
1
+ module Moory
2
+ class Parser
3
+ attr_reader :graph, :staged
4
+
5
+ def initialize
6
+ @graph = {}
7
+ prime
8
+ end
9
+
10
+ def analyse(input)
11
+ input.each_line do |line|
12
+ scan(line); store if valid?
13
+ reset
14
+ end
15
+
16
+ return graph
17
+ end
18
+
19
+ private
20
+
21
+ def scan(string)
22
+ string.chomp.each_char { |c| interpret(c) }
23
+ end
24
+
25
+ def interpret(char)
26
+ if @interpreter.understand?(char)
27
+ @interpreter.putm(char)
28
+ else
29
+ staged.fetch(@focus) { |k| staged[k] = '' } << char
30
+ end
31
+ end
32
+
33
+ def store
34
+ y = graph.fetch(staged['source']) { |k| graph[k] = {} }
35
+ z = y.fetch(staged['stimulus']) { |k| y[k] = {} }
36
+ z.merge!({
37
+ state: staged['target'],
38
+ output: staged['output'],
39
+ effector: staged['effector'] }
40
+ .compact
41
+ )
42
+ end
43
+
44
+ def valid?
45
+ staged['source'] &&
46
+ staged['stimulus'] &&
47
+ staged['target']
48
+ end
49
+
50
+ def prime
51
+ interpreter.state = '0'
52
+ @staged = {}
53
+ source
54
+ end
55
+
56
+ alias reset prime
57
+
58
+ def interpreter
59
+ @interpreter ||= Moory::Interpreter.new(config)
60
+ end
61
+
62
+ def config
63
+ {
64
+ graph: {
65
+ '0' => {
66
+ ':' => { state: '1', effector: 'stimulus' },
67
+ ' ' => { state: '0' },
68
+ '\t' => { state: '0' }
69
+ },
70
+ '1' => {
71
+ '/' => { state: '2', effector: 'output' },
72
+ ':' => { state: '4', effector: 'target' },
73
+ ' ' => { state: '1' },
74
+ '\t' => { state: '1' }
75
+ },
76
+ '2' => {
77
+ ':' => { state: '4', effector: 'target' },
78
+ '/' => { state: '3', effector: 'effector' },
79
+ ' ' => { state: '2' },
80
+ '\t' => { state: '2' }
81
+ },
82
+ '3' => {
83
+ ':' => { state: '4', effector: method(:target) },
84
+ ' ' => { state: '2' },
85
+ '\t' => { state: '2' }
86
+ },
87
+ '4' => {
88
+ ' ' => { state: '4' },
89
+ '\t' => { state: '4' }
90
+ }
91
+ },
92
+ effectors: {
93
+ 'stimulus' => method(:stimulus),
94
+ 'output' => method(:output),
95
+ 'target' => method(:target),
96
+ 'effector' => method(:effector),
97
+ }
98
+ }
99
+ end
100
+
101
+ %w{
102
+ source stimulus target output effector
103
+ }.each { |c| define_method(c) { @focus = c } }
104
+ end
105
+ end
@@ -0,0 +1,3 @@
1
+ module Moory
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,36 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "moory/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "moory"
8
+ spec.version = Moory::VERSION
9
+ spec.authors = ["Adam W. Grant"]
10
+ spec.email = ["adam_grnt@yahoo.co.uk"]
11
+
12
+ spec.summary = %q{Use plain text to define finite machines.}
13
+ spec.license = "MIT"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ # if spec.respond_to?(:metadata)
18
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
19
+ # else
20
+ # raise "RubyGems 2.0 or newer is required to protect against " \
21
+ # "public gem pushes."
22
+ # end
23
+
24
+ # Specify which files should be added to the gem when it is released.
25
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
26
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
27
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "rake", "~> 10.0"
35
+ spec.add_development_dependency "rspec", "~> 3.0"
36
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: moory
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Adam W. Grant
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-07-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - adam_grnt@yahoo.co.uk
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - examples/ab_star.rb
73
+ - examples/silly.rb
74
+ - examples/single_as_and_bs.rb
75
+ - lib/moory.rb
76
+ - lib/moory/acceptor.rb
77
+ - lib/moory/decoder.rb
78
+ - lib/moory/interpreter.rb
79
+ - lib/moory/parser.rb
80
+ - lib/moory/version.rb
81
+ - moory.gemspec
82
+ homepage:
83
+ licenses:
84
+ - MIT
85
+ metadata: {}
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 2.7.6
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Use plain text to define finite machines.
106
+ test_files: []