diff --git a/alib2algo/src/conversions/fa2re/formal/StateEliminationFormal.cpp b/alib2algo/src/conversions/fa2re/formal/StateEliminationFormal.cpp index c872dd3bdf0fb955205d2c3d1e50009383bda4c9..ea5d6d9e74faac3c516aa22462b4abc1fa53b941 100644 --- a/alib2algo/src/conversions/fa2re/formal/StateEliminationFormal.cpp +++ b/alib2algo/src/conversions/fa2re/formal/StateEliminationFormal.cpp @@ -205,7 +205,7 @@ const regexp::FormalRegExpElement* StateEliminationFormal::transition(const auto return ret; } -//TODO prepsat jako konstruktory v alib2data +//TODO pouzit konstruktory v alib2data template<> automaton::ExtendedNFA StateEliminationFormal::constructExtendedNFA(const automaton::MultiInitialStateNFA& automaton) { diff --git a/alib2data/src/automaton/FSM/CompactNFA.cpp b/alib2data/src/automaton/FSM/CompactNFA.cpp index 2a211f329caac78aa0baaf983b3c308be4acc404..14a1d3b804276a8195517e4936681075ce2c440e 100644 --- a/alib2data/src/automaton/FSM/CompactNFA.cpp +++ b/alib2data/src/automaton/FSM/CompactNFA.cpp @@ -6,6 +6,11 @@ */ #include "CompactNFA.h" +#include "EpsilonNFA.h" +#include "MultiInitialStateNFA.h" +#include "NFA.h" +#include "DFA.h" +#include "../Automaton.h" #include "../../std/map.hpp" #include "../AutomatonException.h" #include "../../string/StringAlphabetGetter.h" @@ -19,6 +24,53 @@ CompactNFA::CompactNFA(const State& initialState) : SingleInitialState(initialSt } +CompactNFA::CompactNFA(const EpsilonNFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + if(transition.first.second.is<string::Epsilon>()) { + std::pair<State, string::LinearString> key = std::make_pair(transition.first.first, string::LinearString( std::vector<alphabet::Symbol> { } )); + transitions[key] = transition.second; + } else { + std::pair<State, string::LinearString> key = std::make_pair(transition.first.first, string::LinearString( std::vector<alphabet::Symbol> { transition.first.second.get<alphabet::Symbol>() } )); + transitions[key] = transition.second; + } + } +} + +CompactNFA::CompactNFA(const MultiInitialStateNFA& other) : SingleInitialState(automaton::createUniqueState(automaton::State("q0"), other.getStates())) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, string::LinearString> key = std::make_pair(transition.first.first, string::LinearString( std::vector<alphabet::Symbol> { transition.first.second} )); + transitions[key] = transition.second; + } + std::pair<State, string::LinearString> key = std::make_pair(this->getInitialState(), string::LinearString(std::vector<alphabet::Symbol> {})); + transitions[key] = other.getInitialStates(); +} + +CompactNFA::CompactNFA(const NFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, string::LinearString> key = std::make_pair(transition.first.first, string::LinearString( std::vector<alphabet::Symbol> { transition.first.second} )); + transitions[key] = transition.second; + } +} + +CompactNFA::CompactNFA(const DFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, string::LinearString> key = std::make_pair(transition.first.first, string::LinearString( std::vector<alphabet::Symbol> { transition.first.second} )); + transitions[key].insert(transition.second); + } +} + AutomatonBase* CompactNFA::clone() const { return new CompactNFA(*this); } diff --git a/alib2data/src/automaton/FSM/CompactNFA.h b/alib2data/src/automaton/FSM/CompactNFA.h index 9a5cf0947cc75265917c259b6191985c05cafb61..97941daacb196266c8a23b27b8ad146bb92fc0e8 100644 --- a/alib2data/src/automaton/FSM/CompactNFA.h +++ b/alib2data/src/automaton/FSM/CompactNFA.h @@ -25,6 +25,10 @@ protected: std::map<std::pair<State, string::LinearString>, std::set<State> > transitions; public: explicit CompactNFA(const State& initialState); + explicit CompactNFA(const EpsilonNFA& other); + explicit CompactNFA(const MultiInitialStateNFA& other); + explicit CompactNFA(const NFA& other); + explicit CompactNFA(const DFA& other); virtual AutomatonBase* clone() const; diff --git a/alib2data/src/automaton/FSM/EpsilonNFA.cpp b/alib2data/src/automaton/FSM/EpsilonNFA.cpp index c953b7a25f56d554e65969f8e4ee324807cc9732..1eb93f96494ebfe4af40f3d16977253f30e52ef8 100644 --- a/alib2data/src/automaton/FSM/EpsilonNFA.cpp +++ b/alib2data/src/automaton/FSM/EpsilonNFA.cpp @@ -6,8 +6,12 @@ */ #include "EpsilonNFA.h" +#include "MultiInitialStateNFA.h" +#include "NFA.h" +#include "DFA.h" #include "../../std/map.hpp" #include "../AutomatonException.h" +#include "../Automaton.h" #include <ostream> #include <sstream> @@ -17,6 +21,39 @@ EpsilonNFA::EpsilonNFA(const State& initialState) : SingleInitialState(initialSt } +EpsilonNFA::EpsilonNFA(const MultiInitialStateNFA& other) : SingleInitialState(automaton::createUniqueState(automaton::State("q0"), other.getStates())) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(transition.first.first, std::variant<string::Epsilon, alphabet::Symbol>(transition.first.second)); + transitions[key] = transition.second; + } + std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(this->getInitialState(), std::variant<string::Epsilon, alphabet::Symbol>(string::Epsilon::EPSILON)); + transitions[key] = other.getInitialStates(); + +} + +EpsilonNFA::EpsilonNFA(const NFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(transition.first.first, std::variant<string::Epsilon, alphabet::Symbol>(transition.first.second)); + transitions[key] = transition.second; + } +} + +EpsilonNFA::EpsilonNFA(const DFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(transition.first.first, std::variant<string::Epsilon, alphabet::Symbol>(transition.first.second)); + transitions[key].insert(transition.second); + } +} + AutomatonBase* EpsilonNFA::clone() const { return new EpsilonNFA(*this); } diff --git a/alib2data/src/automaton/FSM/EpsilonNFA.h b/alib2data/src/automaton/FSM/EpsilonNFA.h index 6e829caeeec57f55a1fcbd451df234e20671bc53..ad4f323aa83271fd14e3a827e1c43a9d819d08ea 100644 --- a/alib2data/src/automaton/FSM/EpsilonNFA.h +++ b/alib2data/src/automaton/FSM/EpsilonNFA.h @@ -27,6 +27,9 @@ protected: std::map<std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> >, std::set<State> > transitions; public: explicit EpsilonNFA(const State& initialState); + explicit EpsilonNFA(const MultiInitialStateNFA& other); + explicit EpsilonNFA(const NFA& other); + explicit EpsilonNFA(const DFA& other); virtual AutomatonBase* clone() const; diff --git a/alib2data/src/automaton/FSM/ExtendedNFA.cpp b/alib2data/src/automaton/FSM/ExtendedNFA.cpp index 97b9a670c3d5c885722b3d5d19c2e89b7f6cb36e..bbbc0181973bae23577cb8247a60b2c2b482b848 100644 --- a/alib2data/src/automaton/FSM/ExtendedNFA.cpp +++ b/alib2data/src/automaton/FSM/ExtendedNFA.cpp @@ -6,8 +6,15 @@ */ #include "ExtendedNFA.h" +#include "CompactNFA.h" +#include "EpsilonNFA.h" +#include "MultiInitialStateNFA.h" +#include "NFA.h" +#include "DFA.h" #include "../../std/map.hpp" #include "../AutomatonException.h" +#include "../Automaton.h" +#include "../../regexp/RegExp.h" #include <ostream> #include <algorithm> #include "../../regexp/RegExpAlphabetGetter.h" @@ -19,6 +26,63 @@ ExtendedNFA::ExtendedNFA(const State& initialState) : SingleInitialState(initial } +ExtendedNFA::ExtendedNFA(const CompactNFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, regexp::RegExp> key = std::make_pair(transition.first.first, regexp::RegExp( regexp::regexpFrom ( transition.first.second ))); + transitions[key] = transition.second; + } +} + +ExtendedNFA::ExtendedNFA(const EpsilonNFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + if(transition.first.second.is<string::Epsilon>()) { + std::pair<State, regexp::RegExp> key = std::make_pair(transition.first.first, regexp::RegExp( regexp::regexpFrom ( string::LinearString( std::vector<alphabet::Symbol> { } ) ))); + transitions[key] = transition.second; + } else { + std::pair<State, regexp::RegExp> key = std::make_pair(transition.first.first, regexp::RegExp( regexp::regexpFrom ( transition.first.second.get<alphabet::Symbol>() ))); + transitions[key] = transition.second; + } + } +} + +ExtendedNFA::ExtendedNFA(const MultiInitialStateNFA& other) : SingleInitialState(automaton::createUniqueState(automaton::State("q0"), other.getStates())) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, regexp::RegExp> key = std::make_pair(transition.first.first, regexp::RegExp( regexp::regexpFrom ( transition.first.second ))); + transitions[key] = transition.second; + } + std::pair<State, regexp::RegExp> key = std::make_pair(this->getInitialState(), regexp::RegExp( regexp::regexpFrom ( string::LinearString(std::vector<alphabet::Symbol> {}) ))); + transitions[key] = other.getInitialStates(); +} + +ExtendedNFA::ExtendedNFA(const NFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, regexp::RegExp> key = std::make_pair(transition.first.first, regexp::RegExp( regexp::regexpFrom ( transition.first.second ))); + transitions[key] = transition.second; + } +} + +ExtendedNFA::ExtendedNFA(const DFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + std::pair<State, regexp::RegExp> key = std::make_pair(transition.first.first, regexp::RegExp( regexp::regexpFrom ( transition.first.second ))); + transitions[key].insert(transition.second); + } +} + AutomatonBase* ExtendedNFA::clone() const { return new ExtendedNFA(*this); } diff --git a/alib2data/src/automaton/FSM/ExtendedNFA.h b/alib2data/src/automaton/FSM/ExtendedNFA.h index 0fdb6a5ceb3f3c7b1d5bbafdd08af7bbcbd5d44e..54db04aca6e5bec7d587b73bb44ac17dfbd873ff 100644 --- a/alib2data/src/automaton/FSM/ExtendedNFA.h +++ b/alib2data/src/automaton/FSM/ExtendedNFA.h @@ -25,6 +25,11 @@ protected: std::map<std::pair<State, regexp::RegExp>, std::set<State> > transitions; public: explicit ExtendedNFA(const State& initialState); + explicit ExtendedNFA(const CompactNFA& other); + explicit ExtendedNFA(const EpsilonNFA& other); + explicit ExtendedNFA(const MultiInitialStateNFA& other); + explicit ExtendedNFA(const NFA& other); + explicit ExtendedNFA(const DFA& other); virtual AutomatonBase* clone() const; diff --git a/alib2data/src/automaton/FSM/MultiInitialStateNFA.cpp b/alib2data/src/automaton/FSM/MultiInitialStateNFA.cpp index f134720688e6b877ef7a9097cb870817d4b0ea49..40cf2cd0fa71e99f78c7bb021ee40b6bb356e3e7 100644 --- a/alib2data/src/automaton/FSM/MultiInitialStateNFA.cpp +++ b/alib2data/src/automaton/FSM/MultiInitialStateNFA.cpp @@ -6,12 +6,38 @@ */ #include "MultiInitialStateNFA.h" +#include "NFA.h" +#include "DFA.h" #include "../AutomatonException.h" #include <ostream> #include <sstream> namespace automaton { +MultiInitialStateNFA::MultiInitialStateNFA() { + +} + +MultiInitialStateNFA::MultiInitialStateNFA(const DFA& other) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + initialStates = {other.getInitialState()}; + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + transitions[transition.first].insert(transition.second); + } +} + +MultiInitialStateNFA::MultiInitialStateNFA(const NFA& other) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + initialStates = {other.getInitialState()}; + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + transitions[transition.first] = transition.second; + } +} + AutomatonBase* MultiInitialStateNFA::clone() const { return new MultiInitialStateNFA(*this); } diff --git a/alib2data/src/automaton/FSM/MultiInitialStateNFA.h b/alib2data/src/automaton/FSM/MultiInitialStateNFA.h index 691134e084b10715ffd84ee00ace2882dbee0ae3..830f166f3645ebbecc253bd0a6662dccd00bb43b 100644 --- a/alib2data/src/automaton/FSM/MultiInitialStateNFA.h +++ b/alib2data/src/automaton/FSM/MultiInitialStateNFA.h @@ -25,6 +25,10 @@ class MultiInitialStateNFA : public std::acceptor<MultiInitialStateNFA, Visitabl protected: std::map<std::pair<State, alphabet::Symbol>, std::set<State> > transitions; public: + explicit MultiInitialStateNFA(); + explicit MultiInitialStateNFA(const NFA& other); + explicit MultiInitialStateNFA(const DFA& other); + virtual AutomatonBase* clone() const; virtual AutomatonBase* plunder() &&; diff --git a/alib2data/src/automaton/FSM/NFA.cpp b/alib2data/src/automaton/FSM/NFA.cpp index 97e68dfa94a715058a23b378a7408880618a865a..f5ca944190775e3fd11b485b8c9b3c57782567c0 100644 --- a/alib2data/src/automaton/FSM/NFA.cpp +++ b/alib2data/src/automaton/FSM/NFA.cpp @@ -6,6 +6,7 @@ */ #include "NFA.h" +#include "DFA.h" #include "../AutomatonException.h" #include <ostream> #include <sstream> @@ -16,6 +17,15 @@ NFA::NFA(const State& initialState) : SingleInitialState(initialState) { } +NFA::NFA(const DFA& other) : SingleInitialState(other.getInitialState()) { + inputAlphabet = other.getInputAlphabet(); + states = other.getStates(); + finalStates = other.getFinalStates(); + for(const auto& transition : other.getTransitions()) { + transitions[transition.first].insert(transition.second); + } +} + AutomatonBase* NFA::clone() const { return new NFA(*this); } diff --git a/alib2data/src/automaton/FSM/NFA.h b/alib2data/src/automaton/FSM/NFA.h index 3dd0a8738984b980a8f8ec8e53f797ecfaa5f6e4..24af23d31d5ef6e50b9c9a41c5edef67c169edc6 100644 --- a/alib2data/src/automaton/FSM/NFA.h +++ b/alib2data/src/automaton/FSM/NFA.h @@ -26,6 +26,7 @@ protected: std::map<std::pair<State, alphabet::Symbol>, std::set<State> > transitions; public: explicit NFA(const State& initialState); + explicit NFA(const DFA& other); virtual AutomatonBase* clone() const; diff --git a/alib2data/src/regexp/RegExp.cpp b/alib2data/src/regexp/RegExp.cpp new file mode 100644 index 0000000000000000000000000000000000000000..fde75f2fa71d6fa74b43bea57acdb3689f82ff0e --- /dev/null +++ b/alib2data/src/regexp/RegExp.cpp @@ -0,0 +1,41 @@ +/* + * RegExp.cpp + * + * Created on: Apr 16, 2013 + * Author: Jan Travnicek + */ + +#include "RegExp.h" +#include "../alphabet/Symbol.h" +#include "unbounded/UnboundedRegExpSymbol.h" +#include "unbounded/UnboundedRegExpEmpty.h" +#include "unbounded/UnboundedRegExpConcatenation.h" + +namespace regexp { + +regexp::RegExp regexpFrom( const std::string& string ) { + regexp::UnboundedRegExpConcatenation con; + for(const auto& symbol : string) { + con.appendElement( regexp::UnboundedRegExpSymbol( alphabet::symbolFrom(symbol))); + } + return regexp::RegExp { regexp::UnboundedRegExp ( con ) }; +} + +regexp::RegExp regexpFrom( const string::LinearString& string ) { + regexp::UnboundedRegExpConcatenation con; + for(const auto& symbol : string.getContent()) { + con.appendElement( regexp::UnboundedRegExpSymbol(symbol)); + } + return regexp::RegExp { regexp::UnboundedRegExp ( con ) }; +} + +regexp::RegExp regexpFrom( const alphabet::Symbol& symbol ) { + return regexp::RegExp { regexp::UnboundedRegExp( regexp::UnboundedRegExpSymbol(symbol) ) }; +} + +regexp::RegExp regexpFrom( ) { + return regexp::RegExp { regexp::UnboundedRegExp( regexp::UnboundedRegExpEmpty { } ) }; +} + +} /* namespace regexp */ + diff --git a/alib2data/src/regexp/RegExp.h b/alib2data/src/regexp/RegExp.h index b42a3f670f35d49349fe4408ba06b57ae73d57a5..0446ca02ea7411eb072130201d7f0c39383fc064 100644 --- a/alib2data/src/regexp/RegExp.h +++ b/alib2data/src/regexp/RegExp.h @@ -11,6 +11,9 @@ #include "../std/visitor.hpp" #include "../common/wrapper.hpp" #include "RegExpBase.h" +#include "../alphabet/Symbol.h" +#include "../string/LinearString.h" +#include <string> namespace regexp { @@ -19,6 +22,14 @@ namespace regexp { */ typedef alib::wrapper<RegExpBase> RegExp; +regexp::RegExp regexpFrom( const std::string& string ); + +regexp::RegExp regexpFrom( const string::LinearString& string ); + +regexp::RegExp regexpFrom( const alphabet::Symbol& symbol ); + +regexp::RegExp regexpFrom( ); + } /* namespace regexp */ #endif /* REG_EXP_H_ */ diff --git a/alib2data/src/string/String.cpp b/alib2data/src/string/String.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8fc68125b5d1d3e50ca0a499cc2054c38dea4724 --- /dev/null +++ b/alib2data/src/string/String.cpp @@ -0,0 +1,23 @@ +/* + * String.cpp + * + * Created on: Apr 16, 2013 + * Author: Jan Travnicek + */ + +#include "String.h" +#include "LinearString.h" +#include "../exception/AlibException.h" + +namespace string { + +string::String symbolFrom( const alphabet::Symbol& symbol ) { + return string::String { string::LinearString { std::vector<alphabet::Symbol> { symbol } } }; +} + +string::String symbolFrom( const std::string& string ) { + return string::String { string::LinearString { string } }; +} + +} /* namespace string */ + diff --git a/alib2data/src/string/String.h b/alib2data/src/string/String.h index 9da127df944b3446cdd5e272a4cbeec1dc10b862..617cf4d04df3429452a84c7ee945af9fda9f6c6d 100644 --- a/alib2data/src/string/String.h +++ b/alib2data/src/string/String.h @@ -11,6 +11,7 @@ #include "../std/visitor.hpp" #include "../common/wrapper.hpp" #include "StringBase.h" +#include "../alphabet/Symbol.h" namespace string { @@ -19,6 +20,9 @@ namespace string { */ typedef alib::wrapper<StringBase> String; +string::String stringFrom(const alphabet::Symbol& symbol); +string::String stringFrom(const std::string& str); + } /* namespace string */ #endif /* STRING_H_ */