Added first version of DebugManager. Implemented draft of the rule engine
This commit is contained in:
@@ -1,12 +1,14 @@
|
||||
import sys
|
||||
import time
|
||||
from os import path
|
||||
|
||||
from core.builtin_concepts import BuiltinConcepts
|
||||
from core.builtin_concepts import BuiltinConcepts, BuiltinContainers
|
||||
from core.concept import Concept
|
||||
from core.sheerka.services.sheerka_service import BaseService
|
||||
|
||||
CONCEPTS_FILE_LITE = "_concepts_lite.txt"
|
||||
CONCEPTS_FILE_ALL_CONCEPTS = "_concepts.txt"
|
||||
CONCEPTS_FILE_TO_USE = CONCEPTS_FILE_ALL_CONCEPTS
|
||||
CONCEPTS_FILE_FULL = "_concepts_full.txt"
|
||||
CONCEPTS_FILE_TO_USE = CONCEPTS_FILE_FULL
|
||||
|
||||
|
||||
class SheerkaAdmin(BaseService):
|
||||
@@ -22,6 +24,10 @@ class SheerkaAdmin(BaseService):
|
||||
self.sheerka.bind_service_method(self.concepts, False)
|
||||
self.sheerka.bind_service_method(self.last_created_concept, False)
|
||||
self.sheerka.bind_service_method(self.last_ret, False)
|
||||
self.sheerka.bind_service_method(self.last_error_ret, False)
|
||||
self.sheerka.bind_service_method(self.extended_isinstance, False)
|
||||
self.sheerka.bind_service_method(self.is_container, False)
|
||||
self.sheerka.bind_service_method(self.format_rules, False)
|
||||
|
||||
def caches_names(self):
|
||||
"""
|
||||
@@ -53,18 +59,19 @@ class SheerkaAdmin(BaseService):
|
||||
|
||||
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")
|
||||
file_path = path.join(path.dirname(sys.argv[0]), file_name)
|
||||
if not path.exists(file_path):
|
||||
print(f"\u001b[31mFile '{file_path}' is not found !\u001b[0m")
|
||||
return 0, 0, 1
|
||||
|
||||
with open(file_name, "r") as f:
|
||||
with open(file_path, "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}")
|
||||
print(f"Importing {to_import}")
|
||||
res = restore_from_file(to_import)
|
||||
_nb_lines += res[0]
|
||||
_nb_instructions += res[1]
|
||||
@@ -74,42 +81,49 @@ class SheerkaAdmin(BaseService):
|
||||
if line == "" or line.startswith("#"):
|
||||
continue
|
||||
|
||||
self.sheerka.log.info(line)
|
||||
print(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")
|
||||
print("\u001b[31mError detected !\u001b[0m")
|
||||
|
||||
return _nb_lines, _nb_instructions, _nb_lines_in_error
|
||||
|
||||
if concept_file == "full":
|
||||
concept_file = CONCEPTS_FILE_ALL_CONCEPTS
|
||||
|
||||
elif not concept_file.startswith("_concepts"):
|
||||
if not concept_file.startswith("_concepts"):
|
||||
concept_file = f"_concepts_{concept_file}.txt"
|
||||
|
||||
try:
|
||||
start = time.time_ns()
|
||||
self.sheerka.during_restore = True
|
||||
self.sheerka.save_execution_context = False
|
||||
enable_process_return_values_previous_value = self.sheerka.enable_process_return_values
|
||||
self.sheerka.enable_process_return_values = False
|
||||
|
||||
nb_lines, nb_instructions, nb_lines_in_error = restore_from_file(concept_file)
|
||||
|
||||
self.sheerka.enable_process_return_values = enable_process_return_values_previous_value
|
||||
self.sheerka.save_execution_context = True
|
||||
self.sheerka.during_restore = False
|
||||
stop = time.time_ns()
|
||||
|
||||
nano_sec = stop - start
|
||||
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).")
|
||||
print(f"Imported {nb_lines} line(s) in {elapsed}.")
|
||||
print(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")
|
||||
print(f"\u001b[31m{nb_lines_in_error} errors(s) found.\u001b[0m")
|
||||
else:
|
||||
self.sheerka.log.info(f"No error.")
|
||||
print(f"No error.")
|
||||
except IOError as e:
|
||||
raise e
|
||||
|
||||
def concepts(self):
|
||||
return self.sheerka.sdp.list(self.sheerka.CONCEPTS_BY_ID_ENTRY)
|
||||
return self.sheerka.new(BuiltinConcepts.TO_LIST, body=self.sheerka.sdp.list(self.sheerka.CONCEPTS_BY_ID_ENTRY))
|
||||
|
||||
def format_rules(self):
|
||||
return self.sheerka.new(BuiltinConcepts.TO_LIST, items=self.sheerka.get_format_rules())
|
||||
|
||||
def last_created_concept(self, use_history=False):
|
||||
for exec_result in reversed(self.sheerka.last_executions):
|
||||
@@ -124,4 +138,53 @@ class SheerkaAdmin(BaseService):
|
||||
return self.sheerka.new(BuiltinConcepts.NOT_FOUND)
|
||||
|
||||
def last_ret(self, context, index=-1):
|
||||
return self.sheerka.last_return_values[index]
|
||||
try:
|
||||
last = self.sheerka.last_return_values[index]
|
||||
return last[0] if isinstance(last, list) and len(last) == 1 else last
|
||||
except IndexError:
|
||||
return None
|
||||
|
||||
def last_error_ret(self, context, index=-1):
|
||||
while index >= -len(self.sheerka.last_return_values):
|
||||
last = self.sheerka.last_return_values[index]
|
||||
last = [last] if not hasattr(last, "__iter__") else last
|
||||
last = [ret_val for ret_val in last if not ret_val.status]
|
||||
if len(last) == 0:
|
||||
index -= 1
|
||||
continue
|
||||
|
||||
if len(last) > 1:
|
||||
return context.sheerka.ret(SheerkaAdmin.NAME,
|
||||
False,
|
||||
context.sheerka.new(BuiltinConcepts.TOO_MANY_ERRORS, body=last))
|
||||
|
||||
return last[0]
|
||||
|
||||
return context.sheerka.ret(SheerkaAdmin.NAME,
|
||||
False,
|
||||
context.sheerka.new(BuiltinConcepts.NOT_FOUND))
|
||||
|
||||
def extended_isinstance(self, a, b):
|
||||
"""
|
||||
switch between sheerka.isinstance and builtin.isinstance
|
||||
:param a:
|
||||
:param b:
|
||||
:return:
|
||||
"""
|
||||
|
||||
if isinstance(b, (type, tuple)):
|
||||
return isinstance(a, b)
|
||||
|
||||
return self.sheerka.isinstance(a, b)
|
||||
|
||||
@staticmethod
|
||||
def is_container(obj):
|
||||
"""
|
||||
A container concept is a builtin concept that embed a result
|
||||
:param obj:
|
||||
:return:
|
||||
"""
|
||||
if not isinstance(obj, Concept):
|
||||
return False
|
||||
|
||||
return obj.key in BuiltinContainers
|
||||
|
||||
Reference in New Issue
Block a user