From d66ae44235e4a4ef8b8ecb3fe19fa7ba858dc581 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 4 Apr 2017 16:15:08 +0200 Subject: [PATCH] addTransitions in tree automata --- alib2data/src/automaton/TA/NFTA.h | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/alib2data/src/automaton/TA/NFTA.h b/alib2data/src/automaton/TA/NFTA.h index f7996657cd..df2d4351dd 100644 --- a/alib2data/src/automaton/TA/NFTA.h +++ b/alib2data/src/automaton/TA/NFTA.h @@ -107,6 +107,15 @@ public: */ bool addTransition ( std::ranked_symbol < SymbolType, RankType > current, std::vector < StateType > children, StateType next ); + /** + * Adds transition defined by parameters to the automaton. + * @param current current symbol in node + * @param children states of children of current node + * @param next next state + * @throws AutomatonException when transition already exists or when transition contains state or symbol not present in the automaton + */ + void addTransitions ( std::ranked_symbol < SymbolType, RankType > current, std::vector < StateType > children, std::set < StateType > next ); + /** * Removes transition from the automaton. * @throws AutomatonException when transition doesn't exists. @@ -206,7 +215,27 @@ bool NFTA < SymbolType, RankType, StateType >::addTransition ( std::ranked_symbo } std::pair<std::ranked_symbol < SymbolType, RankType >, std::vector < StateType > > key = std::make_pair ( std::move ( symbol ), std::move ( prevStates ) ); - return transitions [ key ].insert ( std::move ( next ) ).second; + return transitions [ std::move ( key ) ].insert ( std::move ( next ) ).second; +} + +template < class SymbolType, class RankType, class StateType > +void NFTA < SymbolType, RankType, StateType >::addTransitions ( std::ranked_symbol < SymbolType, RankType > symbol, std::vector<StateType> prevStates, std::set < StateType > next) { + if (prevStates.size() != ( size_t ) symbol.getRank() ) + throw AutomatonException("Number of states doesn't match rank of the symbol"); + + if (! getInputAlphabet().count(symbol)) + throw AutomatonException("Input symbol \"" + std::to_string ( symbol ) + "\" doesn't exist."); + + if ( !std::includes ( getStates ( ).begin ( ), getStates ( ).end ( ), next.begin ( ), next.end ( ) ) ) + throw AutomatonException ( "Some target states don't exist." ); + + for (const StateType& it : prevStates) { + if (! getStates().count(it)) + throw AutomatonException("State \"" + std::to_string ( it ) + "\" doesn't exist."); + } + + std::pair<std::ranked_symbol < SymbolType, RankType >, std::vector < StateType > > key = std::make_pair ( std::move ( symbol ), std::move ( prevStates ) ); + transitions [ std::move ( key ) ].insert ( std::make_moveable_set ( next ).begin ( ), std::make_moveable_set ( next ).end ( ) ); } template < class SymbolType, class RankType, class StateType > -- GitLab