diff --git a/alib2algo/src/determinize/common/NFACommon.cpp b/alib2algo/src/determinize/common/NFACommon.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a99e583251737d89f36aae9e055611135f8c11b --- /dev/null +++ b/alib2algo/src/determinize/common/NFACommon.cpp @@ -0,0 +1,32 @@ +/* + * NFACommon.cpp + * + * Created on: 16. 1. 2014 + * Author: Jan Vesely + */ + +#include "NFACommon.h" +#include "label/LabelSetLabel.h" + +#include <deque> +#include <algorithm> + +namespace determinize { + +automaton::State createDFAState(const std::set<automaton::State>& nfaStates) { + std::set<label::Label> labelSet; + for(const automaton::State& state : nfaStates) { + labelSet.insert(state.getName()); + } + return automaton::State(label::Label(label::LabelSetLabel(labelSet))); +} + +std::set<automaton::State> recreateNFAStates(const automaton::State& dfaState) { + std::set<automaton::State> states; + for (const auto& label : static_cast<const label::LabelSetLabel&>(dfaState.getName().getData()).getData()) { + states.insert(automaton::State(label)); + } + return states; +} + +} diff --git a/alib2algo/src/determinize/common/NFACommon.h b/alib2algo/src/determinize/common/NFACommon.h new file mode 100644 index 0000000000000000000000000000000000000000..d68e98f97911e6900768468fe6a5d82445f786fd --- /dev/null +++ b/alib2algo/src/determinize/common/NFACommon.h @@ -0,0 +1,37 @@ +/* + * NFACommon.h + * + * Created on: 16. 1. 2014 + * Author: Jan Vesely + */ + +#ifndef NFA_COMMON_H_ +#define NFA_COMMON_H_ + +#include <automaton/common/State.h> +#include <set> + +namespace determinize { + +/** + * Returns existing state from the resulting automaton, if there is one, or creates new one and adds it into + * the resulting deterministic automaton. + * + * @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm + * @return state of deterministic fsm + */ +automaton::State createDFAState(const std::set<automaton::State>& nfaStates); + +/** + * Finds states from nondeterministic fsm to which at least one state from given set of states have transition + * with given input. + * + * @param fromStates set of states from nondeterministic fsm + * @param input symbol from input alphabet + * @return set of states from nondeterministic fsm + */ +std::set<automaton::State> recreateNFAStates(const automaton::State& dfaState); + +} /* namespace determinize */ + +#endif /* NFA_COMMON_H_ */ diff --git a/alib2algo/src/determinize/idpda/IDPDADeterminizer.cpp b/alib2algo/src/determinize/idpda/IDPDADeterminizer.cpp index cda3ab584173070f5207554484888a0da20654fe..36cd28c3029c6efc3d8550ac853b3f62a01885f6 100644 --- a/alib2algo/src/determinize/idpda/IDPDADeterminizer.cpp +++ b/alib2algo/src/determinize/idpda/IDPDADeterminizer.cpp @@ -6,7 +6,7 @@ */ #include "IDPDADeterminizer.h" -#include "label/LabelSetLabel.h" +#include "../common/NFACommon.h" #include <deque> #include <algorithm> @@ -14,22 +14,6 @@ namespace determinize { -automaton::State IDPDADeterminizer::createDFAState(const std::set<automaton::State>& nfaStates) { - std::set<label::Label> labelSet; - for(const automaton::State& state : nfaStates) { - labelSet.insert(state.getName()); - } - return automaton::State(label::Label(label::LabelSetLabel(labelSet))); -} - -std::set<automaton::State> IDPDADeterminizer::recreateIDPDAStates(const automaton::State& dfaState) { - std::set<automaton::State> states; - for (const auto& label : static_cast<const label::LabelSetLabel&>(dfaState.getName().getData()).getData()) { - states.insert(automaton::State(label)); - } - return states; -} - automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA& nfa) { // 1, 4 automaton::State initialState(createDFAState(nfa.getInitialStates())); @@ -49,7 +33,7 @@ automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA& // 3b for (const auto& input : nfa.getInputAlphabet()) { std::set<automaton::State> targetIDPDAStates; - for(const auto& nfaState : recreateIDPDAStates(state)) { + for(const auto& nfaState : recreateNFAStates(state)) { auto iter = nfa.getTransitions().find(std::make_pair(nfaState, input)); if(iter != nfa.getTransitions().end()) { targetIDPDAStates.insert(iter->second.begin(), iter->second.end()); @@ -70,7 +54,7 @@ automaton::DPDA IDPDADeterminizer::determinize(const automaton::InputDrivenNPDA& // 5 for (const auto& dfaState : res.getStates()) { - std::set<automaton::State> nfaStates = recreateIDPDAStates(dfaState); + std::set<automaton::State> nfaStates = recreateNFAStates(dfaState); if(std::any_of(nfaStates.begin(), nfaStates.end(), [&](const automaton::State& nfaState) { return nfa.getFinalStates().count(nfaState); })) { res.addFinalState(dfaState); } diff --git a/alib2algo/src/determinize/idpda/IDPDADeterminizer.h b/alib2algo/src/determinize/idpda/IDPDADeterminizer.h index 7d8d89cade0824be9681e1931ee85612dd6d2dca..4009c23dd29245ccac294b86ce5a6f916f012789 100644 --- a/alib2algo/src/determinize/idpda/IDPDADeterminizer.h +++ b/alib2algo/src/determinize/idpda/IDPDADeterminizer.h @@ -20,27 +20,6 @@ namespace determinize { * Class for running determinization algorithm on fsm. */ class IDPDADeterminizer { - -private: - - /** - * Returns existing state from the resulting automaton, if there is one, or creates new one and adds it into - * the resulting deterministic automaton. - * - * @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm - * @return state of deterministic fsm - */ - static automaton::State createDFAState(const std::set<automaton::State>& nfaStates); - - /** - * Finds states from nondeterministic fsm to which at least one state from given set of states have transition - * with given input. - * - * @param fromStates set of states from nondeterministic fsm - * @param input symbol from input alphabet - * @return set of states from nondeterministic fsm - */ - static std::set<automaton::State> recreateIDPDAStates(const automaton::State& dfaState); public: /** diff --git a/alib2algo/src/determinize/nfa/NFADeterminizer.cpp b/alib2algo/src/determinize/nfa/NFADeterminizer.cpp index d46299f563498d0164df637a2767794f01ec7a86..c19dab33d1c8ee318bef5de325afbb0efd0a30ef 100644 --- a/alib2algo/src/determinize/nfa/NFADeterminizer.cpp +++ b/alib2algo/src/determinize/nfa/NFADeterminizer.cpp @@ -6,7 +6,7 @@ */ #include "NFADeterminizer.h" -#include "label/LabelSetLabel.h" +#include "../common/NFACommon.h" #include <deque> #include <algorithm> @@ -21,22 +21,6 @@ automaton::Automaton NFADeterminizer::determinize(const automaton::Automaton& au return res; } -automaton::State NFADeterminizer::createDFAState(const std::set<automaton::State>& nfaStates) { - std::set<label::Label> labelSet; - for(const automaton::State& state : nfaStates) { - labelSet.insert(state.getName()); - } - return automaton::State(label::Label(label::LabelSetLabel(labelSet))); -} - -std::set<automaton::State> NFADeterminizer::recreateNFAStates(const automaton::State& dfaState) { - std::set<automaton::State> states; - for (const auto& label : static_cast<const label::LabelSetLabel&>(dfaState.getName().getData()).getData()) { - states.insert(automaton::State(label)); - } - return states; -} - automaton::DFA NFADeterminizer::determinize(const automaton::NFA& nfa) { // 1, 4 automaton::State initialState(createDFAState(nfa.getInitialStates())); diff --git a/alib2algo/src/determinize/nfa/NFADeterminizer.h b/alib2algo/src/determinize/nfa/NFADeterminizer.h index 45d5def47c30ba67083bbb4597c61d451387df34..196f90fb4d4fb97911920044ed88e2463d3f4f79 100644 --- a/alib2algo/src/determinize/nfa/NFADeterminizer.h +++ b/alib2algo/src/determinize/nfa/NFADeterminizer.h @@ -23,28 +23,7 @@ namespace determinize { * Class for running determinization algorithm on fsm. */ class NFADeterminizer : public automaton::VisitableAutomatonBase::const_visitor_type { - private: - - /** - * Returns existing state from the resulting automaton, if there is one, or creates new one and adds it into - * the resulting deterministic automaton. - * - * @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm - * @return state of deterministic fsm - */ - static automaton::State createDFAState(const std::set<automaton::State>& nfaStates); - - /** - * Finds states from nondeterministic fsm to which at least one state from given set of states have transition - * with given input. - * - * @param fromStates set of states from nondeterministic fsm - * @param input symbol from input alphabet - * @return set of states from nondeterministic fsm - */ - static std::set<automaton::State> recreateNFAStates(const automaton::State& dfaState); - void Visit(void*, const automaton::UnknownAutomaton& automaton) const; void Visit(void*, const automaton::EpsilonNFA& automaton) const; void Visit(void*, const automaton::NFA& automaton) const;