Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ExtendedNFTA.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <ostream>
27
28#include <alib/map>
29#include <alib/set>
30#include <alib/vector>
31
32#include <core/components.hpp>
33
34#include <core/normalize.hpp>
35
38
40
41#include <core/normalize.hpp>
44
47
48#include "DFTA.h"
49#include "NFTA.h"
50
51namespace automaton {
52
53class InputAlphabet;
54class States;
55class FinalStates;
56
74template < class SymbolType = DefaultSymbolType, class StateType = DefaultStateType >
75class ExtendedNFTA final : public core::Components < ExtendedNFTA < SymbolType, StateType >, ext::set < common::ranked_symbol < SymbolType > >, component::Set, InputAlphabet, ext::set < StateType >, component::Set, std::tuple < States, FinalStates > > {
80
81public:
85 explicit ExtendedNFTA ( );
86
95
96 /*
97 * \brief Creates a new instance of the automaton based on the Deterministic finite tree automaton.
98 *
99 * \param other the Deterministic finite tree automaton
100 */
101 explicit ExtendedNFTA ( const DFTA < SymbolType, StateType > & other );
102
103 /*
104 * \brief Creates a new instance of the automaton based on the Nondeterministic finite tree automaton.
105 *
106 * \param other the Deterministic finite tree automaton
107 */
108 explicit ExtendedNFTA ( const NFTA < SymbolType, StateType > & other );
109
115 const ext::set < StateType > & getStates ( ) const & {
116 return this->template accessComponent < States > ( ).get ( );
117 }
118
125 return std::move ( this->template accessComponent < States > ( ).get ( ) );
126 }
127
135 bool addState ( StateType state ) {
136 return this->template accessComponent < States > ( ).add ( std::move ( state ) );
137 }
138
145 this->template accessComponent < States > ( ).set ( std::move ( states ) );
146 }
147
155 void removeState ( const StateType & state ) {
156 this->template accessComponent < States > ( ).remove ( state );
157 }
158
165 return this->template accessComponent < FinalStates > ( ).get ( );
166 }
167
174 return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) );
175 }
176
184 bool addFinalState ( StateType state ) {
185 return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
186 }
187
194 this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
195 }
196
204 void removeFinalState ( const StateType & state ) {
205 this->template accessComponent < FinalStates > ( ).remove ( state );
206 }
207
214 return this->template accessComponent < InputAlphabet > ( ).get ( );
215 }
216
223 return std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) );
224 }
225
234 return this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
235 }
236
243 this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
244 }
245
252 this->template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
253 }
254
263 this->template accessComponent < InputAlphabet > ( ).remove ( symbol );
264 }
265
280
295
308
315 return transitions;
316 }
317
324 return std::move ( transitions );
325 }
326
331
336
345 bool isDeterministic ( ) const;
346
352 unsigned transitionsSize ( ) const;
353
361 auto operator <=> ( const ExtendedNFTA & other ) const {
362 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) <=> std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
363 }
364
372 bool operator == ( const ExtendedNFTA & other ) const {
373 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) == std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
374 }
375
384 friend ext::ostream & operator << ( ext::ostream & out, const ExtendedNFTA & instance ) {
385 return out << "(ExtendedNFTA"
386 << " states = " << instance.getStates ( )
387 << " inputAlphabet = " << instance.getInputAlphabet ( )
388 << " finalStates = " << instance.getFinalStates ( )
389 << " transitions = " << instance.getTransitions ( )
390 << ")";
391 }
392};
393
394template < class SymbolType, class StateType >
395ExtendedNFTA < SymbolType, StateType >::ExtendedNFTA ( ext::set < StateType > states, ext::set < common::ranked_symbol < SymbolType > > inputAlphabet, ext::set < StateType > finalStates ) : core::Components < ExtendedNFTA, ext::set < common::ranked_symbol < SymbolType > >, component::Set, InputAlphabet, ext::set < StateType >, component::Set, std::tuple < States, FinalStates > > ( std::move ( inputAlphabet ), std::move ( states ), std::move ( finalStates ) ) {
396}
397
398template < class SymbolType, class StateType >
400}
401
402template < class SymbolType, class StateType >
403ExtendedNFTA < SymbolType, StateType >::ExtendedNFTA(const DFTA < SymbolType, StateType > & other) : ExtendedNFTA ( other.getStates(), other.getInputAlphabet(), other.getFinalStates() ) {
404 for ( const auto & transition : other.getTransitions ( ) ) {
405 // ( symbol, prevStates ) -> nextState
406 // ext::map < ext::pair < rte::FormalRTEStructure < ext::variant < SymbolType, StateType > >, ext::vector < StateType > >, ext::set < StateType > > transitions;
407
409 for ( const StateType & state : transition.first.second )
411
412 common::ranked_symbol < ext::variant < SymbolType, StateType > > symbol ( transition.first.first.getSymbol ( ), transition.first.first.getRank ( ) );
414 transitions [ ext::make_pair ( rte, transition.first.second ) ].insert ( transition.second );
415 }
416}
417
418template < class SymbolType, class StateType >
419ExtendedNFTA < SymbolType, StateType >::ExtendedNFTA(const NFTA < SymbolType, StateType > & other) : ExtendedNFTA ( other.getStates(), other.getInputAlphabet(), other.getFinalStates() ) {
420 for ( const auto & transition : other.getTransitions ( ) ) {
421 // ( symbol, prevStates ) -> nextState
422 // ext::map < ext::pair < rte::FormalRTEStructure < ext::variant < SymbolType, StateType > >, ext::vector < StateType > >, ext::set < StateType > > transitions;
423
425 for ( const StateType & state : transition.first.second )
427
428 common::ranked_symbol < ext::variant < SymbolType, StateType > > symbol ( transition.first.first.getSymbol ( ), transition.first.first.getRank ( ) );
430 transitions [ ext::make_pair ( rte, transition.first.second ) ].insert ( transition.second );
431 }
432}
433
434template < class SymbolType, class StateType >
436 /*
437 if (prevStates.size() != symbol.getRank() )
438 throw AutomatonException("Number of states doesn't match rank of the symbol");
439
440 if (! getInputAlphabet().count(symbol))
441 throw AutomatonException("Input symbol \"" + ext::to_string ( symbol ) + "\" doesn't exist.");
442 */
443
444 if (! getStates().count(next))
445 throw AutomatonException("State \"" + ext::to_string ( next ) + "\" doesn't exist.");
446
447 for (const StateType& it : prevStates) {
448 if (! getStates().count(it))
449 throw AutomatonException("State \"" + ext::to_string ( it ) + "\" doesn't exist.");
450 }
451
453 return transitions [ std::move ( key ) ].insert ( std::move ( next ) ).second;
454}
455
456template < class SymbolType, class StateType >
458 /*
459 if (prevStates.size() != symbol.getRank() )
460 throw AutomatonException("Number of states doesn't match rank of the symbol");
461
462 if (! getInputAlphabet().count(symbol))
463 throw AutomatonException("Input symbol \"" + ext::to_string ( symbol ) + "\" doesn't exist.");
464 */
465
466 if ( !std::includes ( getStates ( ).begin ( ), getStates ( ).end ( ), next.begin ( ), next.end ( ) ) )
467 throw AutomatonException ( "Some target states don't exist." );
468
469 for (const StateType& it : prevStates) {
470 if (! getStates().count(it))
471 throw AutomatonException("State \"" + ext::to_string ( it ) + "\" doesn't exist.");
472 }
473
475 transitions [ std::move ( key ) ].insert ( ext::make_mover ( next ).begin ( ), ext::make_mover ( next ).end ( ) );
476}
477
478template < class SymbolType, class StateType >
481 return transitions [ key ].erase ( next );
482}
483
484template < class SymbolType, class StateType >
486 return false;
487}
488
489
490template < class SymbolType, class StateType >
492 int res = 0;
493
494 for(const auto& transition : transitions )
495 res += transition.second.size();
496
497 return res;
498}
499
500template<class SymbolType, class StateType >
502 if ( !getStates ( ).count ( to ) )
503 throw AutomatonException ( "State \"" + ext::to_string ( to ) + "\" doesn't exist" );
504
506
507 for ( const auto & transition : transitions )
508 if ( transition.second.count ( to ) > 0 )
509 res.insert ( ext::make_pair ( transition.first, to ) );
510
511 return res;
512}
513
514template<class SymbolType, class StateType >
516 if ( !getStates ( ).count ( from ) )
517 throw AutomatonException ( "State \"" + ext::to_string ( from ) + "\" doesn't exist" );
518
520
521 for ( const auto & transition : transitions )
522 if ( std::find ( transition.first.second.begin ( ), transition.first.second.end ( ), from ) != transition.first.second.end ( ) )
523 res.insert ( transition );
524
525 return res;
526}
527
528} /* namespace automaton */
529
530namespace core {
531
538template < class SymbolType, class StateType >
539class SetConstraint< automaton::ExtendedNFTA < SymbolType, StateType >, common::ranked_symbol < SymbolType >, automaton::InputAlphabet > {
540public:
549 static bool used ( const automaton::ExtendedNFTA < SymbolType, StateType > & /*automaton*/, const common::ranked_symbol < SymbolType > & /*symbol*/ ) {
550/* for ( const std::pair<const ext::pair<rte::FormalRTEStructure < ext::variant < SymbolType, StateType > >, ext::vector<StateType> >, ext::set<StateType>>& t : automaton.getTransitions())
551 if (t.first.first == symbol)
552 return true;
553
554 return false;*/
555 return true; // TODO
556 }
557
567 return true;
568 }
569
577 }
578};
579
586template < class SymbolType, class StateType >
587class SetConstraint< automaton::ExtendedNFTA < SymbolType, StateType >, StateType, automaton::States > {
588public:
598 if ( automaton.getFinalStates ( ).count ( state ) )
599 return true;
600
602 if(ext::contains(t.first.second.begin(), t.first.second.end(), state ) || t . second.count ( state ) )
603 return true;
604
605 return false;
606 }
607
617 return true;
618 }
619
627 }
628};
629
635template < class SymbolType, class StateType >
636class SetConstraint< automaton::ExtendedNFTA < SymbolType, StateType >, StateType, automaton::FinalStates > {
637public:
647 return false;
648 }
649
659 return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
660 }
661
669 }
670};
671
677template < class SymbolType, class StateType >
678struct normalize < automaton::ExtendedNFTA < SymbolType, StateType > > {
681 ext::set < DefaultStateType > states = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getStates ( ) );
682 ext::set < DefaultStateType > finalStates = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getFinalStates ( ) );
683
684 automaton::ExtendedNFTA < > res ( std::move ( states ), std::move ( alphabet ), std::move ( finalStates ) );
685
686 for ( std::pair < ext::pair < rte::FormalRTEStructure < ext::variant < SymbolType, StateType > >, ext::vector < StateType > >, ext::set < StateType > > && transition : ext::make_mover ( std::move ( value ).getTransitions ( ) ) ) {
687 rte::FormalRTEStructure < ext::variant < SymbolType, StateType > > rte = transition.first.first; // TODO alphabet::SymbolNormalize::normalizeRankedSymbol ( std::move ( transition.first.first ) );
688 ext::vector < DefaultStateType > from = automaton::AutomatonNormalize::normalizeStates ( std::move ( transition.first.second ) );
690
691 res.addTransitions ( std::move ( rte ), std::move ( from ), std::move ( to ) );
692 }
693
694 return res;
695 }
696};
697
698} /* namespace core */
699
700extern template class automaton::ExtendedNFTA < >;
701
static ext::set< common::ranked_symbol< DefaultSymbolType > > normalizeRankedAlphabet(ext::set< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:59
Definition: AutomatonException.h:15
static ext::multiset< DefaultStateType > normalizeStates(ext::multiset< StateType > &&states)
Definition: AutomatonNormalize.h:49
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: DFTA.h:74
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: ExtendedNFTA.h:75
bool isDeterministic() const
Determines whether the automaton is deterministic.
Definition: ExtendedNFTA.h:485
ext::map< ext::pair< rte::FormalRTEStructure< ext::variant< SymbolType, StateType > >, ext::vector< StateType > >, ext::set< StateType > > && getTransitions() &&
Definition: ExtendedNFTA.h:323
void removeInputSymbol(const common::ranked_symbol< SymbolType > &symbol)
Definition: ExtendedNFTA.h:262
bool operator==(const ExtendedNFTA &other) const
Definition: ExtendedNFTA.h:372
ext::map< ext::pair< rte::FormalRTEStructure< ext::variant< SymbolType, StateType > >, ext::vector< StateType > >, StateType > getTransitionsToState(const StateType &to) const
Definition: ExtendedNFTA.h:501
void addTransitions(rte::FormalRTEStructure< ext::variant< SymbolType, StateType > > rte, ext::vector< StateType >, ext::set< StateType > next)
Add a transition to the automaton.
Definition: ExtendedNFTA.h:457
bool removeTransition(const rte::FormalRTEStructure< ext::variant< SymbolType, StateType > > &rte, const ext::vector< StateType > &prevStates, const StateType &next)
Removes a transition from the automaton.
Definition: ExtendedNFTA.h:479
void setStates(ext::set< StateType > states)
Definition: ExtendedNFTA.h:144
bool addTransition(rte::FormalRTEStructure< ext::variant< SymbolType, StateType > > rte, ext::vector< StateType >, StateType next)
Add a transition to the automaton.
Definition: ExtendedNFTA.h:435
ext::set< common::ranked_symbol< SymbolType > > && getInputAlphabet() &&
Definition: ExtendedNFTA.h:222
const ext::set< StateType > & getStates() const &
Definition: ExtendedNFTA.h:115
friend ext::ostream & operator<<(ext::ostream &out, const ExtendedNFTA &instance)
Definition: ExtendedNFTA.h:384
void removeState(const StateType &state)
Definition: ExtendedNFTA.h:155
void removeFinalState(const StateType &state)
Definition: ExtendedNFTA.h:204
const ext::map< ext::pair< rte::FormalRTEStructure< ext::variant< SymbolType, StateType > >, ext::vector< StateType > >, ext::set< StateType > > & getTransitions() const &
Definition: ExtendedNFTA.h:314
void addInputSymbols(ext::set< common::ranked_symbol< SymbolType > > symbols)
Definition: ExtendedNFTA.h:242
bool addFinalState(StateType state)
Definition: ExtendedNFTA.h:184
ext::set< StateType > && getFinalStates() &&
Definition: ExtendedNFTA.h:173
unsigned transitionsSize() const
Computes number of transitions in the automaton.
Definition: ExtendedNFTA.h:491
bool addInputSymbol(common::ranked_symbol< SymbolType > symbol)
Definition: ExtendedNFTA.h:233
auto operator<=>(const ExtendedNFTA &other) const
Definition: ExtendedNFTA.h:361
const ext::set< StateType > & getFinalStates() const &
Definition: ExtendedNFTA.h:164
ext::set< StateType > && getStates() &&
Definition: ExtendedNFTA.h:124
ExtendedNFTA()
Creates a new instance of the automaton.
Definition: ExtendedNFTA.h:399
const ext::set< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: ExtendedNFTA.h:213
void setFinalStates(ext::set< StateType > states)
Definition: ExtendedNFTA.h:193
bool addState(StateType state)
Definition: ExtendedNFTA.h:135
ext::map< ext::pair< rte::FormalRTEStructure< ext::variant< SymbolType, StateType > >, ext::vector< StateType > >, ext::set< StateType > > getTransitionsFromState(const StateType &from) const
Definition: ExtendedNFTA.h:515
void setInputAlphabet(ext::set< common::ranked_symbol< SymbolType > > symbols)
Definition: ExtendedNFTA.h:251
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: NFTA.h:72
Definition: ranked_symbol.hpp:20
Definition: components.hpp:181
static bool used(const automaton::ExtendedNFTA< SymbolType, StateType > &, const common::ranked_symbol< SymbolType > &)
Definition: ExtendedNFTA.h:549
static bool available(const automaton::ExtendedNFTA< SymbolType, StateType > &, const common::ranked_symbol< SymbolType > &)
Definition: ExtendedNFTA.h:566
static void valid(const automaton::ExtendedNFTA< SymbolType, StateType > &, const common::ranked_symbol< SymbolType > &)
Definition: ExtendedNFTA.h:576
static void valid(const automaton::ExtendedNFTA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFTA.h:668
static bool available(const automaton::ExtendedNFTA< SymbolType, StateType > &automaton, const StateType &state)
Definition: ExtendedNFTA.h:658
static bool used(const automaton::ExtendedNFTA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFTA.h:646
static void valid(const automaton::ExtendedNFTA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFTA.h:626
static bool used(const automaton::ExtendedNFTA< SymbolType, StateType > &automaton, const StateType &state)
Definition: ExtendedNFTA.h:597
static bool available(const automaton::ExtendedNFTA< SymbolType, StateType > &, const StateType &)
Definition: ExtendedNFTA.h:616
Definition: setComponents.hpp:26
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: ostream.h:14
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Implementation of vector storing dynamicaly allocated instances of given type. The class mimicks the ...
Definition: ptr_vector.hpp:44
void push_back(R &&value)
Appends a new value at the end of the container.
Definition: ptr_vector.hpp:1228
Definition: set.hpp:44
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Represents unbounded regular expression structure. Regular expression is stored as a tree of Unbounde...
Definition: FormalRTEStructure.h:41
Represents the terminal symbol in the regular tree expression. The number of children must be the sam...
Definition: FormalRTESymbolAlphabet.h:44
Represents the substitution symbol in the regular tree expression. The node can't have any children.
Definition: FormalRTESymbolSubst.h:43
Definition: BarSymbol.cpp:12
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
p second
Definition: ToRegExpAlgebraic.h:126
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
Definition: normalize.hpp:10
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 &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
bool contains(InputIt first, InputIt last, const Element &elem)
Linear version of search in a range of values.
Definition: algorithm.hpp:170
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: ToFTAGlushkov.h:22
auto & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static automaton::ExtendedNFTA< > eval(automaton::ExtendedNFTA< SymbolType, StateType > &&value)
Definition: ExtendedNFTA.h:679
Definition: normalize.hpp:13