spot  2.16
kripkegraph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) by the Spot authors, see the AUTHORS file for details.
3 //
4 // This file is part of Spot, a model checking library.
5 //
6 // Spot is free software; you can redistribute it and/or modify it
7 // under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // Spot is distributed in the hope that it will be useful, but WITHOUT
12 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 // License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 #pragma once
20 
21 #include <iosfwd>
22 #include <spot/kripke/kripke.hh>
23 #include <spot/graph/graph.hh>
24 #include <spot/tl/formula.hh>
25 
26 namespace spot
27 {
30  struct SPOT_API kripke_graph_state: public spot::state
31  {
32  public:
34  kripke_graph_state(bdd cond = bddfalse) noexcept
35  : cond_(cond)
36  {
37  }
38 
40  kripke_graph_state(const kripke_graph_state& other) noexcept
41  : cond_(other.cond_)
42  {
43  }
44 
47  {
48  cond_ = other.cond_;
49  return *this;
50  }
51 
52  virtual ~kripke_graph_state() noexcept
53  {
54  }
55 
56  virtual int compare(const spot::state* other) const override
57  {
58  auto o = down_cast<const kripke_graph_state*>(other);
59 
60  // Do not simply return "other - this", it might not fit in an int.
61  if (o < this)
62  return -1;
63  if (o > this)
64  return 1;
65  return 0;
66  }
67 
68  virtual size_t hash() const override
69  {
70  return reinterpret_cast<size_t>(this);
71  }
72 
73  virtual kripke_graph_state*
74  clone() const override
75  {
76  return const_cast<kripke_graph_state*>(this);
77  }
78 
79  virtual void destroy() const override
80  {
81  }
82 
84  bdd cond() const
85  {
86  return cond_;
87  }
88 
90  void cond(bdd c)
91  {
92  cond_ = c;
93  }
94 
95  private:
96  bdd cond_;
97  };
98 
101  template<class Graph>
103  {
104  private:
105  typedef typename Graph::edge edge;
106  typedef typename Graph::state_data_t state;
107  const Graph* g_;
108  edge t_;
109  edge p_;
110  public:
113  const typename Graph::state_storage_t* s):
114  kripke_succ_iterator(s->cond()),
115  g_(g),
116  t_(s->succ)
117  {
118  }
119 
121  {
122  }
123 
125  void recycle(const typename Graph::state_storage_t* s)
126  {
127  cond_ = s->cond();
128  t_ = s->succ;
129  }
130 
131  virtual bool first() override
132  {
133  p_ = t_;
134  return p_;
135  }
136 
137  virtual bool next() override
138  {
139  p_ = g_->edge_storage(p_).next_succ;
140  return p_;
141  }
142 
143  virtual bool done() const override
144  {
145  return !p_;
146  }
147 
148  virtual kripke_graph_state* dst() const override
149  {
150  SPOT_ASSERT(!done());
151  return const_cast<kripke_graph_state*>
152  (&g_->state_data(g_->edge_storage(p_).dst));
153  }
154  };
155 
156 
160  class SPOT_API kripke_graph final : public kripke
161  {
162  public:
165  // We avoid using graph_t::edge_storage_t because graph_t is not
166  // instantiated in the SWIG bindings, and SWIG would therefore
167  // handle graph_t::edge_storage_t as an abstract type.
169  typedef internal::edge_storage<unsigned, unsigned, unsigned,
170  internal::boxed_label
171  <void, true>>
173  static_assert(std::is_same<typename graph_t::edge_storage_t,
174  edge_storage_t>::value, "type mismatch");
175 
177  typedef internal::distate_storage<unsigned,
178  internal::boxed_label<kripke_graph_state, false>>
180  static_assert(std::is_same<typename graph_t::state_storage_t,
181  state_storage_t>::value, "type mismatch");
182  typedef std::vector<state_storage_t> state_vector;
183 
184  // We avoid using graph_t::state for the very same reason.
185  typedef unsigned state_num;
186  static_assert(std::is_same<typename graph_t::state, state_num>::value,
187  "type mismatch");
188 
189  protected:
191  mutable unsigned init_number_;
192  public:
195  : kripke(d), init_number_(0)
196  {
197  }
198 
199  virtual ~kripke_graph()
200  {
201  get_dict()->unregister_all_my_variables(this);
202  }
203 
205  unsigned num_states() const
206  {
207  return g_.num_states();
208  }
209 
211  unsigned num_edges() const
212  {
213  return g_.num_edges();
214  }
215 
218  {
219  if (SPOT_UNLIKELY(s >= num_states()))
220  throw std::invalid_argument
221  ("set_init_state() called with nonexistent state");
222  init_number_ = s;
223  }
224 
227  {
228  // If the kripke has no state, it has no initial state.
229  if (num_states() == 0)
230  throw std::runtime_error("kripke has no state at all");
231  return init_number_;
232  }
233 
234  virtual const kripke_graph_state* get_init_state() const override
235  {
236  return state_from_number(get_init_state_number());
237  }
238 
241  succ_iter(const spot::state* st) const override
242  {
243  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
244  SPOT_ASSERT(!s->succ || g_.is_valid_edge(s->succ));
245 
246  if (this->iter_cache_)
247  {
248  auto it =
249  down_cast<kripke_graph_succ_iterator<graph_t>*>(this->iter_cache_);
250  it->recycle(s);
251  this->iter_cache_ = nullptr;
252  return it;
253  }
254  return new kripke_graph_succ_iterator<graph_t>(&g_, s);
255 
256  }
257 
259  state_num
260  state_number(const state* st) const
261  {
262  auto s = down_cast<const typename graph_t::state_storage_t*>(st);
263  return s - &g_.state_storage(0);
264  }
265 
267  const kripke_graph_state*
269  {
270  return &g_.state_data(n);
271  }
272 
276  {
277  return &g_.state_data(n);
278  }
279 
281  std::string format_state(unsigned n) const
282  {
283  auto named = get_named_prop<std::vector<std::string>>("state-names");
284  if (named && n < named->size())
285  return (*named)[n];
286 
287  return std::to_string(n);
288  }
289 
290  virtual std::string format_state(const state* st) const override
291  {
292  return format_state(state_number(st));
293  }
294 
296  virtual bdd state_condition(const state* s) const override
297  {
298  auto gs = down_cast<const kripke_graph_state*>(s);
299  return gs->cond();
300  }
301 
304  {
305  return g_.edge_storage(t);
306  }
307 
309  const edge_storage_t edge_storage(unsigned t) const
310  {
311  return g_.edge_storage(t);
312  }
313 
315  unsigned new_state(bdd cond)
316  {
317  return g_.new_state(cond);
318  }
319 
321  unsigned new_states(unsigned n, bdd cond)
322  {
323  return g_.new_states(n, cond);
324  }
325 
327  unsigned new_edge(unsigned src, unsigned dst)
328  {
329  return g_.new_edge(src, dst);
330  }
331 
332 
333 #ifndef SWIG
335  const state_vector& states() const
336  {
337  return g_.states();
338  }
339 #endif
340 
343  {
344  return g_.states();
345  }
346 
347 #ifndef SWIG
349  internal::all_trans<const graph_t>
350  edges() const noexcept
351  {
352  return g_.edges();
353  }
354 #endif
355 
357  internal::all_trans<graph_t>
358  edges() noexcept
359  {
360  return g_.edges();
361  }
362 
363 #ifndef SWIG
365  internal::state_out<const graph_t>
366  out(unsigned src) const
367  {
368  return g_.out(src);
369  }
370 #endif
371 
373  internal::state_out<graph_t>
374  out(unsigned src)
375  {
376  return g_.out(src);
377  }
378 
379  };
380 
382  typedef std::shared_ptr<kripke_graph> kripke_graph_ptr;
383 
385  inline kripke_graph_ptr
387  {
388  return std::make_shared<kripke_graph>(d);
389  }
390 }
unsigned num_states() const
The number of states in the automaton.
Definition: graph.hh:670
internal::distate_storage< edge, internal::boxed_label< kripke_graph_state > > state_storage_t
State storage type.
Definition: graph.hh:628
const state_vector & states() const
Return the vector of states.
Definition: graph.hh:960
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:709
state new_state(Args &&... args)
Create a new state.
Definition: graph.hh:695
bool is_valid_edge(edge t) const
Test whether the given edge is valid.
Definition: graph.hh:1011
edge_storage_t & edge_storage(edge s)
Return a reference to the storage of an edge.
Definition: graph.hh:760
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:916
state_storage_t & state_storage(state s)
Return a reference to the storage of a state.
Definition: graph.hh:724
unsigned num_edges() const
The number of edges in the automaton.
Definition: graph.hh:678
state_storage_t::data_t & state_data(state s)
Return the State_Data associated to a state.
Definition: graph.hh:742
internal::edge_storage< state, state, edge, internal::boxed_label< void > > edge_storage_t
Edge storage type.
Definition: graph.hh:632
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:797
internal::all_trans< const digraph > edges() const
Return a fake container with all edges (excluding erased edges)
Definition: graph.hh:975
Successor iterator for a graph-based Kripke structure state.
Definition: kripkegraph.hh:103
void recycle(const typename Graph::state_storage_t *s)
Reset the iterator for a new state.
Definition: kripkegraph.hh:125
virtual bool first() override
Position the iterator on the first successor (if any).
Definition: kripkegraph.hh:131
virtual bool done() const override
Check whether the iteration is finished.
Definition: kripkegraph.hh:143
kripke_graph_succ_iterator(const Graph *g, const typename Graph::state_storage_t *s)
Construct iterator over successors of state s in graph g.
Definition: kripkegraph.hh:112
virtual bool next() override
Jump to the next successor (if any).
Definition: kripkegraph.hh:137
virtual kripke_graph_state * dst() const override
Get the destination state of the current edge.
Definition: kripkegraph.hh:148
Kripke Structure.
Definition: kripkegraph.hh:161
unsigned num_states() const
Return the number of states.
Definition: kripkegraph.hh:205
state_vector & states()
Return all states.
Definition: kripkegraph.hh:342
state_num get_init_state_number() const
Return the initial state number.
Definition: kripkegraph.hh:226
internal::edge_storage< unsigned, unsigned, unsigned, internal::boxed_label< void, true > > edge_storage_t
Edge storage type.
Definition: kripkegraph.hh:172
std::string format_state(unsigned n) const
Return string representation of state n.
Definition: kripkegraph.hh:281
unsigned init_number_
Initial state number.
Definition: kripkegraph.hh:191
virtual kripke_graph_succ_iterator< graph_t > * succ_iter(const spot::state *st) const override
Return an iterator on the state passed as a parameter.
Definition: kripkegraph.hh:241
unsigned new_states(unsigned n, bdd cond)
Create n new states with the given condition.
Definition: kripkegraph.hh:321
internal::distate_storage< unsigned, internal::boxed_label< kripke_graph_state, false > > state_storage_t
State storage type.
Definition: kripkegraph.hh:174
digraph< kripke_graph_state, void > graph_t
The underlying graph type.
Definition: kripkegraph.hh:164
unsigned state_num
State number type.
Definition: kripkegraph.hh:185
unsigned new_edge(unsigned src, unsigned dst)
Add a new edge from src to dst.
Definition: kripkegraph.hh:327
internal::state_out< const graph_t > out(unsigned src) const
Return successor range for src.
Definition: kripkegraph.hh:366
state_num state_number(const state *st) const
Return the number of state st.
Definition: kripkegraph.hh:260
internal::state_out< graph_t > out(unsigned src)
Return successor range for src.
Definition: kripkegraph.hh:374
internal::all_trans< graph_t > edges() noexcept
Return all edges.
Definition: kripkegraph.hh:358
const kripke_graph_state * state_from_number(state_num n) const
Return the state with the given number.
Definition: kripkegraph.hh:268
edge_storage_t & edge_storage(unsigned t)
Return edge storage at index t.
Definition: kripkegraph.hh:303
graph_t g_
The underlying graph.
Definition: kripkegraph.hh:187
internal::all_trans< const graph_t > edges() const noexcept
Return all edges.
Definition: kripkegraph.hh:350
kripke_graph(const bdd_dict_ptr &d)
Construct with BDD dictionary d.
Definition: kripkegraph.hh:194
virtual std::string format_state(const state *st) const override
Format the state as a string for printing.
Definition: kripkegraph.hh:290
const edge_storage_t edge_storage(unsigned t) const
Return edge storage at index t.
Definition: kripkegraph.hh:309
unsigned new_state(bdd cond)
Create a new state with the given condition.
Definition: kripkegraph.hh:315
virtual const kripke_graph_state * get_init_state() const override
Get the initial state of the automaton.
Definition: kripkegraph.hh:234
void set_init_state(state_num s)
Set the initial state.
Definition: kripkegraph.hh:217
std::vector< state_storage_t > state_vector
Vector of states.
Definition: kripkegraph.hh:181
unsigned num_edges() const
Return the number of edges.
Definition: kripkegraph.hh:211
kripke_graph_state * state_from_number(state_num n)
Return the state with the given number.
Definition: kripkegraph.hh:275
const state_vector & states() const
Return all states.
Definition: kripkegraph.hh:335
virtual bdd state_condition(const state *s) const override
Get the condition on the state.
Definition: kripkegraph.hh:296
Iterator code for Kripke structure.
Definition: kripke.hh:130
Interface for a Kripke structure.
Definition: kripke.hh:178
Abstract class for states.
Definition: twa.hh:49
LTL/PSL formula interface.
std::shared_ptr< bdd_dict > bdd_dict_ptr
Shared pointer to a bdd_dict.
Definition: bdddict.hh:304
Definition: automata.hh:26
std::shared_ptr< kripke_graph > kripke_graph_ptr
Shared pointer to a kripke_graph.
Definition: kripkegraph.hh:382
kripke_graph_ptr make_kripke_graph(const bdd_dict_ptr &d)
Create a new kripke_graph.
Definition: kripkegraph.hh:386
Concrete class for kripke_graph states.
Definition: kripkegraph.hh:31
bdd cond() const
Return the BDD condition of this state.
Definition: kripkegraph.hh:84
virtual size_t hash() const override
Hash a state.
Definition: kripkegraph.hh:68
kripke_graph_state & operator=(const kripke_graph_state &other) noexcept
Copy assignment operator.
Definition: kripkegraph.hh:46
virtual kripke_graph_state * clone() const override
Duplicate a state.
Definition: kripkegraph.hh:74
kripke_graph_state(const kripke_graph_state &other) noexcept
Copy constructor.
Definition: kripkegraph.hh:40
virtual void destroy() const override
Release a state.
Definition: kripkegraph.hh:79
virtual int compare(const spot::state *other) const override
Compares two states (that come from the same automaton).
Definition: kripkegraph.hh:56
kripke_graph_state(bdd cond=bddfalse) noexcept
Construct with BDD condition cond.
Definition: kripkegraph.hh:34
void cond(bdd c)
Set the BDD condition to c.
Definition: kripkegraph.hh:90

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.9.1