File: invariant_checker.cpp

package info (click to toggle)
mcrl2 201409.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd
  • size: 46,348 kB
  • ctags: 29,960
  • sloc: cpp: 213,160; ansic: 16,219; python: 13,238; yacc: 309; lex: 214; xml: 197; makefile: 83; sh: 82; pascal: 17
file content (218 lines) | stat: -rwxr-xr-x 6,599 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
// Author(s): Luc Engelen
// Copyright: see the accompanying file COPYING or copy at
// https://svn.win.tue.nl/trac/MCRL2/browser/trunk/COPYING
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
/// \file source/invariant_checker.cpp
/// \brief Add your file description here.

#include <sstream>
#include <cstring>

#include "mcrl2/utilities/logger.h"
#include "mcrl2/core/print.h"
#include "mcrl2/lps/invariant_checker.h"
#include "mcrl2/data/detail/prover/solver_type.h"
#include "mcrl2/data/detail/bdd_prover.h"
#include "mcrl2/data/substitutions/mutable_map_substitution.h"
#include "mcrl2/utilities/exception.h"

namespace mcrl2
{
namespace lps
{
namespace detail
{

using namespace mcrl2::data;
using namespace mcrl2::data::detail;
using namespace mcrl2::core;
using namespace mcrl2::log;

// Class Invariant_Checker ------------------------------------------------------------------------
// Class Invariant_Checker - Functions declared private -----------------------------------------

void Invariant_Checker::print_counter_example()
{
  if (f_counter_example)
  {
    data_expression v_counter_example(f_bdd_prover.get_counter_example());
    assert(v_counter_example.defined());
    mCRL2log(info) << "  Counter example: " << data::pp(v_counter_example) << "\n";
  }
}

// --------------------------------------------------------------------------------------------

void Invariant_Checker::save_dot_file(size_t a_summand_number)
{
  if (! f_dot_file_name.empty())
  {
    std::ostringstream v_file_name;

    v_file_name << f_dot_file_name;

    if (a_summand_number == (size_t)-1) // Dangerous
    {
      v_file_name << "-init.dot";
    }
    else
    {
      v_file_name << "-" << a_summand_number << ".dot";
    }
    f_bdd2dot.output_bdd(f_bdd_prover.get_bdd(), v_file_name.str().c_str());
  }
}

// --------------------------------------------------------------------------------------------

bool Invariant_Checker::check_init(const data_expression a_invariant)
{
  data::mutable_map_substitution<> v_substitutions;
  const assignment_list l=f_init.assignments();
  for (assignment_list::const_iterator i=l.begin(); i!=l.end(); ++i)
  {
    v_substitutions[i->lhs()]=i->rhs();
  }

  data_expression b_invariant = data::replace_variables_capture_avoiding(a_invariant, v_substitutions, data::substitution_variables(v_substitutions));
  f_bdd_prover.set_formula(b_invariant);
  if (f_bdd_prover.is_tautology() == answer_yes)
  {
    return true;
  }
  else
  {
    if (f_bdd_prover.is_contradiction() != answer_yes)
    {
      print_counter_example();
      save_dot_file((size_t)(-1));
    }
    return false;
  }
}

// --------------------------------------------------------------------------------------------

bool Invariant_Checker::check_summand(
  const data::data_expression a_invariant,
  const lps::action_summand a_summand,
  const size_t a_summand_number)
{
  using namespace mcrl2::data::sort_bool;
  const data_expression v_condition = a_summand.condition();
  const assignment_list v_assignments = a_summand.assignments();

  data::mutable_map_substitution<> v_substitutions;

  for (assignment_list::const_iterator i=v_assignments.begin(); i!=v_assignments.end(); ++i)
  {
    v_substitutions[i->lhs()]=i->rhs();
  }

  const data_expression v_subst_invariant = data::replace_variables_capture_avoiding(a_invariant, v_substitutions, data::substitution_variables(v_substitutions));

  const data_expression v_formula = implies(and_(a_invariant, v_condition), v_subst_invariant);
  f_bdd_prover.set_formula(v_formula);
  if (f_bdd_prover.is_tautology() == answer_yes)
  {
    mCRL2log(verbose) << "The invariant holds for summand " << a_summand_number << "." << std::endl;
    return true;
  }
  else
  {
    mCRL2log(info) << "The invariant does not hold for summand " << a_summand_number << std::endl;
    if (f_bdd_prover.is_contradiction() != answer_yes)
    {
      print_counter_example();
      save_dot_file(a_summand_number);
    }
    return false;
  }
}

// --------------------------------------------------------------------------------------------

bool Invariant_Checker::check_summands(const data::data_expression a_invariant)
{
  bool v_result = true;
  size_t v_summand_number = 1;

  for (action_summand_vector::const_iterator i=f_summands.begin();
       i!=f_summands.end() && (f_all_violations || v_result); ++i)
  {
    v_result = check_summand(a_invariant, *i, v_summand_number) && v_result;
    v_summand_number++;
  }
  return v_result;
}

// Class Invariant_Checker - Functions declared public --------------------------------------------

Invariant_Checker::Invariant_Checker(
  mcrl2::lps::specification const& a_lps,
  mcrl2::data::rewriter::strategy a_rewrite_strategy, int a_time_limit, bool a_path_eliminator, smt_solver_type a_solver_type,
  bool a_apply_induction, bool a_counter_example, bool a_all_violations, std::string const& a_dot_file_name
):
  f_bdd_prover(a_lps.data(), used_data_equation_selector(a_lps.data()), a_rewrite_strategy, a_time_limit, a_path_eliminator, a_solver_type, a_apply_induction)
{
  f_init = a_lps.initial_process();
  f_summands = a_lps.process().action_summands();
  f_counter_example = a_counter_example;
  f_all_violations = a_all_violations;
  f_dot_file_name = a_dot_file_name;
}

// --------------------------------------------------------------------------------------------

Invariant_Checker::~Invariant_Checker()
{
  // Nothing to free.
}

// --------------------------------------------------------------------------------------------

bool Invariant_Checker::check_invariant(const data::data_expression a_invariant)
{
  bool v_result = true;

  if (check_init(a_invariant))
  {
    mCRL2log(verbose) << "The invariant holds for the initial state." << std::endl;
  }
  else
  {
    mCRL2log(info) << "The invariant does not hold for the initial state." << std::endl;
    v_result = false;
  }
  if ((f_all_violations || v_result))
  {
    if (check_summands(a_invariant))
    {
      mCRL2log(verbose) << "The invariant holds for all summands." << std::endl;
    }
    else
    {
      mCRL2log(info) << "The invariant does not hold for all summands." << std::endl;
      v_result = false;
    }
  }
  if (v_result)
  {
    mCRL2log(info) << "The invariant holds for this LPS." << std::endl;
  }
  else
  {
    mCRL2log(info) << "The invariant does not hold for this LPS." << std::endl;
  }

  return v_result;
}

} // namespace detail
} // namespace lps
} // namespace mcrl2