You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.2 KiB

  1. /*
  2. * This file is part of the program ltl2dstar (http://www.ltl2dstar.de/).
  3. * Copyright (C) 2005-2007 Joachim Klein <j.klein@ltl2dstar.de>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #ifndef DA_STATE_H
  19. #define DA_STATE_H
  20. /** @file
  21. * Provides class DA_State for storing a state of a deterministic omega-automaton.
  22. */
  23. #include "common/Exceptions.hpp"
  24. #include "common/Indexable.hpp"
  25. #include "common/BitSet.hpp"
  26. #include <iostream>
  27. #include <string>
  28. #include <memory>
  29. template <typename Label,
  30. template <typename State> class EdgeContainer,
  31. typename AcceptanceCondition> class DA;
  32. /**
  33. * A state of a deterministic omega-automaton.
  34. * For a description of the template parameters, see class DA.
  35. */
  36. template <typename Label, template <typename N> class EdgeContainer, typename AcceptanceCondition>
  37. class DA_State : public Indexable<DA_State<Label,EdgeContainer,AcceptanceCondition> > {
  38. public:
  39. /** The type of the automaton containing this state */
  40. typedef DA<Label, EdgeContainer,AcceptanceCondition> graph_type;
  41. /** The type of the edges in the DA. */
  42. typedef typename graph_type::edge_type edge_type;
  43. /** The type of the states in the DA (ie this DA_State class). */
  44. typedef typename graph_type::state_type state_type;
  45. /** The type of the EdgeContainer for the DA_State. */
  46. typedef EdgeContainer<DA_State> edge_container_type;
  47. /** The type of an iterator over all the edges of this state. */
  48. typedef typename edge_container_type::iterator edge_iterator;
  49. /**
  50. * Constructor.
  51. * @param graph The automaton (DA) that contains this state.
  52. */
  53. DA_State(graph_type& graph)
  54. : _graph(graph),
  55. _edges(graph.getAPSize()) {;}
  56. ~DA_State() {;}
  57. /** Get the EdgeContainer to access the edges. */
  58. edge_container_type& edges() {return _edges;}
  59. /** Get the EdgeContainer to access the edges. */
  60. const edge_container_type& edges() const {return _edges;}
  61. /** Get the name (index) of this state. */
  62. unsigned int getName() const {
  63. return _graph.getIndexForState(this);
  64. };
  65. /** Print the name of the state on an output stream. */
  66. friend std::ostream& operator<<(std::ostream& out, DA_State& state) {
  67. out << state.getName();
  68. return out;
  69. }
  70. /** Set an description for the state */
  71. void setDescription(const std::string& s) {
  72. _description.reset(new std::string(s));
  73. }
  74. /**
  75. * Get an description for the state (previously set using setDescription()).
  76. * Should only be called after verifying that the state hasDescription()
  77. * @return a const string ref to the description
  78. */
  79. const std::string& getDescription() {
  80. assert(hasDescription());
  81. return *_description;
  82. }
  83. /**
  84. * Check wheter the state has a description.
  85. */
  86. bool hasDescription() {
  87. return _description.get()!=0;
  88. }
  89. /**
  90. * Checks if all transitions originating in this state
  91. * leed back to itself.
  92. */
  93. bool hasOnlySelfLoop() {
  94. for (edge_iterator eit=edges().begin();
  95. eit!=edges().end();
  96. ++eit) {
  97. if (this != (*eit).second) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. /** Get the AcceptanceForState access functor for this state */
  104. typename AcceptanceCondition::AcceptanceForState acceptance() {
  105. typename AcceptanceCondition::AcceptanceForState acc(_graph.acceptance(),
  106. this->getName());
  107. return acc;
  108. }
  109. /** A Functor that gets the name of the to state in an edge */
  110. struct GetToForEdgeFunctor :
  111. public std::unary_function<typename edge_container_type::edge_type, unsigned int> {
  112. unsigned int operator()(typename edge_container_type::edge_type e) const {
  113. return e.second->getName();
  114. }
  115. };
  116. /**
  117. * The type of an iterator over the names (indizes) of the
  118. * states that are reachable in one step from this state.
  119. * Note: A state can occur multiple times!
  120. */
  121. typedef boost::transform_iterator<
  122. GetToForEdgeFunctor,
  123. typename edge_container_type::EdgeIterator
  124. > successor_iterator;
  125. /**
  126. * Returns an iterator over the names of the successors, pointing to the first.
  127. * Note: A state can occur multiple times!
  128. */
  129. successor_iterator successors_begin() {
  130. return successor_iterator(_edges.begin(), GetToForEdgeFunctor());
  131. }
  132. /**
  133. * Returns an iterator over the names of the successors, pointing after the last.
  134. * Note: A state can occur multiple times!
  135. */
  136. successor_iterator successors_end() {
  137. return successor_iterator(_edges.end(), GetToForEdgeFunctor());
  138. }
  139. private:
  140. /** The automaton of which this state is a part. */
  141. graph_type& _graph;
  142. /** The edges */
  143. edge_container_type _edges;
  144. /** A description */
  145. std::auto_ptr<std::string> _description;
  146. };
  147. #endif