28#include <alib/multimap>
31#include <ext/algorithm>
79template <
class SymbolTypeT = DefaultSymbolType,
class StateTypeT = DefaultStateType >
80class ExtendedNFA final :
public core::Components < ExtendedNFA < SymbolTypeT, StateTypeT >, ext::set < SymbolTypeT >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates >, StateTypeT, component::Value, InitialState > {
157 return this->
template accessComponent < InitialState > ( ).get ( );
166 return std::move ( this->
template accessComponent < InitialState > ( ).
get ( ) );
177 return this->
template accessComponent < InitialState > ( ).set ( std::move ( state ) );
186 return this->
template accessComponent < States > ( ).get ( );
195 return std::move ( this->
template accessComponent < States > ( ).
get ( ) );
206 return this->
template accessComponent < States > ( ).add ( std::move ( state ) );
215 this->
template accessComponent < States > ( ).set ( std::move ( states ) );
226 this->
template accessComponent < States > ( ).remove ( state );
235 return this->
template accessComponent < FinalStates > ( ).get ( );
244 return std::move ( this->
template accessComponent < FinalStates > ( ).
get ( ) );
255 return this->
template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
264 this->
template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
275 this->
template accessComponent < FinalStates > ( ).remove ( state );
284 return this->
template accessComponent < InputAlphabet > ( ).get ( );
293 return std::move ( this->
template accessComponent < InputAlphabet > ( ).
get ( ) );
304 return this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
313 this->
template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
322 this->
template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
333 this->
template accessComponent < InputAlphabet > ( ).remove ( symbol );
427 return out <<
"(ExtendedNFA"
428 <<
" states = " << instance.
getStates ( )
453template <
class SymbolType,
class StateType >
464template<
class SymbolType,
class StateType >
465ExtendedNFA < SymbolType, StateType >::ExtendedNFA (
ext::set < StateType > states,
ext::set < SymbolType > inputAlphabet,
StateType initialState,
ext::set < StateType > finalStates ) :
core::Components <
ExtendedNFA,
ext::set < SymbolType >, component::Set, InputAlphabet,
ext::set < StateType >, component::Set, std::tuple < States, FinalStates >,
StateType, component::Value, InitialState > ( std::move ( inputAlphabet ), std::move ( states ), std::move ( finalStates ), std::move ( initialState ) ) {
468template<
class SymbolType,
class StateType >
472template<
class SymbolType,
class StateType >
474 for (
const auto & transition : other.getTransitions ( ) ) {
477 for (
auto & symbol : transition.first.second )
481 transitions.insert ( key, transition.second );
485template <
class SymbolType,
class StateType >
487 for (
const auto & transition : other.getTransitions ( ) ) {
488 if ( transition.first.second.is_epsilon ( ) ) {
490 transitions.insert ( key, transition.second );
493 transitions.insert ( key, transition.second );
498 for (
const StateType & to : other.getInitialStates ( ) )
499 transitions.insert ( key, to );
502template <
class SymbolType,
class StateType >
504 for (
const auto & transition : other.getTransitions ( ) ) {
505 if ( transition.first.second.is_epsilon ( ) ) {
507 transitions.insert ( key, transition.second );
510 transitions.insert ( key, transition.second );
515template<
class SymbolType,
class StateType >
517 for (
const auto & transition : other.getTransitions ( ) ) {
519 transitions.insert ( key, transition.second );
523 for (
const StateType & state : other.getInitialStates ( ) )
524 transitions.insert ( key, state );
527template<
class SymbolType,
class StateType >
529 for (
const auto & transition : other.getTransitions ( ) ) {
531 transitions.insert ( key, transition.second );
535template<
class SymbolType,
class StateType >
537 for (
const auto & transition : other.getTransitions ( ) ) {
539 transitions.insert ( key, transition.second );
543template<
class SymbolType,
class StateType >
545 if ( ! getStates ( ).contains ( from ) )
551 if ( ! std::includes ( getInputAlphabet ( ).
begin ( ), getInputAlphabet ( ).
end ( ), inputRegExpAlphabet.
begin ( ), inputRegExpAlphabet.
end ( ) ) )
552 throw AutomatonException (
"Input string is over different alphabet than automaton" );
554 if ( ! getStates ( ).contains ( to ) )
557 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input ) );
558 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input ) );
559 auto iter = std::lower_bound ( lower_bound, upper_bound, to, [ ] (
const auto & transition,
const auto & target ) {
return transition.second < target; } );
560 if ( iter != upper_bound && to >= iter->second )
564 transitions.insert ( iter,
std::make_pair ( std::move ( key ), std::move ( to ) ) );
568template<
class SymbolType,
class StateType >
570 auto upper_bound = transitions.upper_bound (
ext::tie ( from, input ) );
571 auto lower_bound = transitions.lower_bound (
ext::tie ( from, input ) );
572 auto iter = std::find_if ( lower_bound, upper_bound, [ & ] (
const auto & transition ) {
return transition.second == to; } );
573 if ( iter == upper_bound )
576 transitions.erase ( iter );
580template<
class SymbolType,
class StateType >
585template<
class SymbolType,
class StateType >
587 return std::move ( transitions );
590template<
class SymbolType,
class StateType >
592 if ( ! getStates ( ).contains ( from ) )
598 if ( transition.first.first == from )
599 transitionsFromState.
insert ( transition );
601 return transitionsFromState;
604template<
class SymbolType,
class StateType >
606 if ( ! getStates ( ).contains ( to ) )
612 if ( transition.second == to )
613 transitionsToState.
insert ( transition );
615 return transitionsToState;
628template<
class SymbolType,
class StateType >
677template<
class SymbolType,
class StateType >
689 if (
automaton.getInitialState ( ) == state )
692 if (
automaton.getFinalStates ( ).contains ( state ) )
696 if ( transition.first.first == state || transition.second == state )
730template<
class SymbolType,
class StateType >
754 return automaton.template accessComponent < automaton::States > ( ).get ( ).contains ( state );
773template<
class SymbolType,
class StateType >
785 return automaton.template accessComponent < automaton::States > ( ).get ( ).contains ( state );
803template <
class SymbolType,
class StateType >
818 res.addTransition ( std::move ( from ), std::move ( input ), std::move ( target ) );
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
Definition: AutomatonException.h:15
static DefaultStateType normalizeState(StateType &&state)
Definition: AutomatonNormalize.h:76
static ext::multiset< DefaultStateType > normalizeStates(ext::multiset< StateType > &&states)
Definition: AutomatonNormalize.h:49
static regexp::UnboundedRegExpStructure< DefaultSymbolType > normalizeRegExp(regexp::UnboundedRegExpStructure< SymbolType > &®exp)
Definition: AutomatonNormalize.h:89
Compact nondeterministic finite automaton. Accepts regular languages. The automaton has a list of sym...
Definition: CompactNFA.h:78
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: EpsilonNFA.h:74
Extended nondeterministic finite automaton. Accepts regular languages. The automaton has a regular ex...
Definition: ExtendedNFA.h:80
SymbolTypeT SymbolType
Definition: ExtendedNFA.h:82
bool addTransition(StateType from, regexp::UnboundedRegExpStructure< SymbolType > input, StateType to)
Add a transition to the automaton.
Definition: ExtendedNFA.h:544
void setStates(ext::set< StateType > states)
Definition: ExtendedNFA.h:214
friend ext::ostream & operator<<(ext::ostream &out, const ExtendedNFA &instance)
Definition: ExtendedNFA.h:426
StateTypeT StateType
Definition: ExtendedNFA.h:83
const StateType & getInitialState() const &
Definition: ExtendedNFA.h:156
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ExtendedNFA.h:283
ext::set< StateType > && getStates() &&
Definition: ExtendedNFA.h:194
void removeInputSymbol(const SymbolType &symbol)
Definition: ExtendedNFA.h:332
void setFinalStates(ext::set< StateType > states)
Definition: ExtendedNFA.h:263
ext::set< SymbolType > && getInputAlphabet() &&
Definition: ExtendedNFA.h:292
ext::multimap< ext::pair< StateType, regexp::UnboundedRegExpStructure< SymbolType > >, StateType > getTransitionsToState(const StateType &to) const
Definition: ExtendedNFA.h:605
const ext::multimap< ext::pair< StateType, regexp::UnboundedRegExpStructure< SymbolType > >, StateType > & getTransitions() const &
Definition: ExtendedNFA.h:581
StateType && getInitialState() &&
Definition: ExtendedNFA.h:165
void removeFinalState(const StateType &state)
Definition: ExtendedNFA.h:274
bool operator==(const ExtendedNFA &other) const
Definition: ExtendedNFA.h:414
const ext::set< StateType > & getStates() const &
Definition: ExtendedNFA.h:185
void removeState(const StateType &state)
Definition: ExtendedNFA.h:225
ext::set< StateType > && getFinalStates() &&
Definition: ExtendedNFA.h:243
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: ExtendedNFA.h:321
bool removeTransition(const StateType &from, const regexp::UnboundedRegExpStructure< SymbolType > &input, const StateType &to)
Removes a transition from the automaton.
Definition: ExtendedNFA.h:569
bool addState(StateType state)
Definition: ExtendedNFA.h:205
const ext::set< StateType > & getFinalStates() const &
Definition: ExtendedNFA.h:234
ext::multimap< ext::pair< StateType, regexp::UnboundedRegExpStructure< SymbolType > >, StateType > getTransitionsFromState(const StateType &from) const
Definition: ExtendedNFA.h:591
bool setInitialState(StateType state)
Definition: ExtendedNFA.h:176
void addInputSymbols(ext::set< SymbolType > symbols)
Definition: ExtendedNFA.h:312
ExtendedNFA(StateType initialState)
Creates a new instance of the automaton with a concrete initial state.
Definition: ExtendedNFA.h:469
bool addFinalState(StateType state)
Definition: ExtendedNFA.h:254
bool addInputSymbol(SymbolType symbol)
Definition: ExtendedNFA.h:303
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: MultiInitialStateEpsilonNFA.h:75
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
Nondeterministic finite automaton. Accepts regular languages.
Definition: NFA.h:66
Definition: ExtendedNFA.h:443
Definition: components.hpp:181
static void valid(const automaton::ExtendedNFA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFA.h:794
static bool available(const automaton::ExtendedNFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: ExtendedNFA.h:784
Definition: components.hpp:25
static bool used(const automaton::ExtendedNFA< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: ExtendedNFA.h:639
static void valid(const automaton::ExtendedNFA< SymbolType, StateType > &, const SymbolType &)
Definition: ExtendedNFA.h:667
static bool available(const automaton::ExtendedNFA< SymbolType, StateType > &, const SymbolType &)
Definition: ExtendedNFA.h:657
static bool available(const automaton::ExtendedNFA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFA.h:710
static void valid(const automaton::ExtendedNFA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFA.h:720
static bool used(const automaton::ExtendedNFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: ExtendedNFA.h:688
static bool available(const automaton::ExtendedNFA< SymbolType, StateType > &automaton, const StateType &state)
Definition: ExtendedNFA.h:753
static void valid(const automaton::ExtendedNFA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFA.h:763
static bool used(const automaton::ExtendedNFA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFA.h:741
Definition: setComponents.hpp:26
Class extending the multimap class from the standard library. Original reason is to allow printing of...
Definition: multimap.hpp:48
iterator insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:118
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: set.hpp:99
auto end() &
Inherited behavior of end for non-const instance.
Definition: set.hpp:129
Represents the concatenation operator in the regular expression. The node can have 0 to n children in...
Definition: UnboundedRegExpConcatenation.h:44
void appendElement(UnboundedRegExpElement< SymbolType > &&element)
Definition: UnboundedRegExpConcatenation.h:195
Represents the epsilon expression in the regular expression. The node can't have any children.
Definition: UnboundedRegExpEpsilon.h:41
Represents unbounded regular expression structure. Regular expression is stored as a tree of Unbounde...
Definition: UnboundedRegExpStructure.h:47
Represents the symbol in the regular expression. The can't have any children.
Definition: UnboundedRegExpSymbol.h:42
Definition: BarSymbol.cpp:12
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
constexpr bool isExtendedNFA
Definition: ExtendedNFA.h:462
T createUnique(T object, const Alphabets &... alphabets)
Definition: createUnique.hpp:46
Definition: normalize.hpp:10
Definition: sigHandler.cpp:20
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
reference_mover< T > make_mover(T ¶m)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19
Definition: ToAutomaton.h:15
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static automaton::ExtendedNFA< > eval(automaton::ExtendedNFA< SymbolType, StateType > &&value)
Definition: ExtendedNFA.h:805
Definition: normalize.hpp:13