g_vcsn_handler_internal.hxx 8.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// g_vcsn_handler_internal.hxx: this file is part of Vaucanson.
//
// Vaucanson, a generic library for finite state machines.
//
// Copyright (C) 2006 The Vaucanson Group.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// The complete GNU General Public Licence Notice can be found as the
// `COPYING' file in the root directory.
//
// The Vaucanson Group consists of people listed in the `AUTHORS' file.
//

/**
 * @file g_vcsn_handler_internal.hxx
 * @author Florent D'Halluin <d-halluin@lrde.epita.fr>
 *
 * FIXME
 */

#ifndef   	G_VCSN_HANDLER_INTERNAL_HXX_
# define   	G_VCSN_HANDLER_INTERNAL_HXX_

# include "common.hh"
# include "exceptions.hh"
# include "g_vcsn_handler_internal.hh"

32
33
# include <QMessageBox>

34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
# include <cmath>
# include <sstream>

# include <vaucanson/misc/usual_macros.hh>

namespace gui
{
  namespace handlers
  {
    AddState::AddState(SceneInfo& i, QPointF p)
      : info(i),
	pos(p)
    {
    }

    template<typename Automaton>
    void
    AddState::operator()(Automaton& automaton)
    {
      AUTOMATON_TYPES(Automaton)

      int i = automaton.add_state();

      GState *state = new GState(i, QColor("white"));
      state->setPos(pos);
      info.scene->addItem(state);
      info.scene->connectState(state);
    }

    DelState::DelState(SceneInfo& i, GState* s)
      : info(i),
	state(s)
    {
    }

    template<typename Automaton>
    void
    DelState::operator()(Automaton& automaton)
    {
      AUTOMATON_TYPES(Automaton)

      automaton.del_state(state->index());

      delete (state); // removes it from the scene properly.
      state = 0;
    }

    DelTransition::DelTransition(SceneInfo& i, GTransition* t)
      : info(i),
	transition(t)
    {
    }

    template<typename Automaton>
    void
    DelTransition::operator()(Automaton& automaton)
    {
      AUTOMATON_TYPES(Automaton)

      // FIXME: this cast may be unsafe:
      automaton.del_transition((htransition_t) transition->index());

      delete (transition); // removes it from the scene properly.
      transition = 0;
    }

100
101
102
103
104
105
106
107
108
    SetTransitionLabel::SetTransitionLabel(SceneInfo& i,
					   GTransition* t,
					   QString l)
      : info(i),
	transition(t),
	label(l)
    {
    }

109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
    void
    SetTransitionLabel::operator()
      (vcsn::boolean_automaton::automaton_t& automaton)
    {
      // FIXME: This whole implementation is bogus and wrong but seems
      // to give basic results.  Rewrite.

      using namespace vcsn::boolean_automaton;
      typedef Element<series_set_t, label_t> label_elt_t;

      // FIXME: Unsafe cast?
      htransition_t i = (htransition_t) transition->index();

      label_elt_t lbl(automaton.series());

      // Magic.
      std::pair<bool, std::string> r =
	algebra::parse(label.toStdString(), lbl);

      label_t lbv = lbl.value();

      // algebra::parse() gives seemingly wrong result (e.g. 1+a+b
      // when parsing "a+b").  This is an ugly workaround.
      // Setting an epsilon+letter label (eg. 1+a) is made impossible.
      if (lbv.size() > 1)
	lbv.erase(lbv.begin());

      // Magic ends here.
      automaton.update(i, lbv);

      if (r.first)
	QMessageBox::warning(0, "Set transition label", r.second.c_str());

      std::stringstream str;
      str << automaton.series_of(i);
      transition->set_label_value(str.str().c_str());

      transition->update();
    }

    void
    SetTransitionLabel::operator()
      (vcsn::z_automaton::automaton_t& automaton)
    {
      // FIXME: This whole implementation is bogus and wrong but seems
      // to give basic results.  Rewrite.

      using namespace vcsn::z_automaton;
      typedef Element<series_set_t, label_t> label_elt_t;

      // FIXME: Unsafe cast?
      htransition_t i = (htransition_t) transition->index();

      label_elt_t lbl(automaton.series());

      // Magic.
      std::pair<bool, std::string> r =
	algebra::parse(label.toStdString(), lbl);

      label_t lbv = lbl.value();

      // algebra::parse() gives seemingly wrong result (e.g. 1+a+b
      // when parsing "a+b").  This is an ugly workaround.
      // Setting an epsilon+letter label (eg. 1+a) is made impossible.
      if (lbv.size() > 1)
	lbv.erase(lbv.begin());

      // Magic ends here.
      automaton.update(i, lbv);

      if (r.first)
	QMessageBox::warning(0, "Set transition label", r.second.c_str());

      std::stringstream str;
      str << automaton.series_of(i);
      transition->set_label_value(str.str().c_str());

      transition->update();
    }

189
190
191
192
193
194
195
    template<typename Automaton>
    void
    SetTransitionLabel::operator()(Automaton&)
    {
      AUTOMATON_TYPES(Automaton)

      // FIXME: implement this.
196
197
      QMessageBox::warning(0, "Set transition label",
			   "Cannot set the transition label of this kind "
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
			   "of automaton (not implemented).");
    }

    AddTransition::AddTransition(SceneInfo& i, GState* s, GState* d, QString l)
      : info(i),
	src(s),
	dst(d),
	label(l)
    {
    }

    void
    AddTransition::operator()(vcsn::z_automaton::automaton_t& automaton)
    {
      using namespace vcsn::z_automaton;

      // FIXME: Only for labels_are_series.
      series_set_t s = automaton.series();
      series_set_elt_t se = make_rat_exp(s.monoid().alphabet(),
					 label.toStdString(),
					 *(s.monoid().representation()),
					 *(s.representation()));

      htransition_t i = automaton.add_series_transition(src->index(),
							dst->index(),
							se);

      std::stringstream str;
      str << automaton.series_of(i);

      GTransition* t = new GTransition(i, *src, *dst,
				       QString(str.str().c_str()));
      info.scene->addItem(t);
      info.scene->connectTransition(t);
    }

    void
    AddTransition::operator()(vcsn::boolean_automaton::automaton_t& automaton)
    {
      using namespace vcsn::boolean_automaton;

239
240
241
242
243
244
      std::stringstream str;
      htransition_t i;

      try
      {

245
246
247
248
249
250
251
      // FIXME: Only for labels_are_series.
      series_set_t s = automaton.series();
      series_set_elt_t se = make_rat_exp(s.monoid().alphabet(),
					 label.toStdString(),
					 *(s.monoid().representation()),
					 *(s.representation()));

252
253
254
255
256
257
258
259
260
261
      i = automaton.add_series_transition(src->index(),
					  dst->index(),
					  se);
      }
      catch (std::exception& e)
      {
	QMessageBox::critical(NULL, "Exception occured",
			      e.what ());
	return;
      }
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280

      str << automaton.series_of(i);

      GTransition* t = new GTransition(i, *src, *dst,
				       QString(str.str().c_str()));
      info.scene->addItem(t);
      info.scene->connectTransition(t);
    }

    template<typename Automaton>
    void
    AddTransition::operator()(Automaton&)
    {
      AUTOMATON_TYPES(Automaton)

      // FIXME: implement this.
      QMessageBox::warning(0, "Add transition",
			   "Cannot add a transition to this kind "
			   "of automaton (not implemented).");
281
282
    }

283
284
285
286
287
288
289
290
291
292
293
    SetInitial::SetInitial(SceneInfo& i, GState* s, QString e)
      : info(i),
	state(s),
	entry(e)
    {
    }

    template<typename Automaton>
    void
    SetInitial::operator()(Automaton& automaton)
    {
294
      AUTOMATON_TYPES(Automaton)
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348

      int i = state->index();
      if (entry.isNull())
      {
	automaton.unset_initial(i);
	state->set_initial_entry(0);
      }
      else
      {
	// FIXME: handle entries.
	automaton.set_initial(i);
	std::stringstream str;
	str << automaton.get_initial(i);
	state->set_initial_entry(str.str().c_str());
      }

      state->update();
    }

    SetFinal::SetFinal(SceneInfo& i, GState* s, QString e)
      : info(i),
	state(s),
	entry(e)
    {
    }

    template<typename Automaton>
    void
    SetFinal::operator()(Automaton& automaton)
    {
      AUTOMATON_TYPES(Automaton)

      int i = state->index();
      if (entry.isNull())
      {
	automaton.unset_final(i);
	state->set_final_entry(0);
      }
      else
      {
	// FIXME: Handle entries.
	automaton.set_final(i);
	std::stringstream s;
	s << automaton.get_final(i);
	state->set_final_entry(s.str().c_str());
      }

      state->update();
    }

  } // ! namespace handlers
} // ! namespace gui

#endif	    /* !G_VCSN_HANDLER_INTERNAL_HXX_ */