当前位置: 首页>>代码示例>>Python>>正文


Python Fysom.trigger方法代码示例

本文整理汇总了Python中fysom.Fysom.trigger方法的典型用法代码示例。如果您正苦于以下问题:Python Fysom.trigger方法的具体用法?Python Fysom.trigger怎么用?Python Fysom.trigger使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在fysom.Fysom的用法示例。


在下文中一共展示了Fysom.trigger方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: do_stem

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import trigger [as 别名]
    def do_stem(self, word):
        fsm = Fysom(initial='start', events=self.events)
        # FIXME: uncomment below and make sanitize functions support
        # both Python 2 and 3 versions
        # word = WordProcessor.sanitize(word)
        i = len(word) - 1
        j = len(word)

        while(True):
            if i <= 0:
                break
            v = word[i:j]
            # print v
            res = fsm.can(v)
            if (res):
                if v == 'i' and fsm.can(word[i-1:j]):
                    i = i - 1
                    continue
                fsm.trigger(v)
                if fsm.current == 'h':
                    if word[i-1:i] == 'i':
                        i = i - 1  # skip i
                        if word[i-1:i] == 'n':
                            # ning qushimchasi
                            fsm.current = 'start'
                            continue
                elif fsm.current == 'b':
                    fsm.current = 'start'
                j = i

            i = i - 1

        return word[:j]
开发者ID:MrBrownWins,项目名称:devon,代码行数:35,代码来源:devon.py

示例2: FysomStateTests

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import trigger [as 别名]
class FysomStateTests(unittest.TestCase):
    def setUp(self):
        self.fsm = Fysom({
            'initial': 'green',
            'events': [
                {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                {'name': 'clear', 'src': 'yellow', 'dst': 'green'},
                {'name': 'warm', 'src': 'green', 'dst': 'blue'}
            ]
        })

    def test_is_state_should_succeed_for_initial_state(self):
        self.assertTrue(self.fsm.isstate('green'))

    def test_identity_transition_should_not_be_allowed_by_default(self):
        self.assertFalse(self.fsm.can('clear'))
        self.assertTrue(self.fsm.cannot('clear'))

    def test_configured_transition_should_work(self):
        self.assertTrue(self.fsm.can('warn'))

    def test_transition_should_change_state(self):
        self.fsm.warn()
        self.assertTrue(self.fsm.isstate('yellow'))

    def test_should_raise_exception_when_state_transition_is_not_allowed(self):
        self.assertRaises(FysomError, self.fsm.panic)
        self.assertRaises(FysomError, self.fsm.calm)
        self.assertRaises(FysomError, self.fsm.clear)

    def test_event_handler_has_name_and_docstring(self):
        self.assertEqual(self.fsm.warm.__name__, "warm", "Event handlers do not have appropriate name.")
        self.assertNotEqual(self.fsm.warm.__name__, None, "Docstring for event handler is None!")

    def test_trigger_should_trigger_the_event_handler(self):
        self.assertEqual(self.fsm.current, "green", "The initial state isn't the expected state.")
        self.fsm.trigger("warm")
        make_callable = lambda: self.fsm.trigger("unknowevent")
        self.assertRaises(FysomError, make_callable)
        self.assertEqual(self.fsm.current, "blue", "The initial state isn't the expected state.")
开发者ID:tanwanirahul,项目名称:fysom,代码行数:44,代码来源:fysom_state_tests.py

示例3: Application

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import trigger [as 别名]
class Application():
    def __init__(self, display):
        self.display = display
        self.offset = 0
        self.fsm = Fysom({ 'initial': 'menu',
            'events': [
                {'name': 'start', 'src': 'menu', 'dst': 'game'},
                {'name': 'finish', 'src': 'game', 'dst': 'menu'},
                {'name': 'credits', 'src': 'menu', 'dst': 'credits'},
                {'name': 'menu', 'src': 'credits', 'dst': 'menu'} ] })
        self.fsm.onchangestate = Application.change_state
        self.commands = []
        self.active_state = Menu(self.display)

    @staticmethod
    def change_state(event):
        app = event.app
        if event.dst == "menu":
            app.active_state = Menu(app.display)
        elif event.dst == "game":
            app.active_state = Game(app.display)
        elif event.dst == "credits":
            app.active_state = Credits(app.display)

    def input(self, event):
        command = self.active_state.process_input(event)
        if command != None:
            self.commands.append(command)

    def update(self):
        transition = self.active_state.update(self.commands)
        if transition is not None:
            self.fsm.trigger(transition, app=self)
        self.commands = []

    def render(self):
        self.active_state.render()
开发者ID:fischerq,项目名称:devmania2014,代码行数:39,代码来源:application.py

示例4: __init__

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import trigger [as 别名]
class Menu:
    def __init__(self, display):
        self.display = display
        self.fsm = Fysom({ 'initial': 'start',
            'events': [
                {'name': 'up', 'src': 'credits', 'dst': 'start'},
                {'name': 'up', 'src': 'start', 'dst': 'credits'},
                {'name': 'down', 'src': 'start', 'dst': 'credits'},
                {'name': 'down', 'src': 'credits', 'dst': 'start'}] })
        self.player_controllers = [None, None]

    def process_input(self, event):
        if event.type == KEYDOWN:
                if event.key == K_RETURN:
                    return "enter"
                elif event.key == K_UP:
                    return "up"
                elif event.key == K_DOWN:
                    return "down"
        if self.player_controllers[0] is None or self.player_controllers[1] is None:
            if event.type == JOYBUTTONDOWN and event.button == 9:
                if self.player_controllers[0] is None:
                    self.player_controllers[0] = event.joy
                elif self.player_controllers[1] is None and event.joy != self.player_controllers[0]:
                    self.player_controllers[1] = event.joy
                    resources.set("player_joysticks", self.player_controllers)

        elif event.type == JOYBUTTONDOWN and event.button == 9:
                    return "enter"
        return None

    def update(self, commands):
        for command in commands:
            if command == "enter":
                if self.player_controllers[0] is not None and self.player_controllers[1] is not None:
                    return self.fsm.current
                else:
                    return None
            else:
                self.fsm.trigger(command)
        return None

    def render(self):
        #self.display.blit(resources.get("imgBackgroundMenu"), (0, 0))

        self.display.blit(resources.get("imgMenuTitle"), (self.display.get_rect().centerx - resources.get("imgMenuTitle").get_width()/2, self.display.get_rect().centery - resources.get("imgMenuTitle").get_height()/2 - 200))

        if self.fsm.current == "start":
            color = (0, 0, 0)
        else:
            color = (220, 220, 220)
        text_surface = resources.get("basicFont").render('Start', True, color)
        text_rect = text_surface.get_rect()
        text_rect.right = self.display.get_rect().centerx -  10
        text_rect.centery = self.display.get_rect().centery - 100
        self.display.blit(text_surface, text_rect)

        if self.fsm.current == "credits":
            color = (0, 0, 0)
        else:
            color = (220, 220, 220)
        text_surface = resources.get("basicFont").render('Credits', True, color)
        text_rect = text_surface.get_rect()
        text_rect.left = self.display.get_rect().centerx + 10
        text_rect.centery = self.display.get_rect().centery - 100
        self.display.blit(text_surface, text_rect)

        text_surface = resources.get("basicFont").render('Register controllers by pressing start', True, (100, 100, 100))
        text_rect = text_surface.get_rect()
        text_rect.centerx = self.display.get_rect().centerx
        text_rect.centery = self.display.get_rect().centery +50
        self.display.blit(text_surface, text_rect)

        if self.player_controllers[0] is None:
            text = "P1 open"
        else:
            text = ""
        text_surface = resources.get("basicFont").render(text, True, (0, 0, 0))
        text_rect = text_surface.get_rect()
        text_rect.right = self.display.get_rect().centerx -  10
        text_rect.centery = self.display.get_rect().centery + 100
        self.display.blit(text_surface, text_rect)

        if self.player_controllers[1] is None:
            text = "P2 open"
        else:
            text = ""
        text_surface = resources.get("basicFont").render(text, True, (0, 0, 0))
        text_rect = text_surface.get_rect()
        text_rect.left = self.display.get_rect().centerx +  10
        text_rect.centery = self.display.get_rect().centery + 100
        self.display.blit(text_surface, text_rect)
开发者ID:fischerq,项目名称:devmania2014,代码行数:94,代码来源:Menu.py


注:本文中的fysom.Fysom.trigger方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。