Our class project has hit a dead end, we have three files, two are our transition and state machine programs, the other is our statement text file. Everything seems to work except when we attempt to input our text file, it always classifies it as an error_state. However, if we read the input into the transition program it works properly.
This is our result:
Greg is beautiful
('reached the end state that is a ', 'error_state')
Greg is ugly
('reached the end state that is a ', 'error_state')
Greg is O.K.
('reached the end state that is a ', 'error_state')
We have also tried reading it in as one list and splitting it.
Here is our code:
Statemachine.py
class StateMachine:
def __init__(self):
self.handlers = {}
self.startState = None
self.endStates = []
def add_state(self, name, handler, end_state=0):
name = name.upper()
self.handlers[name] = handler
if end_state:
self.endStates.append(name)
def set_start(self, name):
self.startState = name.upper()
def run(self, cargo):
try:
handler = self.handlers[self.startState]
except:
raise InitializationError("must call .set_start() before .run()")
if not self.endStates:
raise InitializationError("at least one state must be an end_state")
while True:
(newState, cargo) = handler(cargo)
if newState.upper() in self.endStates:
print("reached the end state that is a ", newState)
break
else:
handler = self.handlers[newState.upper()]
Transitions.py
from Statemachine import StateMachine
positive_adjectives = ["great","fun", "handsome", "beautiful"]
negative_adjectives = ["boring", "difficult", "ugly", "goofy"]
def start_transitions(txt):
splitted_txt = txt.split(None,1)
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"")
if word == "Greg":
newState = "Greg_state"
else:
newState = "error_state"
return (newState, txt)
def is_state_transitions(txt):
splitted_txt = txt.split(None,1)
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"")
if word == "is":
newState = "is_state"
else:
newState = "error_state"
return (newState, txt)
def not_state_transitions(txt):
splitted_txt = txt.split(None,1)
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"")
if word == "not":
newState = "not_state"
elif word in positive_adjectives:
newState = "positive_state"
elif word in negative_adjectives:
newState = "negative_state"
else:
newState = "error_state"
return (newState, txt)
def adjective_state_transitions(txt):
splitted_txt = txt.split(None,1)
word, txt = splitted_txt if len(splitted_txt) > 1 else (txt,"")
if word in positive_adjectives:
newState = "negative_state"
elif word in negative_adjectives:
newState = "positive_state"
else:
newState = "error_state"
return (newState, txt)
def neg_state(txt):
print("Not nice :(")
return ("negative_state", "")
if __name__== "__main__":
m = StateMachine()
m.add_state("Start", start_transitions)
m.add_state("Greg_state", is_state_transitions)
m.add_state("is_state", is_state_transitions)
m.add_state("not_state", not_state_transitions)
m.add_state("negative_state", None, end_state=1)
m.add_state("positive_state", None, end_state=1)
m.add_state("error_state", None, end_state=1)
m.set_start("Start")
with open('states.txt', 'r') as f_input:
statement1 = f_input.readline()
statement2 = f_input.readline()
statement3 = f_input.readline()
print (statement1)
m.run (statement1)
print (statement2)
m.run (statement2)
print (statement3)
m.run (statement3)
f_input.close()
states.txt
Greg is beautiful
Greg is ugly
Greg is O.K.
-Thank you!