本文整理汇总了Python中transitions.Machine类的典型用法代码示例。如果您正苦于以下问题:Python Machine类的具体用法?Python Machine怎么用?Python Machine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Machine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_repr
def test_repr(self):
def a_condition(event_data):
self.assertRegex(
str(event_data.transition.conditions),
r"\[<Condition\(<function TestTransitions.test_repr.<locals>"
r".a_condition at [^>]+>\)@\d+>\]")
return True
# No transition has been assigned to EventData yet
def check_prepare_repr(event_data):
self.assertRegex(
str(event_data),
r"<EventData\('<State\('A'\)@\d+>', "
r"None\)@\d+>")
def check_before_repr(event_data):
self.assertRegex(
str(event_data),
r"<EventData\('<State\('A'\)@\d+>', "
r"<Transition\('A', 'B'\)@\d+>\)@\d+>")
m.checked = True
m = Machine(states=['A', 'B'],
prepare_event=check_prepare_repr,
before_state_change=check_before_repr, send_event=True,
initial='A')
m.add_transition('do_strcheck', 'A', 'B', conditions=a_condition)
self.assertTrue(m.do_strcheck())
self.assertIn('checked', vars(m))
示例2: test_state_callable_callbacks
def test_state_callable_callbacks(self):
class Model:
def __init__(self):
self.exit_A_called = False
self.exit_B_called = False
def on_enter_A(self, event):
pass
def on_enter_B(self, event):
pass
states = [State(name='A', on_enter='on_enter_A', on_exit='tests.test_core.on_exit_A'),
State(name='B', on_enter='on_enter_B', on_exit=on_exit_B),
State(name='C', on_enter='tests.test_core.AAAA')]
model = Model()
machine = Machine(model, states=states, send_event=True, initial='A')
state_a = machine.get_state('A')
state_b = machine.get_state('B')
self.assertEqual(len(state_a.on_enter), 1)
self.assertEqual(len(state_a.on_exit), 1)
self.assertEqual(len(state_b.on_enter), 1)
self.assertEqual(len(state_b.on_exit), 1)
model.to_B()
self.assertTrue(model.exit_A_called)
model.to_A()
self.assertTrue(model.exit_B_called)
with self.assertRaises(AttributeError):
model.to_C()
示例3: test_state_callbacks
def test_state_callbacks(self):
class Model:
def on_enter_A(self):
pass
def on_exit_A(self):
pass
def on_enter_B(self):
pass
def on_exit_B(self):
pass
states = [State(name='A', on_enter='on_enter_A', on_exit='on_exit_A'),
State(name='B', on_enter='on_enter_B', on_exit='on_exit_B')]
machine = Machine(Model(), states=states)
state_a = machine.get_state('A')
state_b = machine.get_state('B')
self.assertEqual(len(state_a.on_enter), 1)
self.assertEqual(len(state_a.on_exit), 1)
self.assertEqual(len(state_b.on_enter), 1)
self.assertEqual(len(state_b.on_exit), 1)
示例4: test_agraph_diagram
def test_agraph_diagram(self):
states = ['A', 'B', 'C', 'D']
transitions = [
{'trigger': 'walk', 'source': 'A', 'dest': 'B'},
{'trigger': 'run', 'source': 'B', 'dest': 'C'},
{'trigger': 'sprint', 'source': 'C', 'dest': 'D', 'conditions': 'is_fast'},
{'trigger': 'sprint', 'source': 'C', 'dest': 'B'}
]
m = Machine(states=states, transitions=transitions, initial='A', auto_transitions=False)
graph = m.get_graph()
self.assertIsNotNone(graph)
self.assertTrue("digraph" in str(graph))
# Test that graph properties match the Machine
self.assertEqual(
set(m.states.keys()), set([n.name for n in graph.nodes()]))
triggers = set([n.attr['label'] for n in graph.edges()])
for t in triggers:
self.assertIsNotNone(getattr(m, t))
self.assertEqual(len(graph.edges()), len(transitions))
# check for a valid pygraphviz diagram
# write diagram to temp file
target = tempfile.NamedTemporaryFile()
graph.draw(target.name, prog='dot')
self.assertTrue(os.path.getsize(target.name) > 0)
# cleanup temp file
target.close()
print(graph)
示例5: test_before_after_callback_addition
def test_before_after_callback_addition(self):
m = Machine(Stuff(), states=["A", "B", "C"], initial="A")
m.add_transition("move", "A", "B")
trans = m.events["move"].transitions["A"][0]
trans.add_callback("after", "increase_level")
m.model.move()
self.assertEquals(m.model.level, 2)
示例6: __init__
def __init__(self):
states = ['wait',
'in_trial',
'post_trial',
'done']
transitions = [
{'source': 'wait',
'trigger': 'step',
'conditions': 'check_for_space',
'after': 'remove_text',
'dest': 'in_trial'},
{'source': 'in_trial',
'trigger': 'step',
'prepare': ['update_target_pos', 'update_target_color_and_count'],
'conditions': 'samples_exhausted',
'after': ['draw_time_on_target', 'start_countdown'],
'dest': 'post_trial'},
{'source': 'post_trial',
'trigger': 'step',
'conditions': 'time_elapsed',
'dest': 'done'}
]
Machine.__init__(self, states=states,
transitions=transitions, initial='wait')
示例7: __init__
def __init__(self):
state = ['Up', 'Failed', 'Maintenance', 'Blocked']
Machine.__init__(self, states = state, initial='Up')
self.add_transition('start', 'Up', 'Up', after = "startJob")
self.add_transition('fail', 'Up', 'Failed', after = 'startFail')
self.add_transition('repair', 'Failed', 'Up', after = 'rep')
self.add_transition('maint', 'Up', 'Maintenance', after='startmaint')
self.add_transition('maintcpl', 'Maintenance', 'Up')
self.add_transition('interrep', 'Failed', 'Maintenance', after='startmaint')
self.add_transition('block', 'Up', 'Blocked')
self.add_transition('unblock', 'Blocked', 'Up')
self.queue = SortedSet(key = lambda job: job.arrivalTime)
self.numServers = 1
self.busyServers = 0
self.prevState = None
Server._ids +=1
self.serviceTimeDistribution = None
self.name = "Server {}".format(Server._ids)
self.In = None
self.Out = None
self.scheduler = None
self.activejob = None
self.interuptjob = None
#debugging
self.jobsarrived = 0
self.jobsprocessed = 0
self.numfailures = 0
self.nummaint = 0
self.onzin = 0
示例8: test_before_after_callback_addition_callable
def test_before_after_callback_addition_callable(self):
m = Machine(Stuff(), states=['A', 'B', 'C'], initial='A')
m.add_transition('move', 'A', 'B')
trans = m.events['move'].transitions['A'][0]
trans.add_callback('after', m.model.increase_level)
m.model.move()
self.assertEquals(m.model.level, 2)
示例9: __init__
def __init__(self, manifold):
self.manifold = manifold
self.gui = None
# Record controller states
states = [
State(name='disconnected', on_enter=['unbusyUI', 'updateUI']),
State(name='connected', on_enter=['unbusyUI', 'updateUI']),
State(name='confirmed', on_enter=['unbusyUI', 'updateUI']),
State(name='ready', on_enter=['unbusyUI', 'updateUI', 'prepRecording']),
State(name='started', on_enter=['unbusyUI', 'updateUI', 'startRecording']),
State(name='paused', on_enter=['unbusyUI', 'updateUI', 'pauseRecording'])
]
# Record controller state transition definition
transitions = [
{'trigger': 'connect', 'source': 'disconnected', 'dest': 'connected' , 'prepare': ['busyUI', 'connectToServers', 'pingServers'], 'conditions': 'connection_confirmed'},
{'trigger': 'disconnect', 'source': 'connected', 'dest': 'disconnected' , 'prepare': ['busyUI'] },
{'trigger': 'disconnect', 'source': 'paused', 'dest': 'disconnected' , 'prepare': ['busyUI'] },
{'trigger': 'new', 'source': 'connected', 'dest': 'ready' , 'prepare': ['busyUI'] },
{'trigger': 'new', 'source': 'paused', 'dest': 'ready' , 'prepare': ['busyUI'] },
{'trigger': 'start', 'source': 'ready', 'dest': 'started' , 'prepare': ['busyUI'] },
{'trigger': 'pause', 'source': 'started', 'dest': 'paused' , 'prepare': ['busyUI'] },
]
# Record machine
Machine.__init__(self,
states=states,
transitions=transitions,
initial='disconnected')
示例10: __init__
def __init__(self):
# Define the different states of the state machine
states = ['secure', 'takeoff', 'follow', 'take_picture', \
'land', 'reacquisition', 'emergency']
# Define the transitions between states
# FORMAT: ['trigger_event', 'source_state', 'destination_state']
transitions = [
['takeoff_command', 'secure', 'takeoff'],
['takeoff_alt_reached', 'takeoff', 'follow'],
['picture_command', 'follow', 'take_picture'],
['picture_taken', 'take_picture', 'land'],
['land_alt_reached', 'land', 'secure'],
['emergency_condition', ['takeoff', 'follow', 'take_picture', 'land'], 'emergency'],
['takeoff_tag_lost', 'takeoff', 'reacquisition'],
['follow_tag_lost', 'follow', 'reacquisition'],
['land_tag_lost', 'land', 'reacquisition'],
['take_picture_tag_lost', 'take_picture', 'reacquisition'],
['takeoff_tag_found', 'reacquisition', 'takeoff'],
['follow_tag_found', 'reacquisition', 'follow'],
['land_tag_found', 'reacquisition', 'land'],
['take_picture_tag_found', 'reacquisition', 'take_picture'],
['timed_out', 'reacquisition', 'emergency'],
['reset', 'emergency', 'secure']
]
Machine.__init__(self, states=states, transitions=transitions, \
initial='secure')
示例11: __init__
def __init__(self):
self.states = [
{'name': 'Begin'}, #
{'name': 'End'}, #
{'name': 'ChoosePage'}, #
{'name': 'FirstPage'}, #
{'name': 'SecondPage'}, #
{'name': 'Error'} #
]
self.transitions = [
{'trigger': 'step', 'source': 'Begin', 'dest': 'ChoosePage'},
{'trigger': 'step', 'source': 'ChoosePage', 'dest': 'FirstPage', 'conditions': ['verify', 'answerIsOne'], 'after': 'to_ChoosePage'},
# {'trigger': 'step', 'source': 'ChoosePage', 'dest': 'Error', 'after': 'err'},
# {'trigger': 'step', 'source': 'ChoosePage', 'dest': 'FirstPage', 'conditions': 'verify', 'after': 'to_ChoosePage'},
{'trigger': 'step', 'source': 'FirstPage', 'dest': 'ChoosePage'},
# {'trigger': 'step', 'source': 'FirstPage', 'dest': 'Error', 'after': 'err'},
{'trigger': 'step', 'source': 'ChoosePage', 'dest': 'SecondPage', 'conditions': ['verify', 'answerIsTwo'], 'after': 'to_ChoosePage'},
# {'trigger': 'step', 'source': 'ChoosePage', 'dest': 'SecondPage', 'conditions': 'verify', 'after': 'to_ChoosePage'},
{'trigger': 'step', 'source': 'SecondPage', 'dest': 'ChoosePage'},
{'trigger': 'step', 'source': 'SecondPage', 'dest': 'Error', 'after': 'err'},
# {'trigger': 'step', 'source': 'ChoosePage', 'dest': 'End', 'conditions': ['verify', 'answerIsThree']},
# {'trigger': 'step', 'source': 'ChoosePage', 'dest': 'End', 'conditions': 'verify'}
# {'trigger': 'step', 'source': 'ChoosePage', 'dest': 'End'},
{'trigger': 'step', 'source': '*', 'dest': 'End'}
]
Machine.__init__(self, states=self.states, transitions=self.transitions,
initial='Begin', send_event=True)
self.pageNumber = None
self.answer = None
示例12: test_agraph_diagram
def test_agraph_diagram(self):
states = ["A", "B", "C", "D"]
transitions = [
{"trigger": "walk", "source": "A", "dest": "B"},
{"trigger": "run", "source": "B", "dest": "C"},
{"trigger": "sprint", "source": "C", "dest": "D", "conditions": "is_fast"},
{"trigger": "sprint", "source": "C", "dest": "B"},
]
m = Machine(states=states, transitions=transitions, initial="A", auto_transitions=False)
graph = m.get_graph()
self.assertIsNotNone(graph)
self.assertTrue("digraph" in str(graph))
# Test that graph properties match the Machine
self.assertEqual(set(m.states.keys()), set([n.name for n in graph.nodes()]))
triggers = set([n.attr["label"] for n in graph.edges()])
for t in triggers:
self.assertIsNotNone(getattr(m, t))
self.assertEqual(len(graph.edges()), len(transitions))
# check for a valid pygraphviz diagram
# write diagram to temp file
target = tempfile.NamedTemporaryFile()
graph.draw(target.name, prog="dot")
self.assertTrue(os.path.getsize(target.name) > 0)
# cleanup temp file
target.close()
print(graph)
示例13: __init__
def __init__(self):
states=['chitchat',
'requestingRide', 'offeringRide',
'sendingLocation','sendingDestination',
'waiting',
'matchFound','nomatchFound',
'completed']
transitions = [
{ 'trigger': 'abort', 'source': '*', 'dest': 'chitchat'},
{ 'trigger': 'wantRide', 'source': 'chitchat', 'dest': 'requestingRide'},
{ 'trigger': 'offerRide', 'source': 'chitchat', 'dest': 'offeringRide'},
{ 'trigger': 'hasToSendLocation', 'source': 'requestingRide', 'dest': 'sendingLocation'},
{ 'trigger': 'hasToSendLocation', 'source': 'offeringRide', 'dest': 'sendingLocation' },
{ 'trigger': 'hasToSendDestination', 'source': 'sendingLocation', 'dest': 'sendingDestination' },
{ 'trigger': 'gotPosition', 'source': 'sendingDestination', 'dest': 'waiting'},
{ 'trigger': 'OK', 'source': 'waiting', 'dest': 'matchFound' },
{ 'trigger': 'sorry', 'source': 'waiting', 'dest': 'nomatchFound' },
{ 'trigger': 'done', 'source': 'matchFound', 'dest': 'completed' },
{ 'trigger': 'done', 'source': 'nomatchFound', 'dest': 'completed' },
{ 'trigger': 'end', 'source': 'completed', 'dest': 'chitchat' },
]
Machine.__init__(self, states=states, transitions=transitions,auto_transitions=False,initial='chitchat')
示例14: test_machine_prepare
def test_machine_prepare(self):
global_mock = MagicMock()
local_mock = MagicMock()
def global_callback():
global_mock()
def local_callback():
local_mock()
def always_fails():
return False
transitions = [
{'trigger': 'go', 'source': 'A', 'dest': 'B', 'conditions': always_fails, 'prepare': local_callback},
{'trigger': 'go', 'source': 'A', 'dest': 'B', 'conditions': always_fails, 'prepare': local_callback},
{'trigger': 'go', 'source': 'A', 'dest': 'B', 'conditions': always_fails, 'prepare': local_callback},
{'trigger': 'go', 'source': 'A', 'dest': 'B', 'conditions': always_fails, 'prepare': local_callback},
{'trigger': 'go', 'source': 'A', 'dest': 'B', 'prepare': local_callback},
]
m = Machine(states=['A', 'B'], transitions=transitions,
prepare_event=global_callback, initial='A')
m.go()
self.assertEqual(global_mock.call_count, 1)
self.assertEqual(local_mock.call_count, len(transitions))
示例15: test_dispatch
def test_dispatch(self):
s1, s2 = Stuff(), Stuff()
states = ['A', 'B', 'C']
m = Machine(model=s1, states=states, ignore_invalid_triggers=True,
initial=states[0], transitions=[['go', 'A', 'B'], ['go', 'B', 'C']])
m.add_model(s2, initial='B')
m.dispatch('go')
self.assertEqual(s1.state, 'B')
self.assertEqual(s2.state, 'C')