Fixed #49 : working
This commit is contained in:
@@ -1152,7 +1152,7 @@ isinstance(var, Concept) and var.key == 'hello __var__0'""" + \
|
||||
),
|
||||
|
||||
])
|
||||
def test_i_can_get_rete_conditions_from_recognized(self, test_name, expression, variable_name, expected_as_str):
|
||||
def test_i_can_get_rete_using_recognized_function(self, test_name, expression, variable_name, expected_as_str):
|
||||
sheerka, context, greetings, foo = self.init_test().with_concepts(
|
||||
Concept("greetings", definition="hello a", definition_type=DEFINITION_TYPE_DEF).def_var("a"),
|
||||
Concept("foo"),
|
||||
@@ -1185,6 +1185,107 @@ isinstance(var, Concept) and var.key == 'hello __var__0'""" + \
|
||||
matches = list(network.matches)
|
||||
assert len(matches) == 1
|
||||
|
||||
@pytest.mark.parametrize("expression, variable_name, expected_as_str", [
|
||||
(
|
||||
"greetings",
|
||||
None,
|
||||
["#__x_00__|__name__|'__ret'",
|
||||
"#__x_00__|body|#__x_01__",
|
||||
"#__x_01__|__is_concept__|True",
|
||||
"#__x_01__|name|'greetings'"]
|
||||
),
|
||||
(
|
||||
"hello foo",
|
||||
"foo",
|
||||
["#__x_00__|__name__|'__ret'",
|
||||
"#__x_00__|body|#__x_01__",
|
||||
"#__x_01__|__is_concept__|True",
|
||||
"#__x_01__|key|'hello __var__0'",
|
||||
"#__x_01__|a|#__x_02__",
|
||||
"#__x_02__|__is_concept__|True",
|
||||
"#__x_02__|key|'foo'",
|
||||
]
|
||||
),
|
||||
])
|
||||
def test_i_can_get_rete_when_a_concept_is_recognized(self, expression, variable_name, expected_as_str):
|
||||
sheerka, context, greetings, foo = self.init_test().with_concepts(
|
||||
Concept("greetings", definition="hello a", definition_type=DEFINITION_TYPE_DEF).def_var("a"),
|
||||
Concept("foo"),
|
||||
).unpack()
|
||||
parser = ExpressionParser()
|
||||
expected = get_rete_conditions(*expected_as_str)
|
||||
|
||||
error_sink = ErrorSink()
|
||||
parser_input = ParserInput(expression)
|
||||
parser.reset_parser_input(parser_input, error_sink)
|
||||
parsed = parser.parse_input(context, parser_input, error_sink)
|
||||
|
||||
visitor = ReteConditionExprVisitor(context)
|
||||
conditions = visitor.get_conditions(parsed)
|
||||
|
||||
assert conditions == [expected]
|
||||
|
||||
# check against a Rete network
|
||||
network = ReteNetwork()
|
||||
rule = Rule("test", expression, None)
|
||||
rule.metadata.id = 9999
|
||||
rule.metadata.is_compiled = True
|
||||
rule.metadata.is_enabled = True
|
||||
rule.rete_disjunctions = conditions
|
||||
network.add_rule(rule)
|
||||
|
||||
variable = foo if variable_name == "foo" else sheerka if variable_name == "sheerka" else variable_name
|
||||
to_recognize = sheerka.new_from_template(greetings, greetings.key, a=variable)
|
||||
network.add_obj("__ret", ReturnValueConcept("Test", True, to_recognize))
|
||||
matches = list(network.matches)
|
||||
assert len(matches) == 1
|
||||
|
||||
@pytest.mark.parametrize("expression, expected_as_str", [
|
||||
(
|
||||
"eval(__ret.body, 'foo' starts with 'f')",
|
||||
["#__x_00__|__name__|'__ret'",
|
||||
"#__x_00__|body|#__x_01__",
|
||||
"#__x_01__|__is_concept__|True",
|
||||
"#__x_01__|key|'__var__0 starts with __var__1'",
|
||||
"#__x_01__|x|'foo'",
|
||||
"#__x_01__|y|'f'",
|
||||
"$eval(__x_01__, a, b, c)"]
|
||||
),
|
||||
])
|
||||
def test_i_can_get_rete_conditions_using_eval_function(self, expression, expected_as_str):
|
||||
sheerka, context, start_with = self.init_test().with_concepts(
|
||||
Concept("x starts with y",
|
||||
pre="is_question",
|
||||
body="x.startswith(y)",
|
||||
where="isinstance(x, str)").def_var("x").def_var("y"),
|
||||
).unpack()
|
||||
|
||||
parser = ExpressionParser()
|
||||
expected = get_rete_conditions(*expected_as_str)
|
||||
|
||||
error_sink = ErrorSink()
|
||||
parser_input = ParserInput(expression)
|
||||
parser.reset_parser_input(parser_input, error_sink)
|
||||
parsed = parser.parse_input(context, parser_input, error_sink)
|
||||
|
||||
visitor = ReteConditionExprVisitor(context)
|
||||
conditions = visitor.get_conditions(parsed)
|
||||
|
||||
assert conditions == [expected]
|
||||
|
||||
# check against a Rete network
|
||||
network = ReteNetwork()
|
||||
rule = Rule("test", expression, None)
|
||||
rule.metadata.id = 9999
|
||||
rule.metadata.is_compiled = True
|
||||
rule.metadata.is_enabled = True
|
||||
rule.rete_disjunctions = conditions
|
||||
network.add_rule(rule)
|
||||
|
||||
to_recognize = sheerka.new_from_template(start_with, start_with.key, x="foo", y="f")
|
||||
network.add_obj("__ret", ReturnValueConcept("Test", True, to_recognize))
|
||||
matches = list(network.matches)
|
||||
assert len(matches) == 1
|
||||
|
||||
class TestSheerkaRuleManagerUsingFileBasedSheerka(TestUsingFileBasedSheerka):
|
||||
def test_rules_are_initialized_at_startup(self):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import ast
|
||||
import re
|
||||
from dataclasses import dataclass
|
||||
from typing import Union
|
||||
|
||||
@@ -1317,12 +1318,18 @@ def get_rete_conditions(*conditions_as_string):
|
||||
|
||||
res = []
|
||||
for as_string in conditions_as_string:
|
||||
parts = as_string.split("|")
|
||||
identifier = get_value(parts[0])
|
||||
attribute = parts[1]
|
||||
value = get_value(parts[2])
|
||||
if as_string.startswith("$"):
|
||||
fn_match = re.match(r"(?P<function>\w+)\s?\((?P<args>.+)\)", as_string[1:])
|
||||
as_dict = fn_match.groupdict()
|
||||
pass
|
||||
else:
|
||||
parts = as_string.split("|")
|
||||
identifier = get_value(parts[0])
|
||||
attribute = parts[1]
|
||||
value = get_value(parts[2])
|
||||
|
||||
res.append(Condition(identifier, attribute, value))
|
||||
|
||||
res.append(Condition(identifier, attribute, value))
|
||||
return AndConditions(res)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user