Class: Nfa2Dfa::Automaton

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.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

Instance Method Details

#accepts?(data) ⇒ Boolean

format: pismeno<mezera>pismeno…

Returns:

  • (Boolean)


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

Returns:

  • (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