Exemplo n.º 1
0
    def load_yaml(yaml_node, global_bot_scripting, smalltalk_rule2grammar,
                  constants, text_utils):
        scenario = Scenario()
        scenario.name = yaml_node['name']
        if 'priority' in yaml_node:
            scenario.priority = int(yaml_node['priority'])
        else:
            scenario.priority = 10  # дефолтный уровень приоритета

        if 'steps_policy' in yaml_node:
            scenario.steps_policy = yaml_node[
                'steps_policy']  # TODO - сделать проверку значения
        else:
            scenario.steps_policy = 'sequential'

        if 'chitchat_questions_per_step_rate' in yaml_node:
            scenario.chitchat_questions_per_step_rate = yaml_node[
                'chitchat_questions_per_step_rate']
        else:
            scenario.chitchat_questions_per_step_rate = 0

        if 'steps' in yaml_node:
            for step_node in yaml_node['steps']:
                step = ActorBase.from_yaml(step_node, constants, text_utils)
                scenario.steps.append(step)

        if 'on_start' in yaml_node:
            scenario.on_start = ActorBase.from_yaml(yaml_node['on_start'],
                                                    constants, text_utils)

        if 'termination_policy' in yaml_node:
            scenario.termination_policy.load_yaml(
                yaml_node['termination_policy'], constants, text_utils)

        if 'on_finish' in yaml_node:
            scenario.on_finish = ActorBase.from_yaml(yaml_node['on_finish'],
                                                     constants, text_utils)

        if 'smalltalk_rules' in yaml_node:
            scenario.smalltalk_rules = SmalltalkRules()
            scenario.smalltalk_rules.load_yaml(yaml_node['smalltalk_rules'],
                                               smalltalk_rule2grammar,
                                               constants, text_utils)

        scenario.insteadof_rules = []
        if 'rules' in yaml_node:
            for rule in yaml_node['rules']:
                rule = ScriptingRule.from_yaml(rule['rule'], constants,
                                               text_utils)
                scenario.insteadof_rules.append(rule)

        if 'insteadof_rule_import' in yaml_node:
            insteadof_rule_import = yaml_node['insteadof_rule_import']
            if insteadof_rule_import == 'from_global':
                # добавляем в список глобальные insteadof-правила
                scenario.insteadof_rules.extend(
                    global_bot_scripting.insteadof_rules)

        return scenario
Exemplo n.º 2
0
    def __init__(self, yaml_node, constants, text_utils):
        self.rule_name = None
        if 'name' in yaml_node:
            self.rule_name = yaml_node['name']

        if 'priority' in yaml_node:
            self.priority = float(yaml_node['priority'])
        else:
            self.priority = 1.0

        self.condition1 = BaseRuleCondition.from_yaml(
            yaml_node['switch']['when'], constants, text_utils)
        self.case_handlers = []
        self.default_handler = None
        cases = yaml_node['switch']['cases']
        for answer_case in cases:
            answer_case = answer_case['case']
            if 'if' in answer_case:
                case_handler = ScriptingRuleIf(answer_case, constants,
                                               text_utils)
                self.case_handlers.append(case_handler)
            else:
                raise NotImplementedError()

        if 'default' in yaml_node['switch']:
            self.default_handler = ActorBase.from_yaml(
                yaml_node['switch']['default'], constants, text_utils)
Exemplo n.º 3
0
    def from_yaml(yaml_node):
        form = VerbalForm()
        form.name = yaml_node['name']
        form.ok_action = yaml_node['action']
        form.compiled_ok_action = ActorBase.from_yaml(form.ok_action)

        if 'fields' in yaml_node:
            for field_node in yaml_node['fields']:
                field = VerbalFormField()
                field_node = field_node['field']
                field.name = field_node['name']
                field.question = field_node['question']
                if 'from_entity' in field_node:
                    field.from_entity = field_node['from_entity']
                    field.source = 'entity'
                elif 'from_reflection' in field_node:
                    field.from_reflection = field_node['from_reflection']
                    field.source = 'reflection'
                elif 'source' in field_node:
                    field.source = field_node['source']
                    if field.source not in ('raw_response', 'entity',
                                            'reflection'):
                        logging.error(u'Unknown field source "%s"',
                                      field.source)
                        raise RuntimeError()
                else:
                    # TODO: сделать внятную диагностику
                    raise NotImplementedError()

                form.fields.append(field)

        # TODO: сделать загрузку instead-of и smalltalk правил, которые будут проверяться при работе форму.
        #self.insteadof_rules

        return form
Exemplo n.º 4
0
    def load_yaml(yaml_node, smalltalk_rule2grammar, constants, text_utils):
        scenario = Scenario()
        scenario.name = yaml_node['name']
        if 'priority' in yaml_node:
            scenario.priority = int(yaml_node['priority'])
        else:
            scenario.priority = 10  # дефолтный уровень приоритета

        if 'steps_policy' in yaml_node:
            scenario.steps_policy = yaml_node[
                'steps_policy']  # TODO - сделать проверку значения
        else:
            scenario.steps_policy = 'sequential'

        if 'steps' in yaml_node:
            for step_node in yaml_node['steps']:
                step = ActorBase.from_yaml(step_node, constants, text_utils)
                scenario.steps.append(step)

        if 'on_start' in yaml_node:
            scenario.on_start = ActorBase.from_yaml(yaml_node['on_start'],
                                                    constants, text_utils)

        if 'on_finish' in yaml_node:
            scenario.on_finish = ActorBase.from_yaml(yaml_node['on_finish'],
                                                     constants, text_utils)

        if 'smalltalk_rules' in yaml_node:
            scenario.smalltalk_rules = SmalltalkRules()
            scenario.smalltalk_rules.load_yaml(yaml_node['smalltalk_rules'],
                                               smalltalk_rule2grammar,
                                               constants, text_utils)

        if 'rules' in yaml_node:
            scenario.insteadof_rules = []
            for rule in yaml_node['rules']:
                rule = ScriptingRule.from_yaml(rule['rule'], constants,
                                               text_utils)
                scenario.insteadof_rules.append(rule)

        return scenario
Exemplo n.º 5
0
    def __init__(self, yaml_node, constants, text_utils):
        self.condition = BaseRuleCondition.from_yaml(yaml_node['if'], constants, text_utils)
        self.compiled_action = ActorBase.from_yaml(yaml_node['then'], constants, text_utils)

        if 'name' in yaml_node:
            self.rule_name = yaml_node['name']
        else:
            self.rule_name = self.condition.get_short_repr()

        if 'priority' in yaml_node:
            self.priority = float(yaml_node['priority'])
        else:
            self.priority = 1.0
Exemplo n.º 6
0
    def __init__(self, yaml_node):
        self.condition1 = BaseRuleCondition.from_yaml(
            yaml_node['switch']['when'])
        self.case_handlers = []
        self.default_handler = None
        cases = yaml_node['switch']['cases']
        for answer_case in cases:
            answer_case = answer_case['case']
            if 'if' in answer_case:
                case_handler = ScriptingRuleIf(answer_case['if'],
                                               answer_case['then'])
                self.case_handlers.append(case_handler)
            else:
                raise NotImplementedError()

        if 'default' in yaml_node['switch']:
            self.default_handler = ActorBase.from_yaml(
                yaml_node['switch']['default'])
Exemplo n.º 7
0
 def from_say_actor(step_index, step_node, constants, text_utils):
     step = ScenarioStep()
     step.name = str(step_index)
     step.say = ActorBase.from_yaml(step_node, constants, text_utils)
     return step
Exemplo n.º 8
0
    def load_yaml(yaml_node, global_bot_scripting, smalltalk_rule2grammar, constants, text_utils):
        scenario = Scenario()
        scenario.name = yaml_node['name']
        try:
            if 'priority' in yaml_node:
                scenario.priority = int(yaml_node['priority'])
            else:
                scenario.priority = 10  # дефолтный уровень приоритета

            if 'steps_policy' in yaml_node:
                scenario.steps_policy = yaml_node['steps_policy']
                if scenario.steps_policy not in 'sequential random graf'.split():
                    raise RuntimeError('Scenario "{}" loading error: unknown step_policy "{}"'.format(scenario.name, scenario.steps_policy))
            else:
                scenario.steps_policy = 'sequential'

            if 'chitchat_questions_per_step_rate' in yaml_node:
                scenario.chitchat_questions_per_step_rate = yaml_node['chitchat_questions_per_step_rate']
            else:
                scenario.chitchat_questions_per_step_rate = 0

            if 'steps' in yaml_node:
                if scenario.steps_policy == 'graf':
                    for step_node in yaml_node['steps']:
                        step = ScenarioStep.load_yaml(step_node['step'], constants, text_utils)
                        scenario.steps.append(step)
                else:
                    for step_node in yaml_node['steps']:
                        step = ScenarioStep.from_say_actor(len(scenario.steps), step_node, constants, text_utils)
                        scenario.steps.append(step)

            if 'on_start' in yaml_node:
                scenario.on_start = ActorBase.from_yaml(yaml_node['on_start'], constants, text_utils)

            if 'termination_policy' in yaml_node:
                scenario.termination_policy.load_yaml(yaml_node['termination_policy'], constants, text_utils)

            if 'on_finish' in yaml_node:
                scenario.on_finish = ActorBase.from_yaml(yaml_node['on_finish'], constants, text_utils)

            if 'smalltalk_rules' in yaml_node:
                scenario.smalltalk_rules = SmalltalkRules()
                scenario.smalltalk_rules.load_yaml(yaml_node['smalltalk_rules'], smalltalk_rule2grammar, constants, text_utils)

            scenario.insteadof_rules = []
            if 'rules' in yaml_node:
                for rule in yaml_node['rules']:
                    rule = ScriptingRule.from_yaml(rule['rule'], constants, text_utils)
                    scenario.insteadof_rules.append(rule)

            if 'insteadof_rules_import' in yaml_node:
                insteadof_rule_import = yaml_node['insteadof_rules_import']
                if insteadof_rule_import == 'from_global':
                    # добавляем в список глобальные insteadof-правила
                    scenario.insteadof_rules.extend(global_bot_scripting.insteadof_rules)

            if 'story_rules_import' in yaml_node:
                insteadof_rule_import = yaml_node['story_rules_import']
                if insteadof_rule_import == 'from_global':
                    # добавляем в список глобальные insteadof-правила
                    scenario.story_rules.extend(global_bot_scripting.story_rules)

        except Exception as ex:
            print('Error occured in scenario "{}" body parsing:\n{}'.format(scenario.name, str(ex)))
            raise

        return scenario
Exemplo n.º 9
0
 def __init__(self, condition, action):
     self.condition = BaseRuleCondition.from_yaml(condition)
     self.action = action
     self.compiled_action = ActorBase.from_yaml(action)