From 75e7ada6f267db66314f9ca50741bd650a627ea2 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 4 Apr 2017 08:34:57 +0200 Subject: [PATCH] normalize finite automata --- alib2data/src/automaton/FSM/CompactNFA.h | 20 +++++++++++++++++++ alib2data/src/automaton/FSM/DFA.h | 20 +++++++++++++++++++ alib2data/src/automaton/FSM/EpsilonNFA.h | 20 +++++++++++++++++++ alib2data/src/automaton/FSM/ExtendedNFA.h | 20 +++++++++++++++++++ .../src/automaton/FSM/MultiInitialStateNFA.h | 20 +++++++++++++++++++ alib2data/src/automaton/FSM/NFA.h | 20 +++++++++++++++++++ 6 files changed, 120 insertions(+) diff --git a/alib2data/src/automaton/FSM/CompactNFA.h b/alib2data/src/automaton/FSM/CompactNFA.h index fe83c5c288..a2b01f4a39 100644 --- a/alib2data/src/automaton/FSM/CompactNFA.h +++ b/alib2data/src/automaton/FSM/CompactNFA.h @@ -23,6 +23,7 @@ #include "../AutomatonException.h" #include "../common/AutomatonFromXMLParser.h" #include "../common/AutomatonToXMLComposer.h" +#include "../common/AutomatonNormalize.h" #include "../../label/InitialStateLabel.h" @@ -180,6 +181,25 @@ public: void composeTransitions ( std::deque < sax::Token > & out ) const; virtual alib::ObjectBase * inc ( ) &&; + + virtual AutomatonBase * normalize ( ) && { + std::set < DefaultSymbolType > alphabet = AutomatonNormalize::normalizeAlphabet ( std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) ) ); + std::set < DefaultStateType > states = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < States > ( ).get ( ) ) ); + DefaultStateType initialState = AutomatonNormalize::normalizeState ( std::move ( this->template accessElement < InitialState > ( ).get ( ) ) ); + std::set < DefaultStateType > finalStates = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ) ); + + CompactNFA < > * res = new CompactNFA < > ( std::move ( states ), std::move ( alphabet ), std::move ( initialState ), std::move ( finalStates ) ); + + for ( std::pair < std::pair < StateType, std::vector < SymbolType > >, std::set < StateType > > && transition : std::make_moveable_map ( transitions ) ) { + DefaultStateType from = AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) ); + std::vector < DefaultSymbolType > input = AutomatonNormalize::normalizeSymbols ( std::move ( transition.first.second ) ); + std::set < DefaultStateType > targets = AutomatonNormalize::normalizeStates ( std::move ( transition.second ) ); + + res->addTransitions ( std::move ( from ), std::move ( input ), std::move ( targets ) ); + } + + return res; + } }; } /* namespace automaton */ diff --git a/alib2data/src/automaton/FSM/DFA.h b/alib2data/src/automaton/FSM/DFA.h index 7a30339f08..98bf198fe7 100644 --- a/alib2data/src/automaton/FSM/DFA.h +++ b/alib2data/src/automaton/FSM/DFA.h @@ -21,6 +21,7 @@ #include "../AutomatonException.h" #include "../common/AutomatonFromXMLParser.h" #include "../common/AutomatonToXMLComposer.h" +#include "../common/AutomatonNormalize.h" namespace automaton { @@ -171,6 +172,25 @@ public: void composeTransitions ( std::deque < sax::Token > & out ) const; virtual alib::ObjectBase * inc ( ) &&; + + virtual AutomatonBase * normalize ( ) && { + std::set < DefaultSymbolType > alphabet = AutomatonNormalize::normalizeAlphabet ( std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) ) ); + std::set < DefaultStateType > states = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < States > ( ).get ( ) ) ); + DefaultStateType initialState = AutomatonNormalize::normalizeState ( std::move ( this->template accessElement < InitialState > ( ).get ( ) ) ); + std::set < DefaultStateType > finalStates = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ) ); + + DFA < > * res = new DFA < > ( std::move ( states ), std::move ( alphabet ), std::move ( initialState ), std::move ( finalStates ) ); + + for ( std::pair < std::pair < StateType, SymbolType >, StateType > && transition : std::make_moveable_map ( transitions ) ) { + DefaultStateType from = AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) ); + DefaultSymbolType input = AutomatonNormalize::normalizeSymbol ( std::move ( transition.first.second ) ); + DefaultStateType to = AutomatonNormalize::normalizeState ( std::move ( transition.second ) ); + + res->addTransition ( std::move ( from ), std::move ( input ), std::move ( to ) ); + } + + return res; + } }; template<class SymbolType, class StateType > diff --git a/alib2data/src/automaton/FSM/EpsilonNFA.h b/alib2data/src/automaton/FSM/EpsilonNFA.h index 109a45fb86..23743fefa9 100644 --- a/alib2data/src/automaton/FSM/EpsilonNFA.h +++ b/alib2data/src/automaton/FSM/EpsilonNFA.h @@ -23,6 +23,7 @@ #include "../AutomatonFeatures.h" #include "../common/AutomatonFromXMLParser.h" #include "../common/AutomatonToXMLComposer.h" +#include "../common/AutomatonNormalize.h" #include "../../label/InitialStateLabel.h" @@ -273,6 +274,25 @@ public: void composeTransitions ( std::deque < sax::Token > & out ) const; virtual alib::ObjectBase * inc ( ) &&; + + virtual AutomatonBase * normalize ( ) && { + std::set < DefaultSymbolType > alphabet = AutomatonNormalize::normalizeAlphabet ( std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) ) ); + std::set < DefaultStateType > states = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < States > ( ).get ( ) ) ); + DefaultStateType initialState = AutomatonNormalize::normalizeState ( std::move ( this->template accessElement < InitialState > ( ).get ( ) ) ); + std::set < DefaultStateType > finalStates = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ) ); + + EpsilonNFA < > * res = new EpsilonNFA < > ( std::move ( states ), std::move ( alphabet ), std::move ( initialState ), std::move ( finalStates ) ); + + for ( std::pair < std::pair < StateType, std::variant < EpsilonType, SymbolType > >, std::set < StateType > > && transition : std::make_moveable_map ( transitions ) ) { + DefaultStateType from = AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) ); + std::variant < DefaultEpsilonType, DefaultSymbolType > input = AutomatonNormalize::normalizeSymbolEpsilon ( std::move ( transition.first.second ) ); + std::set < DefaultStateType > targets = AutomatonNormalize::normalizeStates ( std::move ( transition.second ) ); + + res->addTransitions ( std::move ( from ), std::move ( input ), std::move ( targets ) ); + } + + return res; + } }; } /* namespace automaton */ diff --git a/alib2data/src/automaton/FSM/ExtendedNFA.h b/alib2data/src/automaton/FSM/ExtendedNFA.h index 799469260d..f3611ab727 100644 --- a/alib2data/src/automaton/FSM/ExtendedNFA.h +++ b/alib2data/src/automaton/FSM/ExtendedNFA.h @@ -23,6 +23,7 @@ #include "../AutomatonFeatures.h" #include "../common/AutomatonFromXMLParser.h" #include "../common/AutomatonToXMLComposer.h" +#include "../common/AutomatonNormalize.h" #include "../../label/InitialStateLabel.h" #include "../../regexp/unbounded/UnboundedRegExpStructure.h" @@ -184,6 +185,25 @@ public: void composeTransitions ( std::deque < sax::Token > & out ) const; virtual alib::ObjectBase * inc ( ) &&; + + virtual AutomatonBase * normalize ( ) && { + std::set < DefaultSymbolType > alphabet = AutomatonNormalize::normalizeAlphabet ( std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) ) ); + std::set < DefaultStateType > states = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < States > ( ).get ( ) ) ); + DefaultStateType initialState = AutomatonNormalize::normalizeState ( std::move ( this->template accessElement < InitialState > ( ).get ( ) ) ); + std::set < DefaultStateType > finalStates = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ) ); + + ExtendedNFA < > * res = new ExtendedNFA < > ( std::move ( states ), std::move ( alphabet ), std::move ( initialState ), std::move ( finalStates ) ); + + for ( std::pair < std::pair < StateType, regexp::UnboundedRegExpStructure < SymbolType > >, std::set < StateType > > && transition : std::make_moveable_map ( transitions ) ) { + DefaultStateType from = AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) ); + regexp::UnboundedRegExpStructure < DefaultSymbolType > input = AutomatonNormalize::normalizeRegExp ( std::move ( transition.first.second ) ); + std::set < DefaultStateType > targets = AutomatonNormalize::normalizeStates ( std::move ( transition.second ) ); + + res->addTransitions ( std::move ( from ), std::move ( input ), std::move ( targets ) ); + } + + return res; + } }; } /* namespace automaton */ diff --git a/alib2data/src/automaton/FSM/MultiInitialStateNFA.h b/alib2data/src/automaton/FSM/MultiInitialStateNFA.h index 6c9881ffc8..660a157403 100644 --- a/alib2data/src/automaton/FSM/MultiInitialStateNFA.h +++ b/alib2data/src/automaton/FSM/MultiInitialStateNFA.h @@ -21,6 +21,7 @@ #include "../AutomatonFeatures.h" #include "../common/AutomatonFromXMLParser.h" #include "../common/AutomatonToXMLComposer.h" +#include "../common/AutomatonNormalize.h" namespace automaton { @@ -198,6 +199,25 @@ public: void composeTransitions ( std::deque < sax::Token > & out ) const; virtual alib::ObjectBase * inc ( ) &&; + + virtual AutomatonBase * normalize ( ) && { + std::set < DefaultSymbolType > alphabet = AutomatonNormalize::normalizeAlphabet ( std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) ) ); + std::set < DefaultStateType > states = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < States > ( ).get ( ) ) ); + std::set < DefaultStateType > initialStates = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < InitialStates > ( ).get ( ) ) ); + std::set < DefaultStateType > finalStates = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ) ); + + MultiInitialStateNFA < > * res = new MultiInitialStateNFA < > ( std::move ( states ), std::move ( alphabet ), std::move ( initialStates ), std::move ( finalStates ) ); + + for ( std::pair < std::pair < StateType, SymbolType >, std::set < StateType > > && transition : std::make_moveable_map ( transitions ) ) { + DefaultStateType from = AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) ); + DefaultSymbolType input = AutomatonNormalize::normalizeSymbol ( std::move ( transition.first.second ) ); + std::set < DefaultStateType > targets = AutomatonNormalize::normalizeStates ( std::move ( transition.second ) ); + + res->addTransitions ( std::move ( from ), std::move ( input ), std::move ( targets ) ); + } + + return res; + } }; } /* namespace automaton */ diff --git a/alib2data/src/automaton/FSM/NFA.h b/alib2data/src/automaton/FSM/NFA.h index c2148aa88d..82a2c6050e 100644 --- a/alib2data/src/automaton/FSM/NFA.h +++ b/alib2data/src/automaton/FSM/NFA.h @@ -19,6 +19,7 @@ #include "../AutomatonFeatures.h" #include "../common/AutomatonFromXMLParser.h" #include "../common/AutomatonToXMLComposer.h" +#include "../common/AutomatonNormalize.h" namespace automaton { @@ -190,6 +191,25 @@ public: void composeTransitions ( std::deque < sax::Token > & out ) const; virtual alib::ObjectBase * inc ( ) &&; + + virtual AutomatonBase * normalize ( ) && { + std::set < DefaultSymbolType > alphabet = AutomatonNormalize::normalizeAlphabet ( std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) ) ); + std::set < DefaultStateType > states = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < States > ( ).get ( ) ) ); + DefaultStateType initialState = AutomatonNormalize::normalizeState ( std::move ( this->template accessElement < InitialState > ( ).get ( ) ) ); + std::set < DefaultStateType > finalStates = AutomatonNormalize::normalizeStates ( std::move ( this->template accessComponent < FinalStates > ( ).get ( ) ) ); + + NFA < > * res = new NFA < > ( std::move ( states ), std::move ( alphabet ), std::move ( initialState ), std::move ( finalStates ) ); + + for ( std::pair < std::pair < StateType, SymbolType >, std::set < StateType > > && transition : std::make_moveable_map ( transitions ) ) { + DefaultStateType from = AutomatonNormalize::normalizeState ( std::move ( transition.first.first ) ); + DefaultSymbolType input = AutomatonNormalize::normalizeSymbol ( std::move ( transition.first.second ) ); + std::set < DefaultStateType > targets = AutomatonNormalize::normalizeStates ( std::move ( transition.second ) ); + + res->addTransitions ( std::move ( from ), std::move ( input ), std::move ( targets ) ); + } + + return res; + } }; } /* namespace automaton */ -- GitLab