Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DFTA.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>
34
37
39
40#include <core/normalize.hpp>
43
44namespace automaton {
45
46class InputAlphabet;
47class States;
48class FinalStates;
49
73template < class SymbolTypeT = DefaultSymbolType, class StateTypeT = DefaultStateType >
74class DFTA final : public core::Components < DFTA < SymbolTypeT, StateTypeT >, ext::set < common::ranked_symbol < SymbolTypeT > >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates > > {
75public:
76 typedef SymbolTypeT SymbolType;
77 typedef StateTypeT StateType;
78
79private:
84
85public:
89 explicit DFTA ( );
90
99
105 const ext::set < StateType > & getStates ( ) const & {
106 return this->template accessComponent < States > ( ).get ( );
107 }
108
115 return std::move ( this->template accessComponent < States > ( ).get ( ) );
116 }
117
125 bool addState ( StateType state ) {
126 return this->template accessComponent < States > ( ).add ( std::move ( state ) );
127 }
128
135 this->template accessComponent < States > ( ).set ( std::move ( states ) );
136 }
137
145 void removeState ( const StateType & state ) {
146 this->template accessComponent < States > ( ).remove ( state );
147 }
148
155 return this->template accessComponent < FinalStates > ( ).get ( );
156 }
157
164 return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) );
165 }
166
174 bool addFinalState ( StateType state ) {
175 return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
176 }
177
184 this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
185 }
186
194 void removeFinalState ( const StateType & state ) {
195 this->template accessComponent < FinalStates > ( ).remove ( state );
196 }
197
204 return this->template accessComponent < InputAlphabet > ( ).get ( );
205 }
206
213 return std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) );
214 }
215
224 return this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
225 }
226
233 this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
234 }
235
242 this->template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
243 }
244
253 this->template accessComponent < InputAlphabet > ( ).remove ( symbol );
254 }
255
270
282 bool removeTransition ( const common::ranked_symbol < SymbolType > & symbol, const ext::vector < StateType > & states, const StateType & next );
283
290 return transitions;
291 }
292
299 return std::move ( transitions );
300 }
301
307
308 for ( const auto & transition : getTransitions ( ) ) {
309 if ( transition.second == q )
310 res.insert ( std::make_pair ( transition.first, q ) );
311 }
312
313 return res;
314 }
315
321
322 for ( const auto & transition : getTransitions ( ) ) {
323 if ( std::find ( transition.first.second.begin ( ), transition.first.second.end ( ), q ) != transition.first.second.end ( ) )
324 res.insert ( transition );
325 }
326
327 return res;
328 }
329
337 auto operator <=> ( const DFTA & other ) const {
338 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) <=> std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
339 }
340
348 bool operator == ( const DFTA & other ) const {
349 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) == std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
350 }
351
360 friend ext::ostream & operator << ( ext::ostream & out, const DFTA & instance ) {
361 return out << "(DFTA"
362 << " states = " << instance.getStates ( )
363 << " inputAlphabet = " << instance.getInputAlphabet ( )
364 << " finalStates = " << instance.getFinalStates ( )
365 << " transitions = " << instance.getTransitions ( )
366 << ")";
367 }
368};
369
375template < class T >
376class isDFTA_impl : public std::false_type {};
377
386template < class SymbolType, class StateType >
387class isDFTA_impl < DFTA < SymbolType, StateType > > : public std::true_type {};
388
394template < class T >
395constexpr bool isDFTA = isDFTA_impl < T > { };
396
397template<class SymbolType, class StateType >
398DFTA < SymbolType, StateType >::DFTA ( ext::set < StateType > states, ext::set < common::ranked_symbol < SymbolType > > inputAlphabet, ext::set < StateType > finalStates ) : core::Components < DFTA, 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 ) ) {
399}
400
401template<class SymbolType, class StateType >
403}
404
405template<class SymbolType, class StateType >
407 if ( prevStates.size ( ) != symbol.getRank ( ) )
408 throw AutomatonException("Number of states doesn't match rank of the symbol");
409
410 if (! getInputAlphabet().count(symbol))
411 throw AutomatonException("Input symbol \"" + ext::to_string ( symbol ) + "\" doesn't exist.");
412
413 if (! getStates().count(next))
414 throw AutomatonException("State \"" + ext::to_string ( next ) + "\" doesn't exist.");
415
416 for ( const StateType & it : prevStates) {
417 if (! getStates().count(it))
418 throw AutomatonException("State \"" + ext::to_string ( it ) + "\" doesn't exist.");
419 }
420
421 ext::pair<common::ranked_symbol < SymbolType >, ext::vector<StateType> > key = ext::make_pair ( std::move ( symbol ), std::move ( prevStates ) );
422 if ( transitions.find ( key ) != transitions.end ( ) ) {
423 if ( transitions.find ( key )->second == next )
424 return false;
425 else
426 throw AutomatonException("Transition already exists");
427 }
428
429 transitions.insert ( std::move ( key ), std::move ( next ) );
430 return true;
431}
432
433template<class SymbolType, class StateType >
436
437 if ( transitions.find ( key ) == transitions.end ( ) )
438 return false;
439
440 if ( transitions.find ( key )->second != next )
441 throw AutomatonException("Transition does not exist");
442
443 transitions.erase(key);
444 return true;
445}
446
447} /* namespace automaton */
448
449namespace core {
450
457template<class SymbolType, class StateType >
458class SetConstraint< automaton::DFTA < SymbolType, StateType >, common::ranked_symbol < SymbolType >, automaton::InputAlphabet > {
459public:
469 for ( const std::pair<const ext::pair<common::ranked_symbol < SymbolType >, ext::vector<StateType> >, StateType>& t : automaton.getTransitions())
470 if (t.first.first == symbol)
471 return true;
472
473 return false;
474 }
475
485 return true;
486 }
487
495 }
496};
497
504template<class SymbolType, class StateType >
505class SetConstraint< automaton::DFTA < SymbolType, StateType >, StateType, automaton::States > {
506public:
515 static bool used ( const automaton::DFTA < SymbolType, StateType > & automaton, const StateType & state ) {
516 if ( automaton.getFinalStates ( ).count ( state ) )
517 return true;
518
519 for ( const std::pair<const ext::pair<common::ranked_symbol < SymbolType >, ext::vector<StateType> >, StateType>& t : automaton.getTransitions())
520 if(ext::contains(t.first.second.begin(), t.first.second.end(), state ) || t.second == state)
521 return true;
522
523 return false;
524 }
525
535 return true;
536 }
537
545 }
546};
547
554template<class SymbolType, class StateType >
555class SetConstraint< automaton::DFTA < SymbolType, StateType >, StateType, automaton::FinalStates > {
556public:
566 return false;
567 }
568
578 return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
579 }
580
588 }
589};
590
596template < class SymbolType, class StateType >
597struct normalize < automaton::DFTA < SymbolType, StateType > > {
600 ext::set < DefaultStateType > states = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getStates ( ) );
601 ext::set < DefaultStateType > finalStates = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getFinalStates ( ) );
602
603 automaton::DFTA < > res ( std::move ( states ), std::move ( alphabet ), std::move ( finalStates ) );
604
605 for ( std::pair < ext::pair < common::ranked_symbol < SymbolType >, ext::vector < StateType > >, StateType > && transition : ext::make_mover ( std::move ( value ).getTransitions ( ) ) ) {
607 ext::vector < DefaultStateType > from = automaton::AutomatonNormalize::normalizeStates ( std::move ( transition.first.second ) );
608 DefaultStateType to = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.second ) );
609
610 res.addTransition ( std::move ( input ), std::move ( from ), std::move ( to ) );
611 }
612
613 return res;
614 }
615};
616
617} /* namespace core */
618
619extern template class automaton::DFTA < >;
620
static common::ranked_symbol< DefaultSymbolType > normalizeRankedSymbol(common::ranked_symbol< SymbolType > &&symbol)
Definition: SymbolNormalize.h:81
static ext::set< common::ranked_symbol< DefaultSymbolType > > normalizeRankedAlphabet(ext::set< common::ranked_symbol< SymbolType > > &&symbols)
Definition: SymbolNormalize.h:59
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
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: DFTA.h:74
const ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > & getTransitions() const &
Definition: DFTA.h:289
ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > getTransitionsFromState(const StateType &q) const
Definition: DFTA.h:319
friend ext::ostream & operator<<(ext::ostream &out, const DFTA &instance)
Definition: DFTA.h:360
bool addInputSymbol(common::ranked_symbol< SymbolType > symbol)
Definition: DFTA.h:223
StateTypeT StateType
Definition: DFTA.h:77
const ext::set< common::ranked_symbol< SymbolType > > & getInputAlphabet() const &
Definition: DFTA.h:203
bool operator==(const DFTA &other) const
Definition: DFTA.h:348
void removeInputSymbol(const common::ranked_symbol< SymbolType > &symbol)
Definition: DFTA.h:252
DFTA()
Creates a new instance of the automaton.
Definition: DFTA.h:402
bool addFinalState(StateType state)
Definition: DFTA.h:174
ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > getTransitionsToState(const StateType &q) const
Definition: DFTA.h:305
void addInputSymbols(ext::set< common::ranked_symbol< SymbolType > > symbols)
Definition: DFTA.h:232
void removeState(const StateType &state)
Definition: DFTA.h:145
void setFinalStates(ext::set< StateType > states)
Definition: DFTA.h:183
bool addTransition(common::ranked_symbol< SymbolType > symbol, ext::vector< StateType > prevStates, StateType next)
Add a transition to the automaton.
Definition: DFTA.h:406
ext::set< StateType > && getStates() &&
Definition: DFTA.h:114
ext::set< common::ranked_symbol< SymbolType > > && getInputAlphabet() &&
Definition: DFTA.h:212
ext::set< StateType > && getFinalStates() &&
Definition: DFTA.h:163
auto operator<=>(const DFTA &other) const
Definition: DFTA.h:337
bool removeTransition(const common::ranked_symbol< SymbolType > &symbol, const ext::vector< StateType > &states, const StateType &next)
Removes a transition from the automaton.
Definition: DFTA.h:434
void setInputAlphabet(ext::set< common::ranked_symbol< SymbolType > > symbols)
Definition: DFTA.h:241
ext::map< ext::pair< common::ranked_symbol< SymbolType >, ext::vector< StateType > >, StateType > && getTransitions() &&
Definition: DFTA.h:298
bool addState(StateType state)
Definition: DFTA.h:125
void removeFinalState(const StateType &state)
Definition: DFTA.h:194
const ext::set< StateType > & getFinalStates() const &
Definition: DFTA.h:154
void setStates(ext::set< StateType > states)
Definition: DFTA.h:134
const ext::set< StateType > & getStates() const &
Definition: DFTA.h:105
SymbolTypeT SymbolType
Definition: DFTA.h:76
Definition: DFTA.h:376
Definition: ranked_symbol.hpp:20
Definition: components.hpp:181
static void valid(const automaton::DFTA< SymbolType, StateType > &, const StateType &)
Definition: DFTA.h:544
static bool used(const automaton::DFTA< SymbolType, StateType > &automaton, const StateType &state)
Definition: DFTA.h:515
static bool available(const automaton::DFTA< SymbolType, StateType > &, const StateType &)
Definition: DFTA.h:534
static void valid(const automaton::DFTA< SymbolType, StateType > &, const StateType &)
Definition: DFTA.h:587
static bool used(const automaton::DFTA< SymbolType, StateType > &, const StateType &)
Definition: DFTA.h:565
static bool available(const automaton::DFTA< SymbolType, StateType > &automaton, const StateType &state)
Definition: DFTA.h:577
static bool used(const automaton::DFTA< SymbolType, StateType > &automaton, const common::ranked_symbol< SymbolType > &symbol)
Definition: DFTA.h:468
static bool available(const automaton::DFTA< SymbolType, StateType > &, const common::ranked_symbol< SymbolType > &)
Definition: DFTA.h:484
static void valid(const automaton::DFTA< SymbolType, StateType > &, const common::ranked_symbol< SymbolType > &)
Definition: DFTA.h:494
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
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
Definition: Object.h:16
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
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31
constexpr bool isDFTA
Definition: DFTA.h:395
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 & get(ext::ptr_array< Type, N > &tpl)
Specialisation of get function for pointer arrays.
Definition: ptr_array.hpp:693
static automaton::DFTA< > eval(automaton::DFTA< SymbolType, StateType > &&value)
Definition: DFTA.h:598
Definition: normalize.hpp:13