Not initialiazed variables now returns BuiltinConcepts.NOT_INITIALIZED

This commit is contained in:
2020-05-23 10:01:50 +02:00
parent 3ce6ce2a76
commit 32fb0e5398
9 changed files with 25 additions and 22 deletions
+3 -1
View File
@@ -61,6 +61,7 @@ class BuiltinConcepts(Enum):
EXPLANATION = "explanation" EXPLANATION = "explanation"
PRECEDENCE = "precedence" # use to set priority among concepts when parsing PRECEDENCE = "precedence" # use to set priority among concepts when parsing
ASSOCIATIVITY = "associativity" # use to set priority among concepts when parsing ASSOCIATIVITY = "associativity" # use to set priority among concepts when parsing
NOT_INITIALIZED = "not initialized"
NODE = "node" NODE = "node"
GENERIC_NODE = "generic node" GENERIC_NODE = "generic node"
@@ -104,7 +105,8 @@ BuiltinErrors = [str(e) for e in {
BuiltinConcepts.CONCEPT_ALREADY_IN_SET, BuiltinConcepts.CONCEPT_ALREADY_IN_SET,
BuiltinConcepts.NOT_A_SET, BuiltinConcepts.NOT_A_SET,
BuiltinConcepts.WHERE_CLAUSE_FAILED, BuiltinConcepts.WHERE_CLAUSE_FAILED,
BuiltinConcepts.CHICKEN_AND_EGG BuiltinConcepts.CHICKEN_AND_EGG,
BuiltinConcepts.NOT_INITIALIZED
}] }]
""" """
+2 -1
View File
@@ -391,7 +391,8 @@ class Concept:
:return: :return:
""" """
if prop_name not in self.values: if prop_name not in self.values:
return None from core.builtin_concepts import BuiltinConcepts
return BuiltinConcepts.NOT_INITIALIZED
return self.values[prop_name].value return self.values[prop_name].value
def variables(self): def variables(self):
+1 -1
View File
@@ -657,7 +657,7 @@ class Sheerka(Concept):
if not isinstance(obj, Concept): if not isinstance(obj, Concept):
return obj return obj
if obj.body is None: if obj.body is BuiltinConcepts.NOT_INITIALIZED:
return obj return obj
if reduce_simple_list and (isinstance(obj.body, list) or isinstance(obj.body, set)) and len(obj.body) == 1: if reduce_simple_list and (isinstance(obj.body, list) or isinstance(obj.body, set)) and len(obj.body) == 1:
+1 -1
View File
@@ -21,7 +21,7 @@ class EvalEvaluator(AllReturnValuesEvaluator):
result = [] result = []
for ret_val in return_values: for ret_val in return_values:
if ret_val.status and isinstance(ret_val.body, Concept) and ret_val.body.body: if ret_val.status and isinstance(ret_val.body, Concept) and ret_val.body.body != BuiltinConcepts.NOT_INITIALIZED:
context.log(f"Evaluating {ret_val.body}", who=self) context.log(f"Evaluating {ret_val.body}", who=self)
result.append(sheerka.ret(self.name, True, ret_val.body.body, parents=[ret_val])) result.append(sheerka.ret(self.name, True, ret_val.body.body, parents=[ret_val]))
elif ret_val.status and sheerka.isaset(context, ret_val.body): elif ret_val.status and sheerka.isaset(context, ret_val.body):
+1 -1
View File
@@ -88,7 +88,7 @@ class SheerkaPrinter:
if instructions.recursive_props: if instructions.recursive_props:
for k, v in instructions.recursive_props.items(): for k, v in instructions.recursive_props.items():
if hasattr(item, k) and v > 0 and (value := getattr(item, k)) is not None: if hasattr(item, k) and v > 0 and (value := getattr(item, k)) != BuiltinConcepts.NOT_INITIALIZED:
self.fp(instructions.recurse(k), value) self.fp(instructions.recurse(k), value)
@staticmethod @staticmethod
+4 -4
View File
@@ -9,7 +9,7 @@ from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka): class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
@pytest.mark.parametrize("body, expected", [ @pytest.mark.parametrize("body, expected", [
(None, None), (None, BuiltinConcepts.NOT_INITIALIZED),
("", ""), ("", ""),
("1", 1), ("1", 1),
("1+1", 2), ("1+1", 2),
@@ -132,7 +132,7 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
evaluated = sheerka.evaluate_concept(context, concept) evaluated = sheerka.evaluate_concept(context, concept)
assert evaluated == CB("foo", CB("a", None)) assert evaluated == CB("foo", CB("a", BuiltinConcepts.NOT_INITIALIZED))
assert evaluated.metadata.is_evaluated assert evaluated.metadata.is_evaluated
assert evaluated.body.metadata.is_evaluated assert evaluated.body.metadata.is_evaluated
@@ -176,9 +176,9 @@ class TestSheerkaEvaluateConcept(TestUsingMemoryBasedSheerka):
evaluated = sheerka.evaluate_concept(context, concept_d) evaluated = sheerka.evaluate_concept(context, concept_d)
assert evaluated.key == concept_d.key assert evaluated.key == concept_d.key
expected = CB("c", CB("b", CB("a", None))) expected = CB("c", CB("b", CB("a", BuiltinConcepts.NOT_INITIALIZED)))
assert evaluated.body == expected assert evaluated.body == expected
assert sheerka.objvalue(evaluated) == CB("a", None) assert sheerka.objvalue(evaluated) == CB("a", BuiltinConcepts.NOT_INITIALIZED)
assert evaluated.metadata.is_evaluated assert evaluated.metadata.is_evaluated
def test_i_can_evaluate_concept_when_variables_reference_others_concepts(self): def test_i_can_evaluate_concept_when_variables_reference_others_concepts(self):
+2 -2
View File
@@ -186,11 +186,11 @@ class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
new = sheerka.new(template.key) new = sheerka.new(template.key)
assert not new.metadata.is_evaluated assert not new.metadata.is_evaluated
assert not new.body assert new.body == BuiltinConcepts.NOT_INITIALIZED
new = sheerka.new((None, template.id)) new = sheerka.new((None, template.id))
assert not new.metadata.is_evaluated assert not new.metadata.is_evaluated
assert not new.body assert new.body == BuiltinConcepts.NOT_INITIALIZED
def test_i_cannot_instantiate_an_unknown_concept(self): def test_i_cannot_instantiate_an_unknown_concept(self):
sheerka = self.get_sheerka() sheerka = self.get_sheerka()
+2 -2
View File
@@ -64,7 +64,7 @@ class TestSheerkaNonRegMemory(TestUsingMemoryBasedSheerka):
# sanity check # sanity check
evaluated = sheerka.evaluate_concept(self.get_context(eval_body=True), return_value) evaluated = sheerka.evaluate_concept(self.get_context(eval_body=True), return_value)
assert evaluated == simplec("un", simplec("one", None)) assert evaluated == simplec("un", simplec("one", BuiltinConcepts.NOT_INITIALIZED))
def test_i_can_recognize_concept_with_no_body(self): def test_i_can_recognize_concept_with_no_body(self):
sheerka = self.get_sheerka() sheerka = self.get_sheerka()
@@ -734,7 +734,7 @@ as:
assert len(res) == 1 assert len(res) == 1
assert res[0].status assert res[0].status
assert sheerka.isinstance(res[0].body, "foobar") assert sheerka.isinstance(res[0].body, "foobar")
assert res[0].body.body is None assert res[0].body.body == BuiltinConcepts.NOT_INITIALIZED
res = sheerka.evaluate_user_input("eval foo bar") res = sheerka.evaluate_user_input("eval foo bar")
assert len(res) == 1 assert len(res) == 1
+9 -9
View File
@@ -447,11 +447,11 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
# explicit validations of the compiled # explicit validations of the compiled
concept_foo = sequences[0][0].concept concept_foo = sequences[0][0].concept
assert concept_foo.body is None assert concept_foo.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_foo.compiled == {ConceptParts.BODY: DoNotResolve("one two")} assert concept_foo.compiled == {ConceptParts.BODY: DoNotResolve("one two")}
concept_bar = sequences[1][0].concept concept_bar = sequences[1][0].concept
assert concept_bar.body is None assert concept_bar.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_bar.compiled == { assert concept_bar.compiled == {
ConceptParts.BODY: concept_foo, ConceptParts.BODY: concept_foo,
"foo": concept_foo "foo": concept_foo
@@ -473,11 +473,11 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
# explicit validations of the compiled # explicit validations of the compiled
concept_foo = sequences[0][0].concept concept_foo = sequences[0][0].concept
assert concept_foo.body is None assert concept_foo.body == BuiltinConcepts.NOT_INITIALIZED
assert len(concept_foo.compiled) == 0 # because there is a body defined in the metadata assert len(concept_foo.compiled) == 0 # because there is a body defined in the metadata
concept_bar = sequences[1][0].concept concept_bar = sequences[1][0].concept
assert concept_bar.body is None assert concept_bar.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_bar.compiled == { assert concept_bar.compiled == {
ConceptParts.BODY: concept_foo, ConceptParts.BODY: concept_foo,
"foo": concept_foo "foo": concept_foo
@@ -500,18 +500,18 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
# explicit validations of the compiled # explicit validations of the compiled
concept_foo = sequences[0][0].concept concept_foo = sequences[0][0].concept
assert concept_foo.body is None assert concept_foo.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_foo.compiled == {ConceptParts.BODY: DoNotResolve("one two")} assert concept_foo.compiled == {ConceptParts.BODY: DoNotResolve("one two")}
concept_bar = sequences[1][0].concept concept_bar = sequences[1][0].concept
assert concept_bar.body is None assert concept_bar.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_bar.compiled == { assert concept_bar.compiled == {
ConceptParts.BODY: concept_foo, ConceptParts.BODY: concept_foo,
"foo": concept_foo "foo": concept_foo
} }
concept_baz = sequences[2][0].concept concept_baz = sequences[2][0].concept
assert concept_baz.body is None assert concept_baz.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_baz.compiled == { assert concept_baz.compiled == {
ConceptParts.BODY: concept_bar, ConceptParts.BODY: concept_bar,
"bar": concept_bar, "bar": concept_bar,
@@ -631,7 +631,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
# explicit validations of the compiled # explicit validations of the compiled
concept_foo = sequences[0].concept concept_foo = sequences[0].concept
assert concept_foo.body is None assert concept_foo.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_foo.compiled == {'number': my_map["number"], assert concept_foo.compiled == {'number': my_map["number"],
'two': my_map["two"], 'two': my_map["two"],
ConceptParts.BODY: DoNotResolve(value='twenty two')} ConceptParts.BODY: DoNotResolve(value='twenty two')}
@@ -642,7 +642,7 @@ class TestBnfNodeParser(TestUsingMemoryBasedSheerka):
# explicit validations of the compiled # explicit validations of the compiled
concept_foo = sequences[0].concept concept_foo = sequences[0].concept
assert concept_foo.body is None assert concept_foo.body == BuiltinConcepts.NOT_INITIALIZED
assert concept_foo.compiled == {'number': my_map["number"], assert concept_foo.compiled == {'number': my_map["number"],
'one': my_map["one"], 'one': my_map["one"],
ConceptParts.BODY: DoNotResolve(value='twenty one')} ConceptParts.BODY: DoNotResolve(value='twenty one')}