Implemented SheerkaOntology

This commit is contained in:
2021-01-11 15:36:03 +01:00
parent e3c2adb533
commit e26c83a825
119 changed files with 6876 additions and 2002 deletions
+1 -1
View File
@@ -29,5 +29,5 @@ class AddToMemoryEvaluator(OneReturnValueEvaluator):
service.registration.clear()
return None
context.sheerka.add_registered_objects(context)
context.sheerka.commit_registered_objects(context)
return None # no need to have a second pass
+2 -2
View File
@@ -137,7 +137,7 @@ class DefConceptEvaluator(OneReturnValueEvaluator):
names = [str(t.value) for t in ret_value.tokens if t.type in (
TokenKind.IDENTIFIER, TokenKind.STRING, TokenKind.KEYWORD)]
debugger.debug_var("names", names, hint="from NameNode")
return set(filter(lambda x: x in concept_name and context.sheerka.not_is_variable(x), names))
return set(filter(lambda x: x in concept_name and context.sheerka.is_not_a_variable(x), names))
#
# case of BNF
@@ -156,7 +156,7 @@ class DefConceptEvaluator(OneReturnValueEvaluator):
visitor = UnreferencedVariablesVisitor(context)
names = visitor.get_names(python_node.ast_)
debugger.debug_var("names", names, hint="from python node")
return set(filter(lambda x: x in concept_name and context.sheerka.not_is_variable(x), names))
return set(filter(lambda x: x in concept_name and context.sheerka.is_not_a_variable(x), names))
else:
return set()
+4 -3
View File
@@ -1,5 +1,6 @@
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, NotInit
from core.concept import Concept
from core.global_symbols import NotInit
from evaluators.BaseEvaluator import OneReturnValueEvaluator
@@ -8,6 +9,7 @@ class PostExecutionEvaluator(OneReturnValueEvaluator):
Last chance to alter the return_value
This evaluator is supposed to be a generic evaluator for all rules that must be executed just before
the aggregations
As of now, the AUTO_EVAL rule implementation is simply hardcoded
"""
NAME = "PostExecution"
@@ -20,12 +22,11 @@ class PostExecutionEvaluator(OneReturnValueEvaluator):
if len(evaluation_parents) > 1:
return False # It must be executed only when the top level context
# only support the rule for the COMMANDS
value = return_value.body
return isinstance(value, Concept) and context.sheerka.isa(value, context.sheerka.new(BuiltinConcepts.AUTO_EVAL))
def eval(self, context, return_value):
# only support the rule for the COMMANDS ??
# only support the rule for the AUTO_EVAL
return context.sheerka.ret(
self.name,
True,
+16 -6
View File
@@ -7,7 +7,8 @@ import core.builtin_helpers
import core.utils
from core.ast_helpers import UnreferencedNamesVisitor, NamesWithAttributesVisitor
from core.builtin_concepts import BuiltinConcepts, ParserResultConcept
from core.concept import ConceptParts, Concept, NotInit
from core.concept import ConceptParts, Concept
from core.global_symbols import NotInit, NotFound
from core.rule import Rule
from core.sheerka.ExecutionContext import ExecutionContext
from core.tokenizer import Token, TokenKind
@@ -127,12 +128,16 @@ class PythonEvaluator(OneReturnValueEvaluator):
for globals_ in all_possible_globals:
try:
# eval
my_locals = {}
if isinstance(node.ast_, ast.Expression):
context.log("Evaluating using 'eval'.", self.name)
evaluated = eval(node.get_compiled(), globals_, sheerka.locals)
evaluated = eval(node.get_compiled(), globals_, my_locals)
else:
context.log("Evaluating using 'exec'.", self.name)
evaluated = self.exec_with_return(node.ast_, globals_, sheerka.locals)
evaluated = self.exec_with_return(node.ast_, globals_, my_locals)
# TODO find a better implementation using SheerkaMemory
sheerka.locals.update(my_locals)
if not expect_success or evaluated:
break # in this first version, we stop once a success is found
@@ -140,8 +145,8 @@ class PythonEvaluator(OneReturnValueEvaluator):
if concepts_entries is None:
concepts_entries = self.get_concepts_entries_from_globals(my_globals)
eval_error = PythonEvalError(ex,
traceback.format_exc() if get_trace_back else None,
self.get_concepts_values_from_globals(globals_, concepts_entries))
traceback.format_exc() if get_trace_back else None,
self.get_concepts_values_from_globals(globals_, concepts_entries))
errors.append(eval_error)
exception_debugger.debug_var("exception", eval_error.error, is_error=True)
exception_debugger.debug_var("trace", eval_error.traceback, is_error=True)
@@ -223,8 +228,13 @@ class PythonEvaluator(OneReturnValueEvaluator):
my_globals["sheerka"] = Expando(bag)
continue
# search in local variables. To remove when local variables will be merged with memory
if name in context.sheerka.locals:
my_globals[name] = context.sheerka.locals[name]
continue
# search in short term memory
if (obj := context.get_from_short_term_memory(name)) is not None:
if (obj := context.get_from_short_term_memory(name)) is not NotFound:
context.log(f"Resolving '{name}'. Using value found in STM.", self.name)
my_globals[name] = obj
continue
+2 -1
View File
@@ -1,5 +1,6 @@
from core.builtin_concepts import BuiltinConcepts
from core.concept import Concept, NotInit
from core.concept import Concept
from core.global_symbols import NotInit
from evaluators.BaseEvaluator import AllReturnValuesEvaluator
+2 -1
View File
@@ -1,4 +1,5 @@
from core.builtin_concepts import BuiltinConcepts, ParserResultConcept
from core.global_symbols import NotFound
from core.rule import Rule, ACTION_TYPE_DEFERRED
from evaluators.BaseEvaluator import OneReturnValueEvaluator
@@ -35,7 +36,7 @@ class RuleEvaluator(OneReturnValueEvaluator):
# Browse the rules to find possible deferred rules
if r.metadata.action_type == ACTION_TYPE_DEFERRED:
rule_id = sheerka.get_from_short_term_memory(context, r.id)
rule = sheerka.get_rule_by_id(str(rule_id or r.id))
rule = sheerka.get_rule_by_id(str(rule_id if rule_id is not NotFound else r.id))
resolved.append(rule)
success &= isinstance(rule, Rule)
else: