17 #ifndef AWALI_ALGOS_EFSM_HH
18 # define AWALI_ALGOS_EFSM_HH
27 #include <awali/common/qfraction.cc>
29 namespace awali {
namespace sttc {
48 template <
typename LabelSet>
51 static constexpr
size_t value = 1;
54 template <
typename... LabelSet>
57 static constexpr
size_t value =
sizeof...(LabelSet);
65 template <
typename Aut>
80 using super_type::super_type;
88 "me=$(basename \"$0\")\n"
89 "medir=$(mktemp -d \"/tmp/$me.XXXXXX\") || exit 1\n"
97 "cat >$medir/transitions.fsm <<\\EOFSM";
98 output_transitions_();
112 "fstcompile" << (is_transducer ?
"" :
" --acceptor") <<
" \\\n"
113 " --keep_isymbols --isymbols=" << isymbols_ <<
" \\\n"
114 " --keep_osymbols --osymbols=" << osymbols_ <<
" \\\n"
115 " $medir/transitions.fsm \"$@\"\n"
125 template <
typename Label>
126 void output_label_(
const Label& l, std::false_type)
132 template <
typename Label>
133 void output_label_(
const Label& l, std::true_type)
135 ls_.template set<0>().print(std::get<0>(l),
os_);
137 ls_.template set<1>().print(std::get<1>(l),
os_);
143 if (
aut_->dst_of(t) !=
aut_->post())
148 if (
ls_.is_special(
aut_->label_of(t)))
151 output_label_(
aut_->label_of(t), is_transducer);
154 if (
ws_.show_one() || !
ws_.is_one(
aut_->weight_of(t)))
162 void output_transitions_()
167 if (
aut_->initial_transitions().size() != 1)
168 for (
auto t :
aut_->initial_transitions())
171 output_transition_(t);
176 std::vector<state_t>
states(std::begin(
aut_->states()),
177 std::end(
aut_->states()));
181 return (std::forward_as_tuple(!
aut_->is_initial(l), l)
182 < std::forward_as_tuple(!
aut_->is_initial(r), r));
187 for (
auto t :
aut_->final_transitions())
190 output_transition_(t);
194 template <
typename LabelSet,
typename Labels,
typename GetLabel>
195 auto add_alphabet(
const LabelSet& ls, Labels& labels,
196 GetLabel get_label,
int)
197 -> decltype(ls.genset(),
void())
199 for (
auto l : ls.genset())
200 labels.insert(get_label(ls.value(l)));
203 template <
typename LabelSet,
typename Labels,
typename GetLabel>
204 void add_alphabet(
const LabelSet&, Labels&,
230 template <
typename LabelSet,
typename GetLabel>
231 void output_symbols_(
const std::string& name,
236 using labelset_t = LabelSet;
237 using label_t =
typename labelset_t::value_t;
239 std::set<label_t, less<labelset_t>> labels;
240 add_alphabet(*
aut_->labelset(), labels, get_label, 0);
241 for (
auto t :
aut_->transitions())
242 labels.insert(get_label(
aut_->label_of(t)));
247 "cat >" << name <<
" <<\\EOFSM\n"
250 for (
const auto& l: labels)
254 os_ <<
'\t' << ++num <<
'\n';
264 output_symbols_impl_(std::false_type)
266 output_symbols_(isymbols_,
274 output_symbols_impl_(std::true_type)
276 output_symbols_(isymbols_,
277 ls_.template set<0>(),
278 [](
const label_t& l) { return std::get<0>(l); });
279 output_symbols_(osymbols_,
280 ls_.template set<1>(),
281 [](
const label_t& l) { return std::get<1>(l); });
287 output_symbols_impl_<automaton_t>(is_transducer);
292 using is_transducer_t =
293 std::integral_constant<bool,
294 2 <= rank<labelset_t_of<automaton_t>>
::value>;
295 const is_transducer_t is_transducer = {};
298 const char* isymbols_ =
299 is_transducer ?
"$medir/isymbols.txt" :
"$medir/symbols.txt";
301 const char* osymbols_ =
302 is_transducer ?
"$medir/osymbols.txt" :
"$medir/symbols.txt";
310 template <
typename Aut>
312 efsm(
const Aut& aut, std::ostream& out)
Definition: qfraction.hh:26
den_t den
Definition: qfraction.hh:32
num_t num
Definition: qfraction.hh:31
Format automaton to EFSM format, based on FSM format.
Definition: fsm.hh:67
void operator()()
Actually output aut_ on os_.
Definition: fsm.hh:83
const automaton_t & aut_
The automaton we have to output.
Definition: grail.hh:154
typename super_type::label_t label_t
Definition: fsm.hh:72
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: grail.hh:158
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:160
std::ostream & os_
Output stream.
Definition: grail.hh:156
Aut automaton_t
Definition: fsm.hh:69
Factor common bits in automaton formatting.
Definition: grail.hh:43
void output_state_(const state_t s)
Output transitions, sorted lexicographically on (Label, Dest).
Definition: grail.hh:96
const automaton_t & aut_
The automaton we have to output.
Definition: grail.hh:154
const labelset_t_of< automaton_t > & ls_
Short-hand to the labelset.
Definition: grail.hh:158
const weightset_t & ws_
Short-hand to the weightset.
Definition: grail.hh:160
std::ostream & os_
Output stream.
Definition: grail.hh:156
label_t_of< automaton_t > label_t
Definition: grail.hh:60
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
std::vector< state_t > states(abstract_automaton_t const *aut, bool all)
static constexpr TOP< void > value
Definition: priority.hh:93
T evalf(const T &x)
Definition: fsm.hh:39
void sort(Aut a, Compare p)
Definition: sort.hh:28
std::ostream & efsm(const Aut &aut, std::ostream &out)
Format automaton to EFSM format, based on FSM format.
Definition: fsm.hh:312
Main namespace of Awali.
Definition: ato.hh:22
unsigned state_t
Definition: types.hh:21
unsigned transition_t
Definition: types.hh:22
Number of tapes.
Definition: fsm.hh:50
static constexpr size_t value
Definition: fsm.hh:51