Class: Lernen::System::SUL

Inherits:
Object
  • Object
show all
Defined in:
lib/lernen/system/sul.rb

Overview

SUL represents a system under learning.

It is an abtraction of a system under learning (SUL) which accepts an operation called “membership query”; it takes an input string (word) and returns a sequence of outputs corresponding to the input string.

This SUL assumes the system is much like Mealy machine; that is, a transition puts an output, and query does not accept the empty string due to no outputs.

This SUL also implements cache mechanism. The cache mechanism is enabled by the default, and it can be disabled by specifying ‘cache: false`.

On running a SUL, this SUL records the statistics information. We can obtain this information by ‘SUL#stats`.

Note that this class is abstract. You should implement the following method:

  • ‘#step(input)`

Direct Known Subclasses

MooreLikeSUL, TransitionSystemSimulator

Instance Method Summary collapse

Constructor Details

#initialize(cache: true) ⇒ SUL

: (?cache: bool) -> void



34
35
36
37
38
39
# File 'lib/lernen/system/sul.rb', line 34

def initialize(cache: true)
  @cache = cache ? {} : nil
  @num_cached_queries = 0
  @num_queries = 0
  @num_steps = 0
end

Instance Method Details

#query(word) ⇒ Object

Runs a membership query with the given word.

Note that this method does not accept the empty string due to no outpus. If you need to call ‘query` with the empty string, you should use `MooreSUL#query_empty` instead.

: (Array word) -> Array



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/lernen/system/sul.rb', line 66

def query(word)
  cached = (cache = @cache) && cache[word]
  if cached
    @num_cached_queries += 1
    return cached
  end

  if word.empty?
    raise ArgumentError, "`query` does not accept the empty string. Please use `query_empty` instead."
  end

  setup
  outputs = word.map { |input| step(input) }
  shutdown

  @num_queries += 1
  @num_steps += word.size

  cache[word.dup] = outputs if cache

  outputs
end

#query_last(word) ⇒ Object

Runs a membership query with the given word, and returns the last output.

It is the same as ‘query(word).last`.

: (Array word) -> Out



94
# File 'lib/lernen/system/sul.rb', line 94

def query_last(word) = query(word).last # steep:ignore

#setupObject

It is a setup procedure of this SUL.

Note that it does nothing by default.

: () -> void



103
104
# File 'lib/lernen/system/sul.rb', line 103

def setup
end

#shutdownObject

It is a shutdown procedure of this SUL.

Note that it does nothing by default.

: () -> void



111
112
# File 'lib/lernen/system/sul.rb', line 111

def shutdown
end

#statsObject

Returns the statistics information as a ‘Hash` object.

The result hash contains the following values.

  • ‘num_cache`: The number of cached queries.

  • ‘num_cached_queries`: The number of queries uses the cache.

  • ‘num_queries`: The number of (non-cached) queries.

  • ‘num_steps`: The total number of steps.

: () -> Hash[Symbol, Integer]



51
52
53
54
55
56
57
58
# File 'lib/lernen/system/sul.rb', line 51

def stats
  {
    num_cache: @cache&.size || 0,
    num_cached_queries: @num_cached_queries,
    num_queries: @num_queries,
    num_steps: @num_steps
  }
end

#step(input) ⇒ Object

Consumes the given ‘input` and returns the correspoding output.

This is an abstract method.

: (In input) -> Out

Raises:

  • (TypeError)


119
120
121
# File 'lib/lernen/system/sul.rb', line 119

def step(input)
  raise TypeError, "abstract method: `step`"
end