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


Python Fysom.can方法代码示例

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


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

示例1: FysomStateTests

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import can [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'}
            ]
        })

    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)
开发者ID:AFDudley,项目名称:fysom,代码行数:32,代码来源:fysom_state_tests.py

示例2: do_stem

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import can [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

示例3: FysomStateTests

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import can [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

示例4: StatesTestCase

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import can [as 别名]
class StatesTestCase(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'}
            ]
        })

    def test_invalid_state(self):
        self.assertRaises(AttributeError, getattr, self.fsm, 'wtfbbq')

    def test_state_conditions(self):
        """
        This will check for example, if state is in clear, will be able to emit
        warn and will not be able to emit panic, calm or clear again.
        """
        self.assertTrue(self.fsm.isstate('green'))
        self.assertTrue(self.fsm.can('warn'))
        self.assertFalse(self.fsm.can('panic'))
        self.assertTrue(self.fsm.cannot('panic'))
        self.assertFalse(self.fsm.can('clam'))
        self.assertFalse(self.fsm.can('clear'))

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

    def test_state_cannot_be_called_twice(self):
        self.fsm.warn()
        self.assertTrue(self.fsm.isstate('yellow'))
        self.assertRaises(FysomError, self.fsm.warn)

    def test_auto_next_state(self):
        self.assertEqual('green', self.fsm.current)

        self.assertEqual('yellow', self.fsm.next())
        self.assertEqual('yellow', self.fsm.current)

    def test_multiple_possibilities_for_next(self):
        fsm = Fysom({
            'initial': 'green',
            'events': [
                {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                {'name': 'panic', 'src': 'green', 'dst': 'red'},
                {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                {'name': 'clear', 'src': 'yellow', 'dst': 'green'}
            ]
        })

        self.assertRaises(MultiplePossibilitesFound, fsm.next)

    def test_no_possibility_for_next(self):
        fsm = Fysom({
            'initial': 'green',
            'events': [
                {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                {'name': 'clear', 'src': 'yellow', 'dst': 'green'}
            ]
        })

        self.assertRaises(NoPossibilityFound, fsm.next)

    def test_goto(self):
        fsm = Fysom({
            'initial': 'green',
            'events': [
                {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                {'name': 'panic', 'src': 'green', 'dst': 'red'},
                {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                {'name': 'clear', 'src': 'yellow', 'dst': 'green'}
            ]
        })
        self.assertRaises(MultiplePossibilitesFound, fsm.next)
        self.assertEqual('red', fsm.goto('panic'))

    def test_no_possibility_for_goto(self):
        fsm = Fysom({
            'initial': 'green',
            'events': [
                {'name': 'warn', 'src': 'green', 'dst': 'yellow'},
                {'name': 'panic', 'src': 'yellow', 'dst': 'red'},
                {'name': 'panic', 'src': 'green', 'dst': 'red'},
                # when call panic from green, we go to red, from red we
                # should go to yellow, but it is commented and we should
                # have a exception
                # {'name': 'calm', 'src': 'red', 'dst': 'yellow'},
                {'name': 'clear', 'src': 'yellow', 'dst': 'green'}
            ]
        })

        self.assertRaises(MultiplePossibilitesFound, fsm.next)
        self.assertEqual('red', fsm.goto('panic'))
#.........这里部分代码省略.........
开发者ID:titansgroup,项目名称:fysom,代码行数:103,代码来源:test_states.py

示例5: Fysom

# 需要导入模块: from fysom import Fysom [as 别名]
# 或者: from fysom.Fysom import can [as 别名]
#!/usr/bin/python
from fysom import Fysom

print "Experimenting finite state machine package."
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' }
	         ]})


print "Current state = " + fsm.current
print "Is in state 'red? = %r" % fsm.isstate('red')
print "Is in state 'green? = %r" % fsm.isstate('green')
print "Can fire event %s from state %s? = %r" % ('warn', fsm.current, fsm.can('warn'))
print "Can fire event %s from state %s? = %r" % ('panic', fsm.current, fsm.can('panic'))
print "Can fire event %s from state %s? = %r" % ('clear', fsm.current, fsm.can('clear'))

print ""
print "Firing event warn"
fsm.warn()
print "Current state = " + fsm.current
print "Is in state 'red? = %r" % fsm.isstate('red')
print "Is in state 'green? = %r" % fsm.isstate('green')
print "Can fire event %s from state %s? = %r" % ('warn', fsm.current, fsm.can('warn'))
print "Can fire event %s from state %s? = %r" % ('panic', fsm.current, fsm.can('panic'))
print "Can fire event %s from state %s? = %r" % ('clear', fsm.current, fsm.can('clear'))
开发者ID:SamuelToh,项目名称:2147483647_story_ptr_project,代码行数:31,代码来源:finite_state_machine_experiment.py


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