Implemented SheerkaOntology
This commit is contained in:
@@ -2,16 +2,15 @@ import json
|
||||
from logging import Logger
|
||||
|
||||
import core.utils
|
||||
from core.concept import Concept, NotInitialized
|
||||
from core.concept import Concept
|
||||
from core.sheerka.services.SheerkaExecute import ParserInput
|
||||
from core.simple_debug import my_debug
|
||||
from sheerkapickle import utils, tags, handlers
|
||||
|
||||
|
||||
def encode(sheerka, obj):
|
||||
pickler = SheerkaPickler(sheerka)
|
||||
flatten = pickler.flatten(obj)
|
||||
my_debug(f"{obj} ids={len(pickler.ids)}, objs={len(pickler.objs)}")
|
||||
# my_debug(f"{obj} ids={len(pickler.ids)}, objs={len(pickler.objs)}")
|
||||
return json.dumps(flatten)
|
||||
|
||||
|
||||
@@ -38,9 +37,10 @@ class SheerkaPickler:
|
||||
self.to_reduce.append(ToReduce(lambda o: isinstance(o, Logger), lambda o: None))
|
||||
from parsers.BaseParser import BaseParser
|
||||
from evaluators.BaseEvaluator import BaseEvaluator
|
||||
from core.sheerka.SheerkaOntologyManager import Ontology
|
||||
self.to_reduce.append(ToReduce(lambda o: isinstance(o, (BaseParser, BaseEvaluator)), lambda o: o.name))
|
||||
self.to_reduce.append(ToReduce(lambda o: isinstance(o, ParserInput), lambda o: o.as_text()))
|
||||
self.to_reduce.append(ToReduce(lambda o: isinstance(o, NotInitialized), lambda o: None))
|
||||
self.to_reduce.append(ToReduce(lambda o: isinstance(o, Ontology), lambda o: o.name))
|
||||
|
||||
def flatten(self, obj):
|
||||
if utils.is_to_discard(obj):
|
||||
@@ -49,6 +49,9 @@ class SheerkaPickler:
|
||||
if utils.is_primitive(obj):
|
||||
return obj
|
||||
|
||||
if utils.is_custom_type(obj):
|
||||
return self._flatten_custom_type(obj)
|
||||
|
||||
if utils.is_type(obj):
|
||||
return str(obj)
|
||||
|
||||
@@ -133,6 +136,18 @@ class SheerkaPickler:
|
||||
|
||||
return data
|
||||
|
||||
def _flatten_custom_type(self, obj):
|
||||
# check if the object was already seen
|
||||
exists, _id = self.exist(obj)
|
||||
if exists:
|
||||
return {tags.ID: _id}
|
||||
else:
|
||||
self.id_count = self.id_count + 1
|
||||
self.ids[id(obj)] = self.id_count
|
||||
self.objs.append(obj)
|
||||
|
||||
return {tags.CUSTOM: obj.value}
|
||||
|
||||
def exist(self, obj):
|
||||
try:
|
||||
v = self.ids[id(obj)]
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import json
|
||||
|
||||
import core.utils
|
||||
from core.global_symbols import NotInit, NotFound, Removed
|
||||
from sheerkapickle import tags, utils, handlers
|
||||
|
||||
|
||||
@@ -20,6 +21,9 @@ class SheerkaUnpickler:
|
||||
if has_tag(obj, tags.TUPLE):
|
||||
return self._restore_tuple(obj)
|
||||
|
||||
if has_tag(obj, tags.CUSTOM):
|
||||
return self._restore_custom(obj)
|
||||
|
||||
if has_tag(obj, tags.SET):
|
||||
return self._restore_set(obj)
|
||||
|
||||
@@ -43,6 +47,19 @@ class SheerkaUnpickler:
|
||||
def _restore_tuple(self, obj):
|
||||
return tuple([self.restore(v) for v in obj[tags.TUPLE]])
|
||||
|
||||
def _restore_custom(self, obj):
|
||||
if obj[tags.CUSTOM] == NotInit.value:
|
||||
instance = NotInit
|
||||
elif obj[tags.CUSTOM] == NotFound.value:
|
||||
instance = NotFound
|
||||
elif obj[tags.CUSTOM] == Removed.value:
|
||||
instance = Removed
|
||||
else:
|
||||
raise KeyError(f"unknown {obj[tags.CUSTOM]}")
|
||||
|
||||
self.objs.append(instance)
|
||||
return instance
|
||||
|
||||
def _restore_set(self, obj):
|
||||
return set([self.restore(v) for v in obj[tags.SET]])
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import core.utils
|
||||
from core.builtin_concepts import UserInputConcept, ReturnValueConcept, BuiltinConcepts
|
||||
from core.concept import Concept, PROPERTIES_TO_SERIALIZE as CONCEPT_PROPERTIES_TO_SERIALIZE, ConceptParts, NotInit, \
|
||||
get_concept_attrs
|
||||
from core.concept import Concept, PROPERTIES_TO_SERIALIZE as CONCEPT_PROPERTIES_TO_SERIALIZE
|
||||
from core.global_symbols import NotInit
|
||||
from core.rule import Rule
|
||||
from core.sheerka.ExecutionContext import ExecutionContext, PROPERTIES_TO_SERIALIZE as CONTEXT_PROPERTIES_TO_SERIALIZE
|
||||
from core.sheerka.Sheerka import Sheerka
|
||||
@@ -221,4 +221,3 @@ def initialize_pickle_handlers():
|
||||
registry.register(ExecutionContext, ExecutionContextHandler, True)
|
||||
registry.register(Rule, RuleContextHandler, True)
|
||||
registry.register(PythonNode, PythonNodeHandler, True)
|
||||
|
||||
|
||||
@@ -3,3 +3,4 @@ TUPLE = "_sheerka/tuple"
|
||||
SET = "_sheerka/set"
|
||||
OBJECT = "_sheerka/obj"
|
||||
ENUM = "_sheerka/enum"
|
||||
CUSTOM = "_sheerka/custom"
|
||||
|
||||
@@ -2,6 +2,8 @@ import base64
|
||||
import types
|
||||
from enum import Enum
|
||||
|
||||
from core.global_symbols import CustomType
|
||||
|
||||
class_types = (type,)
|
||||
PRIMITIVES = (str, bool, type(None), int, float)
|
||||
|
||||
@@ -17,6 +19,10 @@ def is_enum(obj):
|
||||
return isinstance(obj, Enum)
|
||||
|
||||
|
||||
def is_custom_type(obj):
|
||||
return isinstance(obj, CustomType)
|
||||
|
||||
|
||||
def is_object(obj):
|
||||
"""Returns True is obj is a reference to an object instance."""
|
||||
|
||||
@@ -36,7 +42,7 @@ def is_primitive(obj):
|
||||
|
||||
|
||||
def is_dictionary(obj):
|
||||
return isinstance(obj, dict)
|
||||
return isinstance(obj, dict)
|
||||
|
||||
|
||||
def is_list(obj):
|
||||
|
||||
Reference in New Issue
Block a user