-
-
Save vyruz/5392327 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
fsm.cpp - Finite State Machine | |
Read the header file 'fsm.hpp' for all documentation on individual | |
functions. I suggest you start by getting the unit test cases to | |
pass in order. E.g. start with the Defaults one, then do Add States, | |
and so on. | |
Your Name: Luke Woodruff | |
Your Collaborators: | |
*/ | |
#include "fsm.hpp" | |
using namespace std; | |
FSM::FSM() { | |
state = -1; | |
default_state = -1; | |
} | |
int FSM::addState(string label, bool is_accept_state) { | |
State* st = new State; | |
st->label = label; | |
st->accept = is_accept_state; | |
int id = states.size(); | |
states.push_back(st); | |
return id; | |
} | |
int FSM::addState(string label) { | |
State* st = new State; | |
st->label = label; | |
int id = states.size(); | |
states.push_back(st); | |
return id; | |
} | |
/** | |
* Adds a transition between two states that activates when the | |
* given signal is received. This is considered duplicate if there | |
* is another transition from stateA to stateB with the same | |
* signal. The transition label is ignored when determining | |
* duplicate status. | |
* | |
* If this is a duplicate, nothing is done and -1 is returned. | |
* | |
* If either state is not present in the FSM, nothing is done and -1 | |
* is returned. | |
* | |
* Otherwise, a new transition is installed in the FSM and given a | |
* non-negative id that is unique among transitions. That id is | |
* returned. | |
* | |
* stateA: the start state's id. The FSM must be in this state for | |
* the transition to take place. | |
* | |
* stateB: the end state. After taking the transition, the FSM will | |
* be placed in stateB. | |
* | |
* signal: the signal that (assuming we are in stateA) will trigger | |
* this particular transition. If the signal is | |
* FAILURE_SIGNAL, it is considered the 'failure signal', | |
* which is a catch-all for bad input. If this is the | |
* failure signal, stateA will use this transition if it | |
* receives a signal that it can not otherwise use. | |
* | |
* transLabel: the label for this transition. | |
**/ | |
int FSM::addTransition(int stateA, int stateB, | |
int transSignal, string transLabel) { | |
// implement me | |
Transition* new_trans = new Transition; | |
new_trans->label = transLabel; | |
new_trans->signal = transSignal; | |
new_trans->next_state = stateB; | |
int id = transitions.size(); | |
transitions.push_back(new_trans); | |
return id; | |
} | |
int FSM::countStates() { | |
return states.size(); | |
} | |
int FSM::countTransitions() { | |
return transitions.size(); | |
} | |
int FSM::getCurrentState() { | |
return state; | |
} | |
bool FSM::isAcceptState() { | |
// State* curr_state = states[state]; | |
// return curr_state->accept; | |
} | |
State* FSM::getState(int id) { | |
// implement me | |
bool found = false; | |
for(int i=0; i<states.size(); i++){ | |
if(i == id){ | |
return states[i]; | |
} | |
} | |
if(found == false){ | |
return NULL; | |
} | |
} | |
Transition* FSM::getTransition(int id) { | |
// implement me | |
return transitions[id]; | |
} | |
int FSM::getDefaultState() { | |
return default_state; | |
} | |
void FSM::setState(int id) { | |
// implement me | |
state = id; | |
} | |
bool FSM::handleSignal(int signal) { | |
// implement me | |
return false; | |
} | |
ostream &operator << (ostream& out, State* st) { | |
if (st == NULL) { | |
out << "State: NULL"; | |
} else { | |
out << "State: " << st->label; | |
if (st->accept) { | |
out << " +"; | |
} | |
} | |
return out; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment