explicit.cc 6.36 KB
Newer Older
1
2
// Copyright (C) 2011 Laboratoire de Recherche et Dveloppement de
// l'Epita (LRDE)
3
4
5
6
7
8
9
10
// Copyright (C) 2004  Laboratoire d'Informatique de Paris 6 (LIP6),
// dpartement Systmes Rpartis Coopratifs (SRC), Universit Pierre
// et Marie Curie.
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 3 of the License, or
12
13
14
15
16
17
18
19
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
20
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

#include "explicit.hh"
#include "misc/bareword.hh"
#include "misc/escape.hh"

namespace spot
{
  const evtgba_explicit::state*
  state_evtgba_explicit::get_state() const
  {
    return state_;
  }

  int
  state_evtgba_explicit::compare(const spot::state* other) const
  {
    const state_evtgba_explicit* o =
38
      down_cast<const state_evtgba_explicit*>(other);
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
100
101
102
103
104
105
106
107
108
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
    assert(o);
    return o->get_state() - get_state();
  }

  size_t
  state_evtgba_explicit::hash() const
  {
    return
      reinterpret_cast<const char*>(get_state()) - static_cast<const char*>(0);
  }

  state_evtgba_explicit*
  state_evtgba_explicit::clone() const
  {
    return new state_evtgba_explicit(*this);
  }

  namespace
  {
    class evtgba_explicit_iterator: public evtgba_iterator
    {
    public:
      evtgba_explicit_iterator(const evtgba_explicit::transition_list* s)
	: s_(s), i_(s_->end())
      {
      }

      virtual
      ~evtgba_explicit_iterator()
      {
      }

      virtual void first()
      {
	i_ = s_->begin();
      }

      virtual void
      next()
      {
	++i_;
      }

      virtual bool
      done() const
      {
	return i_ == s_->end();
      }

      virtual const symbol*
      current_label() const
      {
	return (*i_)->label;
      }

      virtual symbol_set
      current_acceptance_conditions() const
      {
	return (*i_)->acceptance_conditions;
      }
    protected:
      const evtgba_explicit::transition_list* s_;
      evtgba_explicit::transition_list::const_iterator i_;
    };



    class evtgba_explicit_iterator_fw: public evtgba_explicit_iterator
    {
    public:
      evtgba_explicit_iterator_fw(const evtgba_explicit::transition_list* s)
	: evtgba_explicit_iterator(s)
      {
      }

      virtual
      ~evtgba_explicit_iterator_fw()
      {
      }

      const state*
      current_state() const
      {
	return new state_evtgba_explicit((*i_)->out);
      }
    };

    class evtgba_explicit_iterator_bw: public evtgba_explicit_iterator
    {
    public:
      evtgba_explicit_iterator_bw(const evtgba_explicit::transition_list* s)
	: evtgba_explicit_iterator(s)
      {
      }

      virtual
      ~evtgba_explicit_iterator_bw()
      {
      }

      const state*
      current_state() const
      {
	return new state_evtgba_explicit((*i_)->in);
      }
    };
  }

  evtgba_explicit::evtgba_explicit()
  {
  }

  evtgba_explicit::~evtgba_explicit()
  {
    for (transition_list::const_iterator i = init_states_.begin();
	 i != init_states_.end(); ++i)
      delete *i;
    for (ns_map::const_iterator j = name_state_map_.begin();
	 j != name_state_map_.end(); ++j)
      {
	for (transition_list::const_iterator i = j->second->out.begin();
	     i != j->second->out.end(); ++i)
	  delete *i;
	delete j->second;
      }
    for (symbol_set::const_iterator i = alphabet_.begin();
	 i != alphabet_.end(); ++i)
      (*i)->unref();
    for (symbol_set::const_iterator i = acc_set_.begin();
	 i != acc_set_.end(); ++i)
      (*i)->unref();
  }

  evtgba_iterator*
  evtgba_explicit::init_iter() const
  {
    return new evtgba_explicit_iterator_fw(&init_states_);
  }

  evtgba_iterator*
  evtgba_explicit::succ_iter(const spot::state* s) const
  {
    const state_evtgba_explicit* u =
182
      down_cast<const state_evtgba_explicit*>(s);
183
184
185
186
187
188
189
190
    assert(u);
    return new evtgba_explicit_iterator_fw(&u->get_state()->out);
  }

  evtgba_iterator*
  evtgba_explicit::pred_iter(const spot::state* s) const
  {
    const state_evtgba_explicit* u =
191
      down_cast<const state_evtgba_explicit*>(s);
192
193
194
195
196
197
198
199
    assert(u);
    return new evtgba_explicit_iterator_fw(&u->get_state()->in);
  }

  std::string
  evtgba_explicit::format_state(const spot::state* s) const
  {
    const state_evtgba_explicit* u =
200
      down_cast<const state_evtgba_explicit*>(s);
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
    assert(u);
    sn_map::const_iterator i = state_name_map_.find(u->get_state());
    assert(i != state_name_map_.end());
    return i->second;
  }

  const symbol_set&
  evtgba_explicit::all_acceptance_conditions() const
  {
    return acc_set_;
  }

  const symbol_set&
  evtgba_explicit::alphabet() const
  {
    return alphabet_;
  }

  evtgba_explicit::state*
  evtgba_explicit::declare_state(const std::string& name)
  {
    ns_map::const_iterator i = name_state_map_.find(name);
    if (i != name_state_map_.end())
      return i->second;
    state* s = new state;
    name_state_map_[name] = s;
    state_name_map_[s] = name;
    return s;
  }

  evtgba_explicit::transition*
  evtgba_explicit::add_transition(const std::string& source,
				  const rsymbol& label,
				  rsymbol_set acc,
				  const std::string& dest)
  {
    state* in = declare_state(source);
    state* out = declare_state(dest);
    transition* t = new transition;
    t->in = in;
    t->label = label;
    t->out = out;
    in->out.push_back(t);
    out->in.push_back(t);

    for (rsymbol_set::const_iterator i = acc.begin(); i != acc.end(); ++i)
      {
	const symbol* s = *i;
	t->acceptance_conditions.insert(s);
	declare_acceptance_condition(*i);
      }

    if (alphabet_.find(t->label) == alphabet_.end())
      {
	alphabet_.insert(t->label);
	t->label->ref();
      }

    return t;
  }

  void
  evtgba_explicit::declare_acceptance_condition(const rsymbol& acc)
  {
    const symbol* s = acc;
    if (acc_set_.find(s) == acc_set_.end())
      {
	acc_set_.insert(s);
	s->ref();
      }
  }


  void
  evtgba_explicit::set_init_state(const std::string& name)
  {
    transition* t = new transition;
    t->in = 0;
    t->out = declare_state(name);
    t->label = 0;
    init_states_.push_back(t);
  }

}