Fixed #61 : SheerkaDebugManager: Add get_value()
Fixed #60 : Hash error when ReturnValue is a list of list Fixed #59 : Implement smart_get() Fixed #58 : SheerkaPromptCompleter: Cannot parse concept token Fixed #57 : SheerkaPrompt: Add concept autocompletion Fixed #56 : automatically backup command Fixed #54 : I can record execution status Fixed #53 : ConceptManager: modify_concept fails
This commit is contained in:
@@ -13,7 +13,8 @@ from core.builtin_concepts_ids import BuiltinConcepts, AllBuiltinConcepts, Built
|
||||
from core.builtin_helpers import ensure_concept, ensure_bnf
|
||||
from core.concept import Concept, DEFINITION_TYPE_DEF, DEFINITION_TYPE_BNF, freeze_concept_attrs, ConceptMetadata, \
|
||||
VARIABLE_PREFIX
|
||||
from core.global_symbols import EVENT_CONCEPT_CREATED, NotInit, NotFound, ErrorObj, EVENT_CONCEPT_DELETED, NoFirstToken
|
||||
from core.global_symbols import EVENT_CONCEPT_CREATED, NotInit, NotFound, ErrorObj, EVENT_CONCEPT_DELETED, NoFirstToken, \
|
||||
EVENT_CONCEPT_MODIFIED
|
||||
from core.sheerka.services.sheerka_service import BaseService
|
||||
from core.tokenizer import Tokenizer, TokenKind
|
||||
from parsers.BnfNodeParser import RegExDef
|
||||
@@ -118,6 +119,7 @@ class SheerkaConceptManager(BaseService):
|
||||
self.sheerka.bind_service_method(self.set_id_if_needed, True)
|
||||
self.sheerka.bind_service_method(self.set_attr, True)
|
||||
self.sheerka.bind_service_method(self.get_attr, False)
|
||||
self.sheerka.bind_service_method(self.smart_get_attr, False)
|
||||
self.sheerka.bind_service_method(self.set_property, True, as_name="set_prop")
|
||||
self.sheerka.bind_service_method(self.get_property, False, as_name="get_prop")
|
||||
self.sheerka.bind_service_method(self.get_by_key, False, visible=False)
|
||||
@@ -346,7 +348,7 @@ class SheerkaConceptManager(BaseService):
|
||||
if modify_source:
|
||||
self._update_concept(context, concept, to_add, to_remove)
|
||||
|
||||
# KSI 2021-02-16 publish the modification of the concept only when someone needs it
|
||||
sheerka.publish(context, EVENT_CONCEPT_MODIFIED, {"old": concept, "new": new_concept})
|
||||
return sheerka.ret(self.NAME, True, sheerka.new(BuiltinConcepts.NEW_CONCEPT, body=new_concept))
|
||||
|
||||
def remove_concept(self, context, concept):
|
||||
@@ -390,7 +392,6 @@ class SheerkaConceptManager(BaseService):
|
||||
def set_attr(self, concept, attribute, value):
|
||||
"""
|
||||
Modifies an attribute of a concept (concept.values)
|
||||
:param context:
|
||||
:param concept:
|
||||
:param attribute:
|
||||
:param value:
|
||||
@@ -416,7 +417,6 @@ class SheerkaConceptManager(BaseService):
|
||||
def get_attr(self, concept, attribute):
|
||||
"""
|
||||
Returns the attribute of a concept
|
||||
:param context:
|
||||
:param concept:
|
||||
:param attribute:
|
||||
:return:
|
||||
@@ -431,6 +431,72 @@ class SheerkaConceptManager(BaseService):
|
||||
return self.sheerka.new(BuiltinConcepts.NOT_FOUND, body={"#concept": concept, "#attr": attribute})
|
||||
return value
|
||||
|
||||
def smart_get_attr(self, concept, attribute):
|
||||
|
||||
def get_obj_value(c, concept_to_look_for):
|
||||
"""
|
||||
Return the body of the concept c if it is an instance of concept_to_look_for
|
||||
Go deeper in the bodies until the concept_to_look_for is found
|
||||
:param c:
|
||||
:param concept_to_look_for:
|
||||
:return:
|
||||
"""
|
||||
while c.body is not NotInit:
|
||||
if self.sheerka.isinstance(c.body, concept_to_look_for):
|
||||
return c.body
|
||||
c = c.body
|
||||
|
||||
return None
|
||||
|
||||
ensure_concept()
|
||||
if not self.sheerka.is_success(concept):
|
||||
return concept
|
||||
|
||||
value = self.get_attr(concept, attribute)
|
||||
if not self.sheerka.isinstance(value, BuiltinConcepts.NOT_FOUND):
|
||||
return value
|
||||
|
||||
if not isinstance(attribute, Concept):
|
||||
return self.sheerka.new(BuiltinConcepts.NOT_FOUND, body={"#concept": concept, "#attr": attribute})
|
||||
|
||||
# try to be smart
|
||||
result = []
|
||||
|
||||
# first look in children
|
||||
for k, v in concept.variables().items():
|
||||
attr_as_concept_key, attr_concept_id = core.utils.unstr_concept(k)
|
||||
if attr_concept_id is not None:
|
||||
attr_as_concept = self.sheerka.fast_resolve((attr_as_concept_key, attr_concept_id), return_new=False)
|
||||
if attr_as_concept is not None and self.sheerka.isa(attribute, attr_as_concept):
|
||||
if hasattr(v, "__iter__"):
|
||||
for _v in v:
|
||||
value = get_obj_value(_v, attribute)
|
||||
if value is not None:
|
||||
result.append(value)
|
||||
else:
|
||||
value = get_obj_value(v, attribute)
|
||||
if value is not None:
|
||||
result.append(value)
|
||||
|
||||
if len(result) > 0:
|
||||
return result[0] if len(result) == 1 else result
|
||||
|
||||
# then try the ancestors
|
||||
for k, v in concept.variables().items():
|
||||
attr_as_concept_key, attr_concept_id = core.utils.unstr_concept(k)
|
||||
if attr_concept_id is not None:
|
||||
attr_as_concept = self.sheerka.fast_resolve((attr_as_concept_key, attr_concept_id), return_new=False)
|
||||
if attr_as_concept is not None and self.sheerka.isa(attr_as_concept, attribute):
|
||||
if isinstance(v, list):
|
||||
result.extend(v)
|
||||
else:
|
||||
result.append(v)
|
||||
|
||||
if len(result) > 0:
|
||||
return result[0] if len(result) == 1 else result
|
||||
|
||||
return self.sheerka.new(BuiltinConcepts.NOT_FOUND, body={"#concept": concept, "#attr": attribute})
|
||||
|
||||
def get_property(self, concept, prop):
|
||||
"""
|
||||
Returns the value of a concept property
|
||||
|
||||
Reference in New Issue
Block a user