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