Class: Nfa2Dfa::Automaton
- Inherits:
-
Object
- Object
- Nfa2Dfa::Automaton
- Defined in:
- lib/automaton.rb
Overview
Representation of Automaton
Class Method Summary collapse
Instance Method Summary collapse
- #accepts?(data) ⇒ Boolean
- #determine ⇒ Object
- #deterministic? ⇒ Boolean
- #to_graph(path) ⇒ Object
- #to_str ⇒ Object
Class Method Details
.init(path) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/automaton.rb', line 35 def self.init(path) File.file?(path) ? nil : (return nil) data_arr = [] index = 0 File.open(path).each_line do |line| data_arr[index] = line index = index + 1 end validate(data_arr) ? get_valid_input(data_arr) : (return nil) end |
.validate(data_arr) ⇒ Object
46 47 48 49 50 51 52 53 |
# File 'lib/automaton.rb', line 46 def self.validate(data_arr) parsed = [] data_arr.each do |item| parsed.push item.split(' ') end validate_transitions(parsed[0], parsed[1], parsed[2]) && validate_states(parsed[0], parsed[3], parsed[4]) end |
Instance Method Details
#accepts?(data) ⇒ Boolean
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/automaton.rb', line 55 def accepts?(data) formatted_input = data.split(' ') @stack = [] @stack.push(@starting_state) if formatted_input.size == 0 @starting_state.is_final else recurs_accepts?(formatted_input, 0) end end |
#determine ⇒ Object
75 76 77 |
# File 'lib/automaton.rb', line 75 def determine deterministic? ? self : determine_prot end |
#deterministic? ⇒ Boolean
66 67 68 69 70 71 72 73 |
# File 'lib/automaton.rb', line 66 def deterministic? @states.each do |state| @alphabet.each do |char| state.get_next(char).size > 1 ? (return false) : nil end end true end |
#to_graph(path) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/automaton.rb', line 24 def to_graph(path) g = GraphViz.new(:G, :type => :digraph) @states.each do |state| state.to_graph_node(g) end @transitions.each do |trans| trans.to_graph_transition(g) end g.output(:png => path) end |
#to_str ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/automaton.rb', line 11 def to_str ret_val = '' str = '' ret_val += item_to_str(@states) + "\n" + item_to_str(@alphabet) + "\n" + item_to_str(@transitions) + "\n" ret_val += @starting_state.id + "\n" @states.each do |state| state.is_final ? str += state.id + ' ' : nil end ret_val += str.byteslice(0, str.length - 1) ret_val end |