word.cc 6.36 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
3
// Copyright (C) 2013, 2014, 2015, 2016 Laboratoire de Recherche et
// Développement de l'Epita (LRDE).
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//
// This file is part of Spot, a model checking library.
//
// Spot is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// Spot is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
// License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

20
21
22
#include <spot/twaalgos/word.hh>
#include <spot/twa/bddprint.hh>
#include <spot/twa/bdddict.hh>
23
24
25
#include <spot/tl/parse.hh>
#include <spot/tl/simplify.hh>
#include <spot/tl/apcollect.hh>
26
27
28

namespace spot
{
29
  twa_word::twa_word(const twa_run_ptr& run)
30
    : dict_(run->aut->get_dict())
31
  {
32
33
34
35
    for (auto& i: run->prefix)
      prefix.push_back(i.label);
    for (auto& i: run->cycle)
      cycle.push_back(i.label);
36
    dict_->register_all_variables_of(run->aut, this);
37
38
  }

39
40
41
42
43
  twa_word::twa_word(const bdd_dict_ptr& dict)
    : dict_(dict)
  {
  }

44
  void
45
  twa_word::simplify()
46
47
48
49
50
51
52
53
54
55
  {
    // If all the formulas on the cycle are compatible, reduce the
    // cycle to a simple conjunction.
    //
    // For instance
    //   !a|!b; b; a&b; cycle{a; b; a&b}
    // can be reduced to
    //   !a|!b; b; a&b; cycle{a&b}
    {
      bdd all = bddtrue;
56
57
      for (auto& i: cycle)
	all &= i;
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
      if (all != bddfalse)
	{
	  cycle.clear();
	  cycle.push_back(all);
	}
    }
    // If the last formula of the prefix is compatible with the
    // last formula of the cycle, we can shift the cycle and
    // reduce the prefix.
    //
    // For instance
    //   !a|!b; b; a&b; cycle{a&b}
    // can be reduced to
    //   !a|!b; cycle{a&b}
    while (!prefix.empty())
      {
	bdd a = prefix.back() & cycle.back();
	if (a == bddfalse)
	  break;
	prefix.pop_back();
	cycle.pop_back();
	cycle.push_front(a);
      }
    // Get rid of any disjunction.
    //
    // For instance
    //   !a|!b; cycle{a&b}
    // can be reduced to
    //   !a&!b; cycle{a&b}
87
88
89
90
    for (auto& i: prefix)
      i = bdd_satone(i);
    for (auto& i: cycle)
      i = bdd_satone(i);
91
92
93
  }

  std::ostream&
94
  operator<<(std::ostream& os, const twa_word& w)
95
  {
96
97
98
    auto d = w.get_dict();
    if (!w.prefix.empty())
      for (auto& i: w.prefix)
99
	{
100
	  bdd_print_formula(os, d, i);
101
102
	  os << "; ";
	}
103
    assert(!w.cycle.empty());
104
105
    bool notfirst = false;
    os << "cycle{";
106
    for (auto& i: w.cycle)
107
108
109
110
      {
	if (notfirst)
	  os << "; ";
	notfirst = true;
111
	bdd_print_formula(os, d, i);
112
      }
113
    os << '}';
114
115
    return os;
  }
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254

  namespace
  {
    static void word_parse_error(const std::string& word,
				 size_t i, parse_error_list pel)
    {
      for (auto& err: pel)
	{
	  err.first.begin.column += i;
	  err.first.end.column += i;
	}
      std::ostringstream os;
      format_parse_errors(os, word, pel);
      throw parse_error(os.str());
    }

    static void word_parse_error(const std::string& word, size_t i,
				 const std::string& message)
    {
      if (i == std::string::npos)
	i = word.size();
      std::ostringstream s;
      s << ">>> " << word << '\n';
      for (auto j = i + 4; j > 0; --j)
	s << ' ';
      s << '^' << '\n';
      s << message << '\n';
      throw parse_error(s.str());
    }

    static size_t skip_next_formula(const std::string& word, size_t begin)
    {
      bool quoted = false;
      auto size = word.size();
      for (auto j = begin; j < size; ++j)
	{
	  auto c = word[j];
	  if (!quoted && (c == ';' || c == '}'))
	    return j;
	  if (c == '"')
	    quoted = !quoted;
	  else if (quoted && c == '\\')
	    ++j;
	}
      if (quoted)
	word_parse_error(word, word.size(), "Unclosed string");
      return std::string::npos;
    }
  }

  twa_word_ptr parse_word(const std::string& word, const bdd_dict_ptr& dict)
  {
    atomic_prop_set aps;
    parse_error_list pel;
    tl_simplifier tls(dict);
    twa_word_ptr tw = make_twa_word(dict);
    size_t i = 0;
    auto ind = i;

    auto extract_bdd =
      [&](typename twa_word::seq_t& seq)
      {
	auto sub = word.substr(i, ind - i);
	formula f = spot::parse_infix_boolean(sub, pel);
	if (!pel.empty())
	  word_parse_error(word, i, pel);
	atomic_prop_collect(f, &aps);
	seq.push_back(tls.as_bdd(f));
	if (word[ind] == '}')
	  return true;
	// Skip blanks after semi-colon
	i = word.find_first_not_of(' ', ind + 1);
	return false;
      };

    // Parse the prefix part. Can be empty.
    while (word.substr(i, 6) != std::string("cycle{"))
      {
	ind = skip_next_formula(word, i);
	if (ind == std::string::npos)
	  word_parse_error(word, word.size(),
			   "A twa_word must contain a cycle");
	if (word[ind] == '}')
	  word_parse_error(word, ind, "Expected ';' delimiter :"
			   "'}' stands for ending a cycle");
	// Exract formula, convert it to bdd and add it to the prefix sequence
	extract_bdd(tw->prefix);
	if (i == std::string::npos)
	  word_parse_error(word, ind + 1, "Missing cycle in formula");
      }
    // Consume "cycle{"
    i += 6;
    while (true)
      {
	ind = skip_next_formula(word, i);
	if (ind == std::string::npos)
	  word_parse_error(word, word.size(),
			   "Missing ';' or '}' after formula");
	// Extract formula, convert it to bdd and add it to the cycle sequence
	// Break if an '}' is encountered
	if (extract_bdd(tw->cycle))
	  break;
	if (i == std::string::npos)
	  word_parse_error(word, ind + 1,
			   "Missing end of cycle character : '}'");
      }
    if (ind != word.size() - 1)
      word_parse_error(word, ind + 1, "Input should be finished after cycle");
    for (auto ap: aps)
      dict->register_proposition(ap, tw.get());
    return tw;
  }

  twa_graph_ptr twa_word::as_automaton() const
  {
    twa_graph_ptr aut = make_twa_graph(dict_);

    aut->prop_weak(true);
    aut->prop_deterministic(true);

    size_t i = 0;
    aut->new_states(prefix.size() + cycle.size());
    for (auto b: prefix)
      {
	aut->new_edge(i, i + 1, b);
	++i;
      }
    size_t j = i;
    auto b = cycle.begin();
    auto end = --cycle.end();
    for (; b != end; ++b)
      {
	aut->new_edge(i, i + 1, *b);
	++i;
      }
    // Close the loop
    aut->new_edge(i, j, *b);
    return aut;
  }
255
}