Implemented SheerkaOntology

This commit is contained in:
2021-01-11 15:36:03 +01:00
parent e3c2adb533
commit e26c83a825
119 changed files with 6876 additions and 2002 deletions
+40 -30
View File
@@ -41,16 +41,14 @@ cmap = {
class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
sheerka = None
shared_ontology = None
@classmethod
def setup_class(cls):
t = TestSyaNodeParser()
TestSyaNodeParser.sheerka, context, _ = t.init_parser(
cmap,
singleton=False,
create_new=True,
init_from_sheerka=True)
init_test_helper = cls().init_test(cache_only=False, ontology="#TestSyaNodeParser#")
sheerka, context, *updated = init_test_helper.with_concepts(*cmap.values(), create_new=True).unpack()
for i, concept_name in enumerate(cmap):
cmap[concept_name] = updated[i]
cmap["plus"].set_prop(BuiltinConcepts.ASSOCIATIVITY, "right")
cmap["mult"].set_prop(BuiltinConcepts.ASSOCIATIVITY, "right")
@@ -66,35 +64,24 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
cmap["minus"],
CONCEPT_COMPARISON_CONTEXT)
# TestSyaNodeParser.sheerka.test_only_force_sya_def(context, [
# (cmap["plus"].id, 5, SyaAssociativity.Right),
# (cmap["mult"].id, 10, SyaAssociativity.Right),
# (cmap["minus"].id, 5, SyaAssociativity.Right)])
cls.shared_ontology = sheerka.get_ontology(context)
sheerka.pop_ontology()
def init_parser(self,
my_concepts_map=None,
sya_def=None,
post_init_concepts=None,
**kwargs):
if my_concepts_map is not None:
# a new concept map is given
# use it but
# do not instantiate a new sheerka
# do not update / init from sheerka
if 'singleton' not in kwargs:
kwargs["singleton"] = True
init_from_sheerka = kwargs.get("init_from_sheerka", False)
sheerka, context, *concepts = self.init_concepts(*my_concepts_map.values(), **kwargs)
else:
# No custom concept map is given -> Use the global cmap
# Sheerka is already initialized (the class instance)
# Use it to initialize the parser
init_from_sheerka = kwargs.get("init_from_sheerka", True)
sheerka = TestSyaNodeParser.sheerka
context = self.get_context(sheerka)
if my_concepts_map is None:
sheerka, context = self.init_test().unpack()
sheerka.add_ontology(context, self.shared_ontology)
concepts = cmap.values()
ALL_ATTRIBUTES.clear()
init_from_sheerka = kwargs.get("init_from_sheerka", True)
else:
sheerka, context, *concepts = self.init_test().with_concepts(*my_concepts_map.values(), **kwargs).unpack()
for i, pair in enumerate(my_concepts_map):
my_concepts_map[pair] = concepts[i]
init_from_sheerka = kwargs.get("init_from_sheerka", False)
if post_init_concepts:
post_init_concepts(sheerka, context)
@@ -112,7 +99,6 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
parser = SyaNodeParser()
if my_concepts_map:
parser.init_from_concepts(context, concepts, sya=sya_def_to_use)
return sheerka, context, parser
@pytest.mark.parametrize("expression, expected_sequences", [
@@ -1346,3 +1332,27 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
assert actual.function == resolved_function_name[0]
else:
assert actual.function is None
def test_i_can_parse_when_multiple_ontologies(self):
sheerka, context, parser = self.init_parser()
text = "suffixed 1 + 1"
res = parser.parse(context, ParserInput(text))
wrapper = res.body
lexer_nodes = res.body.body
assert res.status
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
assert lexer_nodes == [CN(cmap["suffixed"], 0, 6, source=text)]
# add an ontology layer and make sure will still can parse
sheerka.push_ontology(context, "new ontology")
parser = SyaNodeParser(sheerka=sheerka)
res = parser.parse(context, ParserInput(text))
wrapper = res.body
lexer_nodes = res.body.body
assert res.status
assert context.sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
assert lexer_nodes == [CN(cmap["suffixed"], 0, 6, source=text)]