00001 /* 00002 * Name: deterministic_finite_automaton.hh 00003 * Author: Rafael Jesus Alcantara Perez 00004 * Summary: Deterministic finite automaton 00005 * Date: $Date: 2003/04/14 00:18:31 $ 00006 * Revision: $Revision: 1.1 $ 00007 * 00008 * Copyright (C) 1999-2002 Rafael Jesus Alcantara Perez <rafa@dedalo-ing.com> 00009 * 00010 * This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. 00014 * 00015 * This program is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00018 * GNU General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU General Public License 00021 * along with this program; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, 00023 * MA 02111-1307, USA. 00024 */ 00025 00026 #ifndef _MPCL_AUTOMATON_DETERMINISTIC_FINITE_AUTOMATON__ 00027 #define _MPCL_AUTOMATON_DETERMINISTIC_FINITE_AUTOMATON__ 00028 00029 #include <set> 00030 #include "defs.hh" 00031 #include "../util/collection/map.hh" 00032 00033 00035 namespace mpcl 00036 { 00037 00039 namespace automaton 00040 { 00041 00042 using mpcl::util::collection::TMap; 00043 using std::pair; 00044 using std::set; 00045 00049 template <typename TState, typename TEvent> 00050 class TDeterministicFiniteAutomaton 00051 { 00052 00053 protected: 00054 00056 typedef pair<TState, TEvent> TPair; 00057 00059 typedef TMap<TPair, TState> TTransitionMap; 00060 00062 typedef set<TState> TStateSet; 00063 00065 TState tCurrentState; 00066 00068 TStateSet tFinalStateSet; 00069 00071 TState tInitialState; 00072 00074 TTransitionMap tTransitionMap; 00075 00076 00077 public: 00078 00079 // 00080 // C O N S T R U C T O R S 00081 // 00082 00084 TDeterministicFiniteAutomaton (void) : 00085 tCurrentState () , 00086 tFinalStateSet () , 00087 tInitialState () , 00088 tTransitionMap () {} 00089 00091 virtual ~TDeterministicFiniteAutomaton (void) {} 00092 00097 virtual void move (const TState& rktSTATE) 00098 { 00099 tCurrentState = rktSTATE; 00100 } 00101 00102 00103 public: 00104 00105 // 00106 // S E L E C T O R S 00107 // 00108 00113 TState current (void) const 00114 { 00115 return tCurrentState; 00116 } 00117 00122 TState initial (void) const 00123 { 00124 return tInitialState; 00125 } 00126 00132 virtual bool isFinal (const TState& rktSOURCE_STATE) const 00133 { 00134 return ( tFinalStateSet.find (rktSOURCE_STATE) != tFinalStateSet.end() ); 00135 } 00136 00144 const TState& next (const TEvent& rktSOURCE_EVENT) const 00145 throw (TNotFoundException) 00146 { 00147 typename TTransitionMap::const_iterator I; 00148 00149 I = tTransitionMap.find (std::make_pair (tCurrentState, rktSOURCE_EVENT)); 00150 if ( I == tTransitionMap.end() ) 00151 { 00152 throw TNotFoundException ("no next state", __FILE__, __LINE__); 00153 } 00154 return I->second; 00155 } 00156 00157 }; // class TDeterministicFiniteAutomaton 00158 00159 } // namespace automaton 00160 00161 } // namespace mpcl 00162 00163 00164 #endif // not _MPCL_AUTOMATON_DETERMINISTIC_FINITE_AUTOMATON__