Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ArcFactoredDeterministicZAutomaton.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
36
38
39#include <core/normalize.hpp>
42
43namespace automaton {
44
45class InputAlphabet;
46class States;
47class FinalStates;
48
64template < class SymbolTypeT = DefaultSymbolType, class StateTypeT = DefaultStateType >
65class ArcFactoredDeterministicZAutomaton final : public core::Components < ArcFactoredDeterministicZAutomaton < SymbolTypeT, StateTypeT >, ext::set < SymbolTypeT >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates > > {
66public:
67 typedef SymbolTypeT SymbolType;
68 typedef StateTypeT StateType;
69
70private:
75
76public:
81
90
96 const ext::set < StateType > & getStates ( ) const & {
97 return this->template accessComponent < States > ( ).get ( );
98 }
99
106 return std::move ( this->template accessComponent < States > ( ).get ( ) );
107 }
108
116 bool addState ( StateType state ) {
117 return this->template accessComponent < States > ( ).add ( std::move ( state ) );
118 }
119
126 this->template accessComponent < States > ( ).set ( std::move ( states ) );
127 }
128
136 void removeState ( const StateType & state ) {
137 this->template accessComponent < States > ( ).remove ( state );
138 }
139
146 return this->template accessComponent < FinalStates > ( ).get ( );
147 }
148
155 return std::move ( this->template accessComponent < FinalStates > ( ).get ( ) );
156 }
157
165 bool addFinalState ( StateType state ) {
166 return this->template accessComponent < FinalStates > ( ).add ( std::move ( state ) );
167 }
168
175 this->template accessComponent < FinalStates > ( ).set ( std::move ( states ) );
176 }
177
185 void removeFinalState ( const StateType & state ) {
186 this->template accessComponent < FinalStates > ( ).remove ( state );
187 }
188
195 return this->template accessComponent < InputAlphabet > ( ).get ( );
196 }
197
204 return std::move ( this->template accessComponent < InputAlphabet > ( ).get ( ) );
205 }
206
214 bool addInputSymbol ( SymbolType symbol ) {
215 return this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbol ) );
216 }
217
224 this->template accessComponent < InputAlphabet > ( ).add ( std::move ( symbols ) );
225 }
226
233 this->template accessComponent < InputAlphabet > ( ).set ( std::move ( symbols ) );
234 }
235
243 void removeInputSymbol ( const SymbolType & symbol ) {
244 this->template accessComponent < InputAlphabet > ( ).remove ( symbol );
245 }
246
261
262 bool addTransition ( SymbolType lhs, StateType rhs );
263
265
278
285 return transitions;
286 }
287
294 return std::move ( transitions );
295 }
296
302
303 for ( const auto & transition : getTransitions ( ) ) {
304 if ( transition.second == q )
305 res.insert ( std::make_pair ( transition.first, q ) );
306 }
307
308 return res;
309 }
310
316
317 for ( const auto & transition : getTransitions ( ) ) {
318 if ( transition.first.template is < ext::pair < StateType, StateType > > ( ) && transition.first.template get < ext::pair < StateType, StateType > > ( ).second == q )
319 res.insert ( transition );
320 }
321
322 return res;
323 }
324
333 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) <=> std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
334 }
335
344 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) == std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
345 }
346
356 return out << "(ArcFactoredDeterministicZAutomaton "
357 << " states = " << instance.getStates ( )
358 << " inputAlphabet = " << instance.getInputAlphabet ( )
359 << " finalStates = " << instance.getFinalStates ( )
360 << " transitions = " << instance.getTransitions ( )
361 << ")";
362 }
363};
364
370template < class T >
371class isAFDZA_impl : public std::false_type {};
372
381template < class SymbolType, class StateType >
382class isAFDZA_impl < ArcFactoredDeterministicZAutomaton < SymbolType, StateType > > : public std::true_type {};
383
389template < class T >
390constexpr bool isAFDZA = isAFDZA_impl < T > { };
391
392template<class SymbolType, class StateType >
393ArcFactoredDeterministicZAutomaton < SymbolType, StateType >::ArcFactoredDeterministicZAutomaton ( ext::set < StateType > states, ext::set < SymbolType > inputAlphabet, ext::set < StateType > finalStates ) : core::Components < ArcFactoredDeterministicZAutomaton, ext::set < SymbolType >, component::Set, InputAlphabet, ext::set < StateType >, component::Set, std::tuple < States, FinalStates > > ( std::move ( inputAlphabet ), std::move ( states ), std::move ( finalStates ) ) {
394}
395
396template<class SymbolType, class StateType >
398}
399
400template<class SymbolType, class StateType >
402 if ( lhs.template is < SymbolType > ( ) && ! getInputAlphabet ( ).count ( lhs.template get < SymbolType > ( ) ) )
403 throw AutomatonException("Input symbol \"" + ext::to_string ( lhs.template get < SymbolType > ( ) ) + "\" doesn't exist.");
404
405 if ( lhs.template is < ext::pair < StateType, StateType > > ( ) ) {
406 const ext::pair < StateType, StateType > & stateLhs = lhs.template get < ext::pair < StateType, StateType > > ( );
407 if ( ! getStates ( ).count ( stateLhs.first ) )
408 throw AutomatonException("State \"" + ext::to_string ( stateLhs.first ) + "\" doesn't exist.");
409 if ( ! getStates ( ).count ( stateLhs.second ) )
410 throw AutomatonException("State \"" + ext::to_string ( stateLhs.second ) + "\" doesn't exist.");
411 }
412
413 if (! getStates().count(rhs))
414 throw AutomatonException("State \"" + ext::to_string ( rhs ) + "\" doesn't exist.");
415
416 if ( transitions.find ( lhs ) != transitions.end ( ) ) {
417 if ( transitions.find ( lhs )->second == rhs )
418 return false;
419 else
420 throw AutomatonException("Transition already exists");
421 }
422
423 transitions.insert ( std::move ( lhs ), std::move ( rhs ) );
424 return true;
425}
426
427template<class SymbolType, class StateType >
429 return addTransition ( ext::variant < SymbolType, ext::pair < StateType, StateType > > ( std::move ( lhs ) ), std::move ( rhs ) );
430}
431
432template<class SymbolType, class StateType >
434 return addTransition ( ext::variant < SymbolType, ext::pair < StateType, StateType > > ( std::move ( lhs ) ), std::move ( rhs ) );
435}
436
437template<class SymbolType, class StateType >
439 if ( transitions.find ( lhs ) == transitions.end ( ) )
440 return false;
441
442 if ( transitions.find ( lhs )->second != next )
443 throw AutomatonException("Transition does not exist");
444
445 transitions.erase ( lhs );
446 return true;
447}
448
449} /* namespace automaton */
450
451namespace core {
452
459template<class SymbolType, class StateType >
460class SetConstraint< automaton::ArcFactoredDeterministicZAutomaton < SymbolType, StateType >, SymbolType, automaton::InputAlphabet > {
461public:
471 for ( const std::pair < const ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > & t : automaton.getTransitions ( ) )
472 if ( t.first == symbol )
473 return true;
474
475 return false;
476 }
477
487 return true;
488 }
489
497 if ( automaton.template accessComponent < automaton::States > ( ).get ( ).count ( ext::poly_comp ( symbol ) ) )
498 throw automaton::AutomatonException ( "Symbol " + ext::to_string ( symbol ) + " cannot be in the input alphabet since it is already in the states set." );
499 }
500};
501
508template<class SymbolType, class StateType >
509class SetConstraint< automaton::ArcFactoredDeterministicZAutomaton < SymbolType, StateType >, StateType, automaton::States > {
510public:
520 if ( automaton.getFinalStates ( ).count ( state ) )
521 return true;
522
523 for ( const std::pair < const ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > & t : automaton.getTransitions ( ) )
524 if ( t.first.template is < ext::pair < StateType, StateType > > ( ) ) {
525 const ext::pair < StateType, StateType > & lhs = t.first.template get < ext::pair < StateType, StateType > > ( );
526 if ( lhs.first == state || lhs.second == state )
527 return true;
528 }
529
530 return false;
531 }
532
542 return true;
543 }
544
552 if ( automaton.template accessComponent < automaton::InputAlphabet > ( ).get ( ).count ( ext::poly_comp ( state ) ) )
553 throw automaton::AutomatonException ( "State " + ext::to_string ( state ) + " cannot be in the states set since it is already in the input alphabet." );
554 }
555};
556
563template<class SymbolType, class StateType >
564class SetConstraint< automaton::ArcFactoredDeterministicZAutomaton < SymbolType, StateType >, StateType, automaton::FinalStates > {
565public:
575 return false;
576 }
577
587 return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
588 }
589
597 }
598};
599
605template < class SymbolType, class StateType >
606struct normalize < automaton::ArcFactoredDeterministicZAutomaton < SymbolType, StateType > > {
608 ext::set < DefaultSymbolType > alphabet = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getInputAlphabet ( ) );
609 ext::set < DefaultStateType > states = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getStates ( ) );
610 ext::set < DefaultStateType > finalStates = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getFinalStates ( ) );
611
612 automaton::ArcFactoredDeterministicZAutomaton < > res ( std::move ( states ), std::move ( alphabet ), std::move ( finalStates ) );
613
614 for ( std::pair < ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > && transition : ext::make_mover ( std::move ( value ).getTransitions ( ) ) ) {
615 if ( transition.first.template is < SymbolType > ( ) ) {
616 DefaultSymbolType lhs = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( transition.first.template get < SymbolType > ( ) ) );
617 DefaultStateType to = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.second ) );
618
619 res.addTransition ( std::move ( lhs ), std::move ( to ) );
620 } else {
621 ext::pair < StateType, StateType > & lhs = transition.first.template get < ext::pair < StateType, StateType > > ( );
623 DefaultStateType to = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.second ) );
624
625 res.addTransition ( std::move ( lshDefault ), std::move ( to ) );
626 }
627 }
628
629 return res;
630 }
631};
632
633} /* namespace core */
634
static ext::set< DefaultSymbolType > normalizeAlphabet(ext::set< SymbolType > &&symbols)
Definition: SymbolNormalize.h:50
static DefaultSymbolType normalizeSymbol(SymbolType &&symbol)
Definition: SymbolNormalize.h:68
Deterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree la...
Definition: ArcFactoredDeterministicZAutomaton.h:65
ext::set< StateType > && getStates() &&
Definition: ArcFactoredDeterministicZAutomaton.h:105
const ext::set< StateType > & getFinalStates() const &
Definition: ArcFactoredDeterministicZAutomaton.h:145
StateTypeT StateType
Definition: ArcFactoredDeterministicZAutomaton.h:68
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: ArcFactoredDeterministicZAutomaton.h:232
void addInputSymbols(ext::set< SymbolType > symbols)
Definition: ArcFactoredDeterministicZAutomaton.h:223
SymbolTypeT SymbolType
Definition: ArcFactoredDeterministicZAutomaton.h:67
void removeInputSymbol(const SymbolType &symbol)
Definition: ArcFactoredDeterministicZAutomaton.h:243
auto operator<=>(const ArcFactoredDeterministicZAutomaton &other) const
Definition: ArcFactoredDeterministicZAutomaton.h:332
bool operator==(const ArcFactoredDeterministicZAutomaton &other) const
Definition: ArcFactoredDeterministicZAutomaton.h:343
ext::set< SymbolType > && getInputAlphabet() &&
Definition: ArcFactoredDeterministicZAutomaton.h:203
bool addState(StateType state)
Definition: ArcFactoredDeterministicZAutomaton.h:116
void setStates(ext::set< StateType > states)
Definition: ArcFactoredDeterministicZAutomaton.h:125
ext::map< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > && getTransitions() &&
Definition: ArcFactoredDeterministicZAutomaton.h:293
ext::map< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > getTransitionsFromState(const StateType &q) const
Definition: ArcFactoredDeterministicZAutomaton.h:314
const ext::map< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > & getTransitions() const &
Definition: ArcFactoredDeterministicZAutomaton.h:284
ArcFactoredDeterministicZAutomaton()
Creates a new instance of the automaton.
Definition: ArcFactoredDeterministicZAutomaton.h:397
bool addFinalState(StateType state)
Definition: ArcFactoredDeterministicZAutomaton.h:165
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ArcFactoredDeterministicZAutomaton.h:194
bool addInputSymbol(SymbolType symbol)
Definition: ArcFactoredDeterministicZAutomaton.h:214
void removeFinalState(const StateType &state)
Definition: ArcFactoredDeterministicZAutomaton.h:185
const ext::set< StateType > & getStates() const &
Definition: ArcFactoredDeterministicZAutomaton.h:96
friend ext::ostream & operator<<(ext::ostream &out, const ArcFactoredDeterministicZAutomaton &instance)
Definition: ArcFactoredDeterministicZAutomaton.h:355
void removeState(const StateType &state)
Definition: ArcFactoredDeterministicZAutomaton.h:136
ext::set< StateType > && getFinalStates() &&
Definition: ArcFactoredDeterministicZAutomaton.h:154
void setFinalStates(ext::set< StateType > states)
Definition: ArcFactoredDeterministicZAutomaton.h:174
bool removeTransition(const ext::variant< SymbolType, ext::pair< StateType, StateType > > &lhs, const StateType &next)
Removes a transition from the automaton.
Definition: ArcFactoredDeterministicZAutomaton.h:438
ext::map< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > getTransitionsToState(const StateType &q) const
Definition: ArcFactoredDeterministicZAutomaton.h:300
bool addTransition(ext::variant< SymbolType, ext::pair< StateType, StateType > > lhs, StateType rhs)
Add a transition to the automaton.
Definition: ArcFactoredDeterministicZAutomaton.h:401
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
Definition: ArcFactoredDeterministicZAutomaton.h:371
Definition: components.hpp:181
static bool available(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &, const StateType &)
Definition: ArcFactoredDeterministicZAutomaton.h:541
static void valid(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &automaton, const StateType &state)
Definition: ArcFactoredDeterministicZAutomaton.h:551
static bool used(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &automaton, const StateType &state)
Definition: ArcFactoredDeterministicZAutomaton.h:519
static bool available(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &automaton, const StateType &state)
Definition: ArcFactoredDeterministicZAutomaton.h:586
static bool used(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &, const StateType &)
Definition: ArcFactoredDeterministicZAutomaton.h:574
static void valid(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &, const StateType &)
Definition: ArcFactoredDeterministicZAutomaton.h:596
static bool used(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: ArcFactoredDeterministicZAutomaton.h:470
static void valid(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: ArcFactoredDeterministicZAutomaton.h:496
static bool available(const automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &, const SymbolType &)
Definition: ArcFactoredDeterministicZAutomaton.h:486
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
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
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 isAFDZA
Definition: ArcFactoredDeterministicZAutomaton.h:390
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
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
PolyComp< T > poly_comp(const T &inst)
Definition: functional.hpp:60
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::ArcFactoredDeterministicZAutomaton< > eval(automaton::ArcFactoredDeterministicZAutomaton< SymbolType, StateType > &&value)
Definition: ArcFactoredDeterministicZAutomaton.h:607
Definition: normalize.hpp:13