mCRL2
Loading...
Searching...
No Matches
pgsolver.cpp
Go to the documentation of this file.
1// Author(s): Sjoerd Cranen
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 pgsolver.cpp
10/// \brief Add your file description here.
11
12#include "mcrl2/pbes/io.h"
13#include "mcrl2/pbes/join.h"
14#include "mcrl2/pbes/normal_forms.h"
15
16namespace mcrl2
17{
18
19namespace pbes_system
20{
21
23
24///
25/// \brief Convert a sequence of Boolean variables to PGSolver format.
26///
27template <typename Iter>
28std::string boolean_variables2pgsolver(Iter first, Iter last, const variable_map& variables)
29{
30 std::set<std::size_t> variables_int;
31 for (Iter i = first; i != last; ++i)
32 {
34 variable_map::const_iterator j = variables.find(b.name());
35 if (j == variables.end())
36 {
37 throw mcrl2::runtime_error("Found undeclared variable in boolean_variables2pgsolver: " + pbes_system::pp(b));
38 }
39 variables_int.insert(j->second);
40 }
41 return utilities::string_join(variables_int, ", ");
42}
43
44///
45/// \brief Convert a BES expression to PGSolver format.
46///
47static std::string bes_expression2pgsolver(const pbes_expression& p, const variable_map& variables)
48{
49 std::string result;
50 if (is_and(p))
51 {
52 std::set<pbes_expression> expressions = split_and(p);
53 result = boolean_variables2pgsolver(expressions.begin(), expressions.end(), variables);
54 }
55 else if (is_or(p))
56 {
57 std::set<pbes_expression> expressions = split_or(p);
58 result = boolean_variables2pgsolver(expressions.begin(), expressions.end(), variables);
59 }
61 {
63 variable_map::const_iterator i = variables.find(b.name());
64 if (i == variables.end())
65 {
66 throw mcrl2::runtime_error("Found undeclared variable in bes_expression2cwi: " + pbes_system::pp(p));
67 }
68 std::stringstream out;
69 out << i->second;
70 result = out.str();
71 }
72 else
73 {
74 throw mcrl2::runtime_error("Unknown or unsupported expression encountered in bes_expression2cwi: " + pbes_system::pp(p));
75 }
76 return result;
77}
78
79///
80/// \brief Save a sequence of BES equations in to a stream in PGSolver format.
81///
82template <typename Iter>
83void bes2pgsolver(Iter first, Iter last, std::ostream& out, bool maxpg)
84{
85 // Number the variables of the equations 0, 1, ... and put them in the map variables.
86 // Also store player to which variables without operand are assigned, per
87 // block, in block_to_player.
88 variable_map variables;
89 std::size_t index = 0;
90 std::map<int, int> block_to_player;
91
92 bool and_in_block = false;
93 int block = 0;
95 for (Iter i = first; i != last; ++i)
96 {
97 if(i->symbol() != sigma)
98 {
99 block_to_player[block++] = (and_in_block)?1:0;
100 and_in_block = false;
101 sigma = i->symbol();
102 }
103 variables[i->variable().name()] = index++;
104 and_in_block = and_in_block || is_and(i->formula());
105 }
106 block_to_player[block] = (and_in_block)?1:0;
107
108 if(maxpg && block % 2 == 1)
109 {
110 ++block;
111 }
112
113 out << "parity " << index -1 << ";\n";
114
115 int priority = 0;
117 for (Iter i = first; i != last; ++i)
118 {
119 if(i->symbol() != sigma)
120 {
121 ++priority;
122 sigma = i->symbol();
123 }
124
125 out << variables[i->variable().name()]
126 << " "
127 << (maxpg?(block-priority):priority)
128 << " "
129 << (is_and(i->formula()) ? 1 : (is_or(i->formula())?0:block_to_player[priority]))
130 << " "
131 << bes_expression2pgsolver(i->formula(), variables)
132#ifdef MCRL2_BES2PGSOLVER_PRINT_VARIABLE_NAMES
133 << " "
134 << "\""
135 << pbes_systembes::pp(i->variable())
136 << "\""
137#endif
138 << ";\n";
139 }
140}
141
142void save_bes_pgsolver(const pbes& bes, std::ostream& stream, bool maxpg)
143{
144 pbes bes_standard_form(bes);
145 make_standard_form(bes_standard_form, true);
146 if(bes.initial_state().name() != bes.equations().front().variable().name())
147 {
148 std::stringstream ss;
149 ss << "The initial state " << bes_standard_form.initial_state() << " and the left hand side "
150 "of the first equation (" << bes_standard_form.equations().front().variable() << ") do "
151 "not correspond. Cannot save BES to PGSolver format.";
152 throw mcrl2::runtime_error(ss.str());
153 }
154 bes2pgsolver(bes_standard_form.equations().begin(), bes_standard_form.equations().end(), stream, maxpg);
155}
156
157} // namespace pbes_system
158
159} // namespace mcrl2
static fixpoint_symbol nu()
Returns the nu symbol.
fixpoint_symbol & operator=(fixpoint_symbol &&) noexcept=default
parameterized boolean equation system
Definition pbes.h:58
const propositional_variable_instantiation & initial_state() const
Returns the initial state.
Definition pbes.h:192
propositional_variable_instantiation & initial_state()
Returns the initial state.
Definition pbes.h:199
\brief A propositional variable instantiation
Standard exception class for reporting runtime errors.
Definition exception.h:27
The main namespace for the aterm++ library.
Definition algorithm.h:21
The main namespace for the PBES library.
std::map< core::identifier_string, std::size_t > variable_map
Definition pgsolver.cpp:22
std::string boolean_variables2pgsolver(Iter first, Iter last, const variable_map &variables)
Convert a sequence of Boolean variables to PGSolver format.
Definition pgsolver.cpp:28
std::string pp(const pbes_system::propositional_variable_instantiation &x)
Definition pbes.cpp:46
bool is_or(const atermpp::aterm &x)
void bes2pgsolver(Iter first, Iter last, std::ostream &out, bool maxpg)
Save a sequence of BES equations in to a stream in PGSolver format.
Definition pgsolver.cpp:83
std::string pp(const pbes_system::pbes_expression &x)
Definition pbes.cpp:44
void save_bes_pgsolver(const pbes &bes, std::ostream &stream, bool maxpg)
Definition pgsolver.cpp:142
static std::string bes_expression2pgsolver(const pbes_expression &p, const variable_map &variables)
Convert a BES expression to PGSolver format.
Definition pgsolver.cpp:47
bool is_propositional_variable_instantiation(const atermpp::aterm &x)
bool is_and(const atermpp::aterm &x)
void make_standard_form(pbes &eqn, bool recursive_form=false)
Transforms a PBES into standard form.
std::size_t operator()(const atermpp::detail::reference_aterm< T > &t) const