Line data Source code
1 : // Author(s): Wieger Wesselink
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 : /// \file mcrl2/data/anonymize.h
10 : /// \brief add your file description here.
11 :
12 : #ifndef MCRL2_DATA_ANONYMIZE_H
13 : #define MCRL2_DATA_ANONYMIZE_H
14 :
15 : #include "mcrl2/core/detail/print_utility.h"
16 : #include "mcrl2/data/builder.h"
17 : #include "mcrl2/data/data_specification.h"
18 :
19 : namespace mcrl2 {
20 :
21 : namespace data {
22 :
23 : namespace detail {
24 :
25 : template <typename Derived>
26 : struct anonymize_builder: public data::sort_expression_builder<Derived>
27 : {
28 : typedef data::sort_expression_builder<Derived> super;
29 : using super::enter;
30 : using super::leave;
31 : using super::apply;
32 : using super::update;
33 :
34 : data::set_identifier_generator id_generator;
35 : std::map<core::identifier_string, core::identifier_string> sort_name_substitution;
36 : std::map<core::identifier_string, core::identifier_string> variable_name_substitution;
37 : std::map<core::identifier_string, core::identifier_string> function_symbol_name_substitution;
38 :
39 0 : Derived& derived()
40 : {
41 0 : return static_cast<Derived&>(*this);
42 : }
43 :
44 0 : void add_name(const core::identifier_string& name, std::map<core::identifier_string, core::identifier_string>& substitution, const std::string& hint)
45 : {
46 : using utilities::detail::has_key;
47 :
48 : // do not rename numeric values
49 0 : if (std::isdigit(std::string(name)[0]))
50 : {
51 0 : substitution[name] = name;
52 : }
53 :
54 0 : if (!has_key(substitution, name))
55 : {
56 0 : substitution[name] = id_generator(hint);
57 : }
58 0 : }
59 :
60 0 : data_specification make_data_specification(
61 : const basic_sort_vector& user_defined_sorts,
62 : const function_symbol_vector& user_defined_constructors,
63 : const function_symbol_vector& user_defined_mappings,
64 : const alias_vector& user_defined_aliases,
65 : const data_equation_vector& user_defined_equations
66 : )
67 : {
68 0 : data_specification result;
69 0 : for (const auto& x: user_defined_sorts) { result.add_sort(x); }
70 0 : for (const auto& x: user_defined_constructors) { result.add_constructor(x); }
71 0 : for (const auto& x: user_defined_mappings) { result.add_mapping(x); }
72 0 : for (const auto& x: user_defined_aliases) { result.add_alias(x); }
73 0 : for (const auto& x: user_defined_equations) { result.add_equation(x); }
74 0 : return result;
75 0 : }
76 :
77 0 : void update(data_specification& x)
78 : {
79 0 : std::set<sort_expression> system_defined_sorts;
80 0 : std::set<function_symbol> system_defined_constructors;
81 0 : std::set<function_symbol> system_defined_mappings;
82 0 : x.get_system_defined_sorts_constructors_and_mappings(system_defined_sorts, system_defined_constructors, system_defined_mappings);
83 :
84 : // prevent system defined sorts from being renamed
85 0 : for (const auto& s: system_defined_sorts)
86 : {
87 0 : if (is_basic_sort(s))
88 : {
89 0 : const auto& name = atermpp::down_cast<basic_sort>(s).name();
90 0 : sort_name_substitution[name] = name;
91 : }
92 : }
93 :
94 : // prevent system defined functions from being renamed
95 0 : for (const auto& f: system_defined_constructors)
96 : {
97 0 : function_symbol_name_substitution[f.name()] = f.name();
98 : }
99 0 : for (const auto& f: system_defined_mappings)
100 : {
101 0 : function_symbol_name_substitution[f.name()] = f.name();
102 : }
103 :
104 0 : auto user_defined_sorts = x.user_defined_sorts();
105 0 : auto user_defined_constructors = x.user_defined_constructors();
106 0 : auto user_defined_mappings = x.user_defined_mappings();
107 0 : auto user_defined_aliases = x.user_defined_aliases();
108 0 : auto user_defined_equations = x.user_defined_equations();
109 :
110 0 : derived().update(user_defined_sorts );
111 0 : derived().update(user_defined_constructors);
112 0 : derived().update(user_defined_mappings );
113 0 : derived().update(user_defined_aliases );
114 0 : derived().update(user_defined_equations );
115 0 : x = make_data_specification(
116 : user_defined_sorts ,
117 : user_defined_constructors,
118 : user_defined_mappings ,
119 : user_defined_aliases ,
120 : user_defined_equations
121 : );
122 0 : }
123 :
124 0 : void enter(const variable& x)
125 : {
126 0 : add_name(x.name(), variable_name_substitution, "v");
127 0 : }
128 :
129 : template <class T>
130 0 : void apply(T& result, const variable& x)
131 : {
132 0 : derived().enter(x);
133 0 : sort_expression sort;
134 0 : derived().apply(sort, x.sort());
135 0 : make_variable(result,variable_name_substitution[x.name()], sort);
136 0 : derived().leave(x);
137 0 : }
138 :
139 0 : void enter(const function_symbol& x)
140 : {
141 0 : add_name(x.name(), function_symbol_name_substitution, "f");
142 0 : }
143 :
144 : template <class T>
145 0 : void apply(T& result, const function_symbol& x)
146 : {
147 0 : derived().enter(x);
148 0 : sort_expression sort;
149 0 : derived().apply(sort, x.sort());
150 0 : make_function_symbol(result,function_symbol_name_substitution[x.name()], sort);
151 0 : derived().leave(x);
152 0 : }
153 :
154 0 : void enter(const basic_sort& x)
155 : {
156 0 : add_name(x.name(), sort_name_substitution, "s");
157 0 : }
158 :
159 : template <class T>
160 0 : void apply(T& result, const basic_sort& x)
161 : {
162 0 : derived().enter(x);
163 0 : make_basic_sort(result,sort_name_substitution[x.name()]);
164 0 : derived().leave(x);
165 0 : }
166 :
167 0 : void enter(const structured_sort_constructor& x)
168 : {
169 0 : add_name(x.name(), function_symbol_name_substitution, "f");
170 0 : if (x.recogniser() != atermpp::empty_string())
171 : {
172 0 : add_name(x.recogniser(), function_symbol_name_substitution, "f");
173 : }
174 0 : }
175 :
176 : template <class T>
177 0 : void apply(T& result, const structured_sort_constructor& x)
178 : {
179 0 : derived().enter(x);
180 0 : structured_sort_constructor_argument_list arguments;
181 0 : derived().apply(arguments, x.arguments());
182 0 : make_structured_sort_constructor(result,
183 0 : function_symbol_name_substitution[x.name()],
184 : arguments,
185 0 : x.recogniser() == atermpp::empty_string() ? x.recogniser() : function_symbol_name_substitution[x.recogniser()]);
186 0 : derived().leave(x);
187 0 : }
188 :
189 0 : void enter(const structured_sort_constructor_argument& x)
190 : {
191 0 : add_name(x.name(), function_symbol_name_substitution, "f");
192 0 : }
193 :
194 : template <class T>
195 0 : void apply(T& result, const structured_sort_constructor_argument& x)
196 : {
197 0 : derived().enter(x);
198 0 : sort_expression sort;
199 0 : derived().apply(sort, x.sort());
200 0 : result = structured_sort_constructor_argument(function_symbol_name_substitution[x.name()], sort);
201 0 : derived().leave(x);
202 0 : }
203 :
204 : template <class T>
205 0 : void apply(T& result, const alias& x)
206 : {
207 0 : derived().enter(x);
208 0 : sort_expression sort;
209 0 : derived().apply(sort, x.reference());
210 0 : basic_sort name;
211 0 : derived().apply(name, x.name());
212 0 : make_alias(result, name, sort);
213 0 : derived().leave(x);
214 0 : }
215 : };
216 :
217 : struct anonymize_builder_instance: public anonymize_builder<anonymize_builder_instance>
218 : {
219 : };
220 :
221 : } // namespace detail
222 :
223 : inline
224 : void anonymize(data_specification& dataspec)
225 : {
226 : detail::anonymize_builder_instance f;
227 : f.update(dataspec);
228 : std::cout << dataspec << std::endl;
229 : }
230 :
231 : } // namespace data
232 :
233 : } // namespace mcrl2
234 :
235 : #endif // MCRL2_DATA_ANONYMIZE_H
|