Class: FiniteState::Automaton

Inherits:
Object
  • Object
show all
Defined in:
lib/finite_state/automaton.rb

Overview

TODO: documentate that shit

Direct Known Subclasses

Dfa, Nfa

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(states, transitions) ⇒ Automaton

Initializes a new Automaton with given states and transitions


11
12
13
14
# File 'lib/finite_state/automaton.rb', line 11

def initialize states, transitions
  @states = states
  @transitions = transitions
end

Instance Attribute Details

#statesObject (readonly)

Returns the value of attribute states


8
9
10
# File 'lib/finite_state/automaton.rb', line 8

def states
  @states
end

#transitionsObject (readonly)

Returns the value of attribute transitions


8
9
10
# File 'lib/finite_state/automaton.rb', line 8

def transitions
  @transitions
end

Instance Method Details

#alphabetObject

Returns the alphabet extracted from the Transitions


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

def alphabet
  alphabet = Array.new
  @transitions.each do |t|
    alphabet << t.symbol
  end
  alphabet.uniq
end

#end_statesObject

returns all end states of the Automaton


32
33
34
# File 'lib/finite_state/automaton.rb', line 32

def end_states
  @states.select { |s| s.end? }
end

#start_statesObject

returns all start states of the Automaton


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

def start_states
  @states.select { |s| s.start? }
end

#to_dot(size = "8,5") ⇒ Object

**Experimental Feature**


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/finite_state/automaton.rb', line 37

def to_dot(size="8,5")
  dot = "// this is an experimental feature \n"
  dot += "digraph automaton {\n"
  dot += "rankdir=LR;\n"
  dot += "size=\"#{size}\";\n"
  dot += "node[shape = doublecircle]; "

  # add all end states
  end_states.each do |state|
    dot += "\"#{state.name}\" "
  end
  dot += ";\n"

  dot += "node[shape = circle];\n"

  # add all transitions
  @transitions.each do |trans|
    dot += "\"#{trans.from_state.name}\" -> \"#{trans.to_state.name}\" [ label = \"#{trans.symbol}\" ];\n"
  end
  dot += "}"
  dot
end