Class: Lernen::Automaton::Moore

Inherits:
MooreLike show all
Defined in:
lib/lernen/automaton/moore.rb

Overview

Moore represents a [Moore machine](en.wikipedia.org/wiki/Moore_machine).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MooreLike

find_separating_word, #run_empty, #step

Methods inherited from TransitionSystem

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

Constructor Details

#initialize(initial_state, output_function, transition_function) ⇒ Moore

: (

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


20
21
22
23
24
25
26
# File 'lib/lernen/automaton/moore.rb', line 20

def initialize(initial_state, output_function, transition_function)
  super()

  @initial_state = initial_state
  @output_function = output_function
  @transition_function = transition_function
end

Instance Attribute Details

#initial_stateObject (readonly)

: Integer



28
29
30
# File 'lib/lernen/automaton/moore.rb', line 28

def initial_state
  @initial_state
end

#output_functionObject (readonly)

: Hash[Integer, Out]



29
30
31
# File 'lib/lernen/automaton/moore.rb', line 29

def output_function
  @output_function
end

#transition_functionObject (readonly)

: Hash[[Integer, In], Integer]



30
31
32
# File 'lib/lernen/automaton/moore.rb', line 30

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 Moore 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,
) -> Moore[In, Out]


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/lernen/automaton/moore.rb', line 93

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

  output_function = {}
  transition_function.each do |(state, _), next_state|
    [state, next_state].each do |state|
      next if output_function[state]
      output_function[state] = output_alphabet.sample(random:)
    end
  end

  new(0, output_function, transition_function)
end

Instance Method Details

#==(other) ⇒ Object

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

: (untyped other) -> bool



47
48
49
50
# File 'lib/lernen/automaton/moore.rb', line 47

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

#initial_confObject



36
# File 'lib/lernen/automaton/moore.rb', line 36

def initial_conf = initial_state

#output(conf) ⇒ Object



42
# File 'lib/lernen/automaton/moore.rb', line 42

def output(conf) = output_function[conf]

#statesObject

Returns the array of states of this Moore machine.

The result array is sorted.

: () -> Array



57
58
59
60
61
62
63
64
65
66
# File 'lib/lernen/automaton/moore.rb', line 57

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

#step_conf(conf, input) ⇒ Object



39
# File 'lib/lernen/automaton/moore.rb', line 39

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

#to_graphObject



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lernen/automaton/moore.rb', line 69

def to_graph
  nodes =
    states.to_h do |state|
      [state, Graph::Node["#{state} | #{output_function[state].inspect}", :circle]] # steep:ignore
    end

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

  Graph.new(nodes, edges)
end

#typeObject

: () -> :moore



33
# File 'lib/lernen/automaton/moore.rb', line 33

def type = :moore