Enhanced ExecutionContext to keep track of the execution flow
This commit is contained in:
+6
-5
@@ -7,11 +7,12 @@
|
||||
|
||||
### Current supported types
|
||||
- E : events
|
||||
- O : object (with history management)
|
||||
- P : pickle
|
||||
- S : state
|
||||
- C : concept
|
||||
- D : concept definitions
|
||||
- J : Json object (with history management)
|
||||
- P : pickle (no history)
|
||||
- S : state (history, but not managed by the serializer )
|
||||
- C : concept (with history management)
|
||||
- D : concept definitions (no history management)
|
||||
- R : executionContext ('R' stands for Result or ReturnValue, no history management)
|
||||
|
||||
## How concepts are serialized ?
|
||||
- get the id of the concept
|
||||
|
||||
@@ -656,6 +656,30 @@ class SheerkaDataProvider:
|
||||
with self.io.open(target_path, "rb") as f:
|
||||
return self.serializer.deserialize(f, None)
|
||||
|
||||
def save_result(self, execution_context):
|
||||
"""
|
||||
Save the execution context associated with an event
|
||||
To make a long story short,
|
||||
for every single user input, there is an event (which is the first thing that is created)
|
||||
and a result (the ExecutionContext created by sheerka.evaluate_user_input()
|
||||
:param execution_context:
|
||||
:return:
|
||||
"""
|
||||
digest = execution_context.event.get_digest()
|
||||
self.log.debug(f"Saving execution context. digest={digest}")
|
||||
target_path = self.io.get_obj_path(SheerkaDataProvider.EventFolder, digest) + "_result"
|
||||
if self.io.exists(target_path):
|
||||
return digest
|
||||
|
||||
self.io.write_binary(target_path, self.serializer.serialize(execution_context, None).read())
|
||||
return digest
|
||||
|
||||
def load_result(self, digest):
|
||||
target_path = self.io.get_obj_path(SheerkaDataProvider.EventFolder, digest) + "_result"
|
||||
|
||||
with self.io.open(target_path, "rb") as f:
|
||||
return self.serializer.deserialize(f, None)
|
||||
|
||||
def save_state(self, state: State):
|
||||
digest = state.get_digest()
|
||||
self.log.debug(f"Saving new state. digest={digest}")
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import dataclasses
|
||||
import json
|
||||
import pickle
|
||||
import datetime
|
||||
@@ -10,6 +11,9 @@ from enum import Enum
|
||||
import core.utils
|
||||
|
||||
from core.concept import Concept
|
||||
from core.tokenizer import Token
|
||||
from parsers.BaseParser import Node
|
||||
|
||||
|
||||
def json_default_converter(o):
|
||||
"""
|
||||
@@ -23,7 +27,13 @@ def json_default_converter(o):
|
||||
return o.isoformat()
|
||||
|
||||
if isinstance(o, Enum):
|
||||
return o.key
|
||||
return o.name
|
||||
|
||||
raise Exception("Cannot serialize " + o.__class__.__name__)
|
||||
# with open("json_encoding_error.txt", "a") as f:
|
||||
# f.write(o.__class__.__name__ + "\n")
|
||||
|
||||
|
||||
|
||||
|
||||
@dataclass()
|
||||
@@ -51,6 +61,7 @@ class Serializer:
|
||||
self.register(StateSerializer())
|
||||
self.register(ConceptSerializer())
|
||||
self.register(DictionarySerializer())
|
||||
self.register(ExecutionContextSerializer())
|
||||
|
||||
def register(self, serializer):
|
||||
"""
|
||||
@@ -161,9 +172,9 @@ class EventSerializer(BaseSerializer):
|
||||
return event
|
||||
|
||||
|
||||
class ObjectSerializer(BaseSerializer):
|
||||
class JsonSerializer(BaseSerializer):
|
||||
|
||||
def __init__(self, fully_qualified_name, name="O", version=1):
|
||||
def __init__(self, fully_qualified_name, name="J", version=1):
|
||||
BaseSerializer.__init__(self, name, version)
|
||||
self.fully_qualified_name = fully_qualified_name
|
||||
|
||||
@@ -219,9 +230,9 @@ class StateSerializer(PickleSerializer):
|
||||
1)
|
||||
|
||||
|
||||
class ConceptSerializer(ObjectSerializer):
|
||||
class ConceptSerializer(JsonSerializer):
|
||||
def __init__(self):
|
||||
ObjectSerializer.__init__(self, "core.concept.Concept", "C", 1)
|
||||
JsonSerializer.__init__(self, "core.concept.Concept", "C", 1)
|
||||
|
||||
def matches(self, obj):
|
||||
return isinstance(obj, Concept)
|
||||
@@ -235,6 +246,27 @@ class DictionarySerializer(PickleSerializer):
|
||||
"D",
|
||||
1)
|
||||
|
||||
|
||||
class ExecutionContextSerializer(BaseSerializer):
|
||||
def __init__(self):
|
||||
BaseSerializer.__init__(self, "R", 1)
|
||||
|
||||
def matches(self, obj):
|
||||
return core.utils.get_full_qualified_name(obj) == "core.sheerka.ExecutionContext"
|
||||
|
||||
def dump(self, stream, obj, context):
|
||||
as_json = obj.to_dict()
|
||||
stream.write(json.dumps(as_json, default=json_default_converter).encode("utf-8"))
|
||||
stream.seek(0)
|
||||
return stream
|
||||
|
||||
def load(self, stream, context):
|
||||
json_stream = stream.read().decode("utf-8")
|
||||
json_message = json.loads(json_stream)
|
||||
obj = core.utils.get_class("core.sheerka.ExecutionContext")()
|
||||
obj.from_dict(json_message)
|
||||
return obj
|
||||
|
||||
#
|
||||
# class SheerkaSerializer(ObjectSerializer):
|
||||
# def __init__(self):
|
||||
|
||||
Reference in New Issue
Block a user