Fixed some misbehaviours regarding question() + added #import functionality when restoring
This commit is contained in:
@@ -191,6 +191,7 @@ class ExecutionContext:
|
||||
new.protected_hints.update(self.protected_hints)
|
||||
|
||||
self._children.append(new)
|
||||
|
||||
return new
|
||||
|
||||
def add_preprocess(self, name, **kwargs):
|
||||
@@ -295,6 +296,9 @@ class ExecutionContext:
|
||||
to_str = self.return_value_to_str(r)
|
||||
self._logger.debug(f"[{self._id:2}]" + self._tab + "-> " + to_str)
|
||||
|
||||
def debug(self, text):
|
||||
print(text)
|
||||
|
||||
def get_parent(self):
|
||||
return self._parent
|
||||
|
||||
@@ -309,7 +313,7 @@ class ExecutionContext:
|
||||
def in_private_context(self, concept_key):
|
||||
return concept_key in self.private_hints
|
||||
|
||||
def add_to_private_hints (self, concept_key):
|
||||
def add_to_private_hints(self, concept_key):
|
||||
self.private_hints.add(concept_key)
|
||||
|
||||
def add_to_protected_hints(self, concept_key):
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import time
|
||||
from os import path
|
||||
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.sheerka.services.sheerka_service import BaseService
|
||||
@@ -50,6 +51,38 @@ class SheerkaAdmin(BaseService):
|
||||
:return:
|
||||
"""
|
||||
|
||||
def restore_from_file(file_name):
|
||||
_nb_lines, _nb_instructions, _nb_lines_in_error = 0, 0, 0
|
||||
if not path.exists(file_name):
|
||||
self.sheerka.log.error(f"\u001b[31mFile '{file_name}' is not found !\u001b[0m")
|
||||
return 0, 0, 1
|
||||
|
||||
with open(file_name, "r") as f:
|
||||
for line in f.readlines():
|
||||
_nb_lines += 1
|
||||
line = line.strip()
|
||||
|
||||
if line.startswith("#import "):
|
||||
to_import = "_concepts_" + line[8:] + ".txt"
|
||||
self.sheerka.log.info(f"Importing {to_import}")
|
||||
res = restore_from_file(to_import)
|
||||
_nb_lines += res[0]
|
||||
_nb_instructions += res[1]
|
||||
_nb_lines_in_error += res[2]
|
||||
continue
|
||||
|
||||
if line == "" or line.startswith("#"):
|
||||
continue
|
||||
|
||||
self.sheerka.log.info(line)
|
||||
_nb_instructions += 1
|
||||
res = self.sheerka.evaluate_user_input(line)
|
||||
if len(res) > 1 or not res[0].status:
|
||||
_nb_lines_in_error += 1
|
||||
self.sheerka.log.error("\u001b[31mError detected !\u001b[0m")
|
||||
|
||||
return _nb_lines, _nb_instructions, _nb_lines_in_error
|
||||
|
||||
if concept_file == "full":
|
||||
concept_file = CONCEPTS_FILE_ALL_CONCEPTS
|
||||
|
||||
@@ -58,20 +91,8 @@ class SheerkaAdmin(BaseService):
|
||||
|
||||
try:
|
||||
start = time.time_ns()
|
||||
nb_lines = 0
|
||||
nb_lines_in_error = 0
|
||||
self.sheerka.during_restore = True
|
||||
with open(concept_file, "r") as f:
|
||||
for line in f.readlines():
|
||||
nb_lines += 1
|
||||
line = line.strip()
|
||||
if line == "" or line.startswith("#"):
|
||||
continue
|
||||
self.sheerka.log.info(line)
|
||||
res = self.sheerka.evaluate_user_input(line)
|
||||
if len(res) > 1 or not res[0].status:
|
||||
nb_lines_in_error += 1
|
||||
self.sheerka.log.error("\u001b[31mError detected !\u001b[0m")
|
||||
nb_lines, nb_instructions, nb_lines_in_error = restore_from_file(concept_file)
|
||||
self.sheerka.during_restore = False
|
||||
stop = time.time_ns()
|
||||
|
||||
@@ -79,12 +100,13 @@ class SheerkaAdmin(BaseService):
|
||||
dt = nano_sec / 1e6
|
||||
elapsed = f"{dt} ms" if dt < 1000 else f"{dt / 1000} s"
|
||||
self.sheerka.log.info(f"Imported {nb_lines} line(s) in {elapsed}.")
|
||||
self.sheerka.log.info(f"{nb_instructions} instruction(s).")
|
||||
if nb_lines_in_error > 0:
|
||||
self.sheerka.log.info(f"\u001b[31m{nb_lines_in_error} errors(s) found.\u001b[0m")
|
||||
else:
|
||||
self.sheerka.log.info(f"No error.")
|
||||
except IOError:
|
||||
pass
|
||||
except IOError as e:
|
||||
raise e
|
||||
|
||||
def concepts(self):
|
||||
return self.sheerka.sdp.list(self.sheerka.CONCEPTS_BY_ID_ENTRY)
|
||||
|
||||
@@ -4,13 +4,12 @@ from core.concept import Concept, DEFINITION_TYPE_DEF, ensure_concept, DEFINITIO
|
||||
from core.sheerka.services.sheerka_service import BaseService
|
||||
from sdp.sheerkaDataProvider import SheerkaDataProviderDuplicateKeyError
|
||||
|
||||
BNF_NODE_PARSER_CLASS = "parsers.BnfNodeParser_Old.BnfNodeParser"
|
||||
BASE_NODE_PARSER_CLASS = "parsers.BaseNodeParser.BaseNodeParser"
|
||||
|
||||
|
||||
class SheerkaCreateNewConcept(BaseService):
|
||||
"""
|
||||
Manage the creation of a new concept
|
||||
Manages the creation of a new concept
|
||||
"""
|
||||
|
||||
NAME = "CreateNewConcept"
|
||||
|
||||
@@ -166,6 +166,7 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
where_clause_def.trueified,
|
||||
desc=f"Apply where clause on '{where_clause_def.prop}'",
|
||||
expect_success=True,
|
||||
is_question=True,
|
||||
stm={where_clause_def.prop: r.body})
|
||||
one_res = expect_one(context, evaluation_res)
|
||||
if one_res.status:
|
||||
@@ -286,7 +287,6 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
current_prop,
|
||||
current_concept,
|
||||
force_evaluation,
|
||||
expect_success,
|
||||
where_clause_def):
|
||||
"""
|
||||
Resolve a variable or a Concept
|
||||
@@ -295,7 +295,6 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
:param current_prop: current property or ConceptPart
|
||||
:param current_concept: current concept
|
||||
:param force_evaluation: Force body evaluation
|
||||
:param expect_success: for PythonEvaluator, try all possibilities to find a positive result
|
||||
:param where_clause_def: intermediate where clause for variables
|
||||
:return:
|
||||
"""
|
||||
@@ -332,8 +331,10 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
if force_evaluation:
|
||||
sub_context.protected_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED)
|
||||
|
||||
if expect_success:
|
||||
if current_prop in (ConceptParts.WHERE, ConceptParts.PRE):
|
||||
sub_context.protected_hints.add(BuiltinConcepts.EVAL_UNTIL_SUCCESS_REQUESTED)
|
||||
|
||||
if current_prop == ConceptParts.WHERE:
|
||||
sub_context.protected_hints.add(BuiltinConcepts.EVAL_QUESTION_REQUESTED)
|
||||
|
||||
# when it's a concept, evaluate it
|
||||
@@ -384,7 +385,6 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
current_prop,
|
||||
current_concept,
|
||||
force_evaluation,
|
||||
expect_success,
|
||||
where_clause_def):
|
||||
"""When dealing with a list, there are two possibilities"""
|
||||
# It may be a list of ReturnValueConcept to execute (always the case for metadata)
|
||||
@@ -399,7 +399,6 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
current_prop,
|
||||
current_concept,
|
||||
force_evaluation,
|
||||
expect_success,
|
||||
where_clause_def)
|
||||
|
||||
res = []
|
||||
@@ -416,7 +415,6 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
current_prop,
|
||||
current_concept,
|
||||
force_evaluation,
|
||||
expect_success,
|
||||
where_clause_def)
|
||||
if self.sheerka.isinstance(r, BuiltinConcepts.CONCEPT_EVAL_ERROR):
|
||||
return r
|
||||
@@ -476,10 +474,10 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
|
||||
if isinstance(prop_ast, list):
|
||||
# Do not send the current concept for the properties
|
||||
resolved = self.resolve_list(sub_context, prop_ast, var_name, None, True, False, w_clause)
|
||||
resolved = self.resolve_list(sub_context, prop_ast, var_name, None, True, w_clause)
|
||||
else:
|
||||
# Do not send the current concept for the properties
|
||||
resolved = self.resolve(sub_context, prop_ast, var_name, None, True, False, w_clause)
|
||||
resolved = self.resolve(sub_context, prop_ast, var_name, None, True, w_clause)
|
||||
|
||||
if isinstance(resolved, Concept) and not sub_context.sheerka.is_success(resolved):
|
||||
resolved.set_value("concept", concept) # since current concept was not sent
|
||||
@@ -504,18 +502,8 @@ class SheerkaEvaluateConcept(BaseService):
|
||||
# otherwise no need to force
|
||||
force_concept_eval = False if part_key == ConceptParts.BODY else True
|
||||
|
||||
# when resolving predicate (where or pre), we need to make sure that PythonEvaluator
|
||||
# will try every possibilities before returning False
|
||||
expect_success = part_key in (ConceptParts.WHERE, ConceptParts.PRE)
|
||||
|
||||
# resolve
|
||||
resolved = self.resolve(sub_context,
|
||||
metadata_ast,
|
||||
part_key,
|
||||
concept,
|
||||
force_concept_eval,
|
||||
expect_success,
|
||||
None)
|
||||
resolved = self.resolve(sub_context, metadata_ast, part_key, concept, force_concept_eval, None)
|
||||
|
||||
# 'FATAL' error is detected, let's stop
|
||||
if isinstance(resolved, Concept) and not sub_context.sheerka.is_success(resolved):
|
||||
|
||||
@@ -100,4 +100,9 @@ class SheerkaModifyConcept(BaseService):
|
||||
:return:
|
||||
"""
|
||||
ensure_concept()
|
||||
return concept.get_value(attribute)
|
||||
if not self.sheerka.is_success(concept):
|
||||
return concept
|
||||
|
||||
if (value := concept.get_value(attribute)) == BuiltinConcepts.NOT_INITIALIZED:
|
||||
return self.sheerka.new(BuiltinConcepts.NOT_FOUND, body={"#concept": concept, "#attr": attribute})
|
||||
return value
|
||||
|
||||
@@ -10,27 +10,25 @@ class SheerkaQuestion(BaseService):
|
||||
super().__init__(sheerka)
|
||||
|
||||
def initialize(self):
|
||||
self.sheerka.bind_service_method(self.question, False)
|
||||
# self.sheerka.bind_service_method(self.question, False)
|
||||
self.sheerka.bind_service_method(self.is_question, False)
|
||||
|
||||
def question(self, context, q):
|
||||
"""
|
||||
Evaluate q in the context in a question
|
||||
:param context:
|
||||
:param q:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if isinstance(q, Concept):
|
||||
with context.push(BuiltinConcepts.EVALUATE_CONCEPT, q, desc=f"Evaluating question '{q}'") as sub_context:
|
||||
sub_context.global_hints.add(BuiltinConcepts.EVAL_QUESTION_REQUESTED)
|
||||
sub_context.global_hints.add(BuiltinConcepts.EVAL_BODY_REQUESTED)
|
||||
sub_context.global_hints.add(BuiltinConcepts.EVAL_UNTIL_SUCCESS_REQUESTED)
|
||||
sub_context.protected_hints.add(BuiltinConcepts.RETURN_BODY_REQUESTED)
|
||||
|
||||
evaluated = self.sheerka.evaluate_concept(sub_context, q)
|
||||
|
||||
return evaluated
|
||||
# def question(self, context, q):
|
||||
# """
|
||||
# Evaluate q in the context in a question
|
||||
# :param context:
|
||||
# :param q:
|
||||
# :return:
|
||||
# """
|
||||
#
|
||||
# if isinstance(q, Concept):
|
||||
# with context.push(BuiltinConcepts.EVALUATE_CONCEPT, q, desc=f"Evaluating question '{q}'") as sub_context:
|
||||
# sub_context.global_hints.add(BuiltinConcepts.EVAL_QUESTION_REQUESTED)
|
||||
# sub_context.global_hints.add(BuiltinConcepts.EVAL_UNTIL_SUCCESS_REQUESTED)
|
||||
#
|
||||
# evaluated = self.sheerka.evaluate_concept(sub_context, q)
|
||||
#
|
||||
# return evaluated
|
||||
|
||||
def is_question(self, context):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user