Class: Automata::DFA
- Inherits:
-
StateDiagram
- Object
- StateDiagram
- Automata::DFA
- Defined in:
- lib/automata/dfa.rb
Overview
Deterministic Finite Automata.
Instance Attribute Summary
Attributes inherited from StateDiagram
#accept, #alphabet, #start, #states, #transitions
Instance Method Summary collapse
-
#accepts?(input) ⇒ Boolean
Determines whether the DFA accepts a given string.
-
#feed(input) ⇒ Hash
Runs the input on the machine and returns a Hash describing the machine’s final state after running.
-
#is_accept_state?(state) ⇒ Boolean
Determines if a given state is an accept state.
-
#valid? ⇒ Boolean
Verifies that the initialized DFA is valid.
Methods inherited from StateDiagram
Constructor Details
This class inherits a constructor from Automata::StateDiagram
Instance Method Details
#accepts?(input) ⇒ Boolean
Determines whether the DFA accepts a given string.
45 46 47 48 |
# File 'lib/automata/dfa.rb', line 45 def accepts?(input) resp = feed(input) resp[:accept] end |
#feed(input) ⇒ Hash
Runs the input on the machine and returns a Hash describing the machine’s final state after running.
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/automata/dfa.rb', line 29 def feed(input) head = @start.to_s input.each_char { |symbol| head = @transitions[head][symbol] } accept = is_accept_state? head resp = { input: input, accept: accept, head: head } resp end |
#is_accept_state?(state) ⇒ Boolean
Determines if a given state is an accept state.
54 55 56 |
# File 'lib/automata/dfa.rb', line 54 def is_accept_state?(state) @accept.include? state.to_s end |
#valid? ⇒ Boolean
Verifies that the initialized DFA is valid. Checks that each state has a transition for each symbol in the alphabet.
9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/automata/dfa.rb', line 9 def valid? # @todo Check that each state is connected. # Iterate through each states to verify the graph # is not disjoint. @transitions.each do |key, val| @alphabet.each do |a| return false unless @transitions[key].has_key? a.to_s end end return true end |