Class: Nfa2Dfa::Automaton

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_valid_input(data_arr) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/automaton.rb', line 135

def self.get_valid_input(data_arr)
  states = Array.new
  fin = data_arr[4].split(' ')
  
  data_arr[0].split(' ').each do |wrd|
    states.push State.new(wrd)
  end
  fin.include?(states[0].id)
  states.each do |st|
    (fin.include? st.id) ? st.finalize : NIL
  end
  alphabet = Array.new
  data_arr[1].split(' ').each do |wrd|
    alphabet.insert(alphabet.size, wrd)
  end
  transitions = Array.new
  data_arr[2].split(' ').each do |wrd|
    trans = wrd.split('-')
    state1 = NIL
    state2 = NIL
    states.each do |item|
      trans[0] == item.id ? state1 = item : NIL
      trans[2] == item.id ? state2 = item : NIL
      state1 != NIL && state2 != NIL ? break : NIL
    end
    transitions.insert(transitions.size, Transition.new(state1, trans[1], state2))
    state1.add_transition(transitions[transitions.size-1])
  end
  starting = NIL
  states.each do |item2|
    if item2.id == data_arr[3].split(' ')[0]
      starting = item2
      item2.to_starting_node
      break
    end
  end
  Automaton.new(states, alphabet, transitions, starting)
end

.init(path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/automaton.rb', line 61

def self.init(path)
  #nacteni ze souboru
  if File.file?(path)
    data_arr = Array.new
    index = 0
    File.open(path).each_line do |line|
      data_arr[index] = line
      index = index + 1
    end
    if validate(data_arr)
      get_valid_input(data_arr)
    else
      puts "Invalid input"
      NIL
    end
  else
    puts "Invalid input"
    NIL
  end
end

.validate(data_arr) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/automaton.rb', line 82

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

.validate_states(states, start, final) ⇒ Object



291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/automaton.rb', line 291

def self.validate_states(states, start, final)
  if states.include? start[0]
    final.each do |fin|
      if !(states.include? fin)
        return false
      end
      return true
    end
    return false
  end
  return false
end

.validate_transitions(states, alphabet, transitions) ⇒ Object



304
305
306
307
308
309
310
# File 'lib/automaton.rb', line 304

def self.validate_transitions(states, alphabet, transitions)
  transitions.each do |tr|
    trans = tr.split('-')
    ((states.include? trans[0]) && (states.include? trans[2]) && (alphabet.include? trans[1])) ? NIL : (return false)
  end
  true
end

Instance Method Details

#accepts?(data) ⇒ Boolean

format: pismeno<mezera>pismeno…



91
92
93
94
95
96
# File 'lib/automaton.rb', line 91

def accepts?(data)
  formatted_input = data.split(' ')
  @stack = Array.new
  @stack.push(@starting_state)
  formatted_input.size == 0 ? @starting_state.is_final : recurs_accepts?(formatted_input, 0)
end

#determineObject



109
110
111
# File 'lib/automaton.rb', line 109

def determine
  deterministic? ? self : determine_prot
end

#deterministic?Boolean



98
99
100
101
102
103
104
105
106
107
# File 'lib/automaton.rb', line 98

def deterministic?
  @states.each do |state|
    @alphabet.each do |char|
      if state.get_next(char).size > 1
        return false
      end
    end
  end
  true
end

#to_graph(path) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/automaton.rb', line 43

def to_graph(path)
  g = GraphViz.new( :G, :type => :digraph)
  @states.each do |state|
    #puts state.id
    state.to_graph_node(g)
  end
  g.each_node() do |name, node|
    # puts name
  end
  @transitions.each do |trans|
    trans.to_graph_transition(g)
  end
  g.each_edge do |ed|
    #puts ed.node_one + " " + ed.node_two
  end
  g.output( :png => path )
end

#to_strObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/automaton.rb', line 15

def to_str
  ret_val = ""
  str = ""
  @states.each do |state|
    str += state.id + " "
  end
  ret_val += str.byteslice(0, str.length-1) + "\n"
  str = ""
  @alphabet.each do |a|
    str+= a + " "
  end
  ret_val += str.byteslice(0, str.length-1) + "\n"
  str = ""
  @transitions.each do |trans|
    str += trans.beginning_state.id + "-" + trans.alphabet + "-" + trans.ending_state.id + " "
  end
  ret_val += str.byteslice(0, str.length-1) + "\n"
  str = ""
  ret_val += @starting_state.id + "\n"
  @states.each do |state|
    if state.is_final == true
      str += state.id + " "
    end
  end
  ret_val += str.byteslice(0, str.length-1)
  ret_val
end