Skip to content
Snippets Groups Projects
Commit bdb0e903 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

Split automata properties and algorithms

parent 4dada919
No related branches found
No related tags found
No related merge requests found
/*
* AutomatonPropertiesFSM.cpp
*
* Created on: 23. 3. 2014
* Author: Tomas Pecka
*/
#include "AutomatonPropertiesFSM.h"
#include <exception/AlibException.h>
#include <automaton/FSM/ExtendedNFA.h>
#include <automaton/FSM/CompactNFA.h>
#include <automaton/FSM/EpsilonNFA.h>
#include <automaton/FSM/NFA.h>
#include <automaton/FSM/DFA.h>
namespace automaton {
template<class T>
std::set<automaton::State> AutomatonPropertiesFSM::getUsefullStates( const T & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ) = fsm.getFinalStates( );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) ); // copy Qi-1 into Qi
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & t : fsm.getTransitionsToState( p ) )
Qi.at( i ).insert( t.first.first );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
return Qi.at( i );
}
template std::set<automaton::State> AutomatonPropertiesFSM::getUsefullStates( const automaton::EpsilonNFA & fsm );
template std::set<automaton::State> AutomatonPropertiesFSM::getUsefullStates( const automaton::NFA & fsm );
template std::set<automaton::State> AutomatonPropertiesFSM::getUsefullStates( const automaton::CompactNFA & fsm );
template std::set<automaton::State> AutomatonPropertiesFSM::getUsefullStates( const automaton::ExtendedNFA & fsm );
template<>
std::set<automaton::State> AutomatonPropertiesFSM::getUsefullStates( const automaton::DFA & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ) = fsm.getFinalStates( );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) ); // copy Qi-1 into Qi
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & t : fsm.getTransitionsToState( p ) )
Qi.at( i ).insert( t.first.first );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
return Qi.at( i );
}
template<class T>
std::set<automaton::State> AutomatonPropertiesFSM::getUnreachableStates( const T & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ) = fsm.getInitialStates( );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) );
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & transition : fsm.getTransitionsFromState( p ) )
Qi.at( i ).insert( transition.second.begin(), transition.second.end() );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
return Qi.at( i );
}
template std::set<automaton::State> AutomatonPropertiesFSM::getUnreachableStates( const automaton::EpsilonNFA & fsm );
template std::set<automaton::State> AutomatonPropertiesFSM::getUnreachableStates( const automaton::NFA & fsm );
template std::set<automaton::State> AutomatonPropertiesFSM::getUnreachableStates( const automaton::CompactNFA & fsm );
template std::set<automaton::State> AutomatonPropertiesFSM::getUnreachableStates( const automaton::ExtendedNFA & fsm );
template<>
std::set<automaton::State> AutomatonPropertiesFSM::getUnreachableStates( const automaton::DFA & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ). insert( fsm.getInitialState( ) );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) );
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & transition : fsm.getTransitionsFromState( p ) )
Qi.at( i ).insert( transition.second );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
return Qi.at( i );
}
}
/*
* AutomatonPropertiesFSM.h
*
* Created on: 23. 3. 2014
* Author: Tomas Pecka
*/
#ifndef AUTOMATON_PROPERTIED_FSM_H_
#define AUTOMATON_PROPERTIED_FSM_H_
#include <algorithm>
#include <deque>
#include <set>
#include <automaton/common/State.h>
namespace automaton {
class AutomatonPropertiesFSM {
public:
/**
* Removes dead states from FSM. Melichar 2.32
*/
template<class T>
static std::set<automaton::State> getUsefullStates( const T & fsm );
/**
* Removes dead states from FSM. Melichar 2.29
*/
template<class T>
static std::set<automaton::State> getUnreachableStates( const T & fsm );
};
}
#endif /* AUTOMATON_PROPERTIED_FSM_H_ */
......@@ -14,30 +14,14 @@
#include <automaton/FSM/NFA.h>
#include <automaton/FSM/DFA.h>
 
#include "../../automaton/AutomatonPropertiesFSM.h"
namespace trim {
 
template<class T>
T TrimFSM::removeUselessStates( const T & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ) = fsm.getFinalStates( );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) ); // copy Qi-1 into Qi
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & t : fsm.getTransitionsToState( p ) )
Qi.at( i ).insert( t.first.first );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
const std::set<automaton::State>& Qu = Qi.at( i );
// 1.
std::set<automaton::State> Qu = automaton::AutomatonPropertiesFSM::getUsefullStates( fsm );
 
// 2.
T M;
......@@ -69,26 +53,8 @@ template automaton::ExtendedNFA TrimFSM::removeUselessStates( const automaton::E
 
template<>
automaton::DFA TrimFSM::removeUselessStates( const automaton::DFA & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ) = fsm.getFinalStates( );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) ); // copy Qi-1 into Qi
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & t : fsm.getTransitionsToState( p ) )
Qi.at( i ).insert( t.first.first );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
const std::set<automaton::State>& Qu = Qi.at( i );
// 1.
std::set<automaton::State> Qu = automaton::AutomatonPropertiesFSM::getUsefullStates( fsm );
 
// 2.
automaton::DFA M ( fsm.getInitialState () );
......@@ -112,27 +78,7 @@ automaton::DFA TrimFSM::removeUselessStates( const automaton::DFA & fsm ) {
template<class T>
T TrimFSM::removeUnreachableStates( const T & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ) = fsm.getInitialStates( );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) );
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & transition : fsm.getTransitionsFromState( p ) )
Qi.at( i ).insert( transition.second.begin(), transition.second.end() );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
const std::set<automaton::State> Qa = Qi.at( i );
std::set<automaton::State> Qa = automaton::AutomatonPropertiesFSM::getUnreachableStates( fsm );
 
// 2
T M;
......@@ -167,27 +113,7 @@ template automaton::ExtendedNFA TrimFSM::removeUnreachableStates( const automato
template<>
automaton::DFA TrimFSM::removeUnreachableStates( const automaton::DFA & fsm ) {
// 1a
std::deque<std::set<automaton::State>> Qi;
Qi.push_back( std::set<automaton::State>( ) );
Qi.at( 0 ). insert( fsm.getInitialState( ) );
int i = 1;
// 1bc
while( true ) {
Qi.push_back( Qi.at( i - 1 ) );
for( const auto & p : Qi.at( i - 1 ) )
for( const auto & transition : fsm.getTransitionsFromState( p ) )
Qi.at( i ).insert( transition.second );
if( Qi.at( i ) == Qi.at( i - 1 ) )
break;
i = i + 1;
}
const std::set<automaton::State> Qa = Qi.at( i );
std::set<automaton::State> Qa = automaton::AutomatonPropertiesFSM::getUnreachableStates( fsm );
 
// 2
automaton::DFA M(fsm.getInitialState() );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment