From f3a9045469cf9989a596f63ef1b7b54e10956acc Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Sun, 25 Oct 2015 21:19:49 +0100 Subject: [PATCH] run RHDPDA --- alib2algo/src/automaton/run/Accept.cpp | 9 +++ alib2algo/src/automaton/run/Accept.h | 1 + alib2algo/src/automaton/run/Occurrences.cpp | 9 +++ alib2algo/src/automaton/run/Occurrences.h | 1 + alib2algo/src/automaton/run/Result.cpp | 11 +++ alib2algo/src/automaton/run/Result.h | 1 + alib2algo/src/automaton/run/Run.cpp | 83 ++++++++++++++++++++- alib2algo/src/automaton/run/Run.h | 1 + 8 files changed, 113 insertions(+), 3 deletions(-) diff --git a/alib2algo/src/automaton/run/Accept.cpp b/alib2algo/src/automaton/run/Accept.cpp index af6d1a08d6..618dd93ef1 100644 --- a/alib2algo/src/automaton/run/Accept.cpp +++ b/alib2algo/src/automaton/run/Accept.cpp @@ -14,6 +14,7 @@ #include <automaton/TA/NFTA.h> #include <automaton/PDA/InputDrivenDPDA.h> #include <automaton/PDA/VisiblyPushdownDPDA.h> +#include <automaton/PDA/RealTimeHeightDeterministicDPDA.h> #include <automaton/PDA/DPDA.h> #include <deque> @@ -87,6 +88,14 @@ bool Accept::accept ( const automaton::VisiblyPushdownDPDA & automaton, const st auto AcceptVisiblyPushdownDPDALinearString = Accept::RegistratorWrapper < bool, automaton::VisiblyPushdownDPDA, string::LinearString > ( Accept::getInstance ( ), Accept::accept ); +bool Accept::accept ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); + + return std::get < 0 > ( res ) && automaton.getFinalStates ( ).count ( std::get < 1 > ( res ) ); +} + +auto AcceptRealTimeHeightDeterministicDPDALinearString = Accept::RegistratorWrapper < bool, automaton::RealTimeHeightDeterministicDPDA, string::LinearString > ( Accept::getInstance ( ), Accept::accept ); + bool Accept::accept ( const automaton::DPDA & automaton, const string::LinearString & string ) { std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); diff --git a/alib2algo/src/automaton/run/Accept.h b/alib2algo/src/automaton/run/Accept.h index c3a7877115..fab2a68803 100644 --- a/alib2algo/src/automaton/run/Accept.h +++ b/alib2algo/src/automaton/run/Accept.h @@ -34,6 +34,7 @@ public: static bool accept ( const automaton::NFTA & automaton, const tree::RankedTree & tree ); static bool accept ( const automaton::InputDrivenDPDA & automaton, const string::LinearString & string ); static bool accept ( const automaton::VisiblyPushdownDPDA & automaton, const string::LinearString & string ); + static bool accept ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ); static bool accept ( const automaton::DPDA & automaton, const string::LinearString & string ); public: diff --git a/alib2algo/src/automaton/run/Occurrences.cpp b/alib2algo/src/automaton/run/Occurrences.cpp index 7080c357b0..1530f533a1 100644 --- a/alib2algo/src/automaton/run/Occurrences.cpp +++ b/alib2algo/src/automaton/run/Occurrences.cpp @@ -13,6 +13,7 @@ #include <automaton/TA/DFTA.h> #include <automaton/PDA/InputDrivenDPDA.h> #include <automaton/PDA/VisiblyPushdownDPDA.h> +#include <automaton/PDA/RealTimeHeightDeterministicDPDA.h> #include <automaton/PDA/DPDA.h> #include <deque> @@ -65,6 +66,14 @@ std::set < unsigned > Occurrences::occurrences ( const automaton::VisiblyPushdow auto OccurrencesVisiblyPushdownDPDALinearString = Occurrences::RegistratorWrapper < std::set < unsigned >, automaton::VisiblyPushdownDPDA, string::LinearString > ( Occurrences::getInstance ( ), Occurrences::occurrences ); +std::set < unsigned > Occurrences::occurrences ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); + + return std::get < 2 > ( res ); +} + +auto OccurrencesRealTimeHeightDeterministicDPDALinearString = Occurrences::RegistratorWrapper < std::set < unsigned >, automaton::RealTimeHeightDeterministicDPDA, string::LinearString > ( Occurrences::getInstance ( ), Occurrences::occurrences ); + std::set < unsigned > Occurrences::occurrences ( const automaton::DPDA & automaton, const string::LinearString & string ) { std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); diff --git a/alib2algo/src/automaton/run/Occurrences.h b/alib2algo/src/automaton/run/Occurrences.h index 5ed8a1a157..ef9f39c572 100644 --- a/alib2algo/src/automaton/run/Occurrences.h +++ b/alib2algo/src/automaton/run/Occurrences.h @@ -32,6 +32,7 @@ public: static std::set < unsigned > occurrences ( const automaton::DFTA & automaton, const tree::RankedTree & tree ); static std::set < unsigned > occurrences ( const automaton::InputDrivenDPDA & automaton, const string::LinearString & string ); static std::set < unsigned > occurrences ( const automaton::VisiblyPushdownDPDA & automaton, const string::LinearString & string ); + static std::set < unsigned > occurrences ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ); static std::set < unsigned > occurrences ( const automaton::DPDA & automaton, const string::LinearString & string ); public: diff --git a/alib2algo/src/automaton/run/Result.cpp b/alib2algo/src/automaton/run/Result.cpp index 94c77562ae..52a269eae7 100644 --- a/alib2algo/src/automaton/run/Result.cpp +++ b/alib2algo/src/automaton/run/Result.cpp @@ -12,6 +12,7 @@ #include <automaton/TA/DFTA.h> #include <automaton/PDA/InputDrivenDPDA.h> #include <automaton/PDA/VisiblyPushdownDPDA.h> +#include <automaton/PDA/RealTimeHeightDeterministicDPDA.h> #include <automaton/PDA/DPDA.h> #include <label/LabelSetLabel.h> @@ -65,6 +66,16 @@ label::Label Result::result ( const automaton::VisiblyPushdownDPDA & automaton, auto ResultVisiblyPushdownDPDALinearString = Result::RegistratorWrapper < label::Label, automaton::VisiblyPushdownDPDA, string::LinearString > ( Result::getInstance ( ), Result::result ); +label::Label Result::result ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ) { + std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); + + if ( std::get < 0 > ( res ) ) return std::get < 1 > ( res ).getName ( ); + + return label::labelFrom ( "fail" ); +} + +auto ResultRealTimeHeightDeterministicDPDALinearString = Result::RegistratorWrapper < label::Label, automaton::RealTimeHeightDeterministicDPDA, string::LinearString > ( Result::getInstance ( ), Result::result ); + label::Label Result::result ( const automaton::DPDA & automaton, const string::LinearString & string ) { std::tuple < bool, automaton::State, std::set < unsigned > > res = Run::calculateState ( automaton, string ); diff --git a/alib2algo/src/automaton/run/Result.h b/alib2algo/src/automaton/run/Result.h index d4be690eac..0e973578ed 100644 --- a/alib2algo/src/automaton/run/Result.h +++ b/alib2algo/src/automaton/run/Result.h @@ -31,6 +31,7 @@ public: static label::Label result ( const automaton::DFTA & automaton, const tree::RankedTree & tree ); static label::Label result ( const automaton::InputDrivenDPDA & automaton, const string::LinearString & string ); static label::Label result ( const automaton::VisiblyPushdownDPDA & automaton, const string::LinearString & string ); + static label::Label result ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ); static label::Label result ( const automaton::DPDA & automaton, const string::LinearString & string ); static Result & getInstance ( ) { diff --git a/alib2algo/src/automaton/run/Run.cpp b/alib2algo/src/automaton/run/Run.cpp index 7a20f97d8d..e9ce55a14f 100644 --- a/alib2algo/src/automaton/run/Run.cpp +++ b/alib2algo/src/automaton/run/Run.cpp @@ -13,6 +13,7 @@ #include <automaton/TA/NFTA.h> #include <automaton/PDA/InputDrivenDPDA.h> #include <automaton/PDA/VisiblyPushdownDPDA.h> +#include <automaton/PDA/RealTimeHeightDeterministicDPDA.h> #include <automaton/PDA/DPDA.h> #include <deque> @@ -296,6 +297,78 @@ std::tuple < bool, State, std::set < unsigned > > Run::calculateState ( const au // ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +std::tuple < bool, State, std::set < unsigned > > Run::calculateState ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ) { + automaton::State state = automaton.getInitialState ( ); + std::deque < alphabet::Symbol > pushdownStore { + automaton.getBottomOfTheStackSymbol ( ) + }; + unsigned i = 0; + std::set < unsigned > occ; + bool res = true; + + if ( automaton.getFinalStates ( ).count ( state ) ) + occ.insert ( i ); + + for ( auto symbolIter = string.getContent ( ).begin ( ); symbolIter != string.getContent ( ).end ( ); ) { + + auto callTransition = automaton.getCallTransitions ( ).find ( std::make_pair ( state, * symbolIter ) ); + + if ( callTransition != automaton.getCallTransitions ( ).end ( ) ) { + symbolIter++; + i++; + } else { + callTransition = automaton.getCallTransitions ( ).find ( std::make_pair ( state, string::Epsilon::EPSILON ) ); + } + + if ( callTransition != automaton.getCallTransitions ( ).end ( ) ) { + pushdownStore.push_back ( callTransition->second.second ); + state = callTransition->second.first; + } else { + res = false; + } + + auto returnTransition = automaton.getReturnTransitions ( ).find ( std::make_tuple ( state, * symbolIter, pushdownStore.back ( ) ) ); + + if ( returnTransition != automaton.getReturnTransitions ( ).end ( ) ) { + symbolIter++; + i++; + } else { + returnTransition = automaton.getReturnTransitions ( ).find ( std::make_tuple ( state, string::Epsilon::EPSILON, pushdownStore.back ( ) ) ); + } + + if ( returnTransition != automaton.getReturnTransitions ( ).end ( ) ) { + pushdownStore.pop_back ( ); + state = returnTransition->second; + } else { + res = false; + } + + auto localTransition = automaton.getLocalTransitions ( ).find ( std::make_pair ( state, * symbolIter ) ); + + if ( localTransition != automaton.getLocalTransitions ( ).end ( ) ) { + symbolIter++; + i++; + } else { + localTransition = automaton.getLocalTransitions ( ).find ( std::make_pair ( state, string::Epsilon::EPSILON ) ); + } + + if ( localTransition != automaton.getLocalTransitions ( ).end ( ) ) + state = localTransition->second; + else + res = false; + + if ( res == false ) + break; + + if ( automaton.getFinalStates ( ).count ( state ) ) + occ.insert ( i ); + } + + return std::make_tuple ( res, state, occ ); +} + +// ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + std::tuple < bool, State, std::set < unsigned > > Run::calculateState ( const automaton::DPDA & automaton, const string::LinearString & string ) { automaton::State state = automaton.getInitialState ( ); std::deque < alphabet::Symbol > pushdownStore { @@ -308,12 +381,12 @@ std::tuple < bool, State, std::set < unsigned > > Run::calculateState ( const au if ( automaton.getFinalStates ( ).count ( state ) ) occ.insert ( i ); - for ( const alphabet::Symbol & symbol : string.getContent ( ) ) { + for ( auto symbolIter = string.getContent ( ).begin ( ); symbolIter != string.getContent ( ).end ( ); ) { auto transitions = automaton.getTransitionsFromState ( state ); auto transition = transitions.begin ( ); for ( auto transition = transitions.begin ( ); transition != transitions.end ( ); transition++ ) { - if ( ( std::get < 1 > ( transition->first ) != symbol ) && !std::get < 1 > ( transition->first ).is < string::Epsilon > ( ) ) continue; + if ( ( std::get < 1 > ( transition->first ) != * symbolIter ) && !std::get < 1 > ( transition->first ).is < string::Epsilon > ( ) ) continue; if ( !canPop ( pushdownStore, std::get < 2 > ( transition->first ) ) ) continue; @@ -330,7 +403,11 @@ std::tuple < bool, State, std::set < unsigned > > Run::calculateState ( const au for ( auto iter = transition->second.second.rbegin ( ); iter != transition->second.second.rend ( ); ++iter ) pushdownStore.push_back ( * iter ); state = transition->second.first; - i++; + + if ( !std::get < 1 > ( transition->first ).is < string::Epsilon > ( ) ) { + i++; + symbolIter++; + } if ( automaton.getFinalStates ( ).count ( state ) ) occ.insert ( i ); diff --git a/alib2algo/src/automaton/run/Run.h b/alib2algo/src/automaton/run/Run.h index 8431858b71..6344f4116c 100644 --- a/alib2algo/src/automaton/run/Run.h +++ b/alib2algo/src/automaton/run/Run.h @@ -29,6 +29,7 @@ public: static std::tuple < bool, std::set < State >, std::set < unsigned > > calculateStates ( const automaton::NFTA & automaton, const tree::RankedTree & tree ); static std::tuple < bool, State, std::set < unsigned > > calculateState ( const automaton::InputDrivenDPDA & automaton, const string::LinearString & string ); static std::tuple < bool, State, std::set < unsigned > > calculateState ( const automaton::VisiblyPushdownDPDA & automaton, const string::LinearString & string ); + static std::tuple < bool, State, std::set < unsigned > > calculateState ( const automaton::RealTimeHeightDeterministicDPDA & automaton, const string::LinearString & string ); static std::tuple < bool, State, std::set < unsigned > > calculateState ( const automaton::DPDA & automaton, const string::LinearString & string ); }; -- GitLab