mCRL2
Loading...
Searching...
No Matches
lts.h
Go to the documentation of this file.
1// Author(s): Jan Friso Groote
2// Copyright: see the accompanying file COPYING or copy at
3// https://github.com/mCRL2org/mCRL2/blob/master/COPYING
4//
5// Distributed under the Boost Software License, Version 1.0.
6// (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9
18#ifndef MCRL2_LTS_LTS_H
19#define MCRL2_LTS_LTS_H
20
21#include <string>
22#include <vector>
23#include <cstdio>
24#include <iostream>
25#include <algorithm>
26#include <cassert>
27#include <set>
28#include <map>
30#include "mcrl2/lts/lts_type.h"
31
32
33namespace mcrl2
34{
35
40namespace lts
41{
42
44{
45 public:
48 {
49 return lts_none;
50 }
51
54 {
55 // Does intentionally not provide any action.
56 }
57
60 bool operator==(const lts_default_base&) const
61 {
62 return true;
63 }
64};
65
66
81template < class STATE_LABEL_T, class ACTION_LABEL_T, class LTS_BASE = lts_default_base>
82class lts: public LTS_BASE
83{
84 public:
85
88 typedef STATE_LABEL_T state_label_t;
89
92 typedef ACTION_LABEL_T action_label_t;
93
96 typedef LTS_BASE base_t;
97
100 typedef typename std::vector < STATE_LABEL_T >::size_type states_size_type;
101
104 typedef typename std::vector < ACTION_LABEL_T >::size_type labels_size_type;
105
108 typedef typename std::vector<transition>::size_type transitions_size_type;
109
112 static constexpr bool is_probabilistic_lts=false;
113
114 protected:
115
118 std::vector<transition> m_transitions;
119 std::vector<STATE_LABEL_T> m_state_labels;
120 std::vector<ACTION_LABEL_T> m_action_labels; // At position 0 we always find the label that corresponds to tau.
121 // The following set contains the labels that are recorded as being hidden.
122 // This allows tools to apply reductions assuming that these actions are hidden, but still provide
123 // feedback, for instance using counter examples, using the original action name.
124 std::set<labels_size_type> m_hidden_label_set;
125
126 // Auxiliary function. Rename the labels according to the action_rename_map;
127 void rename_labels(const std::map<labels_size_type, labels_size_type>& action_rename_map)
128 {
129 if (action_rename_map.size()>0) // Check whether there is something to rename.
130 {
132 {
133 auto i = action_rename_map.find(t.label());
134 if (i!=action_rename_map.end())
135 {
136 t=transition(t.from(),i->second,t.to());
137 }
138 }
139 }
140 }
141
142 // Auxiliary function. a is the partially hidden action label of which the original
143 // action label occurred at position i.
144 // If label a exists at position j, set action_rename_map[i]:=j;
145 // if a does not occur, adapt the action labels by setting label i to j.
146 void store_action_label_to_be_renamed(const ACTION_LABEL_T& a,
147 const labels_size_type i,
148 std::map<labels_size_type, labels_size_type>& action_rename_map)
149 {
150 bool found=false;
151 for (labels_size_type j=0; !found && j< num_action_labels(); ++j)
152 {
153 if (a==action_label(j))
154 {
155 if (i!=j)
156 {
157 action_rename_map[i]=j;
158 }
159 found=true;
160 }
161 }
162 if (!found) // a!=action_label(j) for any j, then rename action_label(i) to a.
163 {
164 set_action_label(i,a);
165 }
166 }
167
168 public:
169
173 : m_nstates(0)
174 {
175 m_action_labels.push_back(ACTION_LABEL_T::tau_action());
176 }
177
180 lts(const lts& l):
181 LTS_BASE(l),
188 {
189 assert(m_action_labels.size()>0 && m_action_labels[const_tau_label_index]==ACTION_LABEL_T::tau_action());
190 }
191
194 lts& operator=(const lts& l)
195 {
196 static_cast<LTS_BASE&>(*this)=l;
197 m_nstates = l.m_nstates;
198 m_init_state = l.m_init_state;
199 m_transitions = l.m_transitions;
200 m_state_labels = l.m_state_labels;
201 m_action_labels = l.m_action_labels;
202 m_hidden_label_set = l.m_hidden_label_set;
203 assert(m_action_labels.size()>0 && m_action_labels[const_tau_label_index]==ACTION_LABEL_T::tau_action());
204 return *this;
205 }
206
209 bool operator==(const lts& other) const
210 {
211 return static_cast<const LTS_BASE&>(*this)==static_cast<const LTS_BASE&>(other) &&
212 m_nstates == other.m_nstates &&
213 m_init_state == other.m_init_state &&
214 m_transitions == other.m_transitions &&
215 m_state_labels == other.m_state_labels &&
216 m_action_labels == other.m_action_labels &&
217 m_hidden_label_set == other.m_hidden_label_set;
218 }
219
222 void swap(lts& l)
223 {
224 static_cast<LTS_BASE&>(*this).swap(static_cast<LTS_BASE&>(l));
225 {
227 m_init_state=l.m_init_state;
228 l.m_init_state=aux;
229 }
230 {
231 const states_size_type aux=m_nstates;
232 m_nstates=l.m_nstates;
233 l.m_nstates=aux;
234 }
235 m_transitions.swap(l.m_transitions);
236 m_state_labels.swap(l.m_state_labels);
237 m_action_labels.swap(l.m_action_labels);
238 assert(m_action_labels.size()>0 && m_action_labels[const_tau_label_index]==ACTION_LABEL_T::tau_action());
239 assert(l.m_action_labels.size()>0 && l.m_action_labels[const_tau_label_index]==ACTION_LABEL_T::tau_action());
240 m_hidden_label_set.swap(l.m_hidden_label_set);
241 }
242
246 {
247 return m_nstates;
248 }
249
253 std::vector<STATE_LABEL_T>& state_labels()
254 {
255 return m_state_labels;
256 }
257
261 const std::vector<STATE_LABEL_T>& state_labels() const
262 {
263 return m_state_labels;
264 }
265
272 {
273 return m_state_labels.size();
274 }
275
280 void set_num_states(const states_size_type n, const bool has_state_labels = true)
281 {
282 m_nstates = n;
283 if (has_state_labels)
284 {
285 if (m_state_labels.size() > 0)
286 {
287 m_state_labels.resize(n);
288 }
289 else
290 {
291 m_state_labels = std::vector<STATE_LABEL_T>(n);
292 }
293 }
294 else
295 {
296 m_state_labels = std::vector<STATE_LABEL_T>();
297 }
298 }
299
303 {
304 return m_transitions.size();
305 }
306
310 void set_num_transitions(const std::size_t n)
311 {
312 m_transitions.resize(n);
313 m_transitions.shrink_to_fit();
314 }
315
320 {
321 m_action_labels.resize(n);
322 assert(m_action_labels.size()>0 && m_action_labels[const_tau_label_index]==ACTION_LABEL_T::tau_action());
323 }
324
328 {
329 assert(m_action_labels.size()>0 && m_action_labels[const_tau_label_index]==ACTION_LABEL_T::tau_action());
330 return m_action_labels.size();
331 }
332
336 {
337 return m_init_state;
338 }
339
344 {
345 assert(state<m_nstates);
347 }
348
356 states_size_type add_state(const STATE_LABEL_T& label=STATE_LABEL_T())
357 {
358 if (label!=STATE_LABEL_T())
359 {
361 m_state_labels.push_back(label);
362 }
363 return m_nstates++;
364 }
365
370 labels_size_type add_action(const ACTION_LABEL_T& label)
371 {
372 if (label==ACTION_LABEL_T::tau_action())
373 {
375 }
376 assert(std::find(m_action_labels.begin(),m_action_labels.end(),label)==m_action_labels.end()); // Action labels must be unique.
377 const labels_size_type label_index=m_action_labels.size();
378 m_action_labels.push_back(label);
379 return label_index;
380 }
381
386 {
388 }
389
393 void set_state_label(const states_size_type state, const STATE_LABEL_T& label)
394 {
395 assert(state<m_nstates);
396 assert(m_nstates==m_state_labels.size());
398 }
399
402 const std::vector<ACTION_LABEL_T>& action_labels() const
403 {
404 return m_action_labels;
405 }
406
411 void set_action_label(const labels_size_type action, const ACTION_LABEL_T& label)
412 {
413 assert(action<m_action_labels.size());
414 assert((action==const_tau_label_index) == (label==ACTION_LABEL_T::tau_action())); // a tau action is always stored at index 0.
415 assert(m_action_labels[action] == label ||
416 std::find(m_action_labels.begin(),m_action_labels.end(),label)==m_action_labels.end()); // Action labels must be unique.
417 m_action_labels[action] = label;
418 }
419
423 const std::set<labels_size_type>& hidden_label_set() const
424 {
425 return m_hidden_label_set;
426 }
427
431 std::set<labels_size_type>& hidden_label_set()
432 {
433 return m_hidden_label_set;
434 }
435
439 void set_hidden_label_set(const std::set<labels_size_type>& m)
440 {
442 }
443
448 {
449 if (!m_hidden_label_set.empty() && m_hidden_label_set.count(action)>0)
450 {
451 return tau_label_index();
452 }
453 return action;
454 }
455
459 STATE_LABEL_T state_label(const states_size_type state) const
460 {
461 assert(state<m_nstates);
462 assert(m_nstates==m_state_labels.size());
463 return m_state_labels[state];
464 }
465
470 {
471 (void)state; // Suppress an unused variable warning.
472 assert(state<m_nstates);
473 return false;
474 }
475
479 ACTION_LABEL_T action_label(const labels_size_type action) const
480 {
481 assert(action < m_action_labels.size());
482 return m_action_labels[action];
483 }
484
493 void clear_transitions(const std::size_t n=0)
494 {
495 m_transitions = std::vector<transition>();
496 m_transitions.reserve(n);
497 }
498
505 {
506 m_action_labels.clear();
507 m_action_labels.push_back(ACTION_LABEL_T::tau_action());
508 m_hidden_label_set.clear();
509 }
510
515 {
516 m_state_labels.clear();
517 }
518
522 {
523 m_state_labels.resize(num_states());
524 for(std::size_t i=0; i<num_states(); ++i)
525 {
526 set_state_label(i,STATE_LABEL_T::number_to_label(i)); // YYYYYY TODO FINISH.
527 }
528 }
529
533 void clear()
534 {
538 m_nstates = 0;
539 }
540
545 const std::vector<transition>& get_transitions() const
546 {
547 return m_transitions;
548 }
549
554 std::vector<transition>& get_transitions()
555 {
556 return m_transitions;
557 }
558
564 {
565 m_transitions.push_back(t);
566 }
567
572 bool is_tau(labels_size_type action) const
573 {
574 assert(m_action_labels.size()>0 && m_action_labels[const_tau_label_index]==ACTION_LABEL_T::tau_action());
575 return (action==const_tau_label_index);
576 }
577
578
589 void record_hidden_actions(const std::vector<std::string>& tau_actions)
590 {
591 if (tau_actions.size()==0)
592 {
593 return;
594 }
595
596 std::map<labels_size_type, labels_size_type> action_rename_map;
597 for (labels_size_type i=0; i< num_action_labels(); ++i)
598 {
599 ACTION_LABEL_T a=action_label(i);
600 a.hide_actions(tau_actions);
601 if (a==ACTION_LABEL_T::tau_action())
602 {
604 {
605 m_hidden_label_set.insert(i);
606 }
607 }
608 else if (a!=action_label(i))
609 {
610 /* In this the action_label i is changed by the tau_actions but not renamed to tau.
611 We check whether a maps onto another action label index. If yes, it is added to
612 the rename map, and we explicitly rename transition labels with this label afterwards.
613 If no, we rename the action label.
614 */
615 store_action_label_to_be_renamed(a, i, action_rename_map);
616 }
617 }
618
619 rename_labels(action_rename_map);
620 }
621
628 void apply_hidden_actions(const std::vector<std::string>& tau_actions)
629 {
630 if (tau_actions.size()==0)
631 {
632 return;
633 }
634
635 std::map<labels_size_type, labels_size_type> action_rename_map;
636 for (labels_size_type i=0; i< num_action_labels(); ++i)
637 {
638 ACTION_LABEL_T a=action_label(i);
639 a.hide_actions(tau_actions);
640#ifndef NDEBUG
641 ACTION_LABEL_T b=a;
642 b.hide_actions(tau_actions);
643 assert(a==b); // hide_actions applied twice yields the same result as applying it once.
644#endif
645 store_action_label_to_be_renamed(a, i, action_rename_map);
646 }
647
648 rename_labels(action_rename_map);
649 }
650
655 bool has_state_info() const
656 {
657 return m_state_labels.size() > 0;
658 }
659
664 bool has_action_info() const
665 {
666 return m_action_labels.size() > 1;
667 }
668};
669
670}
671}
672
673#endif // MCRL2_LTS_LTS_H
Read-only balanced binary tree of terms.
void swap(lts_default_base &)
Standard swap function.
Definition lts.h:53
lts_type type()
Provides the type of this lts, in casu lts_none.
Definition lts.h:47
bool operator==(const lts_default_base &) const
Standard equality function.
Definition lts.h:60
A class that contains a labelled transition system.
Definition lts.h:83
states_size_type num_state_labels() const
Gets the number of state labels of this LTS.
Definition lts.h:271
labels_size_type num_action_labels() const
Gets the number of action labels of this LTS.
Definition lts.h:327
states_size_type m_init_state
Definition lts.h:117
void add_transition(const transition &t)
Add a transition to the lts.
Definition lts.h:563
void set_num_action_labels(const labels_size_type n)
Sets the number of action labels of this LTS.
Definition lts.h:319
bool has_action_info() const
Checks whether this LTS has labels associated with its actions, which are numbers.
Definition lts.h:664
bool is_tau(labels_size_type action) const
Checks whether an action is a tau action.
Definition lts.h:572
std::set< labels_size_type > & hidden_label_set()
Returns the hidden label set that tells for each label what its corresponding hidden label is.
Definition lts.h:431
states_size_type num_states() const
Gets the number of states of this LTS.
Definition lts.h:245
void set_hidden_label_set(const std::set< labels_size_type > &m)
Sets the hidden label map that tells for each label what its corresponding hidden label is.
Definition lts.h:439
void clear_transitions(const std::size_t n=0)
Clear the transitions of an lts.
Definition lts.h:493
void set_num_transitions(const std::size_t n)
Sets the number of transitions of this LTS and tries to shrink the datastructure.
Definition lts.h:310
void clear_actions()
Clear the action labels of an lts.
Definition lts.h:504
void set_num_states(const states_size_type n, const bool has_state_labels=true)
Sets the number of states of this LTS.
Definition lts.h:280
void store_action_label_to_be_renamed(const ACTION_LABEL_T &a, const labels_size_type i, std::map< labels_size_type, labels_size_type > &action_rename_map)
Definition lts.h:146
void add_state_number_as_state_information()
Label each state with its state number.
Definition lts.h:521
static constexpr bool is_probabilistic_lts
An indicator that this is not a probabilistic lts.
Definition lts.h:112
bool is_probabilistic(const states_size_type state) const
Gets the label of a state.
Definition lts.h:469
ACTION_LABEL_T action_label_t
The type of action labels.
Definition lts.h:92
const std::set< labels_size_type > & hidden_label_set() const
Returns the hidden label set that tells for each label what its corresponding hidden label is.
Definition lts.h:423
std::vector< transition > m_transitions
Definition lts.h:118
std::vector< STATE_LABEL_T > & state_labels()
Provides the state labels of an LTS.
Definition lts.h:253
ACTION_LABEL_T action_label(const labels_size_type action) const
Gets the label of an action.
Definition lts.h:479
const labels_size_type tau_label_index() const
Provide the index of the label that represents tau.
Definition lts.h:385
const std::vector< ACTION_LABEL_T > & action_labels() const
The action labels in this lts.
Definition lts.h:402
bool has_state_info() const
Checks whether this LTS has state values associated with its states.
Definition lts.h:655
lts(const lts &l)
Creates a copy of the supplied LTS.
Definition lts.h:180
void set_initial_state(const states_size_type state)
Sets the initial state number of this LTS.
Definition lts.h:343
void set_action_label(const labels_size_type action, const ACTION_LABEL_T &label)
Sets the label of an action.
Definition lts.h:411
void rename_labels(const std::map< labels_size_type, labels_size_type > &action_rename_map)
Definition lts.h:127
std::set< labels_size_type > m_hidden_label_set
Definition lts.h:124
std::vector< STATE_LABEL_T >::size_type states_size_type
The sort that contains the indices of states.
Definition lts.h:100
void apply_hidden_actions(const std::vector< std::string > &tau_actions)
Rename actions in the lts by hiding the actions in the vector tau_actions.
Definition lts.h:628
labels_size_type apply_hidden_label_map(const labels_size_type action) const
If the action label is registered hidden, it returns tau, otherwise the original label.
Definition lts.h:447
std::vector< transition > & get_transitions()
Gets a reference to the vector of transitions of the current lts.
Definition lts.h:554
std::vector< ACTION_LABEL_T >::size_type labels_size_type
The sort that represents the indices of labels.
Definition lts.h:104
lts & operator=(const lts &l)
Standard assignment operator.
Definition lts.h:194
const std::vector< STATE_LABEL_T > & state_labels() const
Provides the state labels of an LTS.
Definition lts.h:261
std::vector< STATE_LABEL_T > m_state_labels
Definition lts.h:119
STATE_LABEL_T state_label(const states_size_type state) const
Gets the label of a state.
Definition lts.h:459
states_size_type add_state(const STATE_LABEL_T &label=STATE_LABEL_T())
Adds a state to this LTS.
Definition lts.h:356
STATE_LABEL_T state_label_t
The type of state labels.
Definition lts.h:88
void clear()
Clear the transitions system.
Definition lts.h:533
lts()
Creates an empty LTS.
Definition lts.h:172
bool operator==(const lts &other) const
Standard equality operator.
Definition lts.h:209
states_size_type m_nstates
Definition lts.h:116
const std::vector< transition > & get_transitions() const
Gets a const reference to the vector of transitions of the current lts.
Definition lts.h:545
LTS_BASE base_t
The type of the used lts base.
Definition lts.h:96
std::vector< transition >::size_type transitions_size_type
The sort that contains indices of transitions.
Definition lts.h:108
states_size_type initial_state() const
Gets the initial state number of this LTS.
Definition lts.h:335
void clear_state_labels()
Clear the labels of an lts.
Definition lts.h:514
void swap(lts &l)
Swap this lts with the supplied supplied LTS.
Definition lts.h:222
void record_hidden_actions(const std::vector< std::string > &tau_actions)
Records all actions with a string that occurs in tau_actions internally.
Definition lts.h:589
std::vector< ACTION_LABEL_T > m_action_labels
Definition lts.h:120
transitions_size_type num_transitions() const
Gets the number of transitions of this LTS.
Definition lts.h:302
void set_state_label(const states_size_type state, const STATE_LABEL_T &label)
Sets the label of a state.
Definition lts.h:393
labels_size_type add_action(const ACTION_LABEL_T &label)
Adds an action with a label to this LTS.
Definition lts.h:370
A class containing triples, source label and target representing transitions.
Definition transition.h:48
The file containing the types of labelled transition systems.
atermpp::term_balanced_tree< data::data_expression > state
Definition state.h:24
lts_type
The enumerated type lts_type contains an index for every type type of labelled transition system that...
Definition lts_type.h:37
std::size_t label(const outgoing_pair_t &p)
Label of a pair of a label and target state.
static const std::size_t const_tau_label_index
Definition transition.h:28
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
A header file defining a transition as a triple from,label,to.