Implemented SheerkaOntology
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
import ast
|
||||
import logging
|
||||
|
||||
from cache.Cache import Cache
|
||||
from core.ast_helpers import ast_to_props
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.concept import Concept, NotInit, ConceptParts, DEFINITION_TYPE_BNF, concept_part_value
|
||||
from core.concept import Concept, ConceptParts, DEFINITION_TYPE_BNF, concept_part_value
|
||||
from core.global_symbols import NotInit, NotFound
|
||||
from core.rule import Rule
|
||||
from core.sheerka.services.SheerkaExecute import SheerkaExecute
|
||||
from core.tokenizer import Keywords
|
||||
from core.utils import as_bag
|
||||
from parsers.BaseNodeParser import SourceCodeNode, ConceptNode, UnrecognizedTokensNode, SourceCodeWithConceptNode, \
|
||||
RuleNode
|
||||
from parsers.BaseParser import BaseParser, ParsingError
|
||||
@@ -510,11 +515,11 @@ def get_lexer_nodes_from_unrecognized(context, unrecognized_tokens_node, parsers
|
||||
|
||||
def update_compiled(context, concept, errors, parsers=None):
|
||||
"""
|
||||
recursively iterate thru concept.get_compiled() to replace LexerNode into concepts or list of ReturnValueConcept
|
||||
recursively iterate over concept.get_compiled() to replace LexerNode into concepts or list of ReturnValueConcept
|
||||
When parsing using a LexerNodeParser (SyaNodeParser, BnfNodeParser...)
|
||||
the result will be a LexerNode.
|
||||
In the specific case of a ConceptNode, the compiled variables will also be LexerNode (UnrecognizedTokensNode...)
|
||||
This function iterate thru the compile to transform these nodes into concept of compiled AST
|
||||
This function iterate over the compile to transform these nodes into concept of compiled AST
|
||||
:param context:
|
||||
:param concept:
|
||||
:param errors: a list the must be initialized by the caller
|
||||
@@ -648,3 +653,59 @@ def ensure_concept_or_rule(*items):
|
||||
else:
|
||||
if not isinstance(items, (Concept, Rule)):
|
||||
raise TypeError(f"'{items}' must be a concept or rule")
|
||||
|
||||
|
||||
expressions_cache = Cache()
|
||||
|
||||
|
||||
def evaluate_expression(expr, bag):
|
||||
"""
|
||||
Try to evaluate expr in context of bag
|
||||
:param expr:
|
||||
:param bag:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if expr is None or expr.strip() == "":
|
||||
return None
|
||||
|
||||
if expr in bag:
|
||||
return bag[expr]
|
||||
|
||||
props_definitions = expressions_cache.get(expr)
|
||||
if props_definitions is NotFound:
|
||||
_ast = ast.parse(expr, mode="eval")
|
||||
props_definitions = []
|
||||
ast_to_props(props_definitions, _ast.body, None)
|
||||
props_definitions.reverse()
|
||||
expressions_cache.put(expr, props_definitions)
|
||||
|
||||
return evaluate_object(bag, props_definitions)
|
||||
|
||||
|
||||
def evaluate_object(bag, properties):
|
||||
"""
|
||||
Evaluate the properties of an object
|
||||
Works with evaluate_expression
|
||||
:param bag:
|
||||
:param properties: List of ast_helpers.PropDef
|
||||
:return:
|
||||
"""
|
||||
for prop in properties:
|
||||
try:
|
||||
obj = bag[prop.prop]
|
||||
except KeyError:
|
||||
try:
|
||||
obj = bag["self"][prop.prop]
|
||||
except Exception:
|
||||
raise NameError(prop.prop)
|
||||
|
||||
if obj is None:
|
||||
return None
|
||||
|
||||
if prop.index is not None:
|
||||
obj = obj[prop.index]
|
||||
|
||||
bag = as_bag(obj)
|
||||
|
||||
return obj
|
||||
|
||||
Reference in New Issue
Block a user