本文整理汇总了Python中sismic.interpreter.Interpreter.bind方法的典型用法代码示例。如果您正苦于以下问题:Python Interpreter.bind方法的具体用法?Python Interpreter.bind怎么用?Python Interpreter.bind使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sismic.interpreter.Interpreter
的用法示例。
在下文中一共展示了Interpreter.bind方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: RemoteElevatorTests
# 需要导入模块: from sismic.interpreter import Interpreter [as 别名]
# 或者: from sismic.interpreter.Interpreter import bind [as 别名]
class RemoteElevatorTests(unittest.TestCase):
def setUp(self):
with open('docs/examples/elevator.yaml') as f:
elevator = io.import_from_yaml(f)
with open('docs/examples/elevator_buttons.yaml') as f:
buttons = io.import_from_yaml(f)
self.elevator = Interpreter(elevator)
self.buttons = Interpreter(buttons)
self.buttons.bind(self.elevator)
def test_button(self):
self.assertEqual(self.elevator.context['current'], 0)
self.buttons.queue(Event('button_2_pushed'))
self.buttons.execute()
event = self.elevator._events.pop()
self.assertEqual(event.name, 'floorSelected')
self.assertEqual(event.data['floor'], 2)
self.buttons.queue(Event('button_2_pushed'))
self.buttons.execute()
self.elevator.execute()
self.assertEqual(self.elevator.context['current'], 2)
def test_button_0_on_groundfloor(self):
self.assertEqual(self.elevator.context['current'], 0)
self.buttons.queue(Event('button_0_pushed'))
self.buttons.execute()
self.elevator.execute()
self.assertEqual(self.elevator.context['current'], 0)
示例2: BindTests
# 需要导入模块: from sismic.interpreter import Interpreter [as 别名]
# 或者: from sismic.interpreter.Interpreter import bind [as 别名]
class BindTests(unittest.TestCase):
def setUp(self):
with open('tests/yaml/simple.yaml') as f:
sc = io.import_from_yaml(f)
self.interpreter = Interpreter(sc)
# Stabilization
self.interpreter.execute_once()
def test_bind(self):
with open('tests/yaml/simple.yaml') as f:
other_sc = io.import_from_yaml(f)
other_interpreter = Interpreter(other_sc)
self.interpreter.bind(other_interpreter)
self.assertIn(other_interpreter.queue, self.interpreter._bound)
self.interpreter.raise_event(InternalEvent('test'))
self.assertTrue(self.interpreter._internal_events.pop(), Event('test'))
self.assertTrue(other_interpreter._external_events.pop(), Event('test'))
def test_bind_callable(self):
with open('tests/yaml/simple.yaml') as f:
other_sc = io.import_from_yaml(f)
other_interpreter = Interpreter(other_sc)
self.interpreter.bind(other_interpreter.queue)
self.assertIn(other_interpreter.queue, self.interpreter._bound)
self.interpreter.raise_event(InternalEvent('test'))
self.assertTrue(self.interpreter._internal_events.pop(), Event('test'))
self.assertTrue(other_interpreter._external_events.pop(), Event('test'))
示例3: BindTests
# 需要导入模块: from sismic.interpreter import Interpreter [as 别名]
# 或者: from sismic.interpreter.Interpreter import bind [as 别名]
class BindTests(unittest.TestCase):
def setUp(self):
sc = io.import_from_yaml(open('tests/yaml/simple.yaml'))
self.interpreter = Interpreter(sc)
def test_bind(self):
other_sc = io.import_from_yaml(open('tests/yaml/simple.yaml'))
other_interpreter = Interpreter(other_sc)
self.interpreter.bind(other_interpreter)
self.assertEqual(self.interpreter._bound, [other_interpreter.send])
self.interpreter.send(Event('test'), internal=True)
self.assertTrue(self.interpreter._events.pop(), Event('test'))
self.assertTrue(other_interpreter._events.pop(), Event('test'))
def test_bind_callable(self):
other_sc = io.import_from_yaml(open('tests/yaml/simple.yaml'))
other_interpreter = Interpreter(other_sc)
self.interpreter.bind(other_interpreter.send)
self.assertEqual(self.interpreter._bound, [other_interpreter.send])
self.interpreter.send(Event('test'), internal=True)
self.assertTrue(self.interpreter._events.pop(), Event('test'))
self.assertTrue(other_interpreter._events.pop(), Event('test'))
示例4: StopwatchApplication
# 需要导入模块: from sismic.interpreter import Interpreter [as 别名]
# 或者: from sismic.interpreter.Interpreter import bind [as 别名]
class StopwatchApplication(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
# Initialize widgets
self.create_widgets()
# Create a Stopwatch interpreter
with open('stopwatch.yaml') as f:
statechart = import_from_yaml(f)
self.interpreter = Interpreter(statechart, initial_time=time.time())
# Bind interpreter events to the GUI
self.interpreter.bind(self.event_handler)
# Run the interpreter
self.run()
def run(self):
# This function does essentially the same job than ``sismic.interpreter.run_in_background``
# but uses Tkinter's mainloop instead of a Thread, which is more adequate.
# Update internal clock and execute interpreter
self.interpreter.time = time.time()
self.interpreter.execute()
# Queue a call every 100ms on tk's mainloop
self.after(100, self.run)
# Update the widget that contains the list of active states.
self.w_states['text'] = 'active states: ' + ', '.join(self.interpreter.configuration)
def create_widgets(self):
self.pack()
# Add buttons
self.w_btn_start = tk.Button(self, text='start', command=self._start)
self.w_btn_stop = tk.Button(self, text='stop', command=self._stop)
self.w_btn_split = tk.Button(self, text='split', command=self._split)
self.w_btn_unsplit = tk.Button(self, text='unsplit', command=self._unsplit)
self.w_btn_reset = tk.Button(self, text='reset', command=self._reset)
self.w_btn_quit = tk.Button(self, text='quit', command=self._quit)
# Initial button states
self.w_btn_stop['state'] = tk.DISABLED
self.w_btn_unsplit['state'] = tk.DISABLED
# Pack
self.w_btn_start.pack(side=tk.LEFT,)
self.w_btn_stop.pack(side=tk.LEFT,)
self.w_btn_split.pack(side=tk.LEFT,)
self.w_btn_unsplit.pack(side=tk.LEFT,)
self.w_btn_reset.pack(side=tk.LEFT,)
self.w_btn_quit.pack(side=tk.LEFT,)
# Active states label
self.w_states = tk.Label(root)
self.w_states.pack(side=tk.BOTTOM, fill=tk.X)
# Timer label
self.w_timer = tk.Label(root, font=("Helvetica", 16), pady=5)
self.w_timer.pack(side=tk.BOTTOM, fill=tk.X)
def event_handler(self, event):
# Update text widget when timer value is updated
if event.name == 'refresh':
self.w_timer['text'] = event.time
def _start(self):
self.interpreter.queue(Event('start'))
self.w_btn_start['state'] = tk.DISABLED
self.w_btn_stop['state'] = tk.NORMAL
def _stop(self):
self.interpreter.queue(Event('stop'))
self.w_btn_start['state'] = tk.NORMAL
self.w_btn_stop['state'] = tk.DISABLED
def _reset(self):
self.interpreter.queue(Event('reset'))
def _split(self):
self.interpreter.queue(Event('split'))
self.w_btn_split['state'] = tk.DISABLED
self.w_btn_unsplit['state'] = tk.NORMAL
def _unsplit(self):
self.interpreter.queue(Event('split'))
self.w_btn_split['state'] = tk.NORMAL
self.w_btn_unsplit['state'] = tk.DISABLED
def _quit(self):
self.master.destroy()
示例5: MicrowaveApplication
# 需要导入模块: from sismic.interpreter import Interpreter [as 别名]
# 或者: from sismic.interpreter.Interpreter import bind [as 别名]
class MicrowaveApplication(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
# Initialize widgets
self.create_widgets()
# Create a Stopwatch interpreter
with open('microwave.yaml') as f:
statechart = import_from_yaml(f)
self.interpreter = Interpreter(statechart)
# Bind interpreter events to the GUI
self.interpreter.bind(self.event_handler)
self.execute()
def execute(self):
self.interpreter.execute()
# Update the widget that contains the list of active states.
self.w_states['text'] = '\n'.join(self.interpreter.configuration)
self.w_timer['text'] = 'M.timer: %d' % self.interpreter.context.get('controller.timer', 'undefined')
self.w_power['text'] = 'M.power: %d' % self.interpreter.context.get('controller.power', 'undefined')
def create_widgets(self):
self.pack(fill=tk.BOTH)
# MAIN frame containing all others
left_frame = tk.Frame(self)
left_frame.pack(side=tk.LEFT, fill=tk.BOTH)
# INPUT frame
input_frame = tk.LabelFrame(left_frame, text='INPUT BUTTONS')
input_frame.pack(side=tk.LEFT, fill=tk.BOTH, padx=(8, 8))
self.w_power_inc = tk.Button(input_frame, text='power +',
command=partial(self.send_event, event_name='input_power_inc'))
self.w_power_dec = tk.Button(input_frame, text='power -',
command=partial(self.send_event, event_name='input_power_dec'))
self.w_power_reset = tk.Button(input_frame, text='power reset',
command=partial(self.send_event, event_name='input_power_reset'))
self.w_power_inc.pack(side=tk.TOP, fill=tk.X)
self.w_power_dec.pack(side=tk.TOP, fill=tk.X)
self.w_power_reset.pack(side=tk.TOP, fill=tk.X)
self.w_timer_inc = tk.Button(input_frame, text='timer +',
command=partial(self.send_event, event_name='input_timer_inc'))
self.w_timer_dec = tk.Button(input_frame, text='timer -',
command=partial(self.send_event, event_name='input_timer_dec'))
self.w_timer_reset = tk.Button(input_frame, text='timer 0',
command=partial(self.send_event, event_name='input_timer_reset'))
self.w_timer_inc.pack(side=tk.TOP, fill=tk.X, pady=(8, 0)) # leave some space before first button
self.w_timer_dec.pack(side=tk.TOP, fill=tk.X)
self.w_timer_reset.pack(side=tk.TOP, fill=tk.X)
self.w_cooking_start = tk.Button(input_frame, text='start',
command=partial(self.send_event, event_name='input_cooking_start'))
self.w_cooking_stop = tk.Button(input_frame, text='stop',
command=partial(self.send_event, event_name='input_cooking_stop'))
self.w_cooking_start.pack(side=tk.TOP, fill=tk.X, pady=(8, 0)) # leave some space before first button
self.w_cooking_stop.pack(side=tk.TOP, fill=tk.X)
# SENSORS frame
sensors_frame = tk.LabelFrame(left_frame, text='SENSORS')
sensors_frame.pack(side=tk.LEFT, fill=tk.BOTH, padx=(8, 8))
# SENSORS > Clock frame
clock_frame = tk.LabelFrame(sensors_frame, text='Clock')
clock_frame.pack(side=tk.TOP, fill=tk.BOTH)
self.w_clock = tk.Label(clock_frame)
self.w_tick = tk.Button(clock_frame, text='tick', command=partial(self.send_event, event_name='timer_tick'))
self.w_tick.pack(side=tk.TOP, fill=tk.X)
# SENSORS > WeightSensor frame
weight_frame = tk.LabelFrame(sensors_frame, text='WeightSensor')
weight_frame.pack(side=tk.TOP, fill=tk.BOTH, pady=(8, 0))
self.w_weight = tk.Label(weight_frame)
self.w_item_placed = tk.Button(weight_frame, text='place item',
command=partial(self.send_event, event_name='item_placed'))
self.w_item_removed = tk.Button(weight_frame, text='remove item',
command=partial(self.send_event, event_name='item_removed'))
self.w_item_placed.pack(side=tk.TOP, fill=tk.X)
self.w_item_removed.pack(side=tk.TOP, fill=tk.X)
# SENSORS > Door frame
door_frame = tk.LabelFrame(sensors_frame, text='Door')
door_frame.pack(side=tk.TOP, fill=tk.BOTH, pady=(8, 0))
self.w_door = tk.Label(door_frame)
self.w_door_opened = tk.Button(door_frame, text='open door',
command=partial(self.send_event, event_name='door_opened'))
self.w_door_closed = tk.Button(door_frame, text='close door',
#.........这里部分代码省略.........