Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ArcFactoredNondeterministicZAutomaton.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/multimap>
29#include <alib/set>
30#include <alib/vector>
31
32#include <core/components.hpp>
33
36
38
39#include <core/normalize.hpp>
42
44
45namespace automaton {
46
47class InputAlphabet;
48class States;
49class FinalStates;
50
66template < class SymbolTypeT = DefaultSymbolType, class StateTypeT = DefaultStateType >
67class ArcFactoredNondeterministicZAutomaton final : public core::Components < ArcFactoredNondeterministicZAutomaton < SymbolTypeT, StateTypeT >, ext::set < SymbolTypeT >, component::Set, InputAlphabet, ext::set < StateTypeT >, component::Set, std::tuple < States, FinalStates > > {
68public:
69 typedef SymbolTypeT SymbolType;
70 typedef StateTypeT StateType;
71
72private:
77
78public:
83
92
93 /*
94 * \brief Creates a new instance of the automaton based on the Deterministic finite tree automaton.
95 *
96 * \param other the Deterministic finite tree automaton
97 */
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
223 bool addInputSymbol ( SymbolType symbol ) {
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
252 void removeInputSymbol ( const SymbolType & symbol ) {
253 this->template accessComponent < InputAlphabet > ( ).remove ( symbol );
254 }
255
270
271 bool addTransition ( SymbolType lhs, StateType rhs );
272
274
287
294 return transitions;
295 }
296
303 return std::move ( transitions );
304 }
305
311
312 for ( const auto & transition : getTransitions ( ) ) {
313 if ( transition.second == q )
314 res.insert ( std::make_pair ( transition.first, q ) );
315 }
316
317 return res;
318 }
319
325
326 for ( const auto & transition : getTransitions ( ) ) {
327 if ( transition.first.template is < ext::pair < StateType, StateType > > ( ) && transition.first.template get < ext::pair < StateType, StateType > > ( ).second == q )
328 res.insert ( transition );
329 }
330
331 return res;
332 }
333
342 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) <=> std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
343 }
344
353 return std::tie(getStates(), getInputAlphabet(), getFinalStates(), transitions) == std::tie(other.getStates(), other.getInputAlphabet(), other.getFinalStates(), other.transitions);
354 }
355
365 return out << "(ArcFactoredNondeterministicZAutomaton "
366 << " states = " << instance.getStates ( )
367 << " inputAlphabet = " << instance.getInputAlphabet ( )
368 << " finalStates = " << instance.getFinalStates ( )
369 << " transitions = " << instance.getTransitions ( )
370 << ")";
371 }
372};
373
379template < class T >
380class isAFNZA_impl : public std::false_type {};
381
390template < class SymbolType, class StateType >
392
398template < class T >
399constexpr bool isAFNZA = isAFNZA_impl < T > { };
400
401template<class SymbolType, class StateType >
402ArcFactoredNondeterministicZAutomaton < SymbolType, StateType >::ArcFactoredNondeterministicZAutomaton ( ext::set < StateType > states, ext::set < SymbolType > inputAlphabet, ext::set < StateType > finalStates ) : core::Components < ArcFactoredNondeterministicZAutomaton, ext::set < SymbolType >, component::Set, InputAlphabet, ext::set < StateType >, component::Set, std::tuple < States, FinalStates > > ( std::move ( inputAlphabet ), std::move ( states ), std::move ( finalStates ) ) {
403}
404
405template<class SymbolType, class StateType >
407}
408
409template < class SymbolType, class StateType >
411 transitions.insert ( other.getTransitions ( ).begin ( ), other.getTransitions ( ).end ( ) );
412}
413
414template<class SymbolType, class StateType >
416 if ( lhs.template is < SymbolType > ( ) && ! getInputAlphabet ( ).count ( lhs.template get < SymbolType > ( ) ) )
417 throw AutomatonException("Input symbol \"" + ext::to_string ( lhs.template get < SymbolType > ( ) ) + "\" doesn't exist.");
418
419 if ( lhs.template is < ext::pair < StateType, StateType > > ( ) ) {
420 const ext::pair < StateType, StateType > & stateLhs = lhs.template get < ext::pair < StateType, StateType > > ( );
421 if ( ! getStates ( ).count ( stateLhs.first ) )
422 throw AutomatonException("State \"" + ext::to_string ( stateLhs.first ) + "\" doesn't exist.");
423 if ( ! getStates ( ).count ( stateLhs.second ) )
424 throw AutomatonException("State \"" + ext::to_string ( stateLhs.second ) + "\" doesn't exist.");
425 }
426
427 if (! getStates().count(rhs))
428 throw AutomatonException("State \"" + ext::to_string ( rhs ) + "\" doesn't exist.");
429
430 auto upper_bound = transitions.upper_bound ( lhs );
431 auto lower_bound = transitions.lower_bound ( lhs );
432 auto iter = std::lower_bound ( lower_bound, upper_bound, rhs, [ ] ( const auto & transition, const auto & target ) { return transition.second < target; } );
433 if ( iter != upper_bound && rhs >= iter->second )
434 return false;
435
436 transitions.insert ( iter, std::make_pair ( std::move ( lhs ), std::move ( rhs ) ) );
437 return true;
438}
439
440template<class SymbolType, class StateType >
442 return addTransition ( ext::variant < SymbolType, ext::pair < StateType, StateType > > ( std::move ( lhs ) ), std::move ( rhs ) );
443}
444
445template<class SymbolType, class StateType >
447 return addTransition ( ext::variant < SymbolType, ext::pair < StateType, StateType > > ( std::move ( lhs ) ), std::move ( rhs ) );
448}
449
450template<class SymbolType, class StateType >
452 auto upper_bound = transitions.upper_bound ( lhs );
453 auto lower_bound = transitions.lower_bound ( lhs );
454 auto iter = std::find_if ( lower_bound, upper_bound, [ & ] ( const auto & transition ) { return transition.second == next; } );
455 if ( iter == upper_bound )
456 return false;
457
458 transitions.erase ( iter );
459 return true;
460}
461
462} /* namespace automaton */
463
464namespace core {
465
472template<class SymbolType, class StateType >
473class SetConstraint< automaton::ArcFactoredNondeterministicZAutomaton < SymbolType, StateType >, SymbolType, automaton::InputAlphabet > {
474public:
484 for ( const std::pair < const ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > & t : automaton.getTransitions ( ) )
485 if ( t.first == symbol )
486 return true;
487
488 return false;
489 }
490
500 return true;
501 }
502
510 if ( automaton.template accessComponent < automaton::States > ( ).get ( ).count ( ext::poly_comp ( symbol ) ) )
511 throw automaton::AutomatonException ( "Symbol " + ext::to_string ( symbol ) + " cannot be in the input alphabet since it is already in the states set." );
512 }
513};
514
521template<class SymbolType, class StateType >
522class SetConstraint< automaton::ArcFactoredNondeterministicZAutomaton < SymbolType, StateType >, StateType, automaton::States > {
523public:
533 if ( automaton.getFinalStates ( ).count ( state ) )
534 return true;
535
536 for ( const std::pair < const ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > & t : automaton.getTransitions ( ) )
537 if ( t.first.template is < ext::pair < StateType, StateType > > ( ) ) {
538 const ext::pair < StateType, StateType > & lhs = t.first.template get < ext::pair < StateType, StateType > > ( );
539 if ( lhs.first == state || lhs.second == state )
540 return true;
541 }
542
543 return false;
544 }
545
555 return true;
556 }
557
565 if ( automaton.template accessComponent < automaton::InputAlphabet > ( ).get ( ).count ( ext::poly_comp ( state ) ) )
566 throw automaton::AutomatonException ( "State " + ext::to_string ( state ) + " cannot be in the states set since it is already in the input alphabet." );
567 }
568};
569
576template<class SymbolType, class StateType >
577class SetConstraint< automaton::ArcFactoredNondeterministicZAutomaton < SymbolType, StateType >, StateType, automaton::FinalStates > {
578public:
588 return false;
589 }
590
600 return automaton.template accessComponent < automaton::States > ( ).get ( ).count ( state );
601 }
602
610 }
611};
612
618template < class SymbolType, class StateType >
619struct normalize < automaton::ArcFactoredNondeterministicZAutomaton < SymbolType, StateType > > {
621 ext::set < DefaultSymbolType > alphabet = alphabet::SymbolNormalize::normalizeAlphabet ( std::move ( value ).getInputAlphabet ( ) );
622 ext::set < DefaultStateType > states = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getStates ( ) );
623 ext::set < DefaultStateType > finalStates = automaton::AutomatonNormalize::normalizeStates ( std::move ( value ).getFinalStates ( ) );
624
625 automaton::ArcFactoredNondeterministicZAutomaton < > res ( std::move ( states ), std::move ( alphabet ), std::move ( finalStates ) );
626
627 for ( std::pair < ext::variant < SymbolType, ext::pair < StateType, StateType > >, StateType > && transition : ext::make_mover ( std::move ( value ).getTransitions ( ) ) ) {
628 if ( transition.first.template is < SymbolType > ( ) ) {
629 DefaultSymbolType lhs = alphabet::SymbolNormalize::normalizeSymbol ( std::move ( transition.first.template get < SymbolType > ( ) ) );
630 DefaultStateType to = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.second ) );
631
632 res.addTransition ( std::move ( lhs ), std::move ( to ) );
633 } else {
634 ext::pair < StateType, StateType > & lhs = transition.first.template get < ext::pair < StateType, StateType > > ( );
636 DefaultStateType to = automaton::AutomatonNormalize::normalizeState ( std::move ( transition.second ) );
637
638 res.addTransition ( std::move ( lshDefault ), std::move ( to ) );
639 }
640 }
641
642 return res;
643 }
644};
645
646} /* namespace core */
647
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
Nondeterministic Z-Automaton in Arc-Factored Normal Form. Computation model for unranked regular tree...
Definition: ArcFactoredNondeterministicZAutomaton.h:67
const ext::set< SymbolType > & getInputAlphabet() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:203
ext::set< StateType > && getFinalStates() &&
Definition: ArcFactoredNondeterministicZAutomaton.h:163
void setInputAlphabet(ext::set< SymbolType > symbols)
Definition: ArcFactoredNondeterministicZAutomaton.h:241
void setStates(ext::set< StateType > states)
Definition: ArcFactoredNondeterministicZAutomaton.h:134
const ext::set< StateType > & getFinalStates() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:154
SymbolTypeT SymbolType
Definition: ArcFactoredNondeterministicZAutomaton.h:69
auto operator<=>(const ArcFactoredNondeterministicZAutomaton &other) const
Definition: ArcFactoredNondeterministicZAutomaton.h:341
void removeState(const StateType &state)
Definition: ArcFactoredNondeterministicZAutomaton.h:145
bool addInputSymbol(SymbolType symbol)
Definition: ArcFactoredNondeterministicZAutomaton.h:223
ext::multimap< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > && getTransitions() &&
Definition: ArcFactoredNondeterministicZAutomaton.h:302
bool removeTransition(const ext::variant< SymbolType, ext::pair< StateType, StateType > > &lhs, const StateType &next)
Removes a transition from the automaton.
Definition: ArcFactoredNondeterministicZAutomaton.h:451
void removeFinalState(const StateType &state)
Definition: ArcFactoredNondeterministicZAutomaton.h:194
void setFinalStates(ext::set< StateType > states)
Definition: ArcFactoredNondeterministicZAutomaton.h:183
void addInputSymbols(ext::set< SymbolType > symbols)
Definition: ArcFactoredNondeterministicZAutomaton.h:232
bool operator==(const ArcFactoredNondeterministicZAutomaton &other) const
Definition: ArcFactoredNondeterministicZAutomaton.h:352
ext::multimap< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > getTransitionsToState(const StateType &q) const
Definition: ArcFactoredNondeterministicZAutomaton.h:309
friend ext::ostream & operator<<(ext::ostream &out, const ArcFactoredNondeterministicZAutomaton &instance)
Definition: ArcFactoredNondeterministicZAutomaton.h:364
ArcFactoredNondeterministicZAutomaton()
Creates a new instance of the automaton.
Definition: ArcFactoredNondeterministicZAutomaton.h:406
const ext::multimap< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > & getTransitions() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:293
bool addState(StateType state)
Definition: ArcFactoredNondeterministicZAutomaton.h:125
void removeInputSymbol(const SymbolType &symbol)
Definition: ArcFactoredNondeterministicZAutomaton.h:252
ext::set< StateType > && getStates() &&
Definition: ArcFactoredNondeterministicZAutomaton.h:114
StateTypeT StateType
Definition: ArcFactoredNondeterministicZAutomaton.h:70
ext::set< SymbolType > && getInputAlphabet() &&
Definition: ArcFactoredNondeterministicZAutomaton.h:212
bool addFinalState(StateType state)
Definition: ArcFactoredNondeterministicZAutomaton.h:174
const ext::set< StateType > & getStates() const &
Definition: ArcFactoredNondeterministicZAutomaton.h:105
bool addTransition(ext::variant< SymbolType, ext::pair< StateType, StateType > > lhs, StateType rhs)
Add a transition to the automaton.
Definition: ArcFactoredNondeterministicZAutomaton.h:415
ext::multimap< ext::variant< SymbolType, ext::pair< StateType, StateType > >, StateType > getTransitionsFromState(const StateType &q) const
Definition: ArcFactoredNondeterministicZAutomaton.h:323
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: ArcFactoredNondeterministicZAutomaton.h:380
Definition: components.hpp:181
static void valid(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &automaton, const StateType &state)
Definition: ArcFactoredNondeterministicZAutomaton.h:564
static bool used(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &automaton, const StateType &state)
Definition: ArcFactoredNondeterministicZAutomaton.h:532
static bool available(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &, const StateType &)
Definition: ArcFactoredNondeterministicZAutomaton.h:554
static bool used(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: ArcFactoredNondeterministicZAutomaton.h:483
static bool available(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &, const SymbolType &)
Definition: ArcFactoredNondeterministicZAutomaton.h:499
static void valid(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &automaton, const SymbolType &symbol)
Definition: ArcFactoredNondeterministicZAutomaton.h:509
static bool used(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &, const StateType &)
Definition: ArcFactoredNondeterministicZAutomaton.h:587
static bool available(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &automaton, const StateType &state)
Definition: ArcFactoredNondeterministicZAutomaton.h:599
static void valid(const automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &, const StateType &)
Definition: ArcFactoredNondeterministicZAutomaton.h:609
Definition: setComponents.hpp:26
Class extending the multimap class from the standard library. Original reason is to allow printing of...
Definition: multimap.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 isAFNZA
Definition: ArcFactoredNondeterministicZAutomaton.h:399
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::ArcFactoredNondeterministicZAutomaton< > eval(automaton::ArcFactoredNondeterministicZAutomaton< SymbolType, StateType > &&value)
Definition: ArcFactoredNondeterministicZAutomaton.h:620
Definition: normalize.hpp:13