diff --git a/alib2/src/automaton/FSM/EpsilonNFA.cpp b/alib2/src/automaton/FSM/EpsilonNFA.cpp index 22a3a936d6810e719c5e71b5a8e816265ab664f1..9d6aba7e2e79fe827abcffb3a646f35e3ba35691 100644 --- a/alib2/src/automaton/FSM/EpsilonNFA.cpp +++ b/alib2/src/automaton/FSM/EpsilonNFA.cpp @@ -51,43 +51,35 @@ void EpsilonNFA::removeInputSymbol(const alphabet::Symbol& symbol) { } -void EpsilonNFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) { +void EpsilonNFA::addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const State& to) { if (states.find(from) == states.end()) throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); - if (inputAlphabet.find(input) == inputAlphabet.end()) - throw AutomatonException("Input symbol \"" + input.getSymbol() + "\" doesn't exist."); + if (input.is<alphabet::Symbol>() && inputAlphabet.find(input.get<alphabet::Symbol>()) == inputAlphabet.end()) + throw AutomatonException("Input symbol \"" + input.get<alphabet::Symbol>().getSymbol() + "\" doesn't exist."); if (states.find(to) == states.end()) throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); - std::variant<string::Epsilon, alphabet::Symbol> inputVariant; - inputVariant.set<alphabet::Symbol>(input); - std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(from, inputVariant); + std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(from, input); std::pair<std::set<automaton::State>::iterator, bool> ret = transitions[key].insert(to); if (!ret.second) throw AutomatonException( - "Transition (\"" + from.getName() + "\", \"" + input.getSymbol() + "\") -> \"" + to.getName() + "Transition (\"" + from.getName() + "\", \"" + input.get<alphabet::Symbol>().getSymbol() + "\") -> \"" + to.getName() + "\" already exists."); } -void EpsilonNFA::addTransition(const State& from, const State& to) { - if (states.find(from) == states.end()) - throw AutomatonException("State \"" + from.getName() + "\" doesn't exist."); - - if (states.find(to) == states.end()) - throw AutomatonException("State \"" + to.getName() + "\" doesn't exist."); +void EpsilonNFA::addTransition(const State& from, const alphabet::Symbol& input, const State& to) { + std::variant<string::Epsilon, alphabet::Symbol> inputVariant; + inputVariant.set<alphabet::Symbol>(input); + addTransition(from, inputVariant, to); +} +void EpsilonNFA::addTransition(const State& from, const State& to) { std::variant<string::Epsilon, alphabet::Symbol> inputVariant; inputVariant.set<string::Epsilon>(string::Epsilon()); - std::pair<State, std::variant<string::Epsilon, alphabet::Symbol> > key = std::make_pair(from, inputVariant); - - std::pair<std::set<automaton::State>::iterator, bool> ret = transitions[key].insert(to); - if (!ret.second) - throw AutomatonException( - "Transition (\"" + from.getName() + "\", \" \\eps \") -> \"" + to.getName() - + "\" already exists."); + addTransition(from, inputVariant, to); } void EpsilonNFA::removeTransition(const State& from, const alphabet::Symbol& input, const State& to) { diff --git a/alib2/src/automaton/FSM/EpsilonNFA.h b/alib2/src/automaton/FSM/EpsilonNFA.h index 364adfc85dcf300bdc335a50652cc9b8e73227b7..97161275a364455bbb2282a5fe622a45689196fd 100644 --- a/alib2/src/automaton/FSM/EpsilonNFA.h +++ b/alib2/src/automaton/FSM/EpsilonNFA.h @@ -47,7 +47,16 @@ public: * @param next next state * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton */ - void addTransition(const State& current, const alphabet::Symbol& input, const State& next); + void addTransition(const State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const State& to); + + /** + * Adds transition defined by parameters to the automaton. + * @param current current state + * @param input input symbol + * @param next next state + * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + */ + void addTransition(const State& from, const alphabet::Symbol& input, const State& to); /** * Adds transition defined by parameters to the automaton. @@ -55,7 +64,7 @@ public: * @param next next state * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton */ - void addTransition(const State& current, const State& next); + void addTransition(const State& from, const State& to); /** * Removes transition from the automaton.