Vcsn  2.0
Be Rational
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
edit-automaton.cc
Go to the documentation of this file.
3 #include <vcsn/dyn/algos.hh>
4 #include <vcsn/dyn/automaton.hh>
5 #include <vcsn/misc/builtins.hh>
6 
7 namespace vcsn
8 {
9 
10  /*------------------------.
11  | lazy_automaton_editor. |
12  `------------------------*/
13 
14  void
16  {
17  initial_states_.emplace_back(s, w);
18  }
19 
20  void
22  {
23  final_states_.emplace_back(s, w);
24  }
25 
26  namespace
27  {
28  using labelset_type = lazy_automaton_editor::labelset_type;
29 
30  labelset_type type(lazy_automaton_editor::string_t lbl)
31  {
32  if (lbl.get().empty())
33  return labelset_type::empty;
34  else if (lbl == "\\e")
35  return labelset_type::lan;
36  else if (1 < lbl.get().size())
37  return labelset_type::law;
38  else
39  return labelset_type::lal;
40  }
41 
42  std::string to_string(labelset_type l)
43  {
44  switch (l)
45  {
46  case labelset_type::empty: return {};
47  case labelset_type::lal: return "lal_char()";
48  case labelset_type::lan: return "lan<lal_char()>";
49  case labelset_type::law: return "law_char()";
50  }
52  }
53  }
54 
56  void
58  string_t dst,
59  string_t lbl1,
60  string_t lbl2,
62  {
63  input_type_ = std::max(input_type_, type(lbl1));
64  if (!lbl2.get().empty())
65  {
66  output_type_ = std::max(output_type_, type(lbl2));
67  lbl1 = "(" + lbl1.get() + "," + lbl2.get() + ")";
68  }
69  transitions_.emplace_back(src, dst, lbl1, weight);
70 
71  if (!weight.get().empty())
72  {
73  weighted_ = true;
74  if (!real_
75  && weight.get().find('.') != std::string::npos)
76  real_ = true;
77  }
78  }
79 
81  void
83  string_t dst,
84  string_t lbl,
86  {
87  add_transition(src, dst, lbl, string_t{}, weight);
88  }
89 
90  bool
92  {
93  std::swap(open_, o);
94  return o;
95  }
96 
99  {
100  // If there are no transitions (e.g., standard("\e")), consider
101  // the labelset is a plain lal.
104  auto ctx = to_string(input_type_);
106  ctx = "lat<" + ctx + "," + to_string(output_type_) + ">";
107  ctx += ", ";
108  ctx += (real_ ? "r"
109  : weighted_ ? "z"
110  : "b");
111  auto c = vcsn::dyn::make_context(ctx);
112  auto edit = vcsn::dyn::make_automaton_editor(c);
113  edit->open(open_);
114 
115  for (auto t: transitions_)
116  edit->add_transition(std::get<0>(t), std::get<1>(t),
117  std::get<2>(t), std::get<3>(t));
118 
119  for (auto p: initial_states_)
120  edit->add_initial(p.first, p.second);
121 
122  for (auto p: final_states_)
123  edit->add_final(p.first, p.second);
124  return edit->result();
125  }
126 
127  void
129  {
130  transitions_.clear();
131  final_states_.clear();
132  initial_states_.clear();
133  }
134 
135 
136  namespace dyn
137  {
138  REGISTER_DEFINE(make_automaton_editor);
139 
142  {
143  return detail::make_automaton_editor_registry().call(ctx);
144  }
145  }
146 }
context make_context(const std::string &name)
Build a context from its name.
Definition: make-context.cc:38
std::vector< std::pair< string_t, string_t > > initial_states_
The collected initial states: (State, Weight).
bool open(bool o)
Whether unknown letters should be added, or rejected.
automaton_editor::string_t string_t
A hash-cons'ed string type.
std::shared_ptr< detail::automaton_base > automaton
Definition: automaton.hh:71
bool real_
Whether we saw a period in a the weight.
void reset()
Get ready to build another automaton.
labelset_type input_type_
Labelset type for input tape.
std::shared_ptr< const detail::weight_base > weight
Definition: fwd.hh:82
void add_initial(string_t s, string_t w=string_t{})
Add s as an initial state.
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.
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).
automaton_editor * make_automaton_editor(const context &ctx)
Build an automatonset from its context.
dyn::automaton result()
Return the built automaton.
Abstract Builder (the design pattern) for automata.
bool weighted_
Whether we saw a non-empty weight.
std::shared_ptr< const detail::context_base > context
Definition: context.hh:71
std::vector< std::tuple< string_t, string_t, string_t, string_t > > transitions_
The collected transitions: (Source, Destination, Label, Weight).
labelset_type
Labelset types, increasing generality.
#define BUILTIN_UNREACHABLE()
Definition: builtins.hh:14
bool open_
Whether the labelset is open.
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7