Refactored to allow ConceptEvaluator

This commit is contained in:
2019-11-14 22:04:38 +01:00
parent 576ce77740
commit 9e10e77737
30 changed files with 2406 additions and 1007 deletions
+40 -28
View File
@@ -3,8 +3,10 @@ from os import path
import shutil
import os
from core.builtin_concepts import ParserResultConcept, BuiltinConcepts
from core.concept import Concept, Property
from core.sheerka import Sheerka, ExecutionContext
from core.tokenizer import Tokenizer
from parsers.DefaultParser import DefaultParser
from parsers.ExactConceptParser import ExactConceptParser
@@ -54,29 +56,28 @@ def test_i_can_compute_combinations_with_duplicates():
def test_i_can_recognize_a_simple_concept():
sheerka = get_sheerka()
context = get_context()
concept = get_concept("hello world", [])
sheerka.add_in_cache(concept)
context.sheerka.add_in_cache(concept)
source = "hello world"
context = ExecutionContext(sheerka, "xxxx")
results = ExactConceptParser().parse(context, source)
assert len(results) == 1
assert results[0].status
assert results[0].value.key == concept.key
assert results[0].value == concept
def test_i_can_recognize_concepts_defined_several_times():
sheerka = get_sheerka()
sheerka.add_in_cache(get_concept("hello world", []))
sheerka.add_in_cache(get_concept("hello a", ["a"]))
context = get_context()
context.sheerka.add_in_cache(get_concept("hello world", []))
context.sheerka.add_in_cache(get_concept("hello a", ["a"]))
source = "hello world"
context = ExecutionContext(sheerka, "xxxx")
results = ExactConceptParser().parse(context, source)
assert len(results) == 2
results = sorted(results, key=lambda x: x.value.name) # because of the usage of sets
results = sorted(results, key=lambda x: x.value.name) # because of the usage of sets
assert results[0].status
assert results[0].value.name == "hello a"
@@ -87,11 +88,10 @@ def test_i_can_recognize_concepts_defined_several_times():
def test_i_can_recognize_a_concept_with_variables():
sheerka = get_sheerka()
context = get_context()
concept = get_concept("a + b", ["a", "b"])
sheerka.concepts_cache[concept.key] = concept
context.sheerka.add_in_cache(concept)
source = "10 + 5"
context = ExecutionContext(sheerka, "xxxx")
results = ExactConceptParser().parse(context, source)
assert len(results) == 1
@@ -102,11 +102,10 @@ def test_i_can_recognize_a_concept_with_variables():
def test_i_can_recognize_a_concept_with_duplicate_variables():
sheerka = get_sheerka()
context = get_context()
concept = get_concept("a + b + a", ["a", "b"])
sheerka.concepts_cache[concept.key] = concept
context.sheerka.concepts_cache[concept.key] = concept
source = "10 + 5 + 10"
context = ExecutionContext(sheerka, "xxxx")
results = ExactConceptParser().parse(context, source)
assert len(results) == 1
@@ -117,23 +116,43 @@ def test_i_can_recognize_a_concept_with_duplicate_variables():
def test_i_can_manage_unknown_concept():
sheerka = get_sheerka()
context = get_context()
source = "def concept hello world" # this is not a concept by itself
context = ExecutionContext(sheerka, "xxxx")
res = ExactConceptParser().parse(context, source)
assert not res.status
assert sheerka.isinstance(res.value, Sheerka.UNKNOWN_CONCEPT_NAME)
assert context.sheerka.isinstance(res.value, BuiltinConcepts.UNKNOWN_CONCEPT)
assert res.value.obj == "def concept hello world"
def test_i_can_detect_concepts_too_long():
sheerka = get_sheerka()
context = get_context()
source = "a very very long concept that cannot be an unique one"
context = ExecutionContext(sheerka, "xxxx")
res = ExactConceptParser().parse(context, source)
assert not res.status
assert sheerka.isinstance(res.value, Sheerka.CONCEPT_TOO_LONG_CONCEPT_NAME)
assert context.sheerka.isinstance(res.value, BuiltinConcepts.CONCEPT_TOO_LONG)
assert res.value.obj == "a very very long concept that cannot be an unique one"
def test_i_can_detect_concept_from_tokens():
context = get_context()
concept = get_concept("hello world", [])
context.sheerka.add_in_cache(concept)
source = "hello world"
results = ExactConceptParser().parse(context, list(Tokenizer(source)))
assert len(results) == 1
assert results[0].status
assert results[0].value == concept
def get_context():
sheerka = Sheerka()
sheerka.initialize(root_folder)
return ExecutionContext("sheerka", "xxxx", sheerka)
def get_concept(name, variables):
@@ -143,10 +162,3 @@ def get_concept(name, variables):
c.props[v] = Property(v, None)
c.init_key()
return c
def get_sheerka():
sheerka = Sheerka()
sheerka.initialize(root_folder)
return sheerka