Working on #98 : Persist attribute value when global_truth is set to true
This commit is contained in:
@@ -7,7 +7,7 @@ from core.concept import VARIABLE_PREFIX, Concept, DEFINITION_TYPE_BNF, DEFINITI
|
||||
from core.sheerka.services.SheerkaConceptManager import NoFirstTokenError
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from core.tokenizer import Tokenizer
|
||||
from evaluators.DefConceptEvaluator import DefConceptEvaluator, PossibleVariable, CertainVariable
|
||||
from evaluators.DefConceptEvaluator import DefConceptEvaluator, PossibleVariable, CertainVariable, ParameterVariable
|
||||
from parsers.BaseParser import BaseParser
|
||||
from parsers.BnfDefinitionParser import BnfDefinitionParser
|
||||
from parsers.BnfNodeParser import Sequence, StrMatch, ZeroOrMore, ConceptExpression, VariableExpression
|
||||
@@ -200,7 +200,7 @@ class TestDefConceptEvaluator(TestUsingMemoryBasedSheerka):
|
||||
def_ret_val = DefConceptParser().parse(context, ParserInput(text))
|
||||
concept_definition = def_ret_val.value.body.definition
|
||||
|
||||
expected = [CertainVariable("unit"), CertainVariable("one"), CertainVariable("two")]
|
||||
expected = [ParameterVariable("unit", True), CertainVariable("one"), CertainVariable("two")]
|
||||
assert DefConceptEvaluator.get_variables(context, concept_definition, []) == expected
|
||||
|
||||
def test_i_can_recognize_variables_when_referencing_other_concepts(self):
|
||||
@@ -326,6 +326,84 @@ class TestDefConceptEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert evaluated.body.body.key == "foo2 __var__0"
|
||||
assert evaluated.body.body.get_metadata().variables == [("x", None)]
|
||||
|
||||
def test_i_can_eval_when_bnf_with_implicit_variables_from_named_ordered_choice(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two", create_new=True)
|
||||
text = "def concept plus from bnf (one|two)=n1 'plus' (one|two)=n2 as n1 + n2"
|
||||
def_ret_val = DefConceptParser().parse(context, ParserInput(text))
|
||||
evaluated = DefConceptEvaluator().eval(context, def_ret_val)
|
||||
|
||||
assert evaluated.status
|
||||
assert context.sheerka.isinstance(evaluated.body, BuiltinConcepts.NEW_CONCEPT)
|
||||
|
||||
created_concept = evaluated.body.body
|
||||
assert created_concept.get_metadata().name == "plus"
|
||||
assert created_concept.get_metadata().key == "plus"
|
||||
assert created_concept.get_metadata().definition == "(one|two)=n1 'plus' (one|two)=n2"
|
||||
assert created_concept.get_metadata().definition_type == "bnf"
|
||||
assert created_concept.get_metadata().variables == [('n1', None), ('one', None), ('two', None), ('n2', None)]
|
||||
assert created_concept.get_metadata().parameters == ['n1', 'n2']
|
||||
|
||||
def test_i_can_eval_when_bnf_with_implicit_variables_from_unamed_ordered_choice(self):
|
||||
sheerka, context, one, two, three = self.init_concepts("one", "two", "three", create_new=True)
|
||||
text = "def concept choice from bnf (one|two) 'or' (two|three) 'or' (two|three)=c3"
|
||||
def_ret_val = DefConceptParser().parse(context, ParserInput(text))
|
||||
evaluated = DefConceptEvaluator().eval(context, def_ret_val)
|
||||
|
||||
assert evaluated.status
|
||||
assert context.sheerka.isinstance(evaluated.body, BuiltinConcepts.NEW_CONCEPT)
|
||||
|
||||
created_concept = evaluated.body.body
|
||||
assert created_concept.get_metadata().name == "choice"
|
||||
assert created_concept.get_metadata().key == "choice"
|
||||
assert created_concept.get_metadata().definition == "(one|two) 'or' (two|three) 'or' (two|three)=c3"
|
||||
assert created_concept.get_metadata().definition_type == "bnf"
|
||||
assert created_concept.get_metadata().variables == [('one', None), ('two', None), ('three', None), ('c3', None)]
|
||||
assert created_concept.get_metadata().parameters == ['__OrderedChoice__1', '__OrderedChoice__2', "c3"]
|
||||
|
||||
def test_i_can_eval_when_bnf_with_implicit_variables_from_unordered_choice(self):
|
||||
# as it's not possible to directly defined UnorderedChoice, we test isa concept
|
||||
sheerka, context, one, two, number = self.init_concepts("one", "two", "number", create_new=True)
|
||||
global_truth_context = self.get_context(global_truth=True)
|
||||
sheerka.set_isa(global_truth_context, one, number)
|
||||
sheerka.set_isa(global_truth_context, two, number)
|
||||
|
||||
text = "def concept plus from bnf number=n1 'plus' number=n2 as n1 + n2"
|
||||
def_ret_val = DefConceptParser().parse(context, ParserInput(text))
|
||||
evaluated = DefConceptEvaluator().eval(context, def_ret_val)
|
||||
|
||||
assert evaluated.status
|
||||
assert context.sheerka.isinstance(evaluated.body, BuiltinConcepts.NEW_CONCEPT)
|
||||
|
||||
created_concept = evaluated.body.body
|
||||
assert created_concept.get_metadata().name == "plus"
|
||||
assert created_concept.get_metadata().key == "plus"
|
||||
assert created_concept.get_metadata().definition == "number=n1 'plus' number=n2"
|
||||
assert created_concept.get_metadata().definition_type == "bnf"
|
||||
assert created_concept.get_metadata().variables == [('n1', None), ('n2', None)]
|
||||
assert created_concept.get_metadata().parameters == ['n1', 'n2']
|
||||
|
||||
def test_i_can_eval_when_bnf_with_implicit_variables_from_unamed_unordered_choice(self):
|
||||
# as it's not possible to directly defined UnorderedChoice, we test isa concept
|
||||
sheerka, context, one, two, number = self.init_concepts("one", "two", "number", create_new=True)
|
||||
global_truth_context = self.get_context(global_truth=True)
|
||||
sheerka.set_isa(global_truth_context, one, number)
|
||||
sheerka.set_isa(global_truth_context, two, number)
|
||||
|
||||
text = "def concept plus from bnf number 'plus' number as number[0] + number[1]"
|
||||
def_ret_val = DefConceptParser().parse(context, ParserInput(text))
|
||||
evaluated = DefConceptEvaluator().eval(context, def_ret_val)
|
||||
|
||||
assert evaluated.status
|
||||
assert context.sheerka.isinstance(evaluated.body, BuiltinConcepts.NEW_CONCEPT)
|
||||
|
||||
created_concept = evaluated.body.body
|
||||
assert created_concept.get_metadata().name == "plus"
|
||||
assert created_concept.get_metadata().key == "plus"
|
||||
assert created_concept.get_metadata().definition == "number 'plus' number"
|
||||
assert created_concept.get_metadata().definition_type == "bnf"
|
||||
assert created_concept.get_metadata().variables == [('number', None)]
|
||||
assert created_concept.get_metadata().parameters == ['number']
|
||||
|
||||
def test_i_can_eval_when_bnf_concept_with_regex(self):
|
||||
context = self.get_context()
|
||||
def_ret_val = DefConceptParser().parse(context, ParserInput("def concept hello a from bnf r'[a-z]+'=a 'hello'"))
|
||||
@@ -339,6 +417,8 @@ class TestDefConceptEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert created_concept.get_metadata().key == "hello __var__0"
|
||||
assert created_concept.get_metadata().definition == "r'[a-z]+'=a 'hello'"
|
||||
assert created_concept.get_metadata().definition_type == "bnf"
|
||||
assert created_concept.get_metadata().variables == [('a', None)]
|
||||
assert created_concept.get_metadata().parameters == ['a']
|
||||
|
||||
def test_i_can_eval_when_bnf_concept_with_variable(self):
|
||||
context = self.get_context()
|
||||
@@ -355,6 +435,8 @@ class TestDefConceptEvaluator(TestUsingMemoryBasedSheerka):
|
||||
assert created_concept.get_metadata().definition_type == "bnf"
|
||||
assert created_concept.get_metadata().variables == [("x", None)]
|
||||
assert created_concept._bnf == Sequence(StrMatch("hello"), VariableExpression("x"))
|
||||
assert created_concept.get_metadata().variables == [('x', None)]
|
||||
assert created_concept.get_metadata().parameters == ['x']
|
||||
|
||||
def test_i_can_eval_when_auto_eval_is_true(self):
|
||||
sheerka, context = self.init_test().unpack()
|
||||
|
||||
Reference in New Issue
Block a user