Ejemplo n.º 1
0
    def setUp(self):
        super(MultipleJumpsFromNormalStateTests, self).setUp()
        self.restriction = restrictions.MultipleJumpsFromNormalState()

        self.kb += [
            facts.Start(uid='start', type='test', nesting=0),
            facts.Choice(uid='state_1'),
            facts.State(uid='state_2'),
            facts.Question(uid='state_3', condition=()),
            facts.Jump(state_from='state_2', state_to='state_3'),
            facts.Finish(start='start', uid='finish_1', results={}, nesting=0),
            facts.Finish(start='start', uid='finish_2', results={}, nesting=0),
            facts.Jump(state_from='start', state_to='state_1'),
            facts.Option(state_from='state_1',
                         state_to='state_2',
                         type='opt_1',
                         markers=()),
            facts.Option(state_from='state_1',
                         state_to='finish_1',
                         type='opt_2',
                         markers=()),
            facts.Answer(state_from='state_3',
                         state_to='finish_1',
                         condition=True),
            facts.Answer(state_from='state_3',
                         state_to='finish_2',
                         condition=False)
        ]
Ejemplo n.º 2
0
    def test_do_step__next_jump(self):
        jump_1 = facts.Jump(state_from=self.start.uid, state_to=self.state_1.uid)
        jump_2 = facts.Jump(state_from=self.state_1.uid, state_to=self.state_2.uid)
        self.kb += [jump_1, jump_2]

        self.machine.step()

        calls_manager = mock.MagicMock()

        pointer = self.machine.pointer
        self.assertEqual(pointer.state, self.start.uid)
        self.assertEqual(pointer.jump, None)

        with mock.patch.object(self.machine.interpreter, 'on_state__before_actions') as on_state__before_actions:
            with mock.patch.object(self.machine.interpreter, 'on_state__after_actions') as on_state__after_actions:
                with mock.patch.object(self.machine.interpreter, 'on_jump_start__before_actions') as on_jump_start__before_actions:
                    with mock.patch.object(self.machine.interpreter, 'on_jump_start__after_actions') as on_jump_start__after_actions:
                        with mock.patch.object(self.machine.interpreter, 'on_jump_end__before_actions') as on_jump_end__before_actions:
                            with mock.patch.object(self.machine.interpreter, 'on_jump_end__after_actions') as on_jump_end__after_actions:

                                calls_manager.attach_mock(on_state__before_actions, 'on_state__before_actions')
                                calls_manager.attach_mock(on_state__after_actions, 'on_state__after_actions')
                                calls_manager.attach_mock(on_jump_start__before_actions, 'on_jump_start__before_actions')
                                calls_manager.attach_mock(on_jump_start__after_actions, 'on_jump_start__after_actions')
                                calls_manager.attach_mock(on_jump_end__before_actions, 'on_jump_end__before_actions')
                                calls_manager.attach_mock(on_jump_end__after_actions, 'on_jump_end__after_actions')

                                self.machine.step()

                                self.assertEqual(calls_manager.mock_calls, [mock.call.on_jump_start__before_actions(jump=jump_1),
                                                                            mock.call.on_jump_start__after_actions(jump=jump_1)])

        pointer = self.machine.pointer
        self.assertEqual(pointer.state, self.start.uid)
        self.assertEqual(pointer.jump, jump_1.uid)
Ejemplo n.º 3
0
    def test_get_available_jumps__all_jumps(self):
        jump_1 = facts.Jump(state_from=self.start.uid, state_to=self.state_1.uid)
        jump_2 = facts.Jump(state_from=self.start.uid, state_to=self.state_2.uid)
        self.kb += [jump_1, jump_2]

        self.assertEqual(set(jump.uid for jump in self.machine.get_available_jumps(self.start)),
                         set([jump_1.uid, jump_2.uid]))
Ejemplo n.º 4
0
    def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position):

        hero = selector.heroes()[0]

        ns = selector._kb.get_next_ns()

        start = facts.Start(uid=ns+'start',
                            type=cls.TYPE,
                            nesting=nesting,
                            description='Начало: помочь знакомому',
                            require=[requirements.LocatedIn(object=hero.uid, place=initiator_position.uid),
                                     requirements.LocatedIn(object=receiver.uid, place=receiver_position.uid)],
                            actions=[actions.Message(type='intro')])

        participants = [facts.QuestParticipant(start=start.uid, participant=initiator.uid, role=ROLES.INITIATOR),
                        facts.QuestParticipant(start=start.uid, participant=receiver.uid, role=ROLES.RECEIVER) ]

        finish_successed = facts.Finish(uid=ns+'finish_successed',
                                        start=start.uid,
                                        results={ initiator.uid: RESULTS.SUCCESSED,
                                                  receiver.uid: RESULTS.SUCCESSED},
                                        nesting=nesting,
                                        description='помощь оказана',
                                        require=[requirements.LocatedIn(object=hero.uid, place=initiator_position.uid)],
                                        actions=[actions.GiveReward(object=hero.uid, type='finish_successed')])

        finish_failed = facts.Finish(uid=ns+'finish_failed',
                                     start=start.uid,
                                     results={ initiator.uid: RESULTS.FAILED,
                                               receiver.uid: RESULTS.FAILED},
                                     nesting=nesting,
                                     description='не удалось помочь',
                                     actions=[actions.GiveReward(object=hero.uid, type='finish_failed')])

        help_quest = selector.create_quest_from_person(nesting=nesting+1, initiator=receiver, tags=('can_continue',))
        help_extra = []

        for help_fact in logic.filter_subquest(help_quest, nesting):
            if isinstance(help_fact, facts.Start):
                help_extra.append(facts.Jump(state_from=start.uid, state_to=help_fact.uid, start_actions=[actions.Message(type='before_help')]))
            elif isinstance(help_fact, facts.Finish):
                if help_fact.results[receiver.uid] == RESULTS.SUCCESSED:
                    help_extra.append(facts.Jump(state_from=help_fact.uid, state_to=finish_successed.uid, start_actions=[actions.Message(type='after_successed_help')]))
                else:
                    help_extra.append(facts.Jump(state_from=help_fact.uid, state_to=finish_failed.uid))

        subquest = facts.SubQuest(uid=ns+'help_subquest', members=logic.get_subquest_members(help_quest))

        line = [ start,
                 finish_successed,
                 finish_failed,
                 subquest ]

        line.extend(participants)
        line.extend(help_quest)
        line.extend(help_extra)

        return line
Ejemplo n.º 5
0
    def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position):

        hero = selector.heroes()[0]

        ns = selector._kb.get_next_ns()

        start = facts.Start(uid=ns+'start',
                            type=cls.TYPE,
                            nesting=nesting,
                            description=u'Начало: посетить родной города',
                            require=[requirements.LocatedIn(object=hero.uid, place=initiator_position.uid)],
                            actions=[actions.Message(type='intro')])

        participants = [facts.QuestParticipant(start=start.uid, participant=receiver_position.uid, role=ROLES.RECEIVER_POSITION) ]

        arriving = facts.State(uid=ns+'arriving',
                               description=u'Прибытие в город',
                               require=[requirements.LocatedIn(object=hero.uid, place=receiver_position.uid)])

        action_choices = [facts.State(uid=ns+'drunk_song', description=u'спеть пьяную песню', actions=[actions.Message(type='drunk_song'),
                                                                                                       actions.DoNothing(type='drunk_song')]),
                          facts.State(uid=ns+'stagger_streets', description=u'пошататься по улицам', actions=[actions.Message(type='stagger_streets'),
                                                                                                              actions.DoNothing(type='stagger_streets')]),
                          facts.State(uid=ns+'chatting', description=u'пообщаться с друзьями', actions=[actions.Message(type='chatting'),
                                                                                                        actions.DoNothing(type='chatting')]),
                          facts.State(uid=ns+'search_old_friends', description=u'искать старого друга', actions=[actions.Message(type='search_old_friends'),
                                                                                                                 actions.DoNothing(type='search_old_friends')]),
                          facts.State(uid=ns+'remember_names', description=u'вспоминать имена друзей', actions=[actions.Message(type='remember_names'),
                                                                                                                actions.DoNothing(type='remember_names')])]

        home_actions = random.sample(action_choices, 3)

        finish = facts.Finish(uid=ns+'finish',
                              start=start.uid,
                              results={ receiver_position.uid: RESULTS.SUCCESSED},
                              nesting=nesting,
                              description=u'завершить посещение города',
                              actions=[actions.GiveReward(object=hero.uid, type='finish')])

        line = [ start,

                 facts.Jump(state_from=start.uid, state_to=arriving.uid),

                 arriving,

                 facts.Jump(state_from=arriving.uid, state_to=home_actions[0].uid),
                 facts.Jump(state_from=home_actions[0].uid, state_to=home_actions[1].uid),
                 facts.Jump(state_from=home_actions[1].uid, state_to=home_actions[2].uid),
                 facts.Jump(state_from=home_actions[2].uid, state_to=finish.uid),

                 finish
               ]

        line.extend(home_actions)
        line.extend(participants)

        return line
Ejemplo n.º 6
0
    def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position):

        hero = selector.heroes()[0]

        ns = selector._kb.get_next_ns()

        start = facts.Start(uid=ns+'start',
                            type=cls.TYPE,
                            nesting=nesting,
                            description=u'Начало: навестить соратника',
                            require=[requirements.LocatedIn(object=hero.uid, place=initiator_position.uid)],
                            actions=[actions.Message(type='intro')])

        participants = [facts.QuestParticipant(start=start.uid, participant=receiver.uid, role=ROLES.RECEIVER) ]

        meeting = facts.State(uid=ns+'meeting',
                              description=u'встреча с соратником',
                              require=[requirements.LocatedIn(object=hero.uid, place=receiver_position.uid)])

        finish_meeting = facts.Finish(uid=ns+'finish_meeting',
                                      start=start.uid,
                                      results={receiver.uid: RESULTS.SUCCESSED},
                                      nesting=nesting,
                                      description=u'соратнику оказана помощь',
                                      require=[requirements.LocatedIn(object=hero.uid, place=receiver_position.uid)],
                                      actions=[actions.GiveReward(object=hero.uid, type='finish_meeting'),
                                               actions.GivePower(object=receiver.uid, power=1)])

        help_quest = selector.create_quest_from_person(nesting=nesting+1, initiator=receiver, tags=('can_continue',))
        help_extra = []

        for help_fact in logic.filter_subquest(help_quest, nesting):
            if isinstance(help_fact, facts.Start):
                help_extra.append(facts.Jump(state_from=meeting.uid, state_to=help_fact.uid, start_actions=[actions.Message(type='before_help')]))
            elif isinstance(help_fact, facts.Finish):
                if help_fact.results[receiver.uid] == RESULTS.SUCCESSED:
                    help_extra.append(facts.Jump(state_from=help_fact.uid, state_to=finish_meeting.uid, start_actions=[actions.Message(type='after_help')]))

        subquest = facts.SubQuest(uid=ns+'help_subquest', members=logic.get_subquest_members(help_quest))

        line = [ start,

                 facts.Jump(state_from=start.uid, state_to=meeting.uid),

                 meeting,

                 finish_meeting,

                 subquest
                ]

        line.extend(participants)
        line.extend(help_quest)
        line.extend(help_extra)

        return line
Ejemplo n.º 7
0
 def setUp(self):
     super(AllStatesHasJumpsTests, self).setUp()
     self.kb += [
         facts.Start(uid='start', type='test', nesting=0),
         facts.State(uid='state_1'),
         facts.Finish(start='start', uid='finish_1', results={}, nesting=0),
         facts.Jump(state_from='start', state_to='state_1'),
         facts.Jump(state_from='state_1', state_to='finish_1')
     ]
     self.restriction = restrictions.AllStatesHasJumps()
Ejemplo n.º 8
0
    def test_get_next_jump__require_one_jump__exception(self):
        jump_1 = facts.Jump(state_from=self.start.uid,
                            state_to=self.state_1.uid)
        jump_2 = facts.Jump(state_from=self.start.uid,
                            state_to=self.state_2.uid)
        self.kb += [jump_1, jump_2]

        self.assertRaises(exceptions.MoreThenOneJumpsAvailableError,
                          self.machine.get_next_jump,
                          self.start,
                          single=True)
Ejemplo n.º 9
0
    def test_get_next_jump__all_jumps(self):
        jump_1 = facts.Jump(state_from=self.start.uid, state_to=self.state_1.uid)
        jump_2 = facts.Jump(state_from=self.start.uid, state_to=self.state_2.uid)
        self.kb += [jump_1, jump_2]

        jumps = set()

        for i in xrange(100):
            jumps.add(self.machine.get_next_jump(self.start, single=False).uid)

        self.assertEqual(jumps, set([jump_1.uid, jump_2.uid]))
Ejemplo n.º 10
0
    def test_get_start_state(self):
        start_2 = facts.Start(uid='s2', type='test', nesting=0)
        start_3 = facts.Start(uid='start_3', type='test', nesting=0)


        self.kb += [ start_2,
                     start_3,
                     facts.Jump(state_from=self.start.uid, state_to=start_2.uid),
                     facts.Jump(state_from=start_3.uid, state_to=self.start.uid) ]

        self.assertEqual(self.machine.get_start_state().uid, start_3.uid)
Ejemplo n.º 11
0
    def construct(cls, nesting, selector, initiator, initiator_position,
                  receiver, receiver_position):

        hero = selector.heroes()[0]

        ns = selector._kb.get_next_ns()

        start = facts.Start(uid=ns + 'start',
                            type=cls.TYPE,
                            nesting=nesting,
                            description=u'Начало: простейшее задание',
                            require=[
                                requirements.LocatedIn(
                                    object=hero.uid,
                                    place=initiator_position.uid)
                            ],
                            actions=[actions.Message(type='intro')])

        participants = [
            facts.QuestParticipant(start=start.uid,
                                   participant=receiver_position.uid,
                                   role=ROLES.RECEIVER_POSITION)
        ]

        arriving = facts.State(uid=ns + 'arriving',
                               description=u'Прибытие в другой город',
                               require=[
                                   requirements.LocatedIn(
                                       object=hero.uid,
                                       place=receiver_position.uid)
                               ])

        facts.State(uid=ns + 'any_action',
                    description=u'выполнить какое-то действие',
                    actions=[actions.Message(type='do smth')])

        finish = facts.Finish(
            uid=ns + 'finish',
            start=start.uid,
            results={receiver_position.uid: RESULTS.SUCCESSED},
            nesting=nesting,
            description=u'завершить задание',
            actions=[actions.GiveReward(object=hero.uid, type='finish')])

        line = [
            start,
            facts.Jump(state_from=start.uid, state_to=arriving.uid), arriving,
            facts.Jump(state_from=arriving.uid, state_to=finish.uid), finish
        ]

        line.extend(participants)

        return line
Ejemplo n.º 12
0
    def test_get_nearest_choice__choice_after_finish(self):
        choice = facts.Choice(uid='choice')
        option_1 = facts.Option(state_from=choice.uid, state_to=self.state_1.uid, type='opt_1', markers=())
        option_2 = facts.Option(state_from=choice.uid, state_to=self.state_2.uid, type='opt_2', markers=())
        path = facts.ChoicePath(choice=choice.uid, option=option_2.uid, default=True)
        self.kb += (choice,
                    option_1,
                    option_2,
                    path,
                    facts.Jump(state_from=self.start.uid, state_to=self.finish_1.uid),
                    facts.Jump(state_from=self.finish_1.uid, state_to=choice.uid))

        self.assertEqual(self.machine.get_nearest_choice(), (None, None, None))
Ejemplo n.º 13
0
    def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position):

        hero = selector.heroes()[0]

        ns = selector._kb.get_next_ns()

        start = facts.Start(uid=ns+'start',
                            type=cls.TYPE,
                            nesting=nesting,
                            description=u'Начало: посетить святой город',
                            require=[requirements.LocatedIn(object=hero.uid, place=initiator_position.uid)],
                            actions=[actions.Message(type='intro')])

        participants = [facts.QuestParticipant(start=start.uid, participant=receiver_position.uid, role=ROLES.RECEIVER_POSITION) ]

        arriving = facts.State(uid=ns+'arriving',
                               description=u'Прибытие в город',
                               require=[requirements.LocatedIn(object=hero.uid, place=receiver_position.uid)])

        action_choices = [facts.State(uid=ns+'speak_with_guru', description=u'поговорить с гуру', actions=[actions.Message(type='speak_with_guru'),
                                                                                                           actions.DoNothing(type='speak_with_guru')]),
                          facts.State(uid=ns+'stagger_holy_streets', description=u'пошататься по улицам', actions=[actions.Message(type='stagger_holy_streets'),
                                                                                                                   actions.DoNothing(type='stagger_holy_streets')])]

        holy_actions = random.sample(action_choices, 1)

        finish = facts.Finish(uid=ns+'finish',
                              start=start.uid,
                              results={ receiver_position.uid: RESULTS.SUCCESSED},
                              nesting=nesting,
                              description=u'завершить посещение города',
                              actions=[actions.GiveReward(object=hero.uid, type='finish'),
                                       actions.GivePower(object=receiver_position.uid, power=1)])

        line = [ start,

                 facts.Jump(state_from=start.uid, state_to=arriving.uid),

                 arriving,

                 facts.Jump(state_from=arriving.uid, state_to=holy_actions[0].uid),
                 facts.Jump(state_from=holy_actions[0].uid, state_to=finish.uid),

                 finish
               ]

        line.extend(holy_actions)
        line.extend(participants)

        return line
Ejemplo n.º 14
0
    def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position):

        hero = selector.heroes()[0]

        ns = selector._kb.get_next_ns()

        antagonist = selector.new_person(first_initiator=False)
        antagonist_position = selector.place_for(objects=(antagonist.uid,))

        start = facts.Start(uid=ns+'start',
                            type=cls.TYPE,
                            nesting=nesting,
                            description=u'Начало: навредить противнику',
                            require=[requirements.LocatedIn(object=hero.uid, place=initiator_position.uid)],
                            actions=[actions.Message(type='intro')])

        participants = [facts.QuestParticipant(start=start.uid, participant=receiver.uid, role=ROLES.RECEIVER),
                        facts.QuestParticipant(start=start.uid, participant=antagonist_position.uid, role=ROLES.ANTAGONIST_POSITION) ]

        finish = facts.Finish(uid=ns+'finish',
                              start=start.uid,
                              results={ receiver.uid: RESULTS.FAILED,
                                        antagonist_position.uid: RESULTS.NEUTRAL},
                              nesting=nesting,
                              description=u'навредили противнику',
                              actions=[actions.GiveReward(object=hero.uid, type='finish')])

        help_quest = selector.create_quest_between_2(nesting=nesting+1, initiator=antagonist, receiver=receiver, tags=('can_continue',))
        help_extra = []

        for help_fact in logic.filter_subquest(help_quest, nesting):
            if isinstance(help_fact, facts.Start):
                help_extra.append(facts.Jump(state_from=start.uid, state_to=help_fact.uid))
            elif isinstance(help_fact, facts.Finish):
                if help_fact.results[receiver.uid] == RESULTS.FAILED:
                    help_extra.append(facts.Jump(state_from=help_fact.uid, state_to=finish.uid, start_actions=[actions.Message(type='after_interfere')]))

        subquest = facts.SubQuest(uid=ns+'interfere_subquest', members=logic.get_subquest_members(help_quest))

        line = [ start,
                 finish,
                 subquest
                ]

        line.extend(participants)
        line.extend(help_quest)
        line.extend(help_extra)

        return line
Ejemplo n.º 15
0
    def setUp(self):
        super(NoCirclesInStateJumpGraphTests, self).setUp()
        self.restriction = restrictions.NoCirclesInStateJumpGraph()

        self.kb += [
            facts.Start(uid='start', type='test', nesting=0),
            facts.State(uid='state_1'),
            facts.State(uid='state_2'),
            facts.Start(uid='start_3', type='test', nesting=0),
            facts.Finish(start='start', uid='finish_1', results={}, nesting=0),
            facts.Jump(state_from='start', state_to='state_1'),
            facts.Jump(state_from='state_1', state_to='state_2'),
            facts.Jump(state_from='state_2', state_to='start_3'),
            facts.Jump(state_from='start_3', state_to='finish_1')
        ]
Ejemplo n.º 16
0
 def test_error(self):
     self.kb += [
         facts.State(uid='state_2'),
         facts.Jump(state_from='start', state_to='state_2')
     ]
     self.assertRaises(self.restriction.Error, self.restriction.validate,
                       self.kb)
Ejemplo n.º 17
0
    def test_get_nearest_choice__2_choices__second_choice(self):
        choice_1 = facts.Choice(uid='choice_1')

        choice_2 = facts.Choice(uid='choice_2')
        option_2_1 = facts.Option(state_from=choice_2.uid, state_to=self.state_1.uid, type='opt_2_1', markers=())
        option_2_2 = facts.Option(state_from=choice_2.uid, state_to=self.state_2.uid, type='opt_2_2', markers=())
        path_2 = facts.ChoicePath(choice=choice_2.uid, option=option_2_2.uid, default=True)

        option_1_1 = facts.Option(state_from=choice_1.uid, state_to=self.state_1.uid, type='opt_1_1', markers=())
        option_1_2 = facts.Option(state_from=choice_1.uid, state_to=choice_2.uid, type='opt_1_2', markers=())
        path_1 = facts.ChoicePath(choice=choice_1.uid, option=option_1_2.uid, default=True)

        self.kb += (choice_1,
                    option_1_1,
                    option_1_2,
                    path_1,

                    choice_2,
                    option_2_1,
                    option_2_2,
                    path_2,
                    facts.Jump(state_from=self.start.uid, state_to=choice_1.uid),

                    facts.Pointer(state=choice_2.uid)
            )

        choice_, options_, path_ = self.machine.get_nearest_choice()
        self.assertEqual(choice_.uid, choice_2.uid)
        self.assertEqual(set(o.uid for o in options_), set([option_2_1.uid, option_2_2.uid]))
        self.assertEqual([p.uid for p in path_], [path_2.uid])
Ejemplo n.º 18
0
    def construct_from_place(cls, nesting, selector, start_place):
        from questgen.quests.base_quest import ROLES

        initiator = selector.new_person(first_initiator=(nesting==0), restrict_places=False, places=(start_place.uid, ))
        receiver = selector.new_person(first_initiator=False)

        initiator_position = selector.place_for(objects=(initiator.uid,))
        receiver_position = selector.place_for(objects=(receiver.uid,))

        ns = selector._kb.get_next_ns()

        start = facts.Start(uid=ns+'start', type=cls.TYPE, nesting=nesting)

        choice_1 = facts.Choice(uid=ns+'choice_1')

        choice_2 = facts.Choice(uid=ns+'choice_2')

        finish_1_1 = facts.Finish(uid=ns+'finish_1_1',
                                  start=start.uid,
                                  results={initiator.uid: RESULTS.SUCCESSED,
                                           initiator_position.uid: RESULTS.FAILED,
                                           receiver.uid: RESULTS.SUCCESSED,
                                           receiver_position.uid: RESULTS.SUCCESSED},
                                  nesting=nesting)
        finish_1_2 = facts.Finish(uid=ns+'finish_1_2',
                                  start=start.uid,
                                  results={initiator.uid: RESULTS.FAILED,
                                           initiator_position.uid: RESULTS.FAILED,
                                           receiver.uid: RESULTS.FAILED,
                                           receiver_position.uid: RESULTS.FAILED},
                                  nesting=nesting)
        finish_2 = facts.Finish(uid=ns+'finish_2',
                                start=start.uid,
                                results={initiator.uid: RESULTS.SUCCESSED,
                                         initiator_position.uid: RESULTS.SUCCESSED,
                                         receiver.uid: RESULTS.FAILED,
                                         receiver_position.uid: RESULTS.FAILED},
                                nesting=nesting)

        participants = [facts.QuestParticipant(start=start.uid, participant=initiator.uid, role=ROLES.INITIATOR),
                        facts.QuestParticipant(start=start.uid, participant=initiator_position.uid, role=ROLES.INITIATOR_POSITION),
                        facts.QuestParticipant(start=start.uid, participant=receiver.uid, role=ROLES.RECEIVER),
                        facts.QuestParticipant(start=start.uid, participant=receiver_position.uid, role=ROLES.RECEIVER_POSITION) ]

        quest_facts =  [ start,
                         choice_1,
                         choice_2,
                         finish_1_1,
                         finish_1_2,
                         finish_2,

                         facts.Jump(state_from=start.uid, state_to=choice_1.uid),

                         facts.Option(state_from=choice_1.uid, state_to=finish_2.uid, type='opt_1', markers=()),
                         facts.Option(state_from=choice_1.uid, state_to=choice_2.uid, type='opt_2', markers=()),
                         facts.Option(state_from=choice_2.uid, state_to=finish_1_1.uid, type='opt_2_1', markers=()),
                         facts.Option(state_from=choice_2.uid, state_to=finish_1_2.uid, type='opt_2_2', markers=())
                        ]

        return participants + quest_facts
Ejemplo n.º 19
0
    def test_linked_choices__linked_option_with_processed_choice(self):
        is_raised = False

        for i in xrange(100):
            kb = KnowledgeBase()
            start = facts.Start(uid='start', type='test', nesting=0)
            choice_1 = facts.Choice(uid='choice_1')
            choice_2 = facts.Choice(uid='choice_2')
            finish_1 = facts.Finish(uid='finish_1',
                                    results={},
                                    nesting=0,
                                    start='start')
            finish_2 = facts.Finish(uid='finish_2',
                                    results={},
                                    nesting=0,
                                    start='start')

            option_1 = facts.Option(state_from=choice_1.uid,
                                    state_to=finish_1.uid,
                                    type='opt_1',
                                    markers=())
            option_2 = facts.Option(state_from=choice_1.uid,
                                    state_to=choice_2.uid,
                                    type='opt_2',
                                    markers=())

            option_2_1 = facts.Option(state_from=choice_2.uid,
                                      state_to=finish_1.uid,
                                      type='opt_2_1',
                                      markers=())
            option_2_2 = facts.Option(state_from=choice_2.uid,
                                      state_to=finish_2.uid,
                                      type='opt_2_2',
                                      markers=())

            facts_list = [
                start,
                facts.Jump(state_from=start.uid,
                           state_to=choice_1.uid), choice_1, choice_2,
                finish_1, finish_2, option_1, option_2, option_2_1, option_2_2,
                facts.OptionsLink(options=(option_2.uid, option_2_2.uid))
            ]

            kb += facts_list

            try:
                transformators.determine_default_choices(kb)
            except exceptions.LinkedOptionWithProcessedChoiceError:
                is_raised = True
                self.assertEqual(len(list(kb.filter(facts.ChoicePath))), 1)
                self.assertEqual(
                    [path.choice for path in kb.filter(facts.ChoicePath)],
                    [choice_2.uid])
                self.assertEqual(
                    [path.option for path in kb.filter(facts.ChoicePath)],
                    [option_2_1.uid])
                break

        self.assertFalse(is_raised)
Ejemplo n.º 20
0
 def test_state_not_reached(self):
     self.kb += [
         facts.State(uid='state_3'),
         facts.State(uid='state_4'),
         facts.Jump(state_from='state_3', state_to='state_4')
     ]
     self.assertRaises(self.restriction.Error, self.restriction.validate,
                       self.kb)
Ejemplo n.º 21
0
 def test_wrong_end_action(self):
     self.kb += [
         facts.Jump(state_from='start',
                    state_to='state_1',
                    end_actions=[facts.Place(uid='wrong action')])
     ]
     self.assertRaises(self.restriction.Error, self.restriction.validate,
                       self.kb)
Ejemplo n.º 22
0
    def test_do_step__step_after_finish(self):
        jump_1 = facts.Jump(state_from=self.start.uid, state_to=self.finish_1.uid)
        self.kb += jump_1

        self.machine.step()
        self.machine.step()
        self.machine.step()

        self.assertRaises(exceptions.NoJumpsFromLastStateError, self.machine.step)
Ejemplo n.º 23
0
    def construct(cls, nesting, selector, initiator, initiator_position, receiver, receiver_position):

        ns = selector._kb.get_next_ns()

        start = facts.Start(uid=ns+'start',
                            type=cls.TYPE,
                            nesting=nesting,
                            description=u'Начало: самый простой квест')

        participants = [facts.QuestParticipant(start=start.uid, participant=initiator.uid, role=ROLES.INITIATOR),
                        facts.QuestParticipant(start=start.uid, participant=initiator_position.uid, role=ROLES.INITIATOR_POSITION),
                        facts.QuestParticipant(start=start.uid, participant=receiver.uid, role=ROLES.RECEIVER),
                        facts.QuestParticipant(start=start.uid, participant=receiver_position.uid, role=ROLES.RECEIVER_POSITION) ]

        finish_successed = facts.Finish(uid=ns+'finish_successed',
                                        start=start.uid,
                                        results={initiator.uid: RESULTS.SUCCESSED,
                                                 initiator_position.uid: RESULTS.SUCCESSED,
                                                 receiver.uid: RESULTS.SUCCESSED,
                                                 receiver_position.uid: RESULTS.SUCCESSED},
                                        nesting=nesting,
                                        description=u'завершить задание удачно')

        finish_failed = facts.Finish(uid=ns+'finish_failed',
                                     start=start.uid,
                                     results={initiator.uid: RESULTS.FAILED,
                                              initiator_position.uid: RESULTS.FAILED,
                                              receiver.uid: RESULTS.FAILED,
                                              receiver_position.uid: RESULTS.FAILED},
                                     nesting=nesting,
                                     description=u'завершить задание плохо')

        event = facts.Event(uid=ns+'event', members=(finish_successed.uid, finish_failed.uid))

        line = [ start,
                 finish_successed,
                 finish_failed,
                 event,
                 facts.Jump(state_from=start.uid, state_to=finish_successed.uid),
                 facts.Jump(state_from=start.uid, state_to=finish_failed.uid) ]

        line.extend(participants)

        return line
Ejemplo n.º 24
0
 def test_no_restricted_states(self):
     facts_list = [
         facts.Start(uid='start', type='test', nesting=0),
         facts.Finish(uid='st_finish', results={}, nesting=0,
                      start='start'),
         facts.Jump(state_from='start', state_to='st_finish')
     ]
     self.kb += facts_list
     transformators.remove_restricted_states(self.kb)
     self.check_in_knowledge_base(self.kb, facts_list)
Ejemplo n.º 25
0
    def test_can_do_step__requirement_failed(self):
        state_3 = facts.State(uid='state_3', require=[requirements.IsAlive(object='hero')])
        jump_3 = facts.Jump(state_from=self.start.uid, state_to=state_3.uid)
        self.kb += [ state_3, jump_3]

        pointer = self.machine.pointer
        self.kb -= pointer
        self.kb += pointer.change(state=self.start.uid, jump=jump_3.uid)

        self.assertFalse(self.machine.can_do_step())
Ejemplo n.º 26
0
 def test_no_choices(self):
     facts_list = [
         facts.Start(uid='start', type='test', nesting=0),
         facts.Finish(uid='st_finish', results={}, nesting=0,
                      start='start'),
         facts.Jump(state_from='start', state_to='st_finish')
     ]
     self.kb += facts_list
     transformators.determine_default_choices(self.kb)
     self.check_in_knowledge_base(self.kb, facts_list)
     self.assertEqual(len(list(self.kb.filter(facts.OptionsLink))), 0)
Ejemplo n.º 27
0
 def test_simple_tagged_jump(self):
     facts_list = [
         facts.Start(uid='start', type='test', nesting=0),
         facts.Finish(uid='st_finish', results={}, nesting=0,
                      start='start'),
         facts.Jump(state_from='start', state_to='st_finish'),
         facts.Event(uid='event_tag', members=('st_finish', ))
     ]
     self.kb += facts_list
     transformators.activate_events(self.kb)
     self.check_in_knowledge_base(self.kb, facts_list)
Ejemplo n.º 28
0
    def setUp(self):
        super(RemoveBrokenQuestionStatesTests, self).setUp()

        self.question = facts.Question(uid='state_1', condition=())
        self.answer_1 = facts.Answer(state_from='state_1',
                                     state_to='state_2',
                                     condition=True)
        self.answer_2 = facts.Answer(state_from='state_1',
                                     state_to='finish_1',
                                     condition=False)

        self.kb += [
            facts.Start(uid='start', type='test', nesting=0), self.question,
            facts.State(uid='state_2'),
            facts.Finish(start='start', uid='finish_1', results={}, nesting=0),
            facts.Finish(start='start', uid='finish_2', results={}, nesting=0),
            facts.Jump(state_from='start', state_to='state_1'), self.answer_1,
            self.answer_2,
            facts.Jump(state_from='state_2', state_to='finish_2')
        ]
Ejemplo n.º 29
0
    def test_finish_at_not_finish_state(self):
        facts_list = [
            facts.Start(uid='start', type='test', nesting=0),
            facts.Finish(uid='st_finish', results={}, nesting=0,
                         start='start'),
            facts.Jump(state_from='start', state_to='st_finish')
        ]
        self.kb += facts_list

        broken_path = [
            facts.State(uid='st_broken_1'),
            facts.Jump(state_from='start', state_to='st_broken_1')
        ]

        self.kb += broken_path

        transformators.remove_broken_states(self.kb)

        self.check_in_knowledge_base(self.kb, facts_list)
        self.check_not_in_knowledge_base(self.kb, broken_path)
Ejemplo n.º 30
0
    def test_can_do_step__success(self):
        state_3 = facts.State(uid='state_3', require=[requirements.IsAlive(object='hero')])
        jump_3 = facts.Jump(state_from=self.start.uid, state_to=state_3.uid)
        self.kb += [ state_3, jump_3]

        pointer = self.machine.pointer
        self.kb -= pointer
        self.kb += pointer.change(state=self.start.uid, jump=jump_3.uid)

        with mock.patch('questgen.machine.Machine.interpreter', FakeInterpreter(check_is_alive=True)):
            self.assertTrue(self.machine.can_do_step())