From 0f2370381f0084d085f2794763e65ade3db48d8c Mon Sep 17 00:00:00 2001 From: Martin Zak <zakmart1@fit.cvut.cz> Date: Sat, 23 Nov 2013 14:05:16 +0100 Subject: [PATCH] Adds isEpsilonFree & isDeterministic methods to FSM --- alib/src/automaton/FSM/FSM.cpp | 32 ++++++++++++++++++++++++++++++++ alib/src/automaton/FSM/FSM.h | 3 +++ 2 files changed, 35 insertions(+) diff --git a/alib/src/automaton/FSM/FSM.cpp b/alib/src/automaton/FSM/FSM.cpp index 9cc2d61a6b..6a90f060c6 100644 --- a/alib/src/automaton/FSM/FSM.cpp +++ b/alib/src/automaton/FSM/FSM.cpp @@ -100,4 +100,36 @@ void FSM::toXML(ostream& out) const { AutomatonPrinter::toXML(*this, out); } +bool FSM::isEpsilonFree() const { + for (auto const& transition : transitions) { + if (transition.getInput() == Symbol("")) { + return false; + } + } + return true; +} + +bool FSM::isDeterministic() const { + if (initialStates.size() != 1) { + return false; + } + + if (!isEpsilonFree()) { + return false; + } + + for (auto transition = transitions.begin(); transition != transitions.end(); transition++) { + auto subTransition = transition; + subTransition++; + for (; subTransition != transitions.end(); subTransition++) { + if (subTransition->getFrom() == transition->getFrom() + && subTransition->getInput() == transition->getInput()) { + return false; + } + } + } + + return true; +} + } /* namespace automaton */ diff --git a/alib/src/automaton/FSM/FSM.h b/alib/src/automaton/FSM/FSM.h index d4bae197eb..373819c2a7 100644 --- a/alib/src/automaton/FSM/FSM.h +++ b/alib/src/automaton/FSM/FSM.h @@ -33,6 +33,9 @@ public: void removeTransition(const TransitionFSM& transition); const set<TransitionFSM>& getTransitions() const; + bool isEpsilonFree() const; + bool isDeterministic() const; + virtual void toXML(ostream& out) const; }; -- GitLab