Fixed #131 : Implement ExprToConditions

Fixed #130 : ArithmeticOperatorParser
Fixed #129 : python_wrapper : create_namespace
Fixed #128 : ExpressionParser: Cannot parse func(x) infixed concept 'xxx'
This commit is contained in:
2021-10-13 16:06:57 +02:00
parent a61a1c0d2b
commit 89e1f20975
76 changed files with 5867 additions and 3206 deletions
+49 -2
View File
@@ -1,12 +1,13 @@
import pytest
from core.builtin_concepts_ids import BuiltinConcepts
from core.builtin_helpers import ensure_asts
from core.concept import Concept
from core.global_symbols import SyaAssociativity
from core.sheerka.ExecutionContext import ExecutionContext
from core.sheerka.services.SheerkaAdmin import SheerkaAdmin
from sheerkapython.python_wrapper import Expando, create_namespace, inject_context, get_sheerka_method, Pipe, \
MethodAccessError
from sheerkapython.python_wrapper import Expando, MethodAccessError, Pipe, create_namespace, get_sheerka_method, \
get_variables_from_concept_asts, inject_context
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
@@ -135,6 +136,27 @@ class TestPythonWrapper(TestUsingMemoryBasedSheerka):
res = create_namespace(context, "TestPythonWrapper", ["foo"], None, objects, False)
assert res == {"foo": objects["foo"]}
def test_external_value_takes_precedence_over_concept_parameter(self):
"""
To manage :
Concept("x is a y").def_var("x").def_var("y"),
"y is a number" -> "call_concept(__o_00__, x=y)"
If 'y' is not given, it will use the concept parameter 'y'
But if y is given (as a short term memory) it must be prioritized
:return:
"""
sheerka, context = self.init_test().unpack()
obj = Concept("x is a y").def_var("x").def_var("y", "concept value").auto_init()
context.obj = obj
objects = {"y": "object value"}
res = create_namespace(context, "TestPythonWrapper", ["y"], None, objects, False)
assert res == {'y': 'concept value'}
context.add_to_short_term_memory("y", "stm value")
res = create_namespace(context, "TestPythonWrapper", ["y"], None, objects, False)
assert res == {'y': 'stm value'}
def test_i_can_get_sheerka_method(self):
context = self.get_context()
@@ -167,3 +189,28 @@ class TestPythonWrapper(TestUsingMemoryBasedSheerka):
res = get_sheerka_method(context, "TestPythonWrapper", "where", True)
assert isinstance(res, Pipe)
@pytest.mark.parametrize("concept, known_variables, expected", [
(Concept("foo").def_var("x", "True"), set(), {}),
(Concept("foo").def_var("x"), set(), {}),
(Concept("foo").def_var("x", "self"), set(), {"x": {"self"}}),
(Concept("foo").def_var("x", "self + a"), set(), {"x": {"self", "a"}}),
(Concept("foo").def_var("x", "self + a").def_var("y", "b"), set(), {'x': {'a', 'self'}, 'y': {'b'}}),
(Concept("foo", body="x").def_var("x"), set(), {}), # 'x' is a concept var, so it can be resolved
(Concept("foo", body="x").def_var("x", "x"), set(), {'x': {'x'}}),
(Concept("foo").def_var("x", "func(y)"), set(), {"x": {"y"}}),
(Concept("foo").def_var("x", "x"), set(), {'x': {'x'}}),
(Concept("foo").def_var("x", "y"), set(), {'x': {'y'}}),
(Concept("foo").def_var("x", "x"), {"x"}, {"x": {"x"}}),
(Concept("foo").def_var("x"), {"x"}, {}), # var x has no value, there no way to link the two 'x's
(Concept("foo", body="x").def_var("x"), {"x"}, {"#body#": {"x"}}),
(Concept("foo").def_var("x", "bar"), set(), {}),
(Concept("foo").def_var("x", "bar"), {"bar"}, {"x": {"bar"}}),
])
def test_get_variables_from_concept_asts(self, concept, known_variables, expected):
sheerka, context, foo, bar = self.init_concepts(concept, "bar")
ensure_asts(context, concept)
variables = get_variables_from_concept_asts(context, concept, known_variables, parameters_only=False)
assert variables == expected