mCRL2
Loading...
Searching...
No Matches
action_label_string.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
19#ifndef MCRL2_LTS_ACTION_LABEL_STRING_H
20#define MCRL2_LTS_ACTION_LABEL_STRING_H
21
22#include <set>
25
26namespace mcrl2
27{
28namespace lts
29{
30
36class action_label_string: public std::string
37{
38 public:
39
40 /* \brief Default constructor. The label will contain the default string.
41 */
43 {}
44
47
50
51 /* \brief A constructor, where the string s is taken to become the action label.
52 */
53 explicit action_label_string(const std::string& s):std::string(sort_multiactions(s))
54 {}
55
56 /* \brief An auxiliary function to hide actions. Makes a best-effort attempt at
57 parsing the string as an mCRL2 multi-action and hiding the actions
58 with a name in string_vector. The format is assumed to be a1(args1)|...|an(argsn),
59 and each action ai that occurs in string_vector will be renamed to tau.
60 If all actions are hidden, the resulting
61 action name will be "tau". The effects of this operation on action
62 labels that are not generated by the pretty printer are undefined. */
63 void hide_actions(const std::vector<std::string>& string_vector)
64 {
65 std::string ns; // New label after hiding.
66 std::string::iterator b = begin(); // Start position of current action.
67 std::string::iterator c = begin(); // End position of current action.
68 while (c != end())
69 {
70 std::string a;
71 // Parse action name into a
72 while (c != end() && *c != '(' && *c != '|')
73 {
74 a.append(1, *c++);
75 }
76 // Skip over parameters of a, if any
77 if (c != end() && *c == '(')
78 {
79 std::size_t nd = 0;
80 do
81 {
82 switch (*c++)
83 {
84 case '(': ++nd; break;
85 case ')': --nd; break;
86 }
87 }
88 while (nd > 0 && c != end());
89 }
90 // Search for the next occurrence of "|" or the end of the string. This is the end of the current action.
91 while (c != end() && *c != '|')
92 {
93 ++c;
94 }
95 // Add the appropriate action to the new action label string
96 if (std::find(string_vector.begin(), string_vector.end(), a) == string_vector.end())
97 {
98 // This action is not hidden.
99 if (!ns.empty())
100 {
101 ns.append("|");
102 }
103 ns.append(b, c);
104 }
105 // Skip the multi-action operator, if any
106 if (c != end() && *c == '|')
107 {
108 ++c;
109 }
110 // The start of the next action is pointed to by c.
111 b = c;
112 }
113 if (ns.empty())
114 {
115 assign(tau_action());
116 }
117 else
118 {
119 assign(ns);
120 }
121 }
122
123 /* \brief A comparison operator comparing the action_label_strings in the same way as strings.
124 */
125 bool operator<(const action_label_string& l) const
126 {
127 return std::string(*this)<std::string(l);
128 }
129
130 /* \brief The action label string that represents the internal action.
131 */
133 {
134 static action_label_string tau_action("tau");
135 return tau_action;
136 }
137
138 protected:
144 static std::string sort_multiactions(const std::string& s)
145 {
146 // Split this multiaction in actions a1(...) , a2(...), a3(...)
147 static std::multiset<std::string> split_actions; // Make a static variable to avoid this multiset being created often.
148 assert(split_actions.empty());
149 size_t nested_bracket_counter=0;
150 size_t start_of_current_action=0;
151 for(size_t i=0; i<s.size(); ++i)
152 {
153 const char c=s[i];
154 if (c==')')
155 { if (nested_bracket_counter==0)
156 {
157 throw mcrl2::runtime_error("The transition label is not a proper multiaction (1): " + s);
158 }
159 else
160 {
161 nested_bracket_counter--;
162 }
163 }
164 else if (c=='(')
165 {
166 nested_bracket_counter++;
167 }
168 else if (c=='|' && nested_bracket_counter==0)
169 {
170 split_actions.insert(s.substr(start_of_current_action,i-start_of_current_action));
171 start_of_current_action=i+1;
172 }
173 }
174 if (start_of_current_action==s.size()) // In this case the multiaction ends with a "|".
175 {
176 throw mcrl2::runtime_error("The transition label is not a proper multiaction (2): " + s);
177 }
178 if (split_actions.size()==0) // No actions were split off. No
179 {
180 // This is not a multiaction.
181 return s;
182 }
183 split_actions.insert(s.substr(start_of_current_action,s.size()-start_of_current_action));
184 std::string result;
185 for(const std::string& s: split_actions)
186 {
187 if (result.size()>0)
188 {
189 result.append("|");
190 }
191 result.append(s);
192 }
193 split_actions.clear();
194 return result;
195 }
196};
197
198/* \brief A pretty print operator on action labels, returning it as a string.
199*/
200inline std::string pp(const action_label_string& l)
201{
202 return l;
203}
204
205} // namespace lts
206} // namespace mcrl2
207
208namespace std
209{
210
212template<>
213struct hash< mcrl2::lts::action_label_string >
214{
215 std::size_t operator()(const mcrl2::lts::action_label_string& as) const
216 {
217 hash<std::string> hasher;
218 return hasher(as);
219 }
220};
221
222} // namespace std
223
224
225#endif
226
227
This class contains strings to be used as values for action labels in lts's.
static std::string sort_multiactions(const std::string &s)
Sort multiactions in a string.
void hide_actions(const std::vector< std::string > &string_vector)
action_label_string & operator=(const action_label_string &)=default
Copy assignment.
static const action_label_string & tau_action()
action_label_string(const std::string &s)
bool operator<(const action_label_string &l) const
action_label_string(const action_label_string &)=default
Copy constructor.
A class that contains a labelled transition system.
Definition lts.h:83
Standard exception class for reporting runtime errors.
Definition exception.h:27
Exception classes for use in libraries and tools.
std::string pp(const abstraction &x)
Definition data.cpp:39
A class that takes a linear process specification and checks all tau-summands of that LPS for conflue...
Definition indexed_set.h:72
STL namespace.
std::size_t operator()(const mcrl2::lts::action_label_string &as) const
String manipulation functions.
#define hash(l, r, m)
Definition tree_set.cpp:23