moory 0.2.0 → 0.3.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.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +77 -5
- data/images/ab_star.png +0 -0
- data/lib/moory/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 73b9daed021ae037a29e58725761a7d3cab583d3b85adf38f33eeda231f34aac
|
|
4
|
+
data.tar.gz: c582208c25b69c71f0b10d3fea858eee59189756d79d15fea9a6b7344483293d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 91a92a25f4585c8e232a7e49f07d9be7e922ba68a336a94b7ea34da1e318758a4b22f1567d7ade42ab03dbf7aafb4314428e027c1b313e68e16f9c0c819a9912
|
|
7
|
+
data.tar.gz: b0282b7fc84c174549448fd13107136347ae7bfa5b879c49be0b0931fcdf9272602df2f2ad82813a279133742b084e7c908035b2875fe3fbf00b06119fc27aaa
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -3,7 +3,83 @@ Welcome to the Moory gem!
|
|
|
3
3
|
|
|
4
4
|
You can use this gem to create various kinds of finite machines using a simple specification language.
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
Below is an example of of how you might use the Acceptor, but until I can get some tutorial material together, please take a look at the examples directory. Therein you'll find the Acceptor, a Decoder (Mealy machine), and something useless but (hopefully) illuminating. Reading the spec files might also help.
|
|
7
|
+
|
|
8
|
+
## Example: Creating an acceptor for ab*
|
|
9
|
+
|
|
10
|
+
### Motivation
|
|
11
|
+
|
|
12
|
+
Imagine that you want to create an incredible machine; one capable of determining whether a given character string belongs to the language described by the regular expression `ab*`. Naturally,
|
|
13
|
+
you would not reach for a Regexp. That would be far too easy. Instead you derive from
|
|
14
|
+
the regular expression a Deterministic Finite Automaton (DFA) that does the job. Here's what it
|
|
15
|
+
might look like:
|
|
16
|
+
|
|
17
|
+

|
|
18
|
+
|
|
19
|
+
Of course, you've set up and solved a system of equations verifying that the design is correct. But how do we implement this in Ruby? We won't bother trying to apply the State design pattern, because it is a pain. Instead, we will employ Moory which is one of the 200+ gems that exist to make creating finite machines easy.
|
|
20
|
+
|
|
21
|
+
### Implementing the Acceptor
|
|
22
|
+
|
|
23
|
+
The first step is to install the Moory gem, achieved by issuing the following command:
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
gem install moory
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
When that's done, start up `irb` and paste the following:
|
|
30
|
+
|
|
31
|
+
```ruby
|
|
32
|
+
require 'moory'
|
|
33
|
+
|
|
34
|
+
ab_star = Moory::Acceptor.create(
|
|
35
|
+
initial: '0',
|
|
36
|
+
transitions: %q{
|
|
37
|
+
0 : a : 1
|
|
38
|
+
0 : b : 2
|
|
39
|
+
1 : a : 2
|
|
40
|
+
1 : b : 1
|
|
41
|
+
2 : a : 2
|
|
42
|
+
2 : b : 2
|
|
43
|
+
},
|
|
44
|
+
final: %w{ 1 }
|
|
45
|
+
)
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
I'll explain the syntax of the transitions later, but, you have done enough to confidently type:
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
ab_star.accepts?(string: "ab")
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
and be delighted to see that the answer is `true`.
|
|
55
|
+
|
|
56
|
+
If you are naturally curious, then you might want to put `ab_star` through its paces with further candidates:
|
|
57
|
+
|
|
58
|
+
```ruby
|
|
59
|
+
ab_star.accepts?(string: "abbb") # => true
|
|
60
|
+
ab_star.accepts?(string: "aab") # => false
|
|
61
|
+
ab_star.accepts?(string: "aba") # => false
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
We aren't stuck with testing the strings against the initial state. We can ask the machine to begin its match in any state:
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
ab_star.accepts?(string: "bbb", in_state: '1')
|
|
68
|
+
# => true
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
But what about including characters that don't belong to the machine's alphabet?
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
ab_star.accepts?(string: "bbc", in_state: '1')
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
Well this one will be uncermenoiously rejected with a runtime error (unless you are using version 0.1.0, where I forgot to handle bad input, sorry!)
|
|
78
|
+
|
|
79
|
+
### Before you go...
|
|
80
|
+
|
|
81
|
+
There's more to Moory than its Acceptor. I'll show you how to use its other features in another memo.
|
|
82
|
+
|
|
7
83
|
|
|
8
84
|
## Installation
|
|
9
85
|
|
|
@@ -21,10 +97,6 @@ Or install it yourself as:
|
|
|
21
97
|
|
|
22
98
|
$ gem install moory
|
|
23
99
|
|
|
24
|
-
## Usage
|
|
25
|
-
|
|
26
|
-
Please see the examples directory, for now.
|
|
27
|
-
|
|
28
100
|
## Development
|
|
29
101
|
|
|
30
102
|
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.
|
data/images/ab_star.png
ADDED
|
Binary file
|
data/lib/moory/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: moory
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Adam W. Grant
|
|
@@ -72,6 +72,7 @@ files:
|
|
|
72
72
|
- examples/ab_star.rb
|
|
73
73
|
- examples/silly.rb
|
|
74
74
|
- examples/single_as_and_bs.rb
|
|
75
|
+
- images/ab_star.png
|
|
75
76
|
- lib/moory.rb
|
|
76
77
|
- lib/moory/acceptor.rb
|
|
77
78
|
- lib/moory/decoder.rb
|