Fixed #131 : Implement ExprToConditions

Fixed #130 : ArithmeticOperatorParser
Fixed #129 : python_wrapper : create_namespace
Fixed #128 : ExpressionParser: Cannot parse func(x) infixed concept 'xxx'
This commit is contained in:
2021-10-13 16:06:57 +02:00
parent a61a1c0d2b
commit 89e1f20975
76 changed files with 5867 additions and 3206 deletions
@@ -1,11 +1,11 @@
from cache.FastCache import FastCache
from core.builtin_concepts_ids import BuiltinContainers, BuiltinConcepts
from core.builtin_concepts_ids import BuiltinConcepts, BuiltinContainers
from core.concept import Concept, ConceptParts
from core.sheerka.services.SheerkaEvaluateRules import SheerkaEvaluateRules
from core.sheerka.services.sheerka_service import BaseService
from core.tokenizer import Tokenizer, TokenKind
from core.tokenizer import TokenKind, Tokenizer
from core.utils import as_bag
from sheerkapython.python_wrapper import create_namespace, ObjectContainer, get_type
from sheerkapython.python_wrapper import ObjectContainer, create_namespace, get_type
from sheerkaql.lexer import Lexer
from sheerkaql.parser import Parser
@@ -17,6 +17,7 @@ class SheerkaQueryManager(BaseService):
NAME = "QueryManager"
OBJECTS_ROOT_ALIAS = "__xxx__objects__xx__"
QUERY_PARAMETER_PREFIX = "__xxx__query_parameter__xx__"
MAPPING_PREFIX = "__xxx__map__xx__"
def __init__(self, sheerka):
super().__init__(sheerka, order=16)
@@ -39,16 +40,19 @@ class SheerkaQueryManager(BaseService):
def initialize_deferred(self, context, is_first_time):
self.rule_evaluator = self.sheerka.services[SheerkaEvaluateRules.NAME]
def get_query_by_kwargs(self, local_namespace, **kwargs):
def get_query_by_kwargs(self, local_namespace, use_mapping, **kwargs):
"""
Create a predicate using kwargs and filter the result
:param local_namespace:
:param use_mapping: True if a mapping is to be used
:param kwargs:
:return:
"""
if not kwargs:
return None
self_ident = f"{self.MAPPING_PREFIX}(self)" if use_mapping else "self"
objects_in_context_index = 0
conditions = []
for k, v in kwargs.items():
@@ -57,28 +61,39 @@ class SheerkaQueryManager(BaseService):
local_namespace[current_variable_name] = v
if k == "__type":
conditions.append(f"get_type(self) == {current_variable_name}")
conditions.append(f"get_type({self_ident}) == {current_variable_name}")
elif k in ("__self", "_"):
conditions.append(f"self == {current_variable_name}")
conditions.append(f"{self_ident} == {current_variable_name}")
elif k.endswith("_contains"):
prop_name = k[:-9]
conditions.append(f"{current_variable_name} in self.{prop_name}")
conditions.append(f"{current_variable_name} in {self_ident}.{prop_name}")
else:
conditions.append(f"self.{k} == {current_variable_name}")
conditions.append(f"{self_ident}.{k} == {current_variable_name}")
return ' and '.join(conditions)
def filter_objects(self, context, objects, predicate=None, **kwargs):
def get_updated_predicate(self, predicate, use_mapping):
if predicate is None:
return None
if use_mapping:
predicate = predicate.replace("self", f"{self.MAPPING_PREFIX}(self)")
return predicate
def filter_objects(self, context, objects, mapping=None, predicate=None, **kwargs):
"""
filter the given objects using the conditions from kwargs
for each k,v in kwargs, the equality k == v is added
for k starting with a double underscore '__', a special treatment may be done
__type : get the type of object (in Sheerka world)
:param context:
:param objects:
:param mapping: mapping to execute on each object before applying the predicate (lambda obj:obj)
:param predicate:
:param kwargs:
:return:
@@ -93,8 +108,9 @@ class SheerkaQueryManager(BaseService):
debugger.debug_entering(nb_objects=len(objects), predicate=predicate, **kwargs)
local_namespace = {}
query_by_kwargs = self.get_query_by_kwargs(local_namespace, **kwargs)
local_namespace = {} if mapping is None else {self.MAPPING_PREFIX: mapping}
query_by_kwargs = self.get_query_by_kwargs(local_namespace, mapping is not None, **kwargs)
predicate = self.get_updated_predicate(predicate, mapping is not None)
if predicate is not None and query_by_kwargs is not None:
query = f"({predicate}) and ({query_by_kwargs})"