Added SheerkaComparisonManager
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
import pytest
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.sheerka.services.SheerkaComparisonManager import SheerkaComparisonManager, ComparisonObj
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
class TestSheerkaGreaterThanManager(TestUsingMemoryBasedSheerka):
|
||||
def test_i_can_add_a_is_greater_than(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two", cache_only=False)
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
res = service.is_greater_than(context, "prop_name", two, one)
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#")]
|
||||
|
||||
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
|
||||
assert weighted == {"1001": 1, "1002": 2}
|
||||
|
||||
# I can commit
|
||||
sheerka.cache_manager.commit(context)
|
||||
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#")]
|
||||
|
||||
def test_i_can_add_a_is_less_than(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two", cache_only=False)
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
res = service.is_less_than(context, "prop_name", one, two)
|
||||
assert res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.SUCCESS)
|
||||
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [ComparisonObj(context.event.get_digest(), "prop_name", one.id, two.id, "<", "#")]
|
||||
|
||||
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
|
||||
assert weighted == {"1001": 1, "1002": 2}
|
||||
|
||||
# I can commit
|
||||
sheerka.cache_manager.commit(context)
|
||||
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_db == [ComparisonObj(context.event.get_digest(), "prop_name", one.id, two.id, "<", "#")]
|
||||
|
||||
def test_i_can_add_multiples_constraints(self):
|
||||
sheerka, context, one, two, three, four = self.init_concepts("one", "two", "three", "four", cache_only=False)
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
service.is_greater_than(context, "prop_name", three, two)
|
||||
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", three.id, two.id, ">", "#")
|
||||
]
|
||||
|
||||
# I can commit
|
||||
sheerka.cache_manager.commit(context)
|
||||
in_db = sheerka.sdp.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_db == [
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", three.id, two.id, ">", "#")
|
||||
]
|
||||
|
||||
sheerka.cache_manager.clear(SheerkaComparisonManager.COMPARISON_ENTRY) # reset the cache
|
||||
|
||||
service.is_greater_than(context, "prop_name", four, three)
|
||||
in_cache = sheerka.cache_manager.get(SheerkaComparisonManager.COMPARISON_ENTRY, "prop_name|#")
|
||||
assert in_cache == [
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", two.id, one.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", three.id, two.id, ">", "#"),
|
||||
ComparisonObj(context.event.get_digest(), "prop_name", four.id, three.id, ">", "#"),
|
||||
]
|
||||
|
||||
weighted = sheerka.cache_manager.get(SheerkaComparisonManager.RESOLVED_COMPARISON_ENTRY, "prop_name|#")
|
||||
assert weighted == {"1001": 1, "1002": 2, "1003": 3, "1004": 4}
|
||||
|
||||
@pytest.mark.parametrize("entries, expected", [
|
||||
(["two > one"], {'1001': 1, '1002': 2}),
|
||||
(["one < two"], {'1001': 1, '1002': 2}),
|
||||
(["three > two", "one < two"], {'1001': 1, '1002': 2, '1003': 3}),
|
||||
(["three > one", "one < two"], {'1001': 1, '1002': 2, '1003': 2}),
|
||||
])
|
||||
def test_i_can_get_concept_weight(self, entries, expected):
|
||||
sheerka, context, *concepts = self.init_concepts("one", "two", "three")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
concepts_map = dict(zip(["one", "two", "three", "four", "five", "six"], concepts))
|
||||
|
||||
for entry in entries:
|
||||
if ">" in entry:
|
||||
a, b = [concepts_map[e.strip()] for e in entry.split(">")]
|
||||
service.is_greater_than(context, "prop_name", a, b)
|
||||
else:
|
||||
a, b = [concepts_map[e.strip()] for e in entry.split("<")]
|
||||
service.is_less_than(context, "prop_name", a, b)
|
||||
|
||||
assert service.get_concepts_weights("prop_name") == expected
|
||||
|
||||
def test_i_can_get_partition(self):
|
||||
sheerka, context, one, two, three = self.init_concepts("one", "two", "three")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
service.is_less_than(context, "prop_name", two, three)
|
||||
|
||||
res = service.get_partition("prop_name")
|
||||
|
||||
assert res == {
|
||||
1: ["1001"],
|
||||
2: ["1002"],
|
||||
3: ["1003"],
|
||||
}
|
||||
|
||||
def test_i_can_detect_chicken_and_egg(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
res = service.is_greater_than(context, "prop_name", one, two)
|
||||
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.CHICKEN_AND_EGG)
|
||||
assert set(res.body.body) == {one, two}
|
||||
|
||||
def test_i_can_detect_more_complex_chicken_and_egg(self):
|
||||
sheerka, context, one, two, three, four, five = self.init_concepts("one", "two", "three", "four", "five")
|
||||
service = sheerka.services[SheerkaComparisonManager.NAME]
|
||||
|
||||
service.is_greater_than(context, "prop_name", two, one)
|
||||
service.is_greater_than(context, "prop_name", five, four)
|
||||
service.is_greater_than(context, "prop_name", four, three)
|
||||
service.is_greater_than(context, "prop_name", five, two)
|
||||
|
||||
res = service.is_greater_than(context, "prop_name", two, one)
|
||||
assert res.status
|
||||
|
||||
res = service.is_greater_than(context, "prop_name", one, five)
|
||||
|
||||
assert not res.status
|
||||
assert sheerka.isinstance(res.body, BuiltinConcepts.CHICKEN_AND_EGG)
|
||||
assert set(res.body.body) == {one, two, five}
|
||||
|
||||
def test_methods_are_correctly_bound(self):
|
||||
sheerka, context, one, two = self.init_concepts("one", "two")
|
||||
res = sheerka.is_greater_than(context, "prop_name", two, one)
|
||||
assert res.status
|
||||
@@ -1,4 +1,4 @@
|
||||
from core.sheerka.Services.SheerkaHistoryManager import hist
|
||||
from core.sheerka.services.SheerkaHistoryManager import hist
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ class TestSheerkaVariable(TestUsingMemoryBasedSheerka):
|
||||
# I can persist in db
|
||||
sheerka.cache_manager.commit(context)
|
||||
|
||||
assert sheerka.sdp.exists(Sheerka.VARIABLES_ENTRY, "TestSheerkaVariable.my_variable")
|
||||
loaded = sheerka.sdp.get(Sheerka.VARIABLES_ENTRY, "TestSheerkaVariable.my_variable")
|
||||
assert sheerka.sdp.exists(Sheerka.VARIABLES_ENTRY, "TestSheerkaVariable|my_variable")
|
||||
loaded = sheerka.sdp.get(Sheerka.VARIABLES_ENTRY, "TestSheerkaVariable|my_variable")
|
||||
assert loaded.event_id == context.event.get_digest()
|
||||
assert loaded.key == "my_variable"
|
||||
assert loaded.value == 1
|
||||
|
||||
@@ -16,6 +16,13 @@ class ConceptWithGetObjValue(Concept):
|
||||
|
||||
class TestSheerkaUsingMemoryBasedSheerka(TestUsingMemoryBasedSheerka):
|
||||
|
||||
def test_i_can_initialize_services(self):
|
||||
sheerka = self.get_sheerka()
|
||||
|
||||
assert len(sheerka.services) > 0
|
||||
assert None not in sheerka.services
|
||||
assert "ComparisonManager" in sheerka.services # test at least one service
|
||||
|
||||
def test_i_can_initialize_builtin_parsers(self):
|
||||
sheerka = self.get_sheerka()
|
||||
|
||||
|
||||
@@ -855,6 +855,24 @@ as:
|
||||
assert res[0].status
|
||||
assert res[0].body == 64
|
||||
|
||||
def test_concepts_parsed_by_atom_parser_must_not_be_evaluated(self):
|
||||
definitions = [
|
||||
"def concept mult from a mult b as a * b",
|
||||
"def concept a mult b as a * b",
|
||||
]
|
||||
|
||||
sheerka = self.init_scenario(definitions)
|
||||
|
||||
res = sheerka.evaluate_user_input("eval mult")
|
||||
|
||||
assert res[0].status
|
||||
assert isinstance(res[0].body, Concept)
|
||||
|
||||
# res = sheerka.evaluate_user_input("eval a mult b")
|
||||
#
|
||||
# assert res[0].status
|
||||
# assert isinstance(res[0].body, Concept)
|
||||
|
||||
|
||||
class TestSheerkaNonRegFile(TestUsingFileBasedSheerka):
|
||||
def test_i_can_def_several_concepts(self):
|
||||
|
||||
@@ -1,22 +1,25 @@
|
||||
import pytest
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from core.concept import Concept, DEFINITION_TYPE_DEF
|
||||
from parsers.AtomNodeParser import AtomNodeParser
|
||||
from parsers.BaseNodeParser import cnode, utnode, CNC, scnode, SCN
|
||||
from parsers.BaseNodeParser import cnode, utnode, CNC, SCN
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
from tests.parsers.parsers_utils import compute_expected_array
|
||||
|
||||
|
||||
class TestAtomsParser(TestUsingMemoryBasedSheerka):
|
||||
def init_parser(self, my_map, create_new=False, singleton=True):
|
||||
def init_parser(self, my_map, create_new=False, singleton=True, use_sheerka=False):
|
||||
sheerka, context, *updated_concepts = self.init_concepts(
|
||||
*my_map.values(),
|
||||
create_new=create_new,
|
||||
singleton=singleton)
|
||||
|
||||
parser = AtomNodeParser()
|
||||
parser.init_from_concepts(context, updated_concepts)
|
||||
if use_sheerka:
|
||||
parser = AtomNodeParser(sheerka=sheerka)
|
||||
else:
|
||||
parser = AtomNodeParser()
|
||||
parser.init_from_concepts(context, updated_concepts)
|
||||
|
||||
return sheerka, context, parser
|
||||
|
||||
@@ -32,15 +35,20 @@ class TestAtomsParser(TestUsingMemoryBasedSheerka):
|
||||
("foo", ["foo"]),
|
||||
("foo bar", ["foo", "bar"]),
|
||||
("foo bar twenties", ["foo", "bar", "twenties"]),
|
||||
# ("plus", ["plus"]),
|
||||
# ("++", ["++"]),
|
||||
# ("a++ foo", ["++", "foo"]),
|
||||
])
|
||||
def test_i_can_parse_simple_sequences(self, text, expected):
|
||||
concepts_map = {
|
||||
"foo": Concept("foo"),
|
||||
"bar": Concept("bar"),
|
||||
"plus": Concept("a plus b").def_var("a").def_var("b"),
|
||||
"++": Concept("++", definition="a++", definition_type=DEFINITION_TYPE_DEF).def_var("a"),
|
||||
"twenties": Concept("twenties", definition="'twenty' ('one'|'two')=unit").def_var("unit"),
|
||||
}
|
||||
|
||||
sheerka, context, parser = self.init_parser(concepts_map)
|
||||
sheerka, context, parser = self.init_parser(concepts_map, create_new=True, use_sheerka=True)
|
||||
res = parser.parse(context, text)
|
||||
wrapper = res.body
|
||||
lexer_nodes = res.body.body
|
||||
@@ -276,3 +284,27 @@ class TestAtomsParser(TestUsingMemoryBasedSheerka):
|
||||
expected_array = compute_expected_array(concepts_map, text, expected)
|
||||
assert sheerka.isinstance(wrapper, BuiltinConcepts.PARSER_RESULT)
|
||||
assert lexer_nodes == expected_array
|
||||
|
||||
@pytest.mark.parametrize("text, expected_is_evaluated", [
|
||||
("foo", False),
|
||||
("bar", False ),
|
||||
("twenties", True),
|
||||
("plus", True),
|
||||
# ("plus", ["plus"]),
|
||||
# ("++", ["++"]),
|
||||
# ("a++ foo", ["++", "foo"]),
|
||||
])
|
||||
def test_concepts_with_variables_must_not_be_evaluated(self, text, expected_is_evaluated):
|
||||
concepts_map = {
|
||||
"foo": Concept("foo"),
|
||||
"bar": Concept("bar", body="'bar'"),
|
||||
"plus": Concept("plus", definition="a plus b", definition_type=DEFINITION_TYPE_DEF).def_var("a").def_var("b"),
|
||||
"twenties": Concept("twenties", definition="'twenty' ('one'|'two')=unit").def_var("unit"),
|
||||
}
|
||||
|
||||
sheerka, context, parser = self.init_parser(concepts_map, create_new=True, use_sheerka=True)
|
||||
res = parser.parse(context, text)
|
||||
lexer_nodes = res.body.body
|
||||
|
||||
assert res.status
|
||||
assert lexer_nodes[0].concept.metadata.is_evaluated == expected_is_evaluated
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import pytest
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept, CC
|
||||
from core.sheerka.services.SheerkaComparisonManager import SheerkaComparisonManager
|
||||
from core.tokenizer import Tokenizer
|
||||
from parsers.BaseNodeParser import utnode, ConceptNode, cnode, short_cnode, UnrecognizedTokensNode, \
|
||||
SCWC, CNC, UTN
|
||||
@@ -48,10 +49,20 @@ class TestSyaNodeParser(TestUsingMemoryBasedSheerka):
|
||||
create_new=True,
|
||||
init_from_sheerka=True)
|
||||
|
||||
TestSyaNodeParser.sheerka.force_sya_def(context, [
|
||||
(cmap["plus"].id, 5, SyaAssociativity.Right),
|
||||
(cmap["mult"].id, 10, SyaAssociativity.Right),
|
||||
(cmap["minus"].id, 10, SyaAssociativity.Right)])
|
||||
cmap["plus"].set_prop(BuiltinConcepts.ASSOCIATIVITY, "right")
|
||||
cmap["mult"].set_prop(BuiltinConcepts.ASSOCIATIVITY, "right")
|
||||
cmap["minus"].set_prop(BuiltinConcepts.ASSOCIATIVITY, "right")
|
||||
TestSyaNodeParser.sheerka.services[SheerkaComparisonManager.NAME].is_greater_than(context,
|
||||
BuiltinConcepts.PRECEDENCE,
|
||||
cmap["mult"], cmap["plus"])
|
||||
TestSyaNodeParser.sheerka.services[SheerkaComparisonManager.NAME].is_greater_than(context,
|
||||
BuiltinConcepts.PRECEDENCE,
|
||||
cmap["mult"], cmap["minus"])
|
||||
|
||||
# TestSyaNodeParser.sheerka.force_sya_def(context, [
|
||||
# (cmap["plus"].id, 5, SyaAssociativity.Right),
|
||||
# (cmap["mult"].id, 10, SyaAssociativity.Right),
|
||||
# (cmap["minus"].id, 5, SyaAssociativity.Right)])
|
||||
|
||||
def init_parser(self,
|
||||
my_concepts_map=None,
|
||||
|
||||
Reference in New Issue
Block a user