Enhanced ExecutionContext to keep track of the execution flow

This commit is contained in:
2020-01-07 15:47:43 +01:00
parent ffd98d7407
commit b4346b5af0
19 changed files with 966 additions and 190 deletions
+33 -4
View File
@@ -1,3 +1,5 @@
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.sheerka import ExecutionContext
@@ -18,9 +20,14 @@ def test_id_is_incremented_by_event_digest():
assert e.id == 1
def test_some_properties_are_given_to_the_child():
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka",
desc="some description",
def test_i_can_use_with_statement():
with ExecutionContext("who_", Event("event"), "fake_sheerka") as e:
pass
assert e.elapsed > 0
def test_i_can_push():
a = ExecutionContext("foo", Event("event_1"), "fake_sheerka", "some description",
obj=Concept("foo"),
step=BuiltinConcepts.EVALUATION,
iteration=15,
@@ -30,10 +37,11 @@ def test_some_properties_are_given_to_the_child():
b = a.push()
assert b._parent == a
assert b.who == a.who
assert b.event == a.event
assert b.sheerka == a.sheerka
assert b.desc == ""
assert b.desc is None
assert b.obj == a.obj
assert b.step == a.step
assert b.iteration == a.iteration
@@ -41,3 +49,24 @@ def test_some_properties_are_given_to_the_child():
assert b.id == a.id + 1
assert b._tab == a._tab + " "
assert b.preprocess == a.preprocess
def test_children_i_created_when_i_push():
e = ExecutionContext("who_", Event("event"), "fake_sheerka")
e.push("a", desc="I do something")
e.push("b", desc="oups! I did a again")
e.push("c", desc="I do something else")
assert len(e.children) == 3
assert e.children[0].who, e.children[0].who == ("a", "I do something")
assert e.children[1].who, e.children[1].who == ("b", "oups! I did a again")
assert e.children[2].who, e.children[2].who == ("c", "I do something else")
def test_i_can_add_variable_when_i_push():
e = ExecutionContext("who_", Event("event"), "fake_sheerka")
sub_e = e.push("a", my_new_var="new var value")
assert sub_e.my_new_var == "new var value"
with pytest.raises(AttributeError):
assert e.my_new_var == "" # my_new_var does not exist in parent
+89 -4
View File
@@ -32,8 +32,37 @@ def test_i_can_serialize():
Test concept.to_dict()
:return:
"""
# TODO
pass
concept = Concept(
name="concept_name",
is_builtin=True,
is_unique=True,
key="concept_key",
body="definition of the body",
where="definition of the where",
pre="definition of the pre",
post="definition of the post",
definition="bnf definition",
definition_type="def type",
desc="this this the desc",
id="123456"
).set_prop("a", 10).set_prop("b", None)
to_dict = concept.to_dict()
assert to_dict == {
'body': 'definition of the body',
'definition': 'bnf definition',
'definition_type': 'def type',
'desc': 'this this the desc',
'id': '123456',
'is_builtin': True,
'is_unique': True,
'key': 'concept_key',
'name': 'concept_name',
'post': 'definition of the post',
'pre': 'definition of the pre',
'props': [('a', 10), ('b', None)],
'where': 'definition of the where'
}
def test_i_can_deserialize():
@@ -41,5 +70,61 @@ def test_i_can_deserialize():
Test concept.from_dict()
:return:
"""
# TODO
pass
from_dict = {
'body': 'definition of the body',
'definition': 'bnf definition',
'definition_type': 'def type',
'desc': 'this this the desc',
'id': '123456',
'is_builtin': True,
'is_unique': True,
'key': 'concept_key',
'name': 'concept_name',
'post': 'definition of the post',
'pre': 'definition of the pre',
'props': [('a', 10), ('b', None)],
'where': 'definition of the where'
}
concept = Concept().from_dict(from_dict)
assert concept == Concept(
name="concept_name",
is_builtin=True,
is_unique=True,
key="concept_key",
body="definition of the body",
where="definition of the where",
pre="definition of the pre",
post="definition of the post",
definition="bnf definition",
definition_type="def type",
desc="this this the desc",
id="123456"
).set_prop("a", 10).set_prop("b", None)
def test_i_can_compare_concept_with_circular_reference():
foo = Concept("foo")
foo.metadata.body = foo
assert foo == foo
def test_i_can_compare_concept_with_sophisticated_circular_reference():
foo = Concept("foo")
bar = Concept("foo", body=foo)
baz = Concept("foo", body=bar)
foo.metadata.body = baz
assert foo != bar
def test_i_can_compare_concept_with_sophisticated_circular_reference_in_other_metadata():
foo = Concept("foo")
bar = Concept("foo", pre=foo)
baz = Concept("foo", pre=bar)
foo.metadata.pre = baz
assert foo != bar
+63 -8
View File
@@ -201,10 +201,10 @@ def test_i_can_get_list_of_concept_when_same_key_when_no_cache():
sheerka.concepts_cache = {} # reset the cache
from_cache = sheerka.get(concept1.key)
assert len(from_cache) == 2
assert from_cache[0] == concept1
assert from_cache[1] == concept2
result = sheerka.get(concept1.key)
assert len(result) == 2
assert result[0] == concept1
assert result[1] == concept2
def test_i_can_get_list_of_concept_when_same_key_when_cache():
@@ -220,10 +220,65 @@ def test_i_can_get_list_of_concept_when_same_key_when_cache():
# sheerka.concepts_cache = {} # Do not reset the cache
from_cache = sheerka.get(concept1.key)
assert len(from_cache) == 2
assert from_cache[0] == concept1
assert from_cache[1] == concept2
result = sheerka.get(concept1.key)
assert len(result) == 2
assert result[0] == concept1
assert result[1] == concept2
def test_i_can_get_the_correct_concept_using_the_id_when_same_key_when_no_cache():
sheerka = get_sheerka()
concept1 = get_default_concept()
concept2 = get_default_concept()
concept2.metadata.body = "a+b"
res1 = sheerka.create_new_concept(get_context(sheerka), concept1)
res2 = sheerka.create_new_concept(get_context(sheerka), concept2)
assert res1.value.body.key == res2.value.body.key # same key
result = sheerka.get(concept1.key, res2.body.body.id)
assert result.name == "a + b"
assert result.body == "a+b"
def test_i_can_get_the_correct_concept_using_the_id__when_same_key_when_cache():
sheerka = get_sheerka()
concept1 = get_default_concept()
concept2 = get_default_concept()
concept2.metadata.body = "a+b"
res1 = sheerka.create_new_concept(get_context(sheerka), concept1)
res2 = sheerka.create_new_concept(get_context(sheerka), concept2)
assert res1.value.body.key == res2.value.body.key # same key
result = sheerka.get(concept1.key, res2.body.body.id)
assert result.name == "a + b"
assert result.body == "a+b"
def test_i_cannot_get_the_correct_concept_id_the_id_is_wrong():
sheerka = get_sheerka()
concept1 = get_default_concept()
concept2 = get_default_concept()
concept2.metadata.body = "a+b"
res1 = sheerka.create_new_concept(get_context(sheerka), concept1)
res2 = sheerka.create_new_concept(get_context(sheerka), concept2)
assert res1.value.body.key == res2.value.body.key # same key
result = sheerka.get(concept1.key, "wrong id")
assert sheerka.isinstance(result, BuiltinConcepts.UNKNOWN_CONCEPT)
def test_i_cannot_get_when_key_is_none():
sheerka = get_sheerka()
res = sheerka.get(None)
assert sheerka.isinstance(res, BuiltinConcepts.ERROR)
assert res.body == "Concept key is undefined."
def test_unknown_concept_is_return_when_the_concept_is_not_found():
+9 -9
View File
@@ -9,7 +9,7 @@ from datetime import date, datetime
import shutil
import json
from sdp.sheerkaSerializer import ObjectSerializer, Serializer, PickleSerializer
from sdp.sheerkaSerializer import JsonSerializer, Serializer, PickleSerializer
import core.utils
tests_root = path.abspath("../build/tests")
@@ -789,7 +789,7 @@ def test_i_can_set_using_reference(root):
def test_i_can_add_an_object_with_a_key_as_a_reference(root):
sdp = SheerkaDataProvider(root)
obj = ObjDumpJson("my_key", "value1")
obj_serializer = ObjectSerializer(core.utils.get_full_qualified_name(obj))
obj_serializer = JsonSerializer(core.utils.get_full_qualified_name(obj))
sdp.serializer.register(obj_serializer)
entry, key = sdp.add(evt_digest, "entry", obj, use_ref=True)
@@ -813,7 +813,7 @@ def test_i_can_add_a_dictionary_as_a_reference(root):
sdp = SheerkaDataProvider(root)
obj = {"my_key": "value1"}
obj_serializer = ObjectSerializer(core.utils.get_full_qualified_name(obj))
obj_serializer = JsonSerializer(core.utils.get_full_qualified_name(obj))
sdp.serializer.register(obj_serializer)
entry, key = sdp.add(evt_digest, "entry", obj, use_ref=True)
@@ -1499,7 +1499,7 @@ def test_i_can_get_an_entry_by_key(root):
def test_i_can_get_object_saved_by_reference(root):
sdp = SheerkaDataProvider(root)
obj = ObjDumpJson("my_key", "value1")
sdp.serializer.register(ObjectSerializer(core.utils.get_full_qualified_name(obj)))
sdp.serializer.register(JsonSerializer(core.utils.get_full_qualified_name(obj)))
entry, key = sdp.add(evt_digest, "entry", obj, use_ref=True)
loaded = sdp.get(entry, key)
@@ -1714,7 +1714,7 @@ def test_i_can_test_than_the_object_exists_when_using_references(root):
def test_i_can_save_and_load_object_ref_with_history(root):
sdp = SheerkaDataProvider(root)
obj = ObjDumpJson("my_key", "value1")
sdp.serializer.register(ObjectSerializer(core.utils.get_full_qualified_name(obj)))
sdp.serializer.register(JsonSerializer(core.utils.get_full_qualified_name(obj)))
entry, key = sdp.add(evt_digest, "entry", obj, use_ref=True)
loaded = sdp.get(entry, key)
@@ -1770,7 +1770,7 @@ def test_i_can_add_obj_with_same_key_and_get_them_back(root):
sdp = SheerkaDataProvider(root)
obj1 = ObjDumpJson("key", "value1")
obj2 = ObjDumpJson("key", "value2")
sdp.serializer.register(ObjectSerializer(core.utils.get_full_qualified_name(obj1)))
sdp.serializer.register(JsonSerializer(core.utils.get_full_qualified_name(obj1)))
entry1, key1 = sdp.add(evt_digest, "entry", obj1, use_ref=True)
entry2, key2 = sdp.add(evt_digest, "entry", obj2, use_ref=True)
@@ -1790,7 +1790,7 @@ def test_i_get_safe_dictionary_without_origin(root):
sdp = SheerkaDataProvider(root)
obj = {"my_key": "value1"}
obj_serializer = ObjectSerializer(core.utils.get_full_qualified_name(obj))
obj_serializer = JsonSerializer(core.utils.get_full_qualified_name(obj))
sdp.serializer.register(obj_serializer)
entry, key = sdp.add(evt_digest, "entry", obj, use_ref=True)
@@ -1814,7 +1814,7 @@ def test_i_get_dictionary_without_origin(root):
sdp = SheerkaDataProvider(root)
obj = {"my_key": "value1"}
obj_serializer = ObjectSerializer(core.utils.get_full_qualified_name(obj))
obj_serializer = JsonSerializer(core.utils.get_full_qualified_name(obj))
sdp.serializer.register(obj_serializer)
entry, key = sdp.add(evt_digest, "entry", obj, use_ref=True)
@@ -1838,7 +1838,7 @@ def test_i_get_safe_object_without_origin(root):
sdp = SheerkaDataProvider(root)
obj = ObjDumpJson("my_key", "value1")
obj_serializer = ObjectSerializer(core.utils.get_full_qualified_name(obj))
obj_serializer = JsonSerializer(core.utils.get_full_qualified_name(obj))
sdp.serializer.register(obj_serializer)
entry, key = sdp.add(evt_digest, "entry", obj, use_ref=True)
+2 -2
View File
@@ -2,7 +2,7 @@ import pytest
from dataclasses import dataclass
from sdp.sheerkaDataProvider import Event
from sdp.sheerkaSerializer import Serializer, ObjectSerializer, SerializerContext
from sdp.sheerkaSerializer import Serializer, JsonSerializer, SerializerContext
from datetime import datetime
import core.utils
@@ -37,7 +37,7 @@ def test_i_can_serialize_an_event():
def test_i_can_serialize_an_object():
obj = Obj("10", "value")
serializer = Serializer()
serializer.register(ObjectSerializer("tests.test_sheerkaSerializer.Obj"))
serializer.register(JsonSerializer("tests.test_sheerkaSerializer.Obj"))
context = SerializerContext("kodjo", "6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b")
stream = serializer.serialize(obj, context)
+5 -6
View File
@@ -1,15 +1,14 @@
import pytest
import os
from os import path
import shutil
from os import path
from core.builtin_concepts import BuiltinConcepts, ReturnValueConcept
import pytest
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, PROPERTIES_TO_SERIALIZE, Property
from core.sheerka import Sheerka, ExecutionContext
from evaluators.MutipleSameSuccessEvaluator import MultipleSameSuccessEvaluator
from parsers.BaseParser import BaseParser
from parsers.ConceptLexerParser import Sequence, ZeroOrMore, StrMatch, OrderedChoice, Optional, ConceptMatch, \
ConceptLexerParser
from parsers.ConceptLexerParser import Sequence, StrMatch, OrderedChoice, Optional, ConceptMatch
from sdp.sheerkaDataProvider import SheerkaDataProvider, Event
tests_root = path.abspath("../build/tests")
+303
View File
@@ -0,0 +1,303 @@
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept
from core.sheerka import Sheerka, ExecutionContext, ExecutionContextIdManager
from core.sheerka_transform import SheerkaTransform, OBJ_TYPE_KEY, SheerkaTransformType, OBJ_ID_KEY
from sdp.sheerkaDataProvider import Event
def get_sheerka():
sheerka = Sheerka()
sheerka.initialize("mem://")
return sheerka
def get_context(sheerka):
return ExecutionContext("test", Event(), sheerka)
def test_i_can_transform_an_unknown_concept():
sheerka = get_sheerka()
foo = Concept("foo", body="body")
concept_with_sub = Concept("concept_with_sub", body=foo)
concept = Concept(
name="concept_name",
is_builtin=True,
is_unique=True,
key="concept_key",
body=concept_with_sub,
where=[foo, 1, "1", True, 1.0],
pre=foo,
post=None, # will not appear
definition="it is a definition",
definition_type="def type",
desc="this this the desc"
).set_prop("a", 10).set_prop("b", foo).set_prop("c", concept_with_sub)
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(concept)
assert to_dict == {
OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 0,
'name': 'concept_name',
'key': 'concept_key',
'is_builtin': True,
'is_unique': True,
'definition': 'it is a definition',
'definition_type': 'def type',
'desc': 'this this the desc',
'where': [{OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 1,
'body': 'body',
'name': 'foo'}, 1, '1', True, 1.0],
'pre': {OBJ_TYPE_KEY: SheerkaTransformType.Reference, OBJ_ID_KEY: 1},
'body': {
OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 2,
'name': 'concept_with_sub',
'body': {OBJ_TYPE_KEY: SheerkaTransformType.Reference, OBJ_ID_KEY: 1}},
'props': [
('a', 10),
('b', {OBJ_TYPE_KEY: SheerkaTransformType.Reference, OBJ_ID_KEY: 1}),
('c', {OBJ_TYPE_KEY: SheerkaTransformType.Reference, OBJ_ID_KEY: 2})
]
}
def test_i_can_transform_unknown_concept_with_almost_same_value():
sheerka = get_sheerka()
concept = Concept("foo")
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(concept)
assert to_dict == {OBJ_TYPE_KEY: SheerkaTransformType.Concept, OBJ_ID_KEY: 0, 'name': 'foo'}
def test_i_can_transform_known_concept_when_the_values_are_the_same():
sheerka = get_sheerka()
concept = Concept(
name="concept_name",
is_builtin=True,
is_unique=False,
key="concept_key",
body="body definition",
where="where definition",
pre="pre definition",
post="post definition",
definition="it is a definition",
definition_type="def type",
desc="this this the desc"
).set_prop("a").set_prop("b")
sheerka.create_new_concept(get_context(sheerka), concept)
new_concept = sheerka.new(concept.key)
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(new_concept)
assert to_dict == {OBJ_TYPE_KEY: SheerkaTransformType.Concept, OBJ_ID_KEY: 0, "id": "1001"}
def test_i_can_transform_known_concept_when_the_values_are_different():
sheerka = get_sheerka()
concept = Concept(
name="concept_name",
is_builtin=True,
is_unique=False,
key="concept_key",
body="body definition",
where="where definition",
pre="pre definition",
post="post definition",
definition="it is a definition",
definition_type="def type",
desc="this this the desc"
).set_prop("a").set_prop("b")
sheerka.create_new_concept(get_context(sheerka), concept)
new_concept = sheerka.new(concept.key, body="another", a=10, pre="another pre")
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(new_concept)
assert to_dict == {
OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 0,
"id": "1001",
'pre': 'another pre',
"body": "another",
'props': [('a', 10)]
}
def test_i_can_transform_concept_with_circular_reference():
sheerka = get_sheerka()
foo = Concept("foo", )
bar = Concept("bar", body=foo)
foo.metadata.body = bar
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(foo)
assert to_dict == {
OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 0,
'name': 'foo',
'body': {OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 1,
'name': 'bar',
'body': {OBJ_TYPE_KEY: SheerkaTransformType.Reference,
OBJ_ID_KEY: 0},
},
}
def test_i_can_transform_concept_with_circular_reference_2():
sheerka = get_sheerka()
foo = Concept("foo", )
bar = Concept("foo", body=foo)
foo.metadata.body = bar
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(foo)
assert to_dict == {
OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 0,
'name': 'foo',
'body': {OBJ_TYPE_KEY: SheerkaTransformType.Concept,
OBJ_ID_KEY: 1,
'name': 'foo',
'body': {OBJ_TYPE_KEY: SheerkaTransformType.Reference,
OBJ_ID_KEY: 0},
},
}
def test_i_can_transform_the_unknown_concept():
sheerka = get_sheerka()
unknown = sheerka.new(BuiltinConcepts.UNKNOWN_CONCEPT)
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(unknown)
assert len(to_dict) == 3
assert to_dict[OBJ_TYPE_KEY] == SheerkaTransformType.Concept
assert to_dict[OBJ_ID_KEY] == 0
assert "id" in to_dict
def test_i_can_transform_simple_execution_context():
sheerka = get_sheerka()
ExecutionContextIdManager.ids = {}
context = ExecutionContext("requester", Event(), sheerka, 'this is the desc')
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(context)
assert to_dict == {
OBJ_TYPE_KEY: SheerkaTransformType.ExecutionContext,
OBJ_ID_KEY: 0,
'_parent': None,
'_id': 0,
'_tab': '',
'_bag': {},
'_start': 0,
'_stop': 0,
'who': 'requester',
'event': {OBJ_TYPE_KEY: SheerkaTransformType.Event, OBJ_ID_KEY: 1, 'digest': 'xxx'},
'desc': 'this is the desc',
'children': [],
'preprocess': None,
'values': {},
'obj': None,
'concepts': {}
}
def test_i_can_transform_list():
sheerka = get_sheerka()
ExecutionContextIdManager.ids = {}
context = ExecutionContext("requester", Event(), sheerka, 'this is the desc')
st = SheerkaTransform(sheerka)
to_dict = st.to_dict([context])
assert len(to_dict) == 1
assert isinstance(to_dict, list)
assert to_dict[0]["who"] == "requester"
assert to_dict[0]["desc"] == "this is the desc"
def test_i_can_transform_set():
sheerka = get_sheerka()
ExecutionContextIdManager.ids = {}
context = ExecutionContext("requester", Event(), sheerka, 'this is the desc')
st = SheerkaTransform(sheerka)
to_dict = st.to_dict({context})
assert len(to_dict) == 1
assert isinstance(to_dict, list)
assert to_dict[0]["who"] == "requester"
assert to_dict[0]["desc"] == "this is the desc"
def test_i_can_transform_dict():
sheerka = get_sheerka()
ExecutionContextIdManager.ids = {}
context = ExecutionContext("requester", Event(), sheerka, 'this is the desc')
known_concept = Concept("foo", body="foo").set_prop("a", "value_of_a").init_key()
sheerka.create_new_concept(get_context(sheerka), known_concept)
unknown_concept = Concept("bar")
known = sheerka.new("foo")
bag = {
"context": context,
"known_concept": known_concept,
"unknown_concept": unknown_concept,
"True": True,
"Number": 1.1,
"String": "a string value",
"None": None,
unknown_concept: "hello",
known: "world"
}
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(bag)
assert isinstance(to_dict, dict)
assert to_dict['Number'] == 1.1
assert to_dict['String'] == 'a string value'
assert to_dict['True']
assert to_dict['None'] is None
assert to_dict["context"][OBJ_TYPE_KEY] == SheerkaTransformType.ExecutionContext
assert to_dict["known_concept"][OBJ_TYPE_KEY] == SheerkaTransformType.Concept
assert to_dict["known_concept"]["id"] == '1001'
assert to_dict["unknown_concept"][OBJ_TYPE_KEY] == SheerkaTransformType.Concept
assert to_dict["(None)bar"] == "hello"
assert to_dict["(1001)foo"] == "world"
def test_i_can_transform_when_circular_references():
sheerka = get_sheerka()
ExecutionContextIdManager.ids = {}
context = ExecutionContext("requester", Event(), sheerka, 'this is the desc')
context.push("another requester", "another desc")
st = SheerkaTransform(sheerka)
to_dict = st.to_dict(context)
assert isinstance(to_dict, dict)
assert to_dict[OBJ_TYPE_KEY] == SheerkaTransformType.ExecutionContext
assert len(to_dict["children"]) == 1
assert to_dict["children"][0][OBJ_TYPE_KEY] == SheerkaTransformType.ExecutionContext
assert to_dict["children"][0]['_parent'][OBJ_TYPE_KEY] == SheerkaTransformType.Reference
assert to_dict["children"][0]['_parent'][OBJ_ID_KEY] == 0
assert to_dict["children"][0]['event'][OBJ_TYPE_KEY] == SheerkaTransformType.Reference
assert to_dict["children"][0]['event'][OBJ_ID_KEY] == 1