18 #ifndef _VATA2_NFA_HH_
19 #define _VATA2_NFA_HH_
25 #include <unordered_map>
42 using PostSymb = std::unordered_map<Symbol, StateSet>;
46 using SubsetMap = std::unordered_map<StateSet, State>;
47 using Path = std::vector<State>;
48 using Word = std::vector<Symbol>;
55 using StringDict = std::unordered_map<std::string, std::string>;
88 throw std::runtime_error(
"Unimplemented");
95 throw std::runtime_error(
"Unimplemented");
114 symbol_map(str_sym_map), cnt_symbol(init_symbol)
116 assert(
nullptr != symbol_map);
119 virtual std::list<Symbol>
get_symbols()
const override;
122 const std::set<Symbol>& syms)
const override;
127 virtual Symbol translate_symb(
const std::string& str)
override
130 std::istringstream stream(str);
142 if (str.length() == 3 &&
143 ((str[0] ==
'\'' && str[2] ==
'\'') ||
144 (str[0] ==
'\"' && str[2] ==
'\"')
151 std::istringstream stream(str);
156 virtual std::list<Symbol>
get_symbols()
const override;
158 const std::set<Symbol>& syms)
const override;
174 template <
class InputIt>
178 for (; first != last; ++first)
181 std::tie(std::ignore, inserted) = symbol_map.insert({*first, cnt++});
184 throw std::runtime_error(
"multiple occurrence of the same symbol");
195 auto it = symbol_map.find(str);
196 if (symbol_map.end() == it)
198 throw std::runtime_error(
"unknown symbol \'" + str +
"\'");
204 virtual std::list<Symbol>
get_symbols()
const override;
206 const std::set<Symbol>& syms)
const override;
264 return this->
has_trans({src, symb, tgt});
306 if (
nullptr == post_s)
316 auto it = transitions.find(state);
317 return (transitions.end() == it)?
nullptr : &it->second;
334 const Alphabet& alphabet,
350 const Alphabet& alphabet,
360 return is_incl(smaller, bigger, alphabet,
nullptr, params);
402 State* last_state_num =
nullptr);
407 State* last_state_num =
nullptr)
410 determinize(&result, aut, subset_map, last_state_num);
417 const Alphabet& alphabet,
424 const Alphabet& alphabet,
433 complement(&result, aut, alphabet, subset_map);
438 void revert(Nfa* result,
const Nfa& aut);
456 bool is_complete(
const Nfa& aut,
const Alphabet& alphabet);
472 construct(&result, parsec, symbol_map, state_map);
490 construct(&result, parsec, alphabet, state_map);
523 const std::vector<std::string>& input)
526 for (
auto str : input) { result.push_back(symbol_map.at(str)); }
531 std::ostream&
operator<<(std::ostream& strm,
const Nfa& nfa);
543 struct hash<Vata2::Nfa::Trans>
547 size_t accum = std::hash<Vata2::Nfa::State>{}(trans.
src);
virtual Symbol translate_symb(const std::string &str) override
translates a string into a symbol
bool haskey(const T &cont, const K &key)
checks whether a container with find contains a key
std::vector< Symbol > Word
a finite-length path through automaton
void add_final(State state)
bool is_deterministic(const Nfa &aut)
Test whether an automaton is deterministic, i.e., whether it has exactly one initial state and every ...
bool is_in_lang(const Nfa &aut, const Word &word)
Checks whether a string is in the language of an automaton.
void determinize(Nfa *result, const Nfa &aut, SubsetMap *subset_map=nullptr, State *last_state_num=nullptr)
Determinize an automaton.
Parsed data (single section)
StateSet::const_iterator ssIt
EnumAlphabet(InputIt first, InputIt last)
bool is_incl(const Nfa &smaller, const Nfa &bigger, const Alphabet &alphabet, Word *cex=nullptr, const StringDict ¶ms={})
Checks inclusion of languages of two automata (smaller <= bigger)?
std::unordered_map< std::string, Symbol > StringToSymbolMap
std::unordered_map< std::string, std::string > StringDict
bool are_state_disjoint(const Nfa &lhs, const Nfa &rhs)
Do the automata have disjoint sets of states?
bool is_universal(const Nfa &aut, const Alphabet &alphabet, Word *cex=nullptr, const StringDict ¶ms={})
Is the language of the automaton universal?
virtual std::list< Symbol > get_complement(const std::set< Symbol > &syms) const
complement of a set of symbols wrt the alphabet
bool operator!=(const Trans &rhs) const
bool is_lang_empty_cex(const Nfa &aut, Word *cex)
std::unordered_map< StateSet, State > SubsetMap
std::unordered_map< Symbol, std::string > SymbolToStringMap
std::set< State > finalstates
virtual Symbol translate_symb(const std::string &symb)=0
translates a string into a symbol
number of transitions; has linear time complexity
virtual Symbol translate_symb(const std::string &str) override
translates a string into a symbol
virtual std::list< Symbol > get_symbols() const
gets a list of symbols in the alphabet
size_t hash_combine(size_t lhs, const T &rhs)
Combine two hash values.
EnumAlphabet(std::initializer_list< std::string > l)
std::unordered_map< State, std::string > StateToStringMap
virtual std::list< Symbol > get_complement(const std::set< Symbol > &syms) const override
complement of a set of symbols wrt the alphabet
size_t operator()(const Vata2::Nfa::Trans &trans) const
void revert(Nfa *result, const Nfa &aut)
Reverting the automaton.
const PostSymb * post(State state) const
bool operator==(const Trans &rhs) const
static const_iterator for_end(const Nfa *nfa)
OnTheFlyAlphabet(StringToSymbolMap *str_sym_map, Symbol init_symbol=0)
std::set< State > initialstates
const_iterator & operator++()
void add_trans(State src, Symbol symb, State tgt)
virtual std::list< Symbol > get_symbols() const override
gets a list of symbols in the alphabet
void init()
global constructor to be called at program startup (from vm-dispatch)
void add_trans(const Trans &trans)
std::pair< Word, bool > get_word_for_path(const Nfa &aut, const Path &path)
Obtains a word corresponding to a path in an automaton (or sets a flag)
Symbol operator[](const std::string &symb)
also translates strings to symbols
std::unordered_map< State, PostSymb > StateToPostMap
post over a symbol
const PostSymb & operator[](State state) const
virtual std::list< Symbol > get_complement(const std::set< Symbol > &syms) const override
complement of a set of symbols wrt the alphabet
virtual std::list< Symbol > get_complement(const std::set< Symbol > &syms) const override
complement of a set of symbols wrt the alphabet
virtual std::list< Symbol > get_symbols() const override
gets a list of symbols in the alphabet
const Trans & operator*() const
const PostSymb EMPTY_POST
void union_norename(Nfa *result, const Nfa &lhs, const Nfa &rhs)
Compute union of a pair of automata Assumes that sets of states of lhs, rhs, and result are disjoint...
std::vector< State > Path
PostSymb::const_iterator psIt
bool has_trans(State src, Symbol symb, State tgt) const
const_iterator end() const
std::unordered_map< Symbol, StateSet > PostSymb
set of states
Vata2::Parser::ParsedSection serialize(const Nfa &aut, const SymbolToStringMap *symbol_map=nullptr, const StateToStringMap *state_map=nullptr)
serializes Nfa into a ParsedSection
static const_iterator for_begin(const Nfa *nfa)
bool is_complete(const Nfa &aut, const Alphabet &alphabet)
Test for automaton completeness wrt an alphabet. An automaton is complete if every reachable state ha...
void construct(Nfa *aut, const Vata2::Parser::ParsedSection &parsec, StringToSymbolMap *symbol_map=nullptr, StringToStateMap *state_map=nullptr)
Loads an automaton from Parsed object.
void add_final(const std::vector< State > vec)
bool operator!=(const const_iterator &rhs) const
std::ostream & operator<<(std::ostream &strm, const Nfa &nfa)
operator<<
const_iterator begin() const
void make_complete(Nfa *aut, const Alphabet &alphabet, State sink_state)
makes the transition relation complete
size_t trans_size() const
bool has_trans(const Trans &trans) const
std::unordered_map< std::string, State > StringToStateMap
a finite-length word
virtual std::list< Symbol > get_symbols() const override
gets a list of symbols in the alphabet
void complement(Nfa *result, const Nfa &aut, const Alphabet &alphabet, SubsetMap *subset_map=nullptr)
Complement.
std::unordered_map< std::pair< State, State >, State > ProductMap
transitions
virtual Symbol translate_symb(const std::string &str) override
translates a string into a symbol
std::set< State > StateSet
bool is_prfx_in_lang(const Nfa &aut, const Word &word)
Checks whether the prefix of a string is in the language of an automaton.
void add_initial(State state)
Word encode_word(const StringToSymbolMap &symbol_map, const std::vector< std::string > &input)
Encodes a vector of strings (each corresponding to one symbol) into a Word instance.
bool operator==(const const_iterator &rhs) const
bool is_lang_empty(const Nfa &aut, Path *cex=nullptr)
Is the language of the automaton empty?
void add_initial(const std::vector< State > vec)
Trans(State src, Symbol symb, State tgt)
StateToPostMap::const_iterator stpmIt
bool has_initial(State state) const
void intersection(Nfa *result, const Nfa &lhs, const Nfa &rhs, ProductMap *prod_map=nullptr)
Compute intersection of a pair of automata.
bool has_final(State state) const