lernen 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/lib/lernen/sul.rb ADDED
@@ -0,0 +1,134 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lernen
4
+ # SUL is a System Under Learning.
5
+ #
6
+ # It is an abtraction of a system under learning (SUL) which accepts an operation
7
+ # called "membership query"; it takes an input string (word) and returns a sequence
8
+ # of outputs corresponding to the input string.
9
+ #
10
+ # This SUL assumes the system is much like Mealy machine; that is, a transition puts
11
+ # an output, and query does not accept the empty string due to no outputs.
12
+ #
13
+ # Note that this class is *abstract*. You should implement the following method:
14
+ #
15
+ # - `#step(input)`
16
+ class SUL
17
+ # Creates a SUL from the given block as an implementation of a membership query.
18
+ def self.from_block(cache: true, &) = BlockSUL.new(cache:, &)
19
+
20
+ def initialize(cache: true)
21
+ @cache = cache ? {} : nil
22
+ @num_cached_queries = 0
23
+ @num_queries = 0
24
+ @num_steps = 0
25
+ end
26
+
27
+ # Returns statistics information as a `Hash` object.
28
+ def stats
29
+ {
30
+ num_cache: @cache&.size || 0,
31
+ num_cached_queries: @num_cached_queries,
32
+ num_queries: @num_queries,
33
+ num_steps: @num_steps
34
+ }
35
+ end
36
+
37
+ # Runs a membership query with the given inputs.
38
+ #
39
+ # Note that this method does not accept the empty string due to no outpus.
40
+ # If you need to call `query` with the empty string, you should use `MooreSUL#query_empty` instead.
41
+ def query(inputs)
42
+ cached = @cache && @cache[inputs]
43
+ if cached
44
+ @num_cached_queries += 1
45
+ return cached
46
+ end
47
+
48
+ if inputs.empty?
49
+ raise ArgumentError, "`query` does not accept the empty string. Please use `query_empty` instead."
50
+ end
51
+
52
+ setup
53
+
54
+ outputs = inputs.map { |input| step(input) }
55
+
56
+ shutdown
57
+
58
+ @num_queries += 1
59
+ @num_steps += inputs.size
60
+
61
+ @cache[inputs] = outputs
62
+
63
+ outputs
64
+ end
65
+
66
+ # It is a setup procedure of this SUL.
67
+ #
68
+ # Note that it does nothing by default.
69
+ def setup
70
+ end
71
+
72
+ # It is a shutdown procedure of this SUL.
73
+ #
74
+ # Note that it does nothing by default.
75
+ def shutdown
76
+ end
77
+
78
+ # Consumes the given `input` and returns the correspoding output.
79
+ #
80
+ # This is *abstract*.
81
+ def step(_input)
82
+ raise TypeError, "abstract method: `step`"
83
+ end
84
+ end
85
+
86
+ # MooreSUL is a System Under Learning (SUL) for a system much like Moore machine.
87
+ #
88
+ # By contrast to `SUL`, this accepts a query with the empty string additionally.
89
+ #
90
+ # Note that this class is *abstract*. You should implement the following method:
91
+ #
92
+ # - `#step(input)`
93
+ # - `#query_empty`
94
+ class MooreSUL < SUL
95
+ def initialize(cache: true)
96
+ super
97
+ end
98
+
99
+ # Runs a membership query with the given inputs.
100
+ #
101
+ # This is *abstract*.
102
+ def query_empty
103
+ raise TypeError, "abstract method: `query_empty`"
104
+ end
105
+ end
106
+
107
+ # BlockSUL is a System Under Learning (SUL) constructed from a block.
108
+ #
109
+ # A block is expected to behave like a membership query.
110
+ class BlockSUL < MooreSUL
111
+ def initialize(cache: true, &block)
112
+ super(cache:)
113
+
114
+ @block = block
115
+ @inputs = []
116
+ end
117
+
118
+ # It is a setup procedure of this SUL.
119
+ def setup
120
+ @inputs = []
121
+ end
122
+
123
+ # Runs a membership query with the given inputs.
124
+ def step(input)
125
+ @inputs << input
126
+ @block.call(@inputs)
127
+ end
128
+
129
+ # Runs a membership query with the given inputs.
130
+ def query_empty
131
+ @block.call([])
132
+ end
133
+ end
134
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lernen
4
+ # The version string.
5
+ VERSION = "0.1.0"
6
+ end
data/lib/lernen.rb ADDED
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "set"
4
+
5
+ # Lernen is a simple automata learning library.
6
+ module Lernen
7
+ # Error is an error class for this library.
8
+ class Error < StandardError
9
+ end
10
+ end
11
+
12
+ require_relative "lernen/automaton"
13
+ require_relative "lernen/cex_processor"
14
+ require_relative "lernen/oracle"
15
+ require_relative "lernen/sul"
16
+ require_relative "lernen/version"
17
+
18
+ require_relative "lernen/lstar"
19
+ require_relative "lernen/kearns_vazirani"
20
+ require_relative "lernen/lsharp"
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lernen
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - TSUYUSATO Kitsune
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-08-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: set
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 1.0.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.3
27
+ description: ''
28
+ email:
29
+ - make.just.on@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - ".editorconfig"
35
+ - ".rubocop.yml"
36
+ - ".yardopts"
37
+ - CODE_OF_CONDUCT.md
38
+ - LICENSE.txt
39
+ - README.md
40
+ - Rakefile
41
+ - lib/lernen.rb
42
+ - lib/lernen/automaton.rb
43
+ - lib/lernen/cex_processor.rb
44
+ - lib/lernen/kearns_vazirani.rb
45
+ - lib/lernen/lsharp.rb
46
+ - lib/lernen/lstar.rb
47
+ - lib/lernen/oracle.rb
48
+ - lib/lernen/sul.rb
49
+ - lib/lernen/version.rb
50
+ homepage: https://github.com/makenowjust/lernen
51
+ licenses:
52
+ - MIT
53
+ metadata:
54
+ homepage_uri: https://github.com/makenowjust/lernen
55
+ source_code_uri: https://github.com/makenowjust/lernen
56
+ rubygems_mfa_required: 'true'
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: 3.1.0
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubygems_version: 3.6.0.dev
73
+ signing_key:
74
+ specification_version: 4
75
+ summary: a simple automata leraning library
76
+ test_files: []