Fixed #68: Implement SheerkaQL
Fixed #70: SheerkaFilterManager : Pipe functions Fixed #71: SheerkaFilterManager : filter_objects Fixed #75: SheerkaMemory: Enhance memory() to use the filtering capabilities Fixed #76: SheerkaEvaluateConcept: Concepts that modify the state of the system must not be evaluated during question
This commit is contained in:
+23
-61
@@ -109,6 +109,7 @@ class Sheerka(Concept):
|
||||
self.enable_commands_backup = True
|
||||
|
||||
self.methods_with_context = {"test_using_context"} # only the names, the method is defined in sheerka_methods
|
||||
self.pipe_functions = set()
|
||||
self.sheerka_methods = {
|
||||
"test": SheerkaMethod(self.test, False),
|
||||
"test_using_context": SheerkaMethod(self.test_using_context, False),
|
||||
@@ -145,13 +146,22 @@ class Sheerka(Concept):
|
||||
if as_name is None:
|
||||
as_name = bound_method.__name__
|
||||
|
||||
if as_name.startswith("pipe_"):
|
||||
as_name = as_name[5:]
|
||||
is_pipe_function = True
|
||||
else:
|
||||
is_pipe_function = False
|
||||
|
||||
if visible:
|
||||
signature = inspect.signature(bound_method)
|
||||
if len(signature.parameters) > 0 and list(signature.parameters.keys())[0] == "context":
|
||||
self.methods_with_context.add(as_name)
|
||||
self.sheerka_methods[as_name] = SheerkaMethod(bound_method, has_side_effect)
|
||||
if is_pipe_function:
|
||||
self.pipe_functions.add(as_name)
|
||||
|
||||
setattr(self, bound_method.__name__, bound_method)
|
||||
if not is_pipe_function:
|
||||
setattr(self, bound_method.__name__, bound_method)
|
||||
|
||||
def initialize(self, root_folder: str = None, **kwargs):
|
||||
"""
|
||||
@@ -655,6 +665,10 @@ class Sheerka(Concept):
|
||||
if obj.body is NotInit:
|
||||
return obj
|
||||
|
||||
if obj.key in BuiltinErrors:
|
||||
# KSI 2021-04-20 # errors should be returned as soon as they are detected
|
||||
return obj
|
||||
|
||||
if reduce_simple_list and (isinstance(obj.body, list) or isinstance(obj.body, set)) and len(obj.body) == 1:
|
||||
body_to_use = obj.body[0]
|
||||
else:
|
||||
@@ -673,39 +687,15 @@ class Sheerka(Concept):
|
||||
|
||||
return (self.objvalue(obj) for obj in objs.body)
|
||||
|
||||
def get_errors(self, obj, **kwargs):
|
||||
def get_errors(self, context, obj, **kwargs):
|
||||
"""
|
||||
Browse obj, looking for error
|
||||
:param context:
|
||||
:param obj:
|
||||
:param kwargs: if defined, specialize the error
|
||||
:return:
|
||||
"""
|
||||
|
||||
def filter_by_type(x, name):
|
||||
if isinstance(x, Concept):
|
||||
return x.name == name
|
||||
else:
|
||||
return type(x).__name__ == name
|
||||
|
||||
def filter_by_attribute(x, attr_name, attr_value):
|
||||
if hasattr(x, "as_bag"):
|
||||
try:
|
||||
return x.as_bag()[attr_name] == attr_value
|
||||
except KeyError:
|
||||
return False
|
||||
else:
|
||||
try:
|
||||
return getattr(x, attr_name) == attr_value
|
||||
except AttributeError:
|
||||
return False
|
||||
|
||||
def and_filter(x, cond):
|
||||
for c in cond:
|
||||
if not c(x):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def is_error(_obj):
|
||||
if isinstance(_obj, ErrorObj):
|
||||
return True
|
||||
@@ -715,29 +705,6 @@ class Sheerka(Concept):
|
||||
|
||||
return False
|
||||
|
||||
def filter_objects(_objects):
|
||||
if kwargs:
|
||||
cond = []
|
||||
for k, v in kwargs.items():
|
||||
if k == "__type":
|
||||
expected_type = v
|
||||
cond.append(lambda x: filter_by_type(x, expected_type))
|
||||
else:
|
||||
attr_name = k
|
||||
expect_value = v
|
||||
cond.append(lambda x: filter_by_attribute(x, attr_name, expect_value))
|
||||
|
||||
if len(cond) > 1:
|
||||
copy_of_conditions = cond.copy()
|
||||
full_cond = lambda x: and_filter(x, copy_of_conditions)
|
||||
|
||||
else:
|
||||
full_cond = cond[0]
|
||||
|
||||
return [o for o in _objects if full_cond(o)]
|
||||
|
||||
return _objects
|
||||
|
||||
def inner_get_errors(_obj):
|
||||
if self.isinstance(_obj, BuiltinConcepts.RETURN_VALUE) and _obj.status:
|
||||
return []
|
||||
@@ -748,6 +715,8 @@ class Sheerka(Concept):
|
||||
if is_error(_obj):
|
||||
if isinstance(_obj, Concept) and _obj.body not in (NotInit, None):
|
||||
return [_obj] + inner_get_errors(_obj.body)
|
||||
if isinstance(_obj, ErrorObj) and hasattr(_obj, "get_error"):
|
||||
return [_obj] + inner_get_errors(_obj.get_error())
|
||||
else:
|
||||
return [_obj]
|
||||
|
||||
@@ -757,10 +726,10 @@ class Sheerka(Concept):
|
||||
return []
|
||||
|
||||
errors = inner_get_errors(obj)
|
||||
return filter_objects([e for e in errors])
|
||||
return self.filter_objects(context, [e for e in errors], **kwargs)
|
||||
|
||||
def has_error(self, obj, **kwargs):
|
||||
errors = self.get_errors(obj, **kwargs)
|
||||
def has_error(self, context, obj, **kwargs):
|
||||
errors = self.get_errors(context, obj, **kwargs)
|
||||
return len(errors) > 0
|
||||
|
||||
def get_evaluator_name(self, name):
|
||||
@@ -797,13 +766,6 @@ class Sheerka(Concept):
|
||||
|
||||
return bool(obj)
|
||||
|
||||
@staticmethod
|
||||
def is_error(obj):
|
||||
"""
|
||||
opposite of is_success
|
||||
"""
|
||||
return not Sheerka.is_success(obj)
|
||||
|
||||
@staticmethod
|
||||
def is_known(obj):
|
||||
if not isinstance(obj, Concept):
|
||||
@@ -844,7 +806,7 @@ class Sheerka(Concept):
|
||||
if isinstance(obj, Concept) and obj.id == self.id:
|
||||
return True
|
||||
|
||||
from evaluators.PythonEvaluator import Expando
|
||||
from sheerkapython.python_wrapper import Expando
|
||||
if isinstance(obj, Expando) and obj.get_name() == "sheerka":
|
||||
return True
|
||||
|
||||
|
||||
Reference in New Issue
Block a user