def create_sample_machine_ordered(self, **kwargs): states = ['A', 'B', 'C'] # See the "alternative initialization" section for an explanation of the 1st argument to init machine = Machine(None, states, initial='A', **kwargs) machine.add_ordered_transitions() # machine.next_state() # print(machine.state) # # >>> 'B' return machine
def create_sample_machine_traffic_light_2state4nested_1(self, **kwargs): states = [{'name': 'unlit', 'children': ['off', 'blank']}, {'name': 'lit', 'children': ['green', 'yellow', 'red']}] order = ['unlit_off', 'unlit_blank', 'lit_red', 'lit_green', 'lit_yellow', 'lit_red'] transitions = [ ['turned_on', 'unlit_off', 'unlit_blank'], ['commence_shutdown', 'lit_green', 'lit_yellow'], ['power_outage', '*', 'unlit_off'] ] m = Machine(states=states, transitions=transitions, initial='unlit_off', **kwargs) m.add_ordered_transitions(order) return m
def create_sample_machine_task(self, **kwargs): states = ['Unstarted', 'In Progress', 'Completed', 'Cancelled'] order = ['Unstarted', 'In Progress', 'Completed'] transitions = [ ['Start', 'Unstarted', 'In Progress'], ['Complete', 'In Progress', 'Completed'], ['Cancel', 'In Progress', 'Cancelled'] ] initial = states[0] machine = Machine(states=states, transitions=transitions, initial=initial, **kwargs) machine.add_ordered_transitions(order) return machine
def create_machine(states, transitions=None, initial=None, order=None, options=None): if not states: raise serializers.ValidationError("Invalid value: '{}'".format('states')) if not transitions: transitions = [] # raise serializers.ValidationError("Invalid value: '{}'".format('transitions')) if not initial: initial = states[0] options_default = {'auto_transitions': False, 'ignore_invalid_triggers': False} if options: # LogUtils.log_object_state(options, level=logging.WARN, name='options', context='original') options = utils.kwargs_merge(options, options_default) # LogUtils.log_object_state(options, level=logging.WARN, name='options', context='parsed') else: options = options_default m = Machine(states=states, transitions=transitions, initial=initial, **options) if order: m.add_ordered_transitions(order) return m
def create_sample_machine_traffic_light_2state3nested_2(self, **kwargs): states = ['off', {'name': 'lit', 'children': ['green', 'yellow', 'red']}] order = ['off', 'lit_red', 'lit_green', 'lit_yellow', 'lit_red'] transitions = [ ['turned_on', 'off', 'lit_red'], ['commence_shutdown', 'lit_green', 'lit_yellow'], # ['flash_on', 'off', ['lit_green', 'lit_yellow', 'lit_red']], ['flash_on_g', 'off', 'lit_green'], ['flash_on_y', 'off', 'lit_yellow'], ['flash_on_r', 'off', 'lit_red'], # ['flash_off', ['lit_green', 'lit_yellow', 'lit_red'], 'off'], ['flash_off_g', 'lit_green', 'off'], ['flash_off_y', 'lit_yellow', 'off'], ['flash_off_r', 'lit_red', 'off'], ['poweroutage', '*', 'off'] ] m = Machine(states=states, transitions=transitions, initial='off', **kwargs) m.add_ordered_transitions(order) return m
def create_sample_machine_ordered2(self, **kwargs): # We can also define a different order of transitions states = ['A', 'B', 'C'] machine = Machine(None, states, initial='A', **kwargs) machine.add_ordered_transitions(['A', 'C', 'B']) return machine
def create_sample_machine_traffic_light_4state_1(self, **kwargs): states = ['green', 'yellow', 'red', 'off'] order = ['off', 'red', 'green', 'yellow', 'red'] m = Machine(states=states, initial='red', **kwargs) m.add_ordered_transitions(order) return m