Class: FiniteState::Dfa

Inherits:
Automaton show all
Defined in:
lib/finite_state/dfa.rb

Instance Attribute Summary

Attributes inherited from Automaton

#states, #transitions

Instance Method Summary collapse

Methods inherited from Automaton

#alphabet, #end_states, #start_states, #to_dot

Constructor Details

#initialize(states, transitions) ⇒ Dfa


3
4
5
6
7
8
9
10
11
12
13
# File 'lib/finite_state/dfa.rb', line 3

def initialize states, transitions
  super(states, transitions)

  if start_states.length < 1
    raise ArgumentError, "There should be at least one start state"
  end

  if !self.valid?
    return Nfa.new(states, transitions)
  end
end

Instance Method Details

#accepts_word?(word) ⇒ Boolean


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/finite_state/dfa.rb', line 24

def accepts_word? word
  current_state = self.start_state
  symbols       = word.chars
  symbols.each do |symbol|
    current_state_copy = current_state
    transitions.each do |t|
      if t.symbol == symbol and current_state == t.from_state
        current_state = t.to_state
      end
    end
    if current_state_copy == current_state
      return false
    end
  end
  current_state.end?
end

#start_stateObject


20
21
22
# File 'lib/finite_state/dfa.rb', line 20

def start_state
  start_states.first
end

#valid?Boolean

!Todo


16
17
18
# File 'lib/finite_state/dfa.rb', line 16

def valid?
  validate_states and validate_transitions
end