tgbamask.cc 3.7 KB
Newer Older
1
// -*- coding: utf-8 -*-
2
// Copyright (C) 2013, 2014 Laboratoire de Recherche et Développement
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
// de l'Epita (LRDE).
//
// 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/>.

#include "tgbamask.hh"
#include <vector>

namespace spot
{
  namespace
  {
    struct transition
    {
      const state* dest;
      bdd cond;
      bdd acc;
    };
    typedef std::vector<transition> transitions;

    struct succ_iter_filtered: public tgba_succ_iterator
    {
      ~succ_iter_filtered()
      {
39 40
	for (auto& t: trans_)
	  t.dest->destroy();
41 42
      }

43
      bool first()
44 45
      {
	it_ = trans_.begin();
46
	return it_ != trans_.end();
47 48
      }

49
      bool next()
50 51
      {
	++it_;
52
	return it_ != trans_.end();
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
      }

      bool done() const
      {
	return it_ == trans_.end();
      }

      state* current_state() const
      {
	return it_->dest->clone();
      }

      bdd current_condition() const
      {
	return it_->cond;
      }

      bdd current_acceptance_conditions() const
      {
	return it_->acc;
      }

      transitions trans_;
      transitions::const_iterator it_;
    };


    class tgba_mask_keep: public tgba_mask
    {
      const state_set& mask_;
    public:
      tgba_mask_keep(const tgba* masked,
		     const state_set& mask,
		     const state* init)
	: tgba_mask(masked, init),
	  mask_(mask)
      {
      }

      bool wanted(const state* s) const
      {
	state_set::const_iterator i = mask_.find(s);
	return i != mask_.end();
      }
    };

    class tgba_mask_ignore: public tgba_mask
    {
      const state_set& mask_;
    public:
      tgba_mask_ignore(const tgba* masked,
		       const state_set& mask,
		       const state* init)
	: tgba_mask(masked, init),
	  mask_(mask)
      {
      }

      bool wanted(const state* s) const
      {
	state_set::const_iterator i = mask_.find(s);
	return i == mask_.end();
      }
    };


  }


  tgba_mask::tgba_mask(const tgba* masked,
		       const state* init)
    : tgba_proxy(masked),
      init_(init)
  {
    if (!init)
      init_ = masked->get_init_state();
  }

  tgba_mask::~tgba_mask()
  {
    init_->destroy();
  }

  state* tgba_mask::get_init_state() const
  {
    return init_->clone();
  }

  tgba_succ_iterator*
142
  tgba_mask::succ_iter(const state* state) const
143
  {
144 145 146 147
    succ_iter_filtered* res;
    if (iter_cache_)
      {
	res = down_cast<succ_iter_filtered*>(iter_cache_);
148
	res->trans_.clear();
149 150 151 152 153 154
	iter_cache_ = nullptr;
      }
    else
      {
	res = new succ_iter_filtered;
      }
155
    for (auto it: original_->succ(state))
156
      {
157
	const spot::state* s = it->current_state();
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
	if (!wanted(s))
	  {
	    s->destroy();
	    continue;
	  }
	transition t = { s,
			 it->current_condition(),
			 it->current_acceptance_conditions() };
	res->trans_.push_back(t);
      }
    return res;
  }


  const tgba*
  build_tgba_mask_keep(const tgba* to_mask,
		       const state_set& to_keep,
		       const state* init)
  {
    return new tgba_mask_keep(to_mask, to_keep, init);
  }

  const tgba*
  build_tgba_mask_ignore(const tgba* to_mask,
			 const state_set& to_ignore,
			 const state* init)
  {
    return new tgba_mask_ignore(to_mask, to_ignore, init);
  }

}