1 #ifndef VCSN_ALGOS_EDIT_AUTOMATON_HH
2 # define VCSN_ALGOS_EDIT_AUTOMATON_HH
6 # include <unordered_map>
56 virtual bool open(
bool o) = 0;
62 virtual void reset() = 0;
81 template <
typename Aut>
163 "edit_automaton: invalid transition from pre to post: ",
164 src,
" -> ", dst,
" (", entry,
")");
165 if (s ==
res_->pre() || d ==
res_->post())
167 if (entry.get().empty())
168 res_->add_transition(s, d,
res_->prepost_label());
176 && (
res_->labelset()->is_special(begin(e)->first)
177 ||
res_->labelset()->is_one(begin(e)->first)))
179 auto w = begin(e)->second;
180 res_->add_transition(s, d,
res_->prepost_label(), w);
183 raise(
"edit_automaton: invalid ",
184 s ==
res_->pre() ?
"initial" :
"final",
185 " entry: ", entry.get());
193 for (
auto e: p.first->second)
194 res_->add_transition(s, d, e.first, e.second);
218 const auto& ls = *
res_->labelset();
221 p.first->second =
conv(ls, l);
222 return p.first->second;
229 const auto& ws = *
res_->weightset();
232 p.first->second = w.get().empty() ? ws.one() :
conv(ws, w);
233 return p.first->second;
240 auto p =
smap_.emplace(k, Aut::element_type::null_state());
242 p.first->second =
res_->new_state();
243 return p.first->second;
252 using state_map = std::unordered_map<string_t, state_t>;
255 using entry_map = std::unordered_map<string_t, entry_t>;
258 using label_map = std::unordered_map<string_t, label_t>;
309 std::vector<std::tuple<string_t, string_t, string_t, string_t>>
transitions_;
331 template <
typename Ctx>
335 const auto& c = ctx->as<Ctx>();
346 #endif // !VCSN_ALGOS_EDIT_AUTOMATON_HH
std::vector< std::pair< string_t, string_t > > initial_states_
The collected initial states: (State, Weight).
virtual bool open(bool o) overridefinal
Linear combination of labels: map labels to weights.
bool open(bool o)
Whether unknown letters should be added, or rejected.
virtual dyn::automaton result()=0
The final result.
automaton_editor::string_t string_t
A hash-cons'ed string type.
std::unordered_map< string_t, entry_t > entry_map
Memoize entry conversion.
state_t state_(string_t k)
Convert a state name to a state handler.
REGISTER_DECLARE(accessible,(const automaton &) -> automaton)
std::shared_ptr< detail::automaton_base > automaton
bool real_
Whether we saw a period in a the weight.
virtual dyn::automaton result() overridefinal
Return the built automaton.
void reset()
Get ready to build another automaton.
labelset_t_of< automaton_t > labelset_t
std::unordered_map< string_t, weight_t > weight_map
Memoize weight conversion.
virtual void add_transition(string_t src, string_t dst, string_t label, string_t weight=string_t{})=0
Add a transition from src to dst.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
labelset_type input_type_
Labelset type for input tape.
virtual bool open(bool o)=0
weight_t weight_(string_t w)
Convert a weight string to its value.
Concrete Builder (the design pattern) for automata.
polynomialset< context_t > ps_
Entries handler.
super_t::string_t string_t
label_t label_(string_t l)
Convert a label string to its value.
virtual void add_final(string_t s, string_t weight=string_t{}) overridefinal
virtual void add_pre(string_t s)=0
Declare that s denotes the preinitial state in entries.
std::shared_ptr< const detail::weight_base > weight
boost::flyweight< std::string, boost::flyweights::no_tracking > symbol
An internalized string.
void add_initial(string_t s, string_t w=string_t{})
Add s as an initial state.
virtual void add_transition(string_t src, string_t dst, string_t label, string_t weight=string_t{}) overridefinal
Add a transition from src to dst.
automaton make_automaton(const Aut &aut)
Build a dyn::automaton.
context_t_of< automaton_t > context_t
auto conv(const ValueSet &vs, const std::string &str, Args &&...args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
virtual void add_final(string_t s, string_t w)=0
state_t_of< automaton_t > state_t
virtual void add_state(string_t s)=0
virtual void add_entry(string_t src, string_t dst, string_t entry)=0
Add an entry from src to dst, with value entry.
typename detail::context_t_of_impl< base_t< ValueSet >>::type context_t_of
virtual void add_pre(string_t s) overridefinal
Register that state named s is preinitial.
void add_transition(string_t src, string_t dst, string_t lbl, string_t w=string_t{})
Add an acceptor transition from src to dst, labeled by lbl.
virtual void add_post(string_t s) overridefinal
Register that state named s is postfinal.
void add_final(string_t s, string_t w=string_t{})
Add s as a final state.
labelset_type output_type_
Labelset type for output tape.
std::vector< std::pair< string_t, string_t > > final_states_
The collected final states: (State, Weight).
typename polynomialset< context_t >::value_t entry_t
typename detail::label_t_of_impl< base_t< ValueSet >>::type label_t_of
std::unordered_map< string_t, state_t > state_map
State name -> state handle.
virtual void add_initial(string_t s, string_t w)=0
virtual void add_state(string_t s) overridefinal
Register the existence of state named s.
dyn::automaton result()
Return the built automaton.
automaton_t res_
The automaton under construction.
std::shared_ptr< const detail::label_base > label
Abstract Builder (the design pattern) for automata.
bool weighted_
Whether we saw a non-empty weight.
edit_automaton(const context_t &ctx)
virtual ~automaton_editor()
std::shared_ptr< const detail::context_base > context
virtual void add_initial(string_t s, string_t weight=string_t{}) overridefinal
void set_separator(char c)
Set the label separator. Defaults to '+'.
virtual void add_post(string_t s)=0
Declare that s denotes the postfinal state in entries.
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
std::vector< std::tuple< string_t, string_t, string_t, string_t > > transitions_
The collected transitions: (Source, Destination, Label, Weight).
weight_t_of< automaton_t > weight_t
labelset_type
Labelset types, increasing generality.
std::unordered_map< string_t, label_t > label_map
Memoize label conversion.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
SharedPtr make_shared_ptr(Args &&...args)
Same as std::make_shared, but parameterized by the shared_ptr type, not the (pointed to) element_type...
automaton_editor * make_automaton_editor(const context &ctx)
Bridge.
virtual void reset()=0
Forget about the current automaton, but do not free it.
virtual void add_entry(string_t src, string_t dst, string_t entry) overridefinal
Add transitions from src to dst, labeled by entry.
virtual void reset() overridefinal
Detach the built automaton.
void require(bool b, Args &&...args)
If b is not verified, raise an error with args as message.
label_t_of< automaton_t > label_t
bool open_
Whether the labelset is open.
Build an automaton with unknown context.
char sep_
The label separator.