Created
June 12, 2017 00:02
-
-
Save aforren1/32fe8192bc1048a422629f340b069b44 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Experiment(Machine): | |
| def is_line_crossed(self): return self.line_crossed | |
| def reset_line(self): self.line_crossed = False | |
| def done_countdown(self): | |
| self.countdown_done = True | |
| print "Done the countdown" | |
| def start_countdown(self): | |
| self.countdown = Timer(5, self.done_countdown) | |
| self.countdown.start() | |
| print "Start the countdown" | |
| def reset_countdown(self): | |
| self.countdown_done = False | |
| def is_countdown_done(self): return self.countdown_done | |
| def my_state(self): | |
| print "state: ", self.state | |
| def __init__(self): | |
| self.line_crossed = False | |
| self.countdown = None | |
| self.countdown_done = False | |
| states = ['pretrial', 'intrial', 'posttrial'] | |
| transitions = [ | |
| {'trigger': 'try_transition', 'source': 'intrial', | |
| 'dest': 'posttrial', 'conditions': 'is_line_crossed', | |
| 'after': ['start_countdown', 'reset_line'], | |
| 'before': 'my_state'}, | |
| {'trigger': 'try_transition', 'source': 'posttrial', | |
| 'dest': 'pretrial', 'conditions': 'is_countdown_done', | |
| 'after': ['reset_countdown', 'start_countdown'], | |
| 'before': 'my_state'}, | |
| {'trigger': 'try_transition', 'source': 'pretrial', | |
| 'dest': 'intrial', 'conditions': 'is_countdown_done', | |
| 'after': ['reset_countdown'], | |
| 'before': 'my_state'} | |
| ] | |
| Machine.__init__(self, states=states, | |
| initial='intrial', transitions=transitions) | |
| if __name__ == '__main__': | |
| exp = Experiment() | |
| exp.try_transition() # no transition | |
| exp.line_crossed = True | |
| exp.try_transition() # transition to 'posttrial' | |
| exp.try_transition() # try immediately after transition; blocks | |
| # wait 5 secs... | |
| sleep(5) | |
| exp.try_transition() # works now (now in 'pretrial') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment