Class: Lernen::Automaton::Mealy

Inherits:
TransitionSystem show all
Defined in:
lib/lernen/automaton/mealy.rb

Overview

Mealy represents a [Mealy machine](en.wikipedia.org/wiki/Mealy_machine).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from TransitionSystem

find_separating_word, random_transition_function, #run, #to_dot, #to_mermaid

Constructor Details

#initialize(initial_state, transition_function) ⇒ Mealy

: (

  Integer initial_state,
  Hash[[Integer, In], [Out, Integer]] transition_function
) -> void


18
19
20
21
22
23
# File 'lib/lernen/automaton/mealy.rb', line 18

def initialize(initial_state, transition_function)
  super()

  @initial_state = initial_state
  @transition_function = transition_function
end

Instance Attribute Details

#initial_stateObject (readonly)

: Integer



25
26
27
# File 'lib/lernen/automaton/mealy.rb', line 25

def initial_state
  @initial_state
end

#transition_functionObject (readonly)

: Hash[[Integer, In], [Out, Integer]]



26
27
28
# File 'lib/lernen/automaton/mealy.rb', line 26

def transition_function
  @transition_function
end

Class Method Details

.random(alphabet:, output_alphabet:, min_state_size: 5, max_state_size: 10, num_reachable_paths: 2, random: Random) ⇒ Object

Generates a Mealy machine randomly.

: [In, Out] (

  alphabet: Array[In],
  output_alphabet: Array[Out],
  ?min_state_size: Integer,
  ?max_state_size: Integer,
  ?num_reachable_paths: Integer,
  ?random: Random,
) -> Mealy[In, Out]


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/lernen/automaton/mealy.rb', line 81

def self.random(
  alphabet:,
  output_alphabet:,
  min_state_size: 5,
  max_state_size: 10,
  num_reachable_paths: 2,
  random: Random
)
  raw_transition_function, =
    TransitionSystem.random_transition_function(
      alphabet:,
      min_state_size:,
      max_state_size:,
      num_reachable_paths:,
      random:
    )

  transition_function = {}
  raw_transition_function.each do |(state, input), next_state|
    output = output_alphabet.sample(random:)
    transition_function[[state, input]] = [output, next_state]
  end

  new(0, transition_function)
end

Instance Method Details

#==(other) ⇒ Object

Checks the structural equality between ‘self` and `other`.

: (untyped other) -> bool



40
41
42
# File 'lib/lernen/automaton/mealy.rb', line 40

def ==(other)
  other.is_a?(Mealy) && initial_state == other.initial_state && transition_function == other.transition_function
end

#initial_confObject



32
# File 'lib/lernen/automaton/mealy.rb', line 32

def initial_conf = initial_state

#statesObject

Returns the array of states of this Mealy machine.

The result array is sorted.

: () -> Array



49
50
51
52
53
54
55
56
57
# File 'lib/lernen/automaton/mealy.rb', line 49

def states
  state_set = Set.new
  state_set << initial_state
  transition_function.each do |(state, _), (_, next_state)|
    state_set << state
    state_set << next_state
  end
  state_set.to_a.sort!
end

#step(conf, input) ⇒ Object



35
# File 'lib/lernen/automaton/mealy.rb', line 35

def step(conf, input) = transition_function[[conf, input]]

#to_graphObject



60
61
62
63
64
65
66
67
68
69
# File 'lib/lernen/automaton/mealy.rb', line 60

def to_graph
  nodes = states.to_h { |state| [state, Graph::Node[state.to_s, :circle]] }

  edges =
    transition_function.map do |(state, input), (output, next_state)|
      Graph::Edge[state, "#{input.inspect} | #{output.inspect}", next_state] # steep:ignore
    end

  Graph.new(nodes, edges)
end

#typeObject

: () -> :mealy



29
# File 'lib/lernen/automaton/mealy.rb', line 29

def type = :mealy