Module: Fae

Defined in:
lib/fae.rb,
lib/fae/state.rb,
lib/fae/version.rb,
lib/fae/language.rb,
lib/fae/exceptions.rb,
lib/fae/finite_automata.rb

Defined Under Namespace

Classes: DuplicateStateException, EmptyStatesException, FaeException, FiniteAutomata, Language, LanguageMismatchException, MissingValidBlockException, State, StateNotFoundException

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.file_mode(filename) ⇒ Object



14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/fae.rb', line 14

def file_mode(filename)
  diagrams = nil
  begin
    diagrams = YAML.load_file(filename)
  rescue Exception => e
    abort "No such file: #{filename}"
  end

  diagrams.each do |diagram|
    language = diagram["language"].split(',')
    language.map(&:strip!)
    lang = Fae::Language.new(language)
    fa = Fae::FiniteAutomata.new(lang, diagram["description"])

    states = []
    diagram["states"].keys.each do |state|
      name = state
      values = diagram["states"][name].split(",")
      values.map(&:strip!)

      paths = {}
      values.each do |value|
        match = value.split("->")
        match.map(&:strip!)
        if (match)
          paths[match[0].to_sym] = match[1]
        end
      end
      states << Fae::State.new(name, paths, values[-1] == "accepting")        
    end

    strings = []
    diagram["strings"].keys.each do |string|
      value = string
      valid = diagram["strings"][string] == "valid"
      strings << String.new(value.dup, valid)
    end

    fa.add_states(states)
    fa.add_strings(strings)
    fa.evaluate!
  end
end

.interactive_modeObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fae.rb', line 58

def interactive_mode
  states  = []
  strings = []

  print "~ Enter the letters of your language separated by a comma: ".colorize(:yellow)
  letters = gets.chomp.split(",")
  letters.map(&:strip!)

  language = Language.new(letters)
  print "~ Enter the description of your state diagram: ".colorize(:yellow)
  description = gets.chomp

  fa = FiniteAutomata.new(language, description)
  print "~ Enter your state names separated by a comma: ".colorize(:yellow)
  state_names = gets.chomp.split(",")
  state_names.map(&:strip!)

  state_names.each do |state|
    paths = {}
    puts "\nState #{state}:".colorize(:blue)

    letters.each do |letter|
      valid = false
      while (!valid)
        print "~ In state ".colorize(:yellow) + state.colorize(:blue) + " the letter ".colorize(:yellow) + letter.colorize(:blue) + " will take you to what state? ".colorize(:yellow)
        next_state = gets.chomp
        if (!state_names.include?(next_state))
          puts "State #{next_state} is not one of your state names. Please choose from the following: #{state_names}".colorize(:red)
        else
          paths[letter.to_sym] = next_state
          valid = true
        end
      end
    end

    print "~ Is state ".colorize(:yellow) + state.colorize(:blue) + " an accepting state? (y/n): ".colorize(:yellow)
    accepting = gets.chomp.casecmp('y').zero?
    states << State.new(state, paths, accepting)
  end

  finished = false
  string_values = []
  puts "~ Enter strings to test your state diagram with (type 'done' when finished):".colorize(:yellow)

  while(!finished)
    value = gets.chomp
    if (value == 'done')
      finished = true
    else
      string_values << value
    end
  end

  string_values.each do |value|
    print "~ Is ".colorize(:yellow) + value.colorize(:blue) + " a valid string for this state diagram? (y/n): ".colorize(:yellow)
    valid = gets.chomp.casecmp('y').zero?
    strings << String.new(value, valid)
  end
  puts

  fa.add_states(states)
  fa.add_strings(strings)
  fa.evaluate!
end