Implemented a first and basic version of a Rete rule engine
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
import pytest
|
||||
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept
|
||||
from core.global_symbols import NotInit, NotFound
|
||||
from core.sheerka.ExecutionContext import ExecutionContext
|
||||
from core.sheerka.services.SheerkaDebugManager import SheerkaDebugManager, DebugItem, ConceptDebugObj
|
||||
from parsers.PythonParser import PythonNode
|
||||
from sdp.sheerkaDataProvider import Event
|
||||
|
||||
from tests.TestUsingMemoryBasedSheerka import TestUsingMemoryBasedSheerka
|
||||
|
||||
|
||||
@@ -21,220 +20,12 @@ class DummyObj:
|
||||
|
||||
class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
|
||||
def test_i_can_activate_debug(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
return_value_id = 0
|
||||
|
||||
sheerka.set_debug(context, True)
|
||||
assert sheerka.debug_activated()
|
||||
|
||||
sheerka.set_debug(context, False)
|
||||
assert not sheerka.debug_activated()
|
||||
|
||||
def test_when_debug_mode_is_activated_context_are_in_debug_mode(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1
|
||||
sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1
|
||||
sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1
|
||||
sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child
|
||||
|
||||
assert context.id == 1
|
||||
assert context.debug_enabled
|
||||
assert sub_context.debug_enabled
|
||||
assert sub_context2.debug_enabled
|
||||
assert sub_sub_context.debug_enabled
|
||||
assert not root_context.debug_enabled
|
||||
|
||||
def test_i_can_activate_debug_for_new_context(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, 1)
|
||||
|
||||
context = ExecutionContext("test", Event(), sheerka, BuiltinConcepts.TESTING, None) # context.id = 1
|
||||
assert context.debug_enabled
|
||||
|
||||
def test_i_can_activate_debug_for_new_context_2(self):
|
||||
"""
|
||||
This time children is also requested
|
||||
:return:
|
||||
"""
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, 1, children=True)
|
||||
|
||||
context = ExecutionContext("test", Event(), sheerka, BuiltinConcepts.TESTING, None) # context.id = 1
|
||||
assert context.debug_enabled
|
||||
|
||||
def test_i_can_activate_debug_for_a_context_using_push(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, 1)
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1
|
||||
sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1
|
||||
|
||||
assert context.id == 1
|
||||
assert 1 in service.context_cache
|
||||
assert context.debug_enabled
|
||||
assert not sub_context.debug_enabled
|
||||
assert not root_context.debug_enabled
|
||||
|
||||
def test_global_debug_must_be_activated_to_activate_context_debug(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
sheerka.set_debug(root_context, False)
|
||||
sheerka.activate_debug_for(root_context, 1)
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1
|
||||
|
||||
assert context.id == 1
|
||||
assert 1 in service.context_cache
|
||||
assert not context.debug_enabled
|
||||
|
||||
def test_i_can_activate_debug_for_sub_children(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, 1, children=True)
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1
|
||||
sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1
|
||||
sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1
|
||||
sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child
|
||||
|
||||
assert context.id == 1
|
||||
assert 1 in service.context_cache
|
||||
assert "1+" in service.context_cache
|
||||
assert context.debug_enabled
|
||||
assert sub_context.debug_enabled
|
||||
assert sub_context2.debug_enabled
|
||||
assert sub_sub_context.debug_enabled
|
||||
assert not root_context.debug_enabled
|
||||
|
||||
def test_i_can_deactivate_debug_for_a_context(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, 1)
|
||||
sheerka.deactivate_debug_for(root_context, 1)
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None)
|
||||
|
||||
assert context.id == 1
|
||||
assert 1 not in service.context_cache
|
||||
assert not context.debug_enabled
|
||||
|
||||
def test_i_can_deactivate_context_but_not_children(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, 1, True)
|
||||
sheerka.deactivate_debug_for(root_context, 1)
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1
|
||||
sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1
|
||||
sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1
|
||||
sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child
|
||||
|
||||
assert context.id == 1
|
||||
assert 1 not in service.context_cache
|
||||
assert "1+" in service.context_cache
|
||||
assert not context.debug_enabled
|
||||
assert sub_context.debug_enabled
|
||||
assert sub_context2.debug_enabled
|
||||
assert sub_sub_context.debug_enabled
|
||||
|
||||
def test_i_can_deactivate_context_and_children(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, 1)
|
||||
sheerka.deactivate_debug_for(root_context, 1, children=True)
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1
|
||||
sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1
|
||||
sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1
|
||||
sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child
|
||||
|
||||
assert 1 not in service.context_cache
|
||||
assert "1+" not in service.context_cache
|
||||
assert not context.debug_enabled
|
||||
assert not sub_context.debug_enabled
|
||||
assert not sub_context2.debug_enabled
|
||||
assert not sub_sub_context.debug_enabled
|
||||
|
||||
def test_i_can_activate_debug_for_a_variable(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
sheerka.set_debug(context)
|
||||
|
||||
sheerka.activate_debug_for(context, "Out")
|
||||
assert "Out" in service.variable_cache
|
||||
assert sheerka.debug_activated_for("Out")
|
||||
|
||||
sheerka.deactivate_debug_for(context, "Out")
|
||||
assert "Out" not in service.variable_cache
|
||||
assert not sheerka.debug_activated_for("Out")
|
||||
|
||||
def test_i_can_activate_debug_for_sub_children_using_the_simplified_form(self):
|
||||
sheerka = self.get_sheerka()
|
||||
ExecutionContext.ids.clear()
|
||||
root_context = self.get_context(sheerka)
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, True)
|
||||
sheerka.activate_debug_for(root_context, "1+")
|
||||
|
||||
context = root_context.push(BuiltinConcepts.NOP, None) # sub_context.id = 1
|
||||
sub_context = context.push(BuiltinConcepts.NOP, None) # sub_sub_context.parent = 1
|
||||
sub_context2 = context.push(BuiltinConcepts.NOP, None) # sub_sub_context2.parent = 1
|
||||
sub_sub_context = sub_context.push(BuiltinConcepts.NOP, None) # is a child
|
||||
|
||||
assert context.id == 1
|
||||
assert 1 in service.context_cache
|
||||
assert "1+" in service.context_cache
|
||||
assert context.debug_enabled
|
||||
assert sub_context.debug_enabled
|
||||
assert sub_context2.debug_enabled
|
||||
assert sub_sub_context.debug_enabled
|
||||
assert not root_context.debug_enabled
|
||||
@classmethod
|
||||
def setup(cls):
|
||||
sheerka = cls().get_sheerka()
|
||||
cls.return_value_id = sheerka.get_by_key("__RETURN_VALUE").id
|
||||
|
||||
@pytest.mark.parametrize("item_type", [
|
||||
"vars", "rules", "concepts"
|
||||
@@ -631,9 +422,6 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
root_context = self.get_context(sheerka)
|
||||
|
||||
sheerka.set_debug(root_context, True)
|
||||
sheerka.set_explicit(root_context, False)
|
||||
sheerka.activate_debug_for(root_context, 1, children=True)
|
||||
sheerka.activate_debug_for(root_context, "SomeVar")
|
||||
sheerka.debug_var(root_context, "service_name.*.var")
|
||||
sheerka.debug_rule(root_context, 1)
|
||||
sheerka.debug_concept(root_context, 1001)
|
||||
@@ -642,9 +430,6 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
another_service.initialize_deferred(root_context, True)
|
||||
|
||||
assert another_service.activated
|
||||
assert not another_service.explicit
|
||||
assert another_service.context_cache == {1, "1+"}
|
||||
assert another_service.variable_cache == {"SomeVar"}
|
||||
assert another_service.debug_vars_settings == [
|
||||
DebugItem('var', 'service_name', None, None, False, None, False, True)]
|
||||
assert another_service.debug_rules_settings == [
|
||||
@@ -688,7 +473,7 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
python_node = PythonNode("one + 1").init_ast()
|
||||
|
||||
res = sheerka.inspect(context, python_node)
|
||||
assert set(res.body.keys()) == {"#type#", 'ast_', 'ast_str', 'compiled', 'objects', 'source'}
|
||||
assert set(res.body.keys()) == {"#type#", 'ast_', 'ast_str', 'compiled', 'objects', 'source', 'original_source'}
|
||||
|
||||
def test_i_can_inspect_object_specified_attributes(self):
|
||||
sheerka, context = self.init_concepts()
|
||||
@@ -872,7 +657,7 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
assert res.body == {
|
||||
'body': concept_debug_obj,
|
||||
'#type#': 'ReturnValueConcept',
|
||||
'id': '43',
|
||||
'id': f'{self.return_value_id}',
|
||||
'key': '__RETURN_VALUE',
|
||||
'name': '__RETURN_VALUE',
|
||||
'parents': [concept_debug_obj],
|
||||
@@ -920,21 +705,18 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
sheerka.push_ontology(context, "new ontology")
|
||||
|
||||
service.set_debug(context)
|
||||
service.set_explicit(context)
|
||||
service.debug_var(context, "var_service.var_method.var_name", "1+", 1)
|
||||
service.debug_rule(context, "rule_service.rule_method.rule_name", "2+", 2)
|
||||
service.debug_concept(context, "concept_service.concept_method.concept_name", "3+", 3)
|
||||
|
||||
# sanity check
|
||||
assert service.activated
|
||||
assert service.explicit
|
||||
assert service.debug_vars_settings != []
|
||||
assert service.debug_rules_settings != []
|
||||
assert service.debug_concepts_settings != []
|
||||
|
||||
sheerka.pop_ontology()
|
||||
sheerka.pop_ontology(context)
|
||||
assert not service.activated
|
||||
assert not service.explicit
|
||||
assert service.context_cache == set()
|
||||
assert service.variable_cache == set()
|
||||
assert service.debug_vars_settings == []
|
||||
@@ -946,7 +728,6 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
service = sheerka.services[SheerkaDebugManager.NAME]
|
||||
|
||||
service.set_debug(context)
|
||||
service.set_explicit(context)
|
||||
service.debug_var(context, "v_service.v_method.v_name", "1+", 1)
|
||||
service.debug_rule(context, "r_service.r_method.r_name", "2+", 2)
|
||||
service.debug_concept(context, "c_serv.c_method.c_name", "3+", 3)
|
||||
@@ -955,7 +736,6 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
|
||||
# modify the state
|
||||
service.set_debug(context, False)
|
||||
service.set_explicit(context, False)
|
||||
service.debug_var(context, "var_service2.var_method2.var_name2", "11+", 11)
|
||||
service.debug_rule(context, "rule_service2.rule_method2.rule_name2", "22+", 22)
|
||||
service.debug_concept(context, "concept_service2.concept_method2.concept_name2", "33+", 33)
|
||||
@@ -967,10 +747,8 @@ class TestSheerkaDebugManager(TestUsingMemoryBasedSheerka):
|
||||
assert len(service.debug_rules_settings) == 2
|
||||
assert len(service.debug_concepts_settings) == 2
|
||||
|
||||
sheerka.pop_ontology()
|
||||
sheerka.pop_ontology(context)
|
||||
assert service.activated
|
||||
assert service.explicit
|
||||
assert service.debug_vars_settings == [DebugItem("v_name", "v_service", "v_method", 1, True, 1, False, True)]
|
||||
assert service.debug_rules_settings == [DebugItem("r_name", "r_service", "r_method", 2, True, 2, False, True)]
|
||||
assert service.debug_concepts_settings == [DebugItem("c_name", "c_serv", "c_method", 3, True, 3, False, True)]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user