Implemented some enhancement request and fixed some bugs
Fixed #2 : Variables are not recognized when inside a rule token Fixed #15 : Rule: rete attributes are lost when a new ontology is created Fixed #14 : ReteNetwork: Format rules must not be added to Rete network Fixed #16 : DefConcept: Variables are not recognized when they are keyword arguments Fixed #4 : Comparison are not correctly set when comparison property is a concept Fixed #14 : Parser: merge FunctionParser.NamesNode and ExpressionParser.NamesNode Fixed #18 : Parser: Add SourceCodeNode test to UnrecognizedNodeParser Fixed #20 : At startup Number concept is saved in db a numerous number of time Fixed #21 : CacheManager: I can remove all elements from a ListIfNeededCache and fill it again Fixed #22 : CacheManager: I can remove all elements from a SetCache and fill it again Fixed #23 : HistoryManager: history() no longer works Fixed #24 : HistoryManager: history() no longer works after creating an exec rule Fixed #25 : SheerkaMemory: Use MemoryObject instead of sheerka.local Fixed #26 : Debugger: add the list all available services.. Fixed #27 : CONCEPTS_GRAMMARS_ENTRY does not seems to be in use any more Fixed #28 : Give order to services
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
import ast
|
||||
|
||||
import pytest
|
||||
|
||||
from core.builtin_concepts import ReturnValueConcept, ParserResultConcept, BuiltinConcepts
|
||||
from core.concept import VARIABLE_PREFIX, Concept, DEFINITION_TYPE_BNF, DEFINITION_TYPE_DEF
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from core.tokenizer import Tokenizer
|
||||
from evaluators.DefConceptEvaluator import DefConceptEvaluator
|
||||
from parsers.BaseParser import BaseParser
|
||||
from parsers.BnfDefinitionParser import BnfDefinitionParser
|
||||
from parsers.BnfNodeParser import Sequence, StrMatch, ZeroOrMore, ConceptExpression
|
||||
from parsers.DefConceptParser import DefConceptNode, NameNode
|
||||
from parsers.DefConceptParser import DefConceptNode, NameNode, DefConceptParser
|
||||
from parsers.PythonParser import PythonNode, PythonParser
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
@@ -244,3 +245,35 @@ class TestDefConceptEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert new_concept.get_metadata().body == 'foo'
|
||||
assert new_concept.values() == {}
|
||||
assert new_concept.get_metadata().variables == []
|
||||
|
||||
def test_i_can_get_variable_when_rule_is_defined(self):
|
||||
sheerka, context = self.init_test().unpack()
|
||||
def_concept_parser = DefConceptParser()
|
||||
|
||||
parsed_ret_val = def_concept_parser.parse(context, ParserInput("def concept rule x as r:|x:"))
|
||||
assert parsed_ret_val.status # sanity check
|
||||
|
||||
evaluated = DefConceptEvaluator().eval(context, parsed_ret_val)
|
||||
assert evaluated.status
|
||||
assert evaluated.body.body.key == "rule __var__0"
|
||||
assert evaluated.body.body.get_metadata().variables == [("x", None)]
|
||||
|
||||
def test_i_can_recognize_variable_when_keyword_argument(self):
|
||||
sheerka, context = self.init_test().unpack()
|
||||
def_concept_parser = DefConceptParser()
|
||||
|
||||
parsed_ret_val = def_concept_parser.parse(context, ParserInput("def concept foo1 x as func(param=x)"))
|
||||
assert parsed_ret_val.status # sanity check
|
||||
|
||||
evaluated = DefConceptEvaluator().eval(context, parsed_ret_val)
|
||||
assert evaluated.status
|
||||
assert evaluated.body.body.key == "foo1 __var__0"
|
||||
assert evaluated.body.body.get_metadata().variables == [("x", None)]
|
||||
|
||||
parsed_ret_val = def_concept_parser.parse(context, ParserInput("def concept foo2 x as func(x=value)"))
|
||||
assert parsed_ret_val.status # sanity check
|
||||
|
||||
evaluated = DefConceptEvaluator().eval(context, parsed_ret_val)
|
||||
assert evaluated.status
|
||||
assert evaluated.body.body.key == "foo2 __var__0"
|
||||
assert evaluated.body.body.get_metadata().variables == [("x", None)]
|
||||
|
||||
@@ -12,7 +12,6 @@ from evaluators.PythonEvaluator import PythonEvaluator, PythonEvalError, NamesWi
|
||||
from parsers.BaseNodeParser import SourceCodeNode, SourceCodeWithConceptNode
|
||||
from parsers.FunctionParser import FunctionParser
|
||||
from parsers.PythonParser import PythonNode, PythonParser
|
||||
from parsers.PythonWithConceptsParser import PythonWithConceptsParser
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
@@ -228,7 +227,7 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
||||
|
||||
assert evaluated.status
|
||||
assert sheerka.get_weights(BuiltinConcepts.PRECEDENCE) == {'c:__var__0 plus __var__1|1001:': 1,
|
||||
'c:__var__0 mult __var__1|1002:': 2}
|
||||
'c:__var__0 mult __var__1|1002:': 2}
|
||||
|
||||
def test_i_can_define_variables(self):
|
||||
sheerka, context = self.init_test().unpack()
|
||||
@@ -293,21 +292,6 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert error1.error.args[0] == 'can only concatenate str (not "int") to str'
|
||||
assert error1.concepts == {'foo': 'string'}
|
||||
|
||||
def test_i_can_use_sheerka_locals(self):
|
||||
sheerka, context = self.init_test().unpack()
|
||||
|
||||
def func(i):
|
||||
return i + 1
|
||||
|
||||
sheerka.locals["func"] = func
|
||||
|
||||
parsed = PythonParser().parse(context, ParserInput("func(10)"))
|
||||
python_evaluator = PythonEvaluator()
|
||||
evaluated = python_evaluator.eval(context, parsed)
|
||||
|
||||
assert evaluated.status
|
||||
assert evaluated.value == 11
|
||||
|
||||
def test_i_can_eval_concept_with_ret(self):
|
||||
sheerka, context, one, the = self.init_concepts("one", Concept("the a", ret="a").def_var("a"))
|
||||
ret_val = get_ret_val_from_source_code(context, "test_using_context(the one)", {
|
||||
@@ -339,6 +323,17 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert evaluated.status
|
||||
assert evaluated.value == "Print return values"
|
||||
|
||||
def test_i_can_update_sheerka_memory_using_assignation(self):
|
||||
sheerka, context = self.init_test().unpack()
|
||||
|
||||
parsed = PythonParser().parse(context, ParserInput("a = 10"))
|
||||
python_evaluator = PythonEvaluator()
|
||||
evaluated = python_evaluator.eval(context, parsed)
|
||||
|
||||
assert evaluated.status
|
||||
from_memory = sheerka.get_from_memory(context, "a")
|
||||
assert from_memory.obj == 10
|
||||
|
||||
@pytest.mark.parametrize("method, expected_status", [
|
||||
("return_return_value(True)", True),
|
||||
("return_return_value(False)", False),
|
||||
@@ -389,3 +384,13 @@ class TestPythonEvaluator(TestUsingMemoryBasedSheerka):
|
||||
|
||||
assert evaluated.status
|
||||
assert evaluated.value == context.sheerka.get_rule_by_id(str(value)).name
|
||||
|
||||
def test_something(self):
|
||||
def func(**kwargs):
|
||||
for k, v in kwargs.items():
|
||||
print(f"{k=}, {v=}")
|
||||
|
||||
to_compile = "func(x=y)"
|
||||
ast_ = ast.parse(to_compile, mode="eval")
|
||||
compiled = compile(ast_, "<test>", mode="eval")
|
||||
eval(compiled, {"x": "hello", "y": "world", "func": func})
|
||||
|
||||
Reference in New Issue
Block a user