I can define and eval BNF definitions

This commit is contained in:
2019-12-17 21:19:44 +01:00
parent c668cc46d2
commit 88cd3162be
25 changed files with 1099 additions and 569 deletions
+178
View File
@@ -0,0 +1,178 @@
import pytest
from core.builtin_concepts import ReturnValueConcept, ParserResultConcept
from core.concept import Concept
from core.sheerka import Sheerka, ExecutionContext
from evaluators.ConceptNodeEvaluator import ConceptNodeEvaluator
from parsers.ConceptLexerParser import ConceptNode, ConceptLexerParser, NonTerminalNode, Sequence, TerminalNode, \
StrMatch, Optional, OrderedChoice
def get_context():
sheerka = Sheerka(skip_builtins_in_db=True)
sheerka.initialize("mem://")
return ExecutionContext("test", "xxx", sheerka)
def get_return_value(nodes, source):
return ReturnValueConcept(
"some_name",
True,
ParserResultConcept(parser=ConceptLexerParser(),
source=source,
value=nodes,
try_parsed=nodes))
def get_concept_node(context, grammar, expression):
parser = ConceptLexerParser()
parser.initialize(context, grammar)
res = parser.parse(context, expression)
assert res.status
return res.value.value[0]
@pytest.mark.parametrize("ret_val, expected", [
(ReturnValueConcept("some_name", True, ParserResultConcept(value=[ConceptNode(Concept(), 0, 0)])), True),
(ReturnValueConcept("some_name", True, ParserResultConcept(value=ConceptNode(Concept(), 0, 0))), True),
(ReturnValueConcept("some_name", False, ParserResultConcept(value=[ConceptNode(Concept(), 0, 0)])), False),
(ReturnValueConcept("some_name", False, ParserResultConcept(value=ConceptNode(Concept(), 0, 0))), False),
(ReturnValueConcept("some_name", True, ParserResultConcept(value="Not a concept node")), False),
(ReturnValueConcept("some_name", True, ParserResultConcept(value=["Not a concept node"])), False),
(ReturnValueConcept("some_name", True, [ConceptNode(Concept(), 0, 0)]), False),
(ReturnValueConcept("some_name", True, ConceptNode(Concept(), 0, 0)), False),
])
def test_i_can_match(ret_val, expected):
context = get_context()
assert ConceptNodeEvaluator().matches(context, ret_val) == expected
def test_concept_is_returned_when_list_of_one_concept_node():
foo = Concept("foo")
context = get_context()
context.sheerka.add_in_cache(foo)
evaluator = ConceptNodeEvaluator()
node = ConceptNode(foo, 0, 0, underlying=TerminalNode(StrMatch("foo"), 0, 0, "foo"))
ret_val = get_return_value([node], "h")
result = evaluator.eval(context, ret_val)
assert result.who == evaluator.name
assert result.status
assert result.value == node.concept
assert result.parents == [ret_val]
def test_concept_property_is_correctly_updated_for_str_match():
context = get_context()
foo = Concept("foo")
concept_node = get_concept_node(context, {foo: StrMatch("foo", rule_name="variable")}, "foo")
updated = ConceptNodeEvaluator().update_concept(context.sheerka, concept_node.concept, concept_node.underlying)
assert "variable" in updated.props
assert updated.props["variable"].value == "foo"
def test_concept_property_is_correctly_updated_for_sequence():
context = get_context()
foo = Concept("foo")
grammar = {foo: Sequence("one", "two", rule_name="variable")}
concept_node = get_concept_node(context, grammar, "one two")
updated = ConceptNodeEvaluator().update_concept(context.sheerka, concept_node.concept, concept_node.underlying)
assert "variable" in updated.props
assert updated.props["variable"].value == "one two"
def test_concept_property_is_updated_for_str_in_sequence():
context = get_context()
foo = Concept("foo")
grammar = {foo: Sequence(StrMatch("one", rule_name="s1"), StrMatch("two", rule_name="s2"), rule_name="variable")}
concept_node = get_concept_node(context, grammar, "one two")
updated = ConceptNodeEvaluator().update_concept(context.sheerka, concept_node.concept, concept_node.underlying)
assert updated.props["variable"].value == "one two"
assert updated.props["s1"].value == "one"
assert updated.props["s2"].value == "two"
def test_concept_property_is_correctly_updated_for_optional():
context = get_context()
foo = Concept("foo")
grammar = {foo: Sequence("one", Optional("two", rule_name="o"), rule_name="variable")}
concept_node = get_concept_node(context, grammar, "one two")
updated = ConceptNodeEvaluator().update_concept(
context.sheerka,
context.sheerka.new(concept_node.concept.key),
concept_node.underlying)
assert "variable" in updated.props
assert updated.props["variable"].value == "one two"
assert updated.props["o"].value == "two"
def test_concept_property_is_correctly_updated_when_list_of_properties():
context = get_context()
foo = Concept("foo")
grammar = {foo: Sequence(StrMatch("one", rule_name="s"), StrMatch("two", rule_name="s"), rule_name="variable")}
concept_node = get_concept_node(context, grammar, "one two")
updated = ConceptNodeEvaluator().update_concept(
context.sheerka,
context.sheerka.new(concept_node.concept.key),
concept_node.underlying)
assert updated.props["variable"].value == "one two"
assert updated.props["s"].value == ["one", "two"]
def test_concept_property_is_correctly_updated_when_another_concept():
context = get_context()
foo = Concept("foo")
bar = Concept("bar")
context.sheerka.add_in_cache(foo)
grammar = {
foo: Sequence("one", "two", rule_name="variable"),
bar: Sequence(foo, "three", rule_name="variable")}
concept_node = get_concept_node(context, grammar, "one two three")
updated = ConceptNodeEvaluator().update_concept(
context.sheerka,
context.sheerka.new(concept_node.concept.key),
concept_node.underlying)
assert updated.props["variable"].value == "one two three"
assert updated.props["foo"].value == Concept("foo").set_prop("variable", "one two").init_key()
def test_concept_property_is_correctly_updated_when_concept_recursion():
context = get_context()
number = Concept("number")
add = Concept("add")
context.sheerka.add_in_cache(number)
context.sheerka.add_in_cache(add)
grammar = {
number: OrderedChoice("one", "two"),
add: Sequence(number, Optional(Sequence(OrderedChoice("plus", "minus", rule_name="op"), add)))
}
concept_node = get_concept_node(context, grammar, "one plus two")
updated = ConceptNodeEvaluator().update_concept(
context.sheerka,
context.sheerka.new(concept_node.concept.key),
concept_node.underlying)
assert updated.props["number"].value == Concept("number").init_key()
assert updated.props["op"].value == "plus"
assert updated.props["add"].value == Concept("add").set_prop("number", Concept("number").init_key()).init_key()