Example #1
0
    def test_outputs(self):
        effects = []

        assert Effect(effects) == Effect.parse_effect("Void")
        effects.append(BasicEffect("v1", "val1"))
        assert Effect(effects) == Effect.parse_effect("v1:=val1")

        effects.append(BasicEffect("v2", ValueFactory.create("val2"), 1, False, False))
        assert Effect(effects) == Effect.parse_effect("v1:=val1 ^ v2+=val2")

        effects.append(BasicEffect("v2", ValueFactory.create("val3"), 1, True, True))
        assert Effect(effects) == Effect.parse_effect("v1:=val1 ^ v2+=val2 ^ v2!=val3")
Example #2
0
    def test_param_4(self):
        system = DialogueSystem(TestParameters.domain1)
        system.detach_module(ForwardPlanner)
        system.get_settings().show_gui = False
        system.start_system()

        rules = TestParameters.domain1.get_models()[1].get_rules()
        outputs = rules[0].get_output(Assignment("u_u", "my name is"))
        o = Effect(BasicEffect("u_u^p", "Pierre"))
        assert isinstance(outputs.get_parameter(o), SingleParameter)
        input = Assignment("theta_5", ValueFactory.create("[0.36, 0.24, 0.40]"))
        assert outputs.get_parameter(o).get_value(input) == pytest.approx(0.36, abs=0.01)

        system.get_state().remove_nodes(system.get_state().get_action_node_ids())
        system.get_state().remove_nodes(system.get_state().get_utility_node_ids())
        system.add_content("u_u", "my name is")

        system.get_state().remove_nodes(system.get_state().get_action_node_ids())
        system.get_state().remove_nodes(system.get_state().get_utility_node_ids())
        system.add_content("u_u", "Pierre")

        system.get_state().remove_nodes(system.get_state().get_action_node_ids())
        system.get_state().remove_nodes(system.get_state().get_utility_node_ids())
        system.add_content("u_u", "my name is")

        system.get_state().remove_nodes(system.get_state().get_action_node_ids())
        system.get_state().remove_nodes(system.get_state().get_utility_node_ids())
        system.add_content("u_u", "Pierre")

        assert system.get_state().query_prob("theta_5").to_continuous().get_function().get_mean()[0] == pytest.approx(0.3, abs=0.12)
    def ground(self, grounding):
        """
        Ground the slots in the effect variable and value (given the assignment) and
        returns the resulting effect.

        :param grounding: the grounding
        :return: the grounded effect
        """
        new_t = Template.create(self._label_template.fill_slots(grounding))
        new_v = Template.create(self._value_template.fill_slots(grounding))
        if new_t.is_under_specified() or new_v.is_under_specified():
            return TemplateEffect(new_t, new_v, self._priority,
                                  self._exclusive, self._negated)
        else:
            return BasicEffect(str(new_t), ValueFactory.create(str(new_v)),
                               self._priority, self._exclusive, self._negated)
Example #4
0
    def parse_effect(str_val):
        """
        Parses the string representing the effect, and returns the corresponding effect.

        :param str_val: the string representing the effect
        :return: the corresponding effect
        """
        if " ^ " in str_val:
            effects = list()
            for split in str_val.split(" ^ "):
                sub_output = Effect.parse_effect(split)
                effects += sub_output.get_sub_effects()
            return Effect(effects)

        else:
            if "Void" in str_val:
                return Effect(list())

            var = ""
            val = ""

            exclusive = True
            negated = False

            if ":=" in str_val:
                var = str_val.split(":=")[0]
                val = str_val.split(":=")[1]
                val = "None" if "{}" in val else val
            elif "!=" in str_val:
                var = str_val.split("!=")[0]
                val = str_val.split("!=")[1]
                negated = True
            elif "+=" in str_val:
                var = str_val.split("+=")[0]
                val = str_val.split("+=")[1]
                exclusive = False

            tvar = Template.create(var)
            tval = Template.create(val)
            if tvar.is_under_specified() or tval.is_under_specified():
                return Effect(TemplateEffect(tvar, tval, 1, exclusive,
                                             negated))
            else:
                return Effect(
                    BasicEffect(var, ValueFactory.create(val), 1, exclusive,
                                negated))
    def _get_sub_effect(node, priority):
        """
        Extracts a basic effect from the XML specification.

        :param node: the XML node
        :param priority: the rule priority
        :return: the corresponding basic effect
        """
        node_keys = node.keys()
        if 'var' not in node_keys:
            raise ValueError()

        variable_name = node.attrib['var']

        if 'value' in node_keys:
            value = node.attrib['value']
        elif 'var2' in node_keys:
            value = '{' + node.attrib['var2'] + '}'
        else:
            value = 'None'
        value = re.sub(r'\s+', ' ', value)

        exclusive = True
        if 'exclusive' in node_keys:
            if node.attrib['exclusive'].lower() == 'false':
                exclusive = False

        negated = 'relation' in node_keys and XMLRuleReader._get_relation(node) == Relation.UNEQUAL

        #  "clear" effect is outdated
        if node.tag.lower() == 'clear':
            value = 'None'

        # checking for other attributes
        for attrib_key in node_keys:
            if attrib_key not in ['var', 'var2', 'value', 'relation', 'exclusive']:
                raise ValueError()

        template_variable = Template.create(variable_name)
        template_value = Template.create(value)

        if template_variable.is_under_specified() or template_value.is_under_specified():
            return TemplateEffect(template_variable, template_value, priority, exclusive, negated)
        else:
            return BasicEffect(variable_name, ValueFactory.create(str(template_value)), priority, exclusive, negated)
Example #6
0
    def test_param_5(self):
        system = DialogueSystem(TestParameters.domain2)
        system.detach_module(ForwardPlanner)
        system.get_settings().show_gui = False
        system.start_system()

        rules = TestParameters.domain2.get_models()[0].get_rules()
        outputs = rules[0].get_output(Assignment("u_u", "brilliant"))
        o = Effect(BasicEffect("a_u", "approval"))
        assert isinstance(outputs.get_parameter(o), ComplexParameter)
        input = Assignment([Assignment("theta_6", 2.1), Assignment("theta_7", 1.3)])
        assert outputs.get_parameter(o).get_value(input) == pytest.approx(0.74, abs=0.01)

        system.get_state().remove_nodes(system.get_state().get_action_node_ids())
        system.get_state().remove_nodes(system.get_state().get_utility_node_ids())
        system.add_content("u_u", "brilliant")

        assert system.get_state().query_prob("theta_6").to_continuous().get_function().get_mean()[0] == pytest.approx(1.0, abs=0.08)

        assert system.get_content("a_u").get_prob("approval") == pytest.approx(0.63, abs=0.08)
        assert system.get_content("a_u").get_prob("irony") == pytest.approx(0.3, abs=0.08)