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

cleanup moved resources

parent 2eacdf32
No related branches found
No related tags found
No related merge requests found
#include "FsmDeterminizer.h"
namespace determinization {
namespace fsm {
void FsmDeterminizer::initDeterminization()
{
this->QPrimed.clear();
Utils::copyInputAlphabet(*this->nfsm, *this->dfsm);
}
const State& FsmDeterminizer::getOrCreateState(const set<State>& originalStates)
{
return FsmUtils::getOrCreateState(originalStates, this->QPrimed, *this->dfsm);
}
set<State> FsmDeterminizer::findToStates(const set<State>& fromStates, const Symbol& input)
{
set<State> toStates;
for (const auto& state : fromStates) {
for (const auto& transition : this->nfsm->getTransitions()) {
if (transition.getFrom() == state && transition.getInput() == input) {
toStates.insert(transition.getTo());
}
}
}
return toStates;
}
FsmDeterminizer::FsmDeterminizer(FSM* nfsm)
{
this->nfsm = nfsm;
}
Automaton* FsmDeterminizer::determinize()
{
this->dfsm = new FSM();
this->initDeterminization();
// 1
const set<State>& q0 = this->nfsm->getInitialStates();
const State& q0Primed = this->getOrCreateState(q0);
while (true) {
// 2
bool allStatesMarked = true;
for (const auto& stateIter : this->QPrimed) {
allStatesMarked &= stateIter.second.isMarked;
}
if (allStatesMarked) {
break;
}
// 3
StateData* qPrimed = NULL;
for (auto& stateIter : this->QPrimed) {
StateData& stateData = stateIter.second;
if (!stateData.isMarked) {
qPrimed = &stateData;
break;
}
}
// 3a and 3b
for (const auto& input : this->nfsm->getInputAlphabet()) {
const set<State>& nfsmToStates = this->findToStates(qPrimed->originalStates, input);
if (nfsmToStates.size() > 0) {
const State& toState = this->getOrCreateState(nfsmToStates);
TransitionFSM transition(qPrimed->state, input, toState);
this->dfsm->addTransition(transition);
}
}
// 3c
qPrimed->isMarked = true;
}
// 4
this->dfsm->addInitialState(q0Primed);
// 5
for (const auto& qPrimedIter : this->QPrimed) {
const StateData& qPrimed = qPrimedIter.second;
if (Utils::containFinalState(qPrimed.originalStates, *this->nfsm)) {
this->dfsm->addFinalState(qPrimed.state);
}
}
return this->dfsm;
}
}
}
#ifndef FSMDETERMINIZER_H_
#define FSMDETERMINIZER_H_
#include <string>
#include <set>
#include <map>
#include "automaton/State.h"
#include "automaton/FSM/FSM.h"
#include "automaton/FSM/TransitionFSM.h"
#include "../common/Utils.h"
#include "../Determinizer.h"
#include "FsmStructs.h"
#include "FsmUtils.h"
using namespace std;
using namespace automaton;
using namespace determinization;
namespace determinization {
namespace fsm {
/**
* Class for running determinization algorithm on fsm.
*/
class FsmDeterminizer : public Determinizer
{
private:
/** Nondeterministic final-state maching */
FSM* nfsm;
/** Deterministinc final-state machine */
FSM* dfsm;
/** Map of states of deterministic fsm, where key is the name of state and value is data about this state */
map<string, StateData> QPrimed;
/**
* Runs some initializiation stuff before determinization algorithm.
*/
void initDeterminization();
/**
* Returns existing state from states map, if there is one with same name, or creates new one and adds it into
* states map and deterministic fsm.
*
* @param originalStates set of states from nondeterministic fsm which represents state of deterministic fsm
* @return state of deterministic fsm
*/
const State& getOrCreateState(const set<State>& originalStates);
/**
* Finds states from nondeterministic fsm to which at least one state from given set of states have transition
* with given input.
*
* @param fromStates set of states from nondeterministic fsm
* @param input symbol from input alphabet
* @return set of states from nondeterministic fsm
*/
set<State> findToStates(const set<State>& fromStates, const Symbol& input);
public:
/**
* @param nfsm nondeterministic final-state machine given for determinization
*/
FsmDeterminizer(FSM* nfsm);
/**
* Runs determinization algorithm on nondeterministic fsm given in constructor.
*
* @return deterministic final-state machine
*/
Automaton* determinize();
};
}
}
#endif
#ifndef FSMSTRUCTS_H_
#define FSMSTRUCTS_H_
#include <set>
#include "automaton/State.h"
using namespace std;
using namespace automaton;
namespace determinization {
namespace fsm {
/**
* Struct for storing data about state of deterministic automaton.
*/
struct StateData
{
/** State of deterministic automaton */
const State state;
/**
* Set of states from nondeterministic automaton which are represented by this state
* of deterministic automaton
*/
const set<State> originalStates;
/** Indicates if this state is already marked by determinization algorithm or not */
bool isMarked;
StateData(const State& state, const set<State>& originalStates)
: state(state),
originalStates(originalStates),
isMarked(false) {}
};
}
}
#endif
#include "FsmUtils.h"
namespace determinization {
namespace fsm {
string FsmUtils::buildStateName(const set<State>& states)
{
string name = "[";
for (const auto& state : states) {
if (&state != &(*begin(states))) {
name += ", ";
}
name += "'" + state.getName() + "'";
}
name += "]";
return name;
}
const State& FsmUtils::getOrCreateState(const set<State>& originalStates, map<string, StateData>& states,
Automaton& automaton)
{
string stateName = buildStateName(originalStates);
map<string, StateData>::iterator stateIter = states.find(stateName);
if (stateIter != states.end()) {
return stateIter->second.state;
}
State state(stateName);
StateData stateData(state, originalStates);
StateData& insertedData = states.insert(pair<string, StateData>(stateName, stateData)).first->second;
automaton.addState(state);
return insertedData.state;
}
}
}
#ifndef FSMUTILS_H_
#define FSMUTILS_H_
#include <string>
#include <set>
#include <map>
#include "automaton/State.h"
#include "automaton/Automaton.h"
#include "FsmStructs.h"
using namespace std;
using namespace automaton;
using namespace determinization;
namespace determinization {
namespace fsm {
/**
* Library class with some methods that can be used by fsm and idpda determinization.
*/
class FsmUtils
{
public:
/**
* Returns name of state of deterministic automaton which is created from given set of states
* from nondeterministic automaton.
*
* @param states set of states from nondeterministinc fsm
* @return name of state
*/
static string buildStateName(const set<State>& states);
/**
* Returns existing state from states map, if there is one with same name, or creates new one and adds it into
* states map and automaton.
*
* @param originalStates set of states from nondeterministic automaton which represents state
* of deterministic automton
* @param states map of existing states, where key is the name of state and value is data about this state
* @param automaton deterministic automaton that contains given map of states
* @return state of deterministic automaton
*/
static const State& getOrCreateState(const set<State>& originalStates, map<string, StateData>& states,
Automaton& automaton);
};
}
}
#endif
#include "IdpdaDeterminizer.h"
namespace determinization {
namespace idpda {
void IdpdaDeterminizer::initDeterminization()
{
Utils::copyInputAlphabet(*this->nidpda, *this->didpda);
Utils::copyStackAlphabet(*this->nidpda, *this->didpda);
this->QPrimed.clear();
this->findPopAndPushSymbols();
}
void IdpdaDeterminizer::findPopAndPushSymbols()
{
for (const auto& symbol: this->nidpda->getInputAlphabet()) {
for (const auto& transition: this->nidpda->getTransitions()) {
if (transition.getInput() == symbol) {
this->popSymbols[symbol] = transition.getPop();
this->pushSymbols[symbol] = transition.getPush();
break;
}
}
}
}
const State& IdpdaDeterminizer::getOrCreateState(const set<State>& originalStates)
{
return FsmUtils::getOrCreateState(originalStates, this->QPrimed, *this->didpda);
}
set<State> IdpdaDeterminizer::findToStates(const set<State>& fromStates, const Symbol& input)
{
set<State> targetStates;
for (const auto& state : fromStates) {
for (const auto& transition : this->nidpda->getTransitions()) {
if (transition.getFrom() == state && transition.getInput() == input) {
targetStates.insert(transition.getTo());
}
}
}
return targetStates;
}
IdpdaDeterminizer::IdpdaDeterminizer(PDA* nidpda)
{
this->nidpda = nidpda;
}
Automaton* IdpdaDeterminizer::determinize()
{
this->didpda = new PDA();
this->initDeterminization();
// 1
const set<State>& q0 = this->nidpda->getInitialStates();
const State& q0Primed = this->getOrCreateState(q0);
while (true) {
// 2
bool allStatesMarked = true;
for (const auto& stateIter : this->QPrimed) {
allStatesMarked &= stateIter.second.isMarked;
}
if (allStatesMarked) {
break;
}
// 3
StateData* qPrimed = NULL;
for (auto& stateIter : this->QPrimed) {
StateData& stateData = stateIter.second;
if (!stateData.isMarked) {
qPrimed = &stateData;
break;
}
}
// 3a
for (const auto& input : this->nidpda->getInputAlphabet()) {
list<Symbol> stackPop = this->popSymbols[input];
list<Symbol> stackPush = this->pushSymbols[input];
const set<State>& nidpdaToStates = this->findToStates(qPrimed->originalStates, input);
if (nidpdaToStates.size() > 0) {
const State& toState = this->getOrCreateState(nidpdaToStates);
TransitionPDA transition(qPrimed->state, input, toState, stackPop, stackPush);
this->didpda->addTransition(transition);
}
}
// 3b
qPrimed->isMarked = true;
}
// 4
this->didpda->addInitialState(q0Primed);
// 5
for (const auto& qPrimedIter : this->QPrimed) {
const StateData& qPrimed = qPrimedIter.second;
if (Utils::containFinalState(qPrimed.originalStates, *this->nidpda)) {
this->didpda->addFinalState(qPrimed.state);
}
}
return this->didpda;
}
}
}
#ifndef IDPDADETERMINIZER_H_
#define IDPDADETERMINIZER_H_
#include <string>
#include <set>
#include <map>
#include <list>
#include "automaton/State.h"
#include "automaton/PDA/PDA.h"
#include "automaton/PDA/TransitionPDA.h"
#include "../common/Utils.h"
#include "../Determinizer.h"
#include "../fsm/FsmUtils.h"
#include "../fsm/FsmStructs.h"
using namespace std;
using namespace automaton;
using namespace determinization;
using namespace determinization::fsm;
namespace determinization {
namespace idpda {
/**
* Class for running determinization algorithm on idpda.
*/
class IdpdaDeterminizer : public Determinizer
{
private:
/** Nondeterministic input-driven pushdown automaton */
PDA* nidpda;
/** Deterministinc input-driven pushdown automaton */
PDA* didpda;
/** Map of states of deterministic idpda, where key is the name of state and value is data about this state */
map<string, StateData> QPrimed;
/** Map of pop symbols for input symbols of deterministic idpda */
map<Symbol, list<Symbol>> popSymbols;
/** Map of push symbols for input symbols of deterministic idpda */
map<Symbol, list<Symbol>> pushSymbols;
/**
* Runs some initializiation stuff before determinization algorithm.
*/
void initDeterminization();
/**
* Finds and saves pop and push symbols for all symbols from input alphabet of idpda.
*/
void findPopAndPushSymbols();
/**
* Returns existing state from states map, if there is one with same name, or creates new one and adds it into
* states map and deterministic idpda.
*
* @param originalStates set of states from nondeterministic idpda which represents state of deterministic idpda
* @return state of deterministic idpda
*/
const State& getOrCreateState(const set<State>& originalStates);
/**
* Finds states from nondeterministic idpda to which at least one state from given set of states have transition
* with given input.
*
* @param fromStates set of states from nondeterministic idpda
* @param input symbol from input alphabet
* @return set of states from nondeterministic idpda
*/
set<State> findToStates(const set<State>& fromStates, const Symbol& input);
public:
/**
* @param nidpda nondeterministic input-driven pushdown automaton given for determinization
*/
IdpdaDeterminizer(PDA* nidpda);
/**
* Runs determinization algorithm on nondeterministic idpda given in constructor.
*
* @return deterministic input-driven pushdown automaton
*/
Automaton* determinize();
};
}
}
#endif
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