17 #ifndef VCSN_TOOLS_FSM_LOAD_HXX
18 # define VCSN_TOOLS_FSM_LOAD_HXX
20 # include <vaucanson/tools/fsm_load.hh>
24 # include <vaucanson/automata/concept/handlers.hh>
25 # include <vaucanson/misc/usual_macros.hh>
26 # include <vaucanson/automata/concept/automata_base.hh>
50 std::pair<std::string, std::string>
51 next_token(std::string line)
54 std::string::iterator i = line.begin();
55 while ((i != line.end()) && ((*i ==
'\t')
56 || (*i ==
' ') || (*i ==
'\0')))
58 for (;i != line.end();++i)
60 if ((*i ==
'\t') || (*i ==
' ')
61 || (*i ==
'\n') || (*i ==
'\0'))
69 return std::make_pair(token,
71 (
unsigned)(i - line.begin()),
72 (
unsigned)(line.end() - i + 1)));
75 return std::make_pair(token, std::string());
78 template <
typename St,
typename AutoType_>
79 void fsm_load(St& in, AutoType_& a)
81 AUTOMATON_TYPES(AutoType_);
82 AUTOMATON_FREEMONOID_TYPES(AutoType_);
86 std::vector<line_data> stock;
88 std::pair<std::string, std::string> tmp;
89 std::vector<std::string> tokens;
99 tmp = next_token(line);
101 if (tmp.first.length() != 0)
102 tokens.push_back(tmp.first);
103 if (line.length() == 0)
106 if (tokens.size() == 0)
112 if (tokens.size() == 1)
114 stock[nb-1].type =
final;
115 stock[nb-1].from = atoi(tokens[0].c_str());
116 stock[nb-1].weight = 0.;
118 else if (tokens.size() == 2)
120 stock[nb-1].type =
final;
121 stock[nb-1].from = atoi(tokens[0].c_str());
122 stock[nb-1].weight = atof(tokens[1].c_str());
124 else if (tokens.size() == 4)
126 stock[nb-1].type = transition;
127 stock[nb-1].from = atoi(tokens[0].c_str());
128 stock[nb-1].to = atoi(tokens[1].c_str());
129 stock[nb-1].letter = tokens[2][0];
130 stock[nb-1].weight = atof(tokens[3].c_str());
131 alpha.insert(stock[nb-1].letter);
135 monoid_t monoid(alpha);
137 series_set_t series(semiring, monoid);
138 automata_set_t aset(series);
139 automaton_t automaton(aset);
140 std::map<int, hstate_t> to_h;
142 for_all_const_(std::vector<line_data>, i, stock)
144 if (i->type == transition)
146 if (to_h.find(i->from) == to_h.end())
147 to_h[i->from] = automaton.add_state();
148 if (to_h.find(i->to) == to_h.end())
149 to_h[i->to] = automaton.add_state();
150 if (i == stock.begin())
151 automaton.set_initial(to_h[i->from]);
153 if (i->letter ==
"1")
154 automaton.add_spontaneous(to_h[i->from],
157 automaton.add_letter_transition(to_h[i->from],
159 vcsn::algebra::letter_traits<letter_t>::literal_to_letter(i->letter).second);
161 else if (i->type ==
final)
163 if (to_h.find(i->from) == to_h.end())
164 to_h[i->from] = automaton.add_state();
165 automaton.set_final(to_h[i->from]);
175 #endif // ! VCSN_TOOLS_FSM_LOAD_HXX